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

fix: use getBoundingClientRect to get the exact size #2482

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions packages/hooks/src/useSize/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('useSize', () => {
expect(await screen.findByText(/^width/)).toHaveTextContent('width: undefined');
expect(await screen.findByText(/^height/)).toHaveTextContent('height: undefined');

act(() => callback([{ target: { clientWidth: 10, clientHeight: 10 } }]));
act(() => callback([{ target: { getBoundingClientRect: () => ({ width: 10, height: 10 }) } }]));
expect(await screen.findByText(/^width/)).toHaveTextContent('width: 10');
expect(await screen.findByText(/^height/)).toHaveTextContent('height: 10');
mockRaf.mockRestore();
Expand All @@ -70,8 +70,10 @@ describe('useSize', () => {
callback([
{
target: {
clientWidth: 100,
clientHeight: 50,
getBoundingClientRect: () => ({
width: 100,
height: 50,
}),
},
},
]);
Expand Down
15 changes: 7 additions & 8 deletions packages/hooks/src/useSize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import useIsomorphicLayoutEffectWithTarget from '../utils/useIsomorphicLayoutEff
type Size = { width: number; height: number };

function useSize(target: BasicTarget): Size | undefined {
const [state, setState] = useRafState<Size | undefined>(
() => {
const el = getTargetElement(target);
return el ? { width: el.clientWidth, height: el.clientHeight } : undefined
},
);
const [state, setState] = useRafState<Size | undefined>(() => {
const el = getTargetElement(target);
const rect = el?.getBoundingClientRect();
return rect ? { width: rect.width, height: rect.height } : undefined;
});

useIsomorphicLayoutEffectWithTarget(
() => {
Expand All @@ -24,8 +23,8 @@ function useSize(target: BasicTarget): Size | undefined {

const resizeObserver = new ResizeObserver((entries) => {
entries.forEach((entry) => {
const { clientWidth, clientHeight } = entry.target;
Copy link
Collaborator

Choose a reason for hiding this comment

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

不能这么改,clientWidth 和 getBoundingClientRect 的 width 是有差别的,clientWidth 是不包含border 的,width 是包含的,这样改会有 break change

Copy link
Author

Choose a reason for hiding this comment

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

👌,那可以考虑加一个 useBoundingClientRect hook?

Copy link
Collaborator

Choose a reason for hiding this comment

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

👌,那可以考虑加一个 useBoundingClientRect hook?

v4 大版本考虑一下怎么把这些相关的统一一下,这个 pr 我先 close 了

setState({ width: clientWidth, height: clientHeight });
const { width, height } = entry.target.getBoundingClientRect();
setState({ width, height });
});
});
resizeObserver.observe(el);
Expand Down