You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

react-use-text-measurer

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-text-measurer - npm Package Compare versions

Comparing version

to
1.0.3

6

package.json
{
"name": "react-use-text-measurer",
"version": "1.0.2",
"version": "1.0.3",
"description": "Synchronously measure text size in an offscreen <canvas /> via react hook.",

@@ -38,3 +38,2 @@ "sideEffects": false,

"devDependencies": {
"@babel/core": "^7.13.8",
"@storybook/addon-actions": "^6.1.20",

@@ -52,3 +51,3 @@ "@storybook/addon-essentials": "^6.1.20",

"@types/react": "^17.0.2",
"babel-loader": "^8.2.2",
"@types/react-window": "^1.8.2",
"canvas": "^2.6.1",

@@ -61,2 +60,3 @@ "del-cli": "^3.0.1",

"react-dom": ">16.8.4",
"react-window": "^1.8.6",
"ts-jest": "^26.5.2",

@@ -63,0 +63,0 @@ "ts-node": "^9.1.1",

# react-use-text-measurer
A hook for synchronously measuring text in react applications without DOM-thrashing! Uses an offscreen `<canvas />` along with [`CanvasRenderingContext2D.measureText()`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/measureText) to make measurements.
A hook for synchronously measuring text in react applications using an offscreen `<canvas />` along with [`CanvasRenderingContext2D.measureText()`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/measureText) to make measurements.
```tsx
const YourComponent = (props) => {
const measureTitle = useTextMeasurer("600 24px 'Source Sans Pro'");
return (
<>
<h1>{props.title}</h1>
<div>👆 {measureTitle(props.title)}px</div>
</>
)
}
```
## Installation

@@ -37,3 +50,2 @@

const measureTitle = useTextMeasurer("600 24px 'Source Sans Pro'");
const titleWidth = measureTitle(props.title);

@@ -43,6 +55,9 @@ return (

<h1>{props.title}</h1>
<div>({titleWidth}px) 👆</div>
<div>👆 {measureTitle(props.title)}px</div>
</>
)
}
```
```
## Why?
For example, to build a [windowed list without DOM measuring](https://github.com/theruther4d/react-use-text-measurer/blob/master/src/stories/examples.stories.tsx#L16).