react-use-text-measurer
Advanced tools
Comparing version
{ | ||
"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). |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18323
1.99%61
32.61%0
-100%