Function that calculates width of typed text
npm install calculate-text-width
How to use
===
``js
import calculateTextWidth from "calculate-text-width";
/*
requires two props "value" and "font"
- defaultFont: normal 500 14px sans-serif
*/
const calculatedWidth = calculateTextWidth('calculate my width', 'normal 500 14px sans-serif')
console.log(calculatedWidth) // 114.37890625
`
How to use with React
===
`jsx
import React, { useState, useMemo } from "react"
import calculateTextWidth from "calculate-text-width"
export default () => {
const [value, setValue] = useState("change me")
return (
<>
,
font: "normal 500 14px sans-serif"
}}
value={value}
type="text"
onChange={evt => setValue(evt.target.value)}
/>
>
)
}``Thank you
#### Yusuf Özlü