# @tag0/use-text-width

> React hook to measure text width

Latest version **1.2.0** (published 2021-08-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tag0/use-text-width
pnpm add @tag0/use-text-width
yarn add @tag0/use-text-width
bun add @tag0/use-text-width
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2021-08-23 |
| First published | 2021-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Maintainers | erhangundogan |
| Keywords | react, hook, canvas, text-width |

## Links

- npm: https://www.npmjs.com/package/@tag0/use-text-width
- Repository: https://github.com/TagZero/use-text-width
- Homepage: https://github.com/TagZero/use-text-width#readme
- Issues: https://github.com/TagZero/use-text-width/issues
- npm.io page: https://npm.io/package/@tag0/use-text-width

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2021-08-23
- 1.1.0 — 2021-02-25
- 1.0.0 — 2021-01-11

## README

# useTextWidth

[![License][license-src]][license-href]

React hook to calculate text width

## Install

```bash
npm install @tag0/use-text-width
# OR
yarn add @tag0/use-text-width
```

## Usage

Run `yarn start` to view [example](src/examples/simple.tsx) 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. 
 
```jsx
import { useTextWidth } from '@tag0/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.
 
```jsx
import { useTextWidth } from '@tag0/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-src]: https://img.shields.io/badge/license-MIT-brightgreen.svg
[license-href]: LICENSE.md

---
_Source: https://npm.io/package/@tag0/use-text-width · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
