-
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
Open
nstepien
wants to merge
13
commits into
main
Choose a base branch
from
externaldimensions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+74
−31
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1f1bd62
useGridDimensions: useSyncExternalStore implementation
nstepien 5a25695
Merge branch 'main' into externaldimensions
nstepien 1c32fe5
increase actionTimeout
nstepien 9e2ddd5
Merge branch 'main' into externaldimensions
nstepien 0617759
Merge branch 'main' into externaldimensions
nstepien f5623d4
Merge branch 'main' into externaldimensions
nstepien a9c35e5
fix comment
nstepien 65fef94
Merge branch 'main' into externaldimensions
nstepien 4b7952b
shrinking master detail
nstepien 72fbfe1
Merge branch 'main' into externaldimensions
nstepien 26ad3ad
"Come on, Mr. Frodo. I can't access ref.current during render... but …
nstepien 5560676
another weakmap
nstepien 56ece28
Merge branch 'main' into externaldimensions
nstepien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,86 @@ | ||
| import { useLayoutEffect, useRef, useState } from 'react'; | ||
| import { flushSync } from 'react-dom'; | ||
| import { useCallback, useLayoutEffect, useRef, useSyncExternalStore, type RefObject } from 'react'; | ||
|
|
||
| const initialSize: ResizeObserverSize = { | ||
| inlineSize: 1, | ||
| blockSize: 1 | ||
| }; | ||
|
|
||
| // use an unmanaged WeakMap so we preserve the cache even when | ||
| // the component partially unmounts via Suspense or Activity | ||
| const sizeMap = new WeakMap<RefObject<HTMLDivElement | null>, ResizeObserverSize>(); | ||
| const targetToRefMap = new WeakMap<HTMLDivElement, RefObject<HTMLDivElement | null>>(); | ||
| const subscribers = new Map<RefObject<HTMLDivElement | null>, () => 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); | ||
|
|
||
| function resizeObserverCallback(entries: ResizeObserverEntry[]) { | ||
| for (const entry of entries) { | ||
| const target = entry.target as HTMLDivElement; | ||
|
|
||
| if (targetToRefMap.has(target)) { | ||
| const ref = targetToRefMap.get(target)!; | ||
| updateSize(ref, entry.contentBoxSize[0]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function updateSize(ref: RefObject<HTMLDivElement | null>, size: ResizeObserverSize) { | ||
| if (sizeMap.has(ref)) { | ||
| const prevSize = sizeMap.get(ref)!; | ||
| if (prevSize.inlineSize === size.inlineSize && prevSize.blockSize === size.blockSize) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| sizeMap.set(ref, size); | ||
| subscribers.get(ref)?.(); | ||
| } | ||
|
|
||
| function getServerSnapshot(): ResizeObserverSize { | ||
| return initialSize; | ||
| } | ||
|
|
||
| export function useGridDimensions() { | ||
| const gridRef = useRef<HTMLDivElement>(null); | ||
| const [inlineSize, setInlineSize] = useState(1); | ||
| const [blockSize, setBlockSize] = useState(1); | ||
| const ref = useRef<HTMLDivElement>(null); | ||
|
|
||
| useLayoutEffect(() => { | ||
| const { ResizeObserver } = window; | ||
| const subscribe = useCallback((onStoreChange: () => void) => { | ||
| subscribers.set(ref, 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(ref); | ||
| }; | ||
| }, []); | ||
|
|
||
| const getSnapshot = useCallback((): ResizeObserverSize => { | ||
| // ref.current is null during the initial render, when suspending, or in <Activity mode="hidden">. | ||
| // We use ref as key instead to access stable values regardless of rendering state. | ||
| return sizeMap.has(ref) ? sizeMap.get(ref)! : initialSize; | ||
| }, []); | ||
|
|
||
| const { clientWidth, clientHeight } = gridRef.current!; | ||
| // We use `useSyncExternalStore` instead of `useState` to avoid tearing, | ||
| // which can lead to flashing scrollbars. | ||
| const { inlineSize, blockSize } = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); | ||
|
|
||
| setInlineSize(clientWidth); | ||
| setBlockSize(clientHeight); | ||
| useLayoutEffect(() => { | ||
| const target = ref.current!; | ||
|
|
||
| const resizeObserver = new ResizeObserver((entries) => { | ||
| const size = entries[0].contentBoxSize[0]; | ||
| targetToRefMap.set(target, ref); | ||
| resizeObserver?.observe(target); | ||
|
|
||
| // we use flushSync here to avoid flashing scrollbars | ||
| flushSync(() => { | ||
| setInlineSize(size.inlineSize); | ||
| setBlockSize(size.blockSize); | ||
| if (!sizeMap.has(ref)) { | ||
| updateSize(ref, { | ||
| inlineSize: target.clientWidth, | ||
| blockSize: target.clientHeight | ||
| }); | ||
| }); | ||
| resizeObserver.observe(gridRef.current!); | ||
| } | ||
|
|
||
| return () => { | ||
| resizeObserver.disconnect(); | ||
| resizeObserver?.unobserve(target); | ||
| }; | ||
| }, []); | ||
|
|
||
| return [gridRef, inlineSize, blockSize] as const; | ||
| return [ref, inlineSize, blockSize] as const; | ||
| } | ||
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
| 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); | ||
| }, | ||
|
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We'll have 1 RO instance for all rendered grids on the page, so all resize events will be batched together -> improved perf?