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

onChange to useWindowSize #2518

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 23 additions & 5 deletions src/useWindowSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@ import { useEffect } from 'react';
import useRafState from './useRafState';
import { isBrowser, off, on } from './misc/util';

const useWindowSize = (initialWidth = Infinity, initialHeight = Infinity) => {
const [state, setState] = useRafState<{ width: number; height: number }>({
export interface Size {
height: number;
width: number;
}

export interface Options {
initialWidth?: number;
initialHeight?: number;
onChange?: (size: Size) => void;
}

const useWindowSize = (options?: Options) => {
const { initialHeight = Infinity, initialWidth = Infinity, onChange } = options || {};

const [state, setState] = useRafState<Size>({
width: isBrowser ? window.innerWidth : initialWidth,
height: isBrowser ? window.innerHeight : initialHeight,
});

useEffect((): (() => void) | void => {
if (isBrowser) {
const handler = () => {
setState({
width: window.innerWidth,
height: window.innerHeight,
setState(() => {
const size = { width: window.innerWidth, height: window.innerHeight };

if (onChange) {
onChange(size);
}

return size;
});
};

Expand Down
43 changes: 42 additions & 1 deletion tests/useWindowSize.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('useWindowSize', () => {
});

it('should use passed parameters as initial values in case of non-browser use', () => {
const hook = getHook(1, 1);
const hook = getHook({ initialWidth: 1, initialHeight: 1 });

expect(hook.result.current.height).toBe(isBrowser ? window.innerHeight : 1);
expect(hook.result.current.width).toBe(isBrowser ? window.innerWidth : 1);
Expand Down Expand Up @@ -85,4 +85,45 @@ describe('useWindowSize', () => {

expect(hook.result.current.width).toBe(2048);
});

it('should run onChange function on re-render', () => {
let testValue = false;

getHook({
onChange: () => {
testValue = true;
},
});

act(() => {
triggerResize('height', 2048);
requestAnimationFrame.step();
});

expect(testValue).toBe(true);
});

it('should provide right size value for onChange function', () => {
let size = { width: Infinity, height: Infinity };

const hook = getHook({
onChange: ({ width, height }) => {
size = { width, height };
},
});

act(() => {
triggerResize('height', 2048);
requestAnimationFrame.step();
});

act(() => {
triggerResize('width', 2048);
requestAnimationFrame.step();
});

expect(hook.result.current.height).toBe(2048);
expect(hook.result.current.width).toBe(2048);
expect(size).toEqual(hook.result.current);
});
});