(
containerRef: {current: HTMLDivElement | null, ...},
totalChildWidth: number,
)
| 112 | } |
| 113 | |
| 114 | export function useIsOverflowing( |
| 115 | containerRef: {current: HTMLDivElement | null, ...}, |
| 116 | totalChildWidth: number, |
| 117 | ): boolean { |
| 118 | const [isOverflowing, setIsOverflowing] = useState<boolean>(false); |
| 119 | |
| 120 | // It's important to use a layout effect, so that we avoid showing a flash of overflowed content. |
| 121 | useLayoutEffect(() => { |
| 122 | if (containerRef.current === null) { |
| 123 | return () => {}; |
| 124 | } |
| 125 | |
| 126 | const container = ((containerRef.current: any): HTMLDivElement); |
| 127 | |
| 128 | const handleResize = () => |
| 129 | setIsOverflowing(container.clientWidth <= totalChildWidth); |
| 130 | |
| 131 | handleResize(); |
| 132 | |
| 133 | // It's important to listen to the ownerDocument.defaultView to support the browser extension. |
| 134 | // Here we use portals to render individual tabs (e.g. Profiler), |
| 135 | // and the root document might belong to a different window. |
| 136 | const ownerWindow = container.ownerDocument.defaultView; |
| 137 | ownerWindow.addEventListener('resize', handleResize); |
| 138 | return () => ownerWindow.removeEventListener('resize', handleResize); |
| 139 | }, [containerRef, totalChildWidth]); |
| 140 | |
| 141 | return isOverflowing; |
| 142 | } |
| 143 | |
| 144 | // Forked from https://usehooks.com/useLocalStorage/ |
| 145 | export function useLocalStorage<T>( |
no test coverage detected