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(useSize): the return value is always undefined #2071

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { renderHook, act } from '@testing-library/react';
import React, { useRef } from 'react';
import { renderHook, act, render, screen } from '@testing-library/react';
import useSize from '../index';

let callback;
Expand All @@ -14,11 +15,41 @@ jest.mock('resize-observer-polyfill', () => {

// test about Resize Observer see https://github.com/que-etc/resize-observer-polyfill/tree/master/tests
describe('useSize', () => {
it('with argument', () => {
it('should work when target is a mounted DOM', () => {
const hook = renderHook(() => useSize(document.body));
expect(hook.result.current).toEqual({ height: 0, width: 0 });
});

it('should work when target is a `MutableRefObject`', async () => {
const mockRaf = jest
.spyOn(window, 'requestAnimationFrame')
.mockImplementation((cb: FrameRequestCallback) => {
cb(0);
return 0;
});

function Setup() {
const ref = useRef(null);
const size = useSize(ref);

return (
<div ref={ref}>
<div>width: {String(size?.width)}</div>
<div>height: {String(size?.height)}</div>
</div>
);
}

render(<Setup />);
expect(await screen.findByText(/^width/)).toHaveTextContent('width: undefined');
expect(await screen.findByText(/^height/)).toHaveTextContent('height: undefined');

act(() => callback([{ target: { clientWidth: 10, clientHeight: 10 } }]));
expect(await screen.findByText(/^width/)).toHaveTextContent('width: 10');
expect(await screen.findByText(/^height/)).toHaveTextContent('height: 10');
mockRaf.mockRestore();
});

it('should not work when target is null', () => {
expect(() => {
renderHook(() => useSize(null));
Expand Down
6 changes: 3 additions & 3 deletions packages/hooks/src/useSize/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ const size = useSize(target);

### Result

| Property | Description | Type |
| -------- | ------------------- | ------------------------------------------------ |
| size | Size of the element | `{ width: number, height: number } \| undefined` |
| Property | Description | Type | Default |
| -------- | ------------------- | ------------------------------------------------ | ------------------------------------------------------------------------- |
| size | Size of the element | `{ width: number, height: number } \| undefined` | `{ width: target.clientWidth, height: target.clientHeight } \| undefined` |
4 changes: 3 additions & 1 deletion packages/hooks/src/useSize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import useIsomorphicLayoutEffectWithTarget from '../utils/useIsomorphicLayoutEff
type Size = { width: number; height: number };

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

useIsomorphicLayoutEffectWithTarget(
() => {
el = getTargetElement(target);

if (!el) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/hooks/src/useSize/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ const size = useSize(target);

### Result

| 参数 | 说明 | 类型 |
| ---- | -------------- | ------------------------------------------------ |
| size | DOM 节点的尺寸 | `{ width: number, height: number } \| undefined` |
| 参数 | 说明 | 类型 | 默认值 |
| ---- | -------------- | ------------------------------------------------ | ------------------------------------------------------------------------- |
| size | DOM 节点的尺寸 | `{ width: number, height: number } \| undefined` | `{ width: target.clientWidth, height: target.clientHeight } \| undefined` |