Skip to content
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

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelogs/upcoming/7518.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**Bug fixes**

- Fixed `useResizeObserver` hook to return the initial dimension correctly
([#7518](https://github.com/elastic/eui/pull/7518)).
2 changes: 1 addition & 1 deletion src-docs/src/views/resize_observer/resize_observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const ResizeObserverExample = () => {
<EuiText>
<p>
<EuiCode>
height: {height}; width: ${width}
height: {height}; width: {width}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the $ typo

image

</EuiCode>
</p>
</EuiText>
Expand Down
7 changes: 5 additions & 2 deletions src/components/observer/resize_observer/resize_observer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ export const useResizeObserver = (
container: Element | null,
dimension?: 'width' | 'height'
) => {
const [size, _setSize] = useState({ width: 0, height: 0 });
const [size, _setSize] = useState<{
width: undefined | number;
height: undefined | number;
}>({ width: undefined, height: undefined });

// _currentDimensions and _setSize are used to only store the
// new state (and trigger a re-render) when the new dimensions actually differ
Expand Down Expand Up @@ -119,5 +122,5 @@ export const useResizeObserver = (
}
}, [container, setSize]);

return size;
return { width: size.width ?? 0, height: size.height ?? 0 };
};
Loading