-
Notifications
You must be signed in to change notification settings - Fork 2.2k
useGridDimensions: useSyncExternalStore implementation #3968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1f1bd62
5a25695
1c32fe5
9e2ddd5
0617759
f5623d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,99 @@ | ||
| import { useLayoutEffect, useRef, useState } from 'react'; | ||
| import { flushSync } from 'react-dom'; | ||
| import { useCallback, useId, useLayoutEffect, useRef, useSyncExternalStore } from 'react'; | ||
|
|
||
| const initialSize: ResizeObserverSize = { | ||
| inlineSize: 1, | ||
| blockSize: 1 | ||
| }; | ||
|
|
||
| const targetToIdMap = new Map<HTMLDivElement, string>(); | ||
| const idToTargetMap = new Map<string, HTMLDivElement>(); | ||
| // use an unmanaged WeakMap so we preserve the cache even when | ||
| // the component partially unmounts via Suspense or Activity | ||
| const sizeMap = new WeakMap<HTMLDivElement, ResizeObserverSize>(); | ||
| const subscribers = new Map<string, () => void>(); | ||
|
|
||
| // don't break in Node.js (SSR), jsdom, and environments that don't support ResizeObserver | ||
| const resizeObserver = | ||
| // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
| globalThis.ResizeObserver == null ? null : new ResizeObserver(resizeObserverCallback); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll have 1 RO instance for all rendered grids on the page, so all resize events will be batched together -> improved perf? |
||
|
|
||
| function resizeObserverCallback(entries: ResizeObserverEntry[]) { | ||
| for (const entry of entries) { | ||
| const target = entry.target as HTMLDivElement; | ||
|
|
||
| if (!targetToIdMap.has(target)) continue; | ||
|
|
||
| const id = targetToIdMap.get(target)!; | ||
|
|
||
| updateSize(target, id, entry.contentBoxSize[0]); | ||
| } | ||
| } | ||
|
|
||
| function updateSize(target: HTMLDivElement, id: string, size: ResizeObserverSize) { | ||
| if (sizeMap.has(target)) { | ||
| const prevSize = sizeMap.get(target)!; | ||
| if (prevSize.inlineSize === size.inlineSize && prevSize.blockSize === size.blockSize) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| sizeMap.set(target, size); | ||
| subscribers.get(id)?.(); | ||
| } | ||
|
|
||
| function getServerSnapshot(): ResizeObserverSize { | ||
| return initialSize; | ||
| } | ||
|
|
||
| export function useGridDimensions() { | ||
| const id = useId(); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using an id to sync things. |
||
| const gridRef = useRef<HTMLDivElement>(null); | ||
| const [inlineSize, setInlineSize] = useState(1); | ||
| const [blockSize, setBlockSize] = useState(1); | ||
|
|
||
| useLayoutEffect(() => { | ||
| const { ResizeObserver } = window; | ||
| const subscribe = useCallback( | ||
| (onStoreChange: () => void) => { | ||
| subscribers.set(id, onStoreChange); | ||
|
|
||
| // don't break in Node.js (SSR), jsdom, and browsers that don't support ResizeObserver | ||
| // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
| if (ResizeObserver == null) return; | ||
| return () => { | ||
| subscribers.delete(id); | ||
| }; | ||
| }, | ||
| [id] | ||
| ); | ||
|
|
||
| const { clientWidth, clientHeight } = gridRef.current!; | ||
| const getSnapshot = useCallback((): ResizeObserverSize => { | ||
| if (idToTargetMap.has(id)) { | ||
| const target = idToTargetMap.get(id)!; | ||
| if (sizeMap.has(target)) { | ||
| return sizeMap.get(target)!; | ||
| } | ||
| } | ||
| return initialSize; | ||
| }, [id]); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| setInlineSize(clientWidth); | ||
| setBlockSize(clientHeight); | ||
| // We use `useSyncExternalStore` instead of `useState` to avoid tearing, | ||
| // which can lead to flashing scrollbars. | ||
| const { inlineSize, blockSize } = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); | ||
|
|
||
| useLayoutEffect(() => { | ||
| const target = gridRef.current!; | ||
|
|
||
| const resizeObserver = new ResizeObserver((entries) => { | ||
| const size = entries[0].contentBoxSize[0]; | ||
| targetToIdMap.set(target, id); | ||
| idToTargetMap.set(id, target); | ||
| resizeObserver?.observe(target); | ||
|
|
||
| // we use flushSync here to avoid flashing scrollbars | ||
| flushSync(() => { | ||
| setInlineSize(size.inlineSize); | ||
| setBlockSize(size.blockSize); | ||
| if (!sizeMap.has(target)) { | ||
| updateSize(target, id, { | ||
| inlineSize: target.clientWidth, | ||
| blockSize: target.clientHeight | ||
| }); | ||
| }); | ||
| resizeObserver.observe(gridRef.current!); | ||
| } | ||
|
|
||
| return () => { | ||
| resizeObserver.disconnect(); | ||
| targetToIdMap.delete(target); | ||
| idToTargetMap.delete(id); | ||
| resizeObserver?.unobserve(target); | ||
| }; | ||
| }, []); | ||
| }, [id]); | ||
|
|
||
| return [gridRef, inlineSize, blockSize] as const; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,6 @@ beforeAll(() => { | |
| globalThis.console = { | ||
| ...console, | ||
| error(...params) { | ||
| if ( | ||
| params[0] instanceof Error && | ||
| params[0].message === 'ResizeObserver loop completed with undelivered notifications.' | ||
| ) { | ||
| return; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just hoping this will be resolved 🤞 |
||
| } | ||
|
|
||
| consoleErrorOrConsoleWarnWereCalled = true; | ||
| console.log(...params); | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took great care to ensure this works great even if the components unmounts but the div isn't deleted.
The updated hook is more complicated than before, but should behave great with concurrent rendering ✌️