Created
May 27, 2021 20:42
-
-
Save jtmthf/60541c203da89a0ad9cd49ad5668ea14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { RefObject, useEffect, useState } from 'react'; | |
| export function useLineCount(ref: RefObject<HTMLElement>): number { | |
| const [lines, setLines] = useState(0); | |
| useEffect(() => { | |
| const resizeObserver = new ResizeObserver(entries => { | |
| entries.forEach(entry => { | |
| const elementHeight = (entry.target as HTMLElement).offsetHeight; | |
| const lineHeight = parseInt(getComputedStyle(entry.target).lineHeight, 10); | |
| setLines(elementHeight / lineHeight); | |
| }) | |
| }); | |
| if (ref.current) { | |
| resizeObserver.observe(ref.current) | |
| } | |
| return () => { | |
| resizeObserver.disconnect(); | |
| } | |
| }, [ref]); | |
| return lines; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment