Download frontend/src/hooks/use-element-width.ts from turtle89431/Moxie-Multimedia: direct link, hf CLI and curl.
- Browser
- Download file 474 Bytes
-
https://huggingface.co/turtle89431/Moxie-Multimedia/resolve/main/frontend/src/hooks/use-element-width.ts
- Command line
-
hf download hf://turtle89431/Moxie-Multimedia/frontend/src/hooks/use-element-width.ts
-
curl -L -o use-element-width.ts https://huggingface.co/turtle89431/Moxie-Multimedia/resolve/main/frontend/src/hooks/use-element-width.ts
474 Bytes
| import { useLayoutEffect, useState } from 'react' | |
| import type { RefObject } from 'react' | |
| export function useElementWidth<T extends HTMLElement>(ref: RefObject<T | null>): number { | |
| const [width, setWidth] = useState(0) | |
| useLayoutEffect(() => { | |
| const el = ref.current | |
| if (!el) return | |
| const ro = new ResizeObserver(() => setWidth(el.clientWidth)) | |
| ro.observe(el) | |
| setWidth(el.clientWidth) | |
| return () => ro.disconnect() | |
| }, [ref]) | |
| return width | |
| } | |