-
Notifications
You must be signed in to change notification settings - Fork 839
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
[useResizeObserver] Fix initial width and height being 0 in docs example #7518
Conversation
Fixes #7458. Signed-off-by: Eng Zer Jun <[email protected]>
👋 Since this is a community submitted pull request, a Buildkite build has not been started automatically. Would an Elastic organization member please verify the contents of this pull request and kick off a build manually? |
Signed-off-by: Eng Zer Jun <[email protected]>
height: {height}; width: ${width} | ||
height: {height}; width: {width} |
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.
Hey @Juneezee! Thanks so much for this PR, it's really helped me realize what the underlying problem is. Rather than fixing the eui/src-docs/src/views/resize_observer/resize_observer_hook.js Lines 27 to 28 in 09e6dd7
to const [resizeRef, setResizeRef] = useState();
const dimensions = useResizeObserver(resizeRef);
// ...
<div className="eui-displayInlineBlock" ref={setResizeRef}> Also solves the issue of returning the correct width/height on mount without requiring any source code change. I think I'd prefer this approach (having consumers solve it) rather than us adding an extra rerender. This allows consumers to opt in to What do you think? |
Reference: #7518 (comment) Signed-off-by: Eng Zer Jun <[email protected]>
@cee-chen Thanks for reviewing this PR! Your suggestion looks great to me. I have updated the PR, please take a look again. Thanks 😄 ! |
buildkite test this |
Preview staging links for this PR:
|
💚 Build Succeeded
|
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.
Thanks again @Juneezee, you're amazing!! 🚀
Summary
Fixes #7458.
Old PR description
In the
useResizeObserver
hook, we set the initial state of width and height as 0:eui/src/components/observer/resize_observer/resize_observer.tsx
Line 73 in 239dc72
This is okay because we have a
useEffect
that will trigger a re-render when thecontainer
has changed:eui/src/components/observer/resize_observer/resize_observer.tsx
Lines 95 to 120 in 239dc72
We are calling our customized
setSize
in theuseEffect
. This customsetSize
only calls the_setSize
(fromuseState
) when the new and old dimension differs.eui/src/components/observer/resize_observer/resize_observer.tsx
Lines 78 to 93 in 239dc72
To fix the bug, we simply set the initial width and height as
undefined
. Then, theelse
block inuseEffect
will "initialise" the width and height to0
. NowsetSize
detects that the old and new dimension differs (undefined
to0
), so it will update the state and trigger a re-render.eui/src/components/observer/resize_observer/resize_observer.tsx
Lines 117 to 119 in 239dc72
As a result,
useResizeObserver
will now return the initial dimension correctly.This solution is inspired by: https://github.com/ZeeCoder/use-resize-observer/blob/465fbc11c92e1b97a1dcb526b44ce4d889956951/src/index.ts#L62-L68
QA
useResizeObserver
now returns the sameheight
andwidth
as theEuiResizeObserver
example2024-02-07.23-51-46.mp4
General checklist
Safari,Edge(I'm using a Linux machine)Added or updated jest and cypress testsThe test is marked asskip
eui/src/components/observer/resize_observer/resize_observer.test.tsx
Line 25 in 239dc72