项目作者: chrisdrackett

项目描述 :
✂️ Adjust text size to fit its container.
高级语言: JavaScript
项目地址: git://github.com/chrisdrackett/react-tailor.git
创建时间: 2018-04-01T04:44:36Z
项目社区:https://github.com/chrisdrackett/react-tailor

开源协议:MIT License

下载


React Tailor ✂️

npm version
Dependency Status

demo

Tailor adjusts the size of text within its children to try and fit it within the available horizontal and vertical space.

Currently tailor only works (and enforces) a single line of non-wrapping text. This might change in the future, but there are also other solution that work with multiple lines of text (see Similar Projects below).

Uses React Measure to resize an element only if its size changes.

Install

yarn add react-tailor

Tailor Component

Wrap any child component and calculate its client rect.

Props

canGrow: boolean

default: false

Can the text size up above its initial size?

minSize: number

default: 11

The minimum size (in px) that text will be sized down to fit. Once text has reached this size and ellipsis will be added to any text cut off.

Example

  1. import Tailor from 'react-tailor'
  2. class Tailor extends Component {
  3. render() {
  4. return (
  5. <div>
  6. <h1>
  7. <Tailor>Title Text</Tailor>
  8. </h1>
  9. <ul>
  10. <li>
  11. <Tailor>List Item</Tailor>
  12. </li>
  13. <li>
  14. <Tailor>List Item 2</Tailor>
  15. </li>
  16. </ul>
  17. </div>
  18. )
  19. }
  20. }

Running the demo locally

  1. clone repo

    git clone git@github.com:chrisdrackett/react-tailor.git

  2. install dependencies

    yarn

  3. run dev mode

    yarn start

  4. open your browser and visit: http://localhost:3000/

Similar Projects

  • React FitText

    similar, but resizes all elements on browser resize even if their parent is the same size. Does not calculate best size automatically.

  • React TextFit

    Supports paragraphs but is heavier.