All Projects → kennethormandy → react-fittext

kennethormandy / react-fittext

Licence: MIT license
FitText.js as a React v16+ component.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to react-fittext

Urbanist
Urbanist is a low-contrast, geometric sans-serif inspired by Modernist design and typography.
Stars: ✭ 374 (+442.03%)
Mutual labels:  typography
harfpy
Python wrapper for HarfBuzz
Stars: ✭ 29 (-57.97%)
Mutual labels:  typography
bettertext.css
Improved default typography for naked HTML or Markdown-generated content.
Stars: ✭ 36 (-47.83%)
Mutual labels:  typography
typography-karaoke
Demonstrating Typography via Karaoke-style cues using HTML5 Audio/Video and WebVTT
Stars: ✭ 15 (-78.26%)
Mutual labels:  typography
fdiff
An OpenType table diff tool for fonts. Based on the fontTools TTX format.
Stars: ✭ 33 (-52.17%)
Mutual labels:  typography
rebass
⚛️ React primitive UI components built with styled-system.
Stars: ✭ 7,844 (+11268.12%)
Mutual labels:  typography
Tehreer-Android
Standalone text engine for Android aimed to be free from platform limitations
Stars: ✭ 61 (-11.59%)
Mutual labels:  typography
Compressed-Punctuation-Sans
包含基础标点挤压效果的中文标点符号字体
Stars: ✭ 49 (-28.99%)
Mutual labels:  typography
php-typography
A PHP library for improving your web typography.
Stars: ✭ 63 (-8.7%)
Mutual labels:  typography
responsive-modular-scale.css
Responsive typography using CSS variables
Stars: ✭ 19 (-72.46%)
Mutual labels:  typography
standard-components
A specification for functional UI components
Stars: ✭ 52 (-24.64%)
Mutual labels:  typography
theme-ui-native
Build consistent, themeable React Native apps based on constraint-based design principles
Stars: ✭ 67 (-2.9%)
Mutual labels:  typography
python freetype
Python language binding for FreeType library
Stars: ✭ 20 (-71.01%)
Mutual labels:  typography
responsive type
A one size fits all responsive type solution. Demo: https://codepen.io/kylevilleneuve/pen/XqeVdZ
Stars: ✭ 53 (-23.19%)
Mutual labels:  typography
glyphcollector
glyphcollector.app
Stars: ✭ 47 (-31.88%)
Mutual labels:  typography
birman-typography-layouts-for-ubuntu
Типографские раскладки Ильи Бирмана для Ubuntu
Stars: ✭ 73 (+5.8%)
Mutual labels:  typography
Gluten
2 Axes/Variable/Super Soft/Display
Stars: ✭ 28 (-59.42%)
Mutual labels:  typography
ukrainian-typographic-layouts
Типографічні розкладки для української та російської мови / Типографские раскладки для украинского и русского языка
Stars: ✭ 69 (+0%)
Mutual labels:  typography
designs
Blockstack designs
Stars: ✭ 26 (-62.32%)
Mutual labels:  typography
ekzo
💫 Functional Sass framework for rapid and painless development
Stars: ✭ 32 (-53.62%)
Mutual labels:  typography

React FitText

FitText.js as a React v16+ component.

If you want to make specific text fit within a container, and then maintain that ratio across screen sizes, this component is for you.

FitText is a particularly useful approach for:

  • Predetermined content (ie. not user generated or dynamic)
  • Text that fits within a container until it hits a minimum or maximum font size, and then reflows normally from there
  • Multi-line text that fits

Alternatives

If you don’t have any of these requirements, another approach might suit you better. Some possible alternatives include:

<div class="example">
  Scale with the viewport
</div>
/* Minimum font size */
.example {
  font-size: 24px;
}

/* Scale linearly after this breakpoint */
@media (min-width: 480px) {
  .example {
    font-size: 5vw;
  }
}

If you’re curious why some sort of automatic scaling isn’t already possible using CSS alone, or why it might still be a challenge in the future, read more in this CSS Working Group drafts issue.

Differences from the existing React FitText

This component is written specifically for React v16 and up, includes tests, and uses state to avoid DOM manipulation.

The existing React FitText component by @gianu should still work with current versions of React, and is stateless, but manipulates the DOM directly to change font sizes.

The approach I’m using feels more React-appropriate, at least to me. I use this component regularly enough that it made sense for me to maintain my own version regardless.

Installation

npm install --save @kennethormandy/react-fittext

Example

import FitText from '@kennethormandy/react-fittext'
<FitText compressor={0.5}>The quick brown fox jumps over the lazy dog.</FitText>

With multiple children:

<FitText compressor={0.5}>
  <React.Fragment>
    <h2>Pangram</h2>
    <p>The quick brown fox jumps over the lazy dog</p>
  </React.Fragment>
</FitText>

Props

compressor

From the original FitText.js documentation:

If your text is resizing poorly, you'll want to turn tweak up/down “The Compressor.” It works a little like a guitar amp. The default is 1. —davatron5000

<FitText compressor={3}>The quick brown fox jumps over the lazy dog.</FitText>
<FitText compressor={1}>The quick brown fox jumps over the lazy dog.</FitText>
<FitText compressor={0.3}>The quick brown fox jumps over the lazy dog.</FitText>

minFontSize and maxFontSize

<FitText compressor={0.5} minFontSize={24} maxFontSize={96}>
  The quick brown fox jumps over the lazy dog.
</FitText>

debounce

Change the included debounce resize timeout. How long should React FitText wait before recalculating the fontSize?

<FitText debounce={3000} compressor={0.5}>
  The very slow brown fox
</FitText>

The default is 100 milliseconds.

defaultFontSize

React FitText needs the viewport size to determine the size the type, but you might want to provide an explicit fallback when using server-side rendering with React.

<FitText defaultFontSize={100} compressor={0.5}>
  The quick brown fox
</FitText>

The default is inherit, so typically you will already have a resonable fallback without using this prop, using CSS only. For example:

.headline {
  font-size: 6.25rem;
}
<div className="headline">
  <FitText compressor={0.5}>The quick brown fox</FitText>
</div>

vertical

Add the vertical prop to scale vertically, rather than horizontally (the default).

<div style={{ height: '75vh' }}>
  <FitText vertical compressor={1.25}>
    <ul>
      <li>Waterfront</li>
      <li>Vancouver City Centre</li>
      <li>Yaletown–Roundhouse</li>
      <li>Olympic Village</li>
      <li>Broadway–City Hall</li>
      <li>King Edward</li>
      <li>Oakridge–41st Avenue</li>
      <li>Langara–49th Avenue</li>
      <li>Marine Drive</li>
    </ul>
  </FitText>
</div>

parent

Use a different parent, other than the immediate parentNode, to calculate the vertical height.

<div id="js-example">
  <AnotherThing>
    <FitText vertical parent="js-example">
      {dynamicChildren}
    </FitText>
  </AnotherThing>
</div>
<div>
  <div style={{ height: '1000px' }} ref={el => (this.parentNode = el)}>
    <h1>A contrived example!</h1>
  </div>
  <FitText vertical parent={this.parentNode}>
    {dynamicChildren}
  </FitText>
</div>

Running locally

git clone https://github.com/kennethormandy/react-fittext
cd kennethormandy

# Install dependencies
npm install

# Run the project
npm start

Now, you can open http://localhost:8080 and modify src/dev.js while working on the project.

To run the Storybook stories instead:

npm run storybook

Samples

I’ve used various versions of this project in the following type specimen sites:

Other projects:

Credits

License

The MIT License (MIT)

Copyright © 2014 Sergio Rafael Gianazza
Copyright © 2017–2019 Kenneth Ormandy Inc.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].