0.4.4 • Published 3 years ago

@imagemarker/use-text-width v0.4.4

Weekly downloads
58
License
MIT
Repository
github
Last release
3 years ago

useTextWidth

React hook to calculate text width

Install

npm install @imagemarker/use-text-width
# OR
yarn add @imagemarker/use-text-width

Usage

Run yarn start to view example on http://localhost:5000

There are 2 types of usages:

1. text & font

You can pass object having text (mandatory, string or string[] returns max one) and font (optional, CSS font string) properties to calculate width.

import { useTextWidth } from '@imagemarker/use-text-width';

const App = () => {
  const width = useTextWidth({ text: 'Hello world!', font: '20px Times' });

  return (
    <p>Hello world! text width: {width}px</p>
  );
};

2. ref

You can pass object having ref (mandatory, HTMLElement) property to calculate width.

import { useTextWidth } from '@imagemarker/use-text-width';

const App = () => {
  const ref = useRef();
  const width = useTextWidth({ ref });

  return (
    <p ref={ref} style={{ fontSize: '16pt', fontWeight: 'bold' }}>
      lorem ipsum! text width: {width}px
    </p>
  );
};

License

The MIT License

Please see License File for more information.

0.4.4

3 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago