GithubHelp home page GithubHelp logo

pierredarrieutort / fitext Goto Github PK

View Code? Open in Web Editor NEW
14.0 5.0 4.0 6.33 MB

Resizes text elements proportionally to fit any element

Home Page: https://pierredarrieutort.github.io/fitext

License: ISC License

JavaScript 36.68% HTML 24.10% SCSS 39.22%

fitext's Introduction

fitext

Fitext is a module which adapts the textual elements so that they are always contained in their parents without ever exceeding whatever the parent height and width. This lightweight library using no-one dependency. You can find a playable demo here.

Logo fitext

Table of contents

  1. Installation
  2. Getting Started
  3. Example
  4. Using a CDN
  5. Options
  6. Performance Checklist
  7. Contributors
  8. License

Installation

$ npm install --save fitext
$ yarn add fitext

Getting Started

HTML :

<body>
    <div class="fit-this-text">
        <h1>Resize your window</h1>
        <h2>Play with responsive ๐Ÿ’ก</h2>
        <p>Insert a huge lorem ipsum text !</p>
    </div>
</body>

ES6 Modules :

import fitext from 'fitext'

fitext(true);

No-module :

<script src='https://cdn.jsdelivr.net/npm/fitext@latest/dist/fitext.umd.js'></script>
<script>
    window.addEventListener('DOMContentLoaded', function() {
        fitext(true);
    });
</script>

Example

import fitext from 'fitext'


const EVENTS = ['DOMContentLoaded', 'resize']

EVENTS.forEach( e => window.addEventListener( e, fitext ) )
//OR
EVENTS.forEach( e => window.addEventListener( e, () => fitext(true, .25) ) )

In the previous example, we listen to all event that can makes overflow texts from their containers, we prevent all break cases by executing the fitext function.

Using a CDN

  • Minified version :

    • https://cdn.jsdelivr.net/npm/fitext@latest/dist/fitext.umd.js
    • https://cdn.jsdelivr.net/npm/fitext@latest/dist/fitext.cjs.js
    • https://cdn.jsdelivr.net/npm/fitext@latest/dist/fitext.esm.js

  • Original version :

    • https://cdn.jsdelivr.net/npm/fitext@latest/src/fitext.js

  • For specific files or enhanced configuration you can go here.

Options

You can pass options in fitext. Allowed values are as follows :

Name Type Default Description
wideable { Boolean } false Enables the font-size enlarger. More
stagger { Float } 1 Change stagger value. More

- wideable :

This option enlarges the font size above the font size established before executing the `fitext` function.
The default behavior of this options limits the font-size to the font-size established before the function execution.

- stagger :

This option increments or decrements the current font-size by the number of pixels desired.
The smaller the value, the beautiful the change but may cause performance issues.

Performance Checklist

Good to know : Fitext is a very lightweight module : Less than 1 kB !

To increase fitext performances :

  • Try to use relative font-size wherever possible.
  • If you targeting a parent element all child are already taken in account, don't execute a fitext function on them.
  • Try to avoid get too much containers into the fittable elements.
  • Don't use a CSS font-size transition, prefer stagger option usage.

Contributors

Pierre Darrieutort
Pierre Darrieutort

Core
Jape Photography
Jape

Branding
Mathieu Daix
Mathieu Daix

Design
Abhishek Shende
Abhishek Shende

Klap Integration

License

This project is under ISC License.

fitext's People

Contributors

geopic avatar osdevisnot avatar pierredarrieutort avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fitext's Issues

Infinite loop when trying reducing the height of the fitter container to the minimum

Expected behaviour

The font size should reduce to the very least (even if not even visible I guess?) when extremely reducing the height of a container.

Actual behaviour

The website stops responding. From what I could notice, this is probably due to an infinite loop trying to update the children font sizes while it's still overflowing...

How to reproduce

Go to the project's demo and try reducing the height to the minimum (it get stuck around a height of 20% (for a width of 60%) if your doing it slowly, but the values don't matter much, it's rather the action of reducing the height to its minimum).

Specifications (just in case)

  • Version: fitext v1.0.5
  • Browser: Google Chrome 81.0.4044.138
  • OS: macOS 10.12.6

fitext hangs firefox in android

Accessing the demo from firefox android
https://pierredarrieutort.github.io/fitext/

The offending line is
update_font_size = (child, reversed) => child.style.fontSize = ${parseFloat(getComputedStyle(child).fontSize) + (reversed ? -stagger : stagger)}px

It happens on the reversed -stagger case. For some reason using a too low stagger,
say 1 or 2 the result actually increases the font size. This results in infinite loop increasing
the font size. I tried a piece of code of mine and using a stagger 10 did actually decreased the font.
Fixing the issue.

But still I think there is something fitext code is not considering. Or firefox maybe.
I traced style.fontSize and getComputedStyle(...).fontSize and they tended to have a
considerable gap of px. This gap grows with the ever increasing font-size too.

Im not sure about using a bigger stagger. My guess is that fitext loses font-size granularity.

TypeScript type declaration file

If you want I can write a TypeScript type declaration file for this package so that TS developers can utilise the type-checking feature of the language as they use this package. Let me know if you are interested.

Example doesn't work

File 'fitext.min.js' not found. There is also no .min. file provided. In 'dist' -folder I just can find .cjs. .esm. and .umd. files. No one of them works. :-(

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.