Skip to content

Commit ea656f7

Browse files
feat: add onChange callback to useWindowSize
1 parent 3eb531a commit ea656f7

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/useWindowSize.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,52 @@ import { useEffect } from 'react';
33
import useRafState from './useRafState';
44
import { isBrowser, off, on } from './misc/util';
55

6+
// Define the type for options that can be passed to the hook
67
interface Options {
7-
initialWidth?: number;
8-
initialHeight?: number;
9-
onChange?: (width: number, height: number) => void;
8+
initialWidth?: number; // Initial width of the window (Default value is Infinity)
9+
initialHeight?: number; // Initial height of the window (Default value is Infinity)
10+
onChange?: (width: number, height: number) => void; // Callback function to execute on window resize (optional)
1011
}
1112

1213
const useWindowSize = ({
1314
initialWidth = Infinity,
1415
initialHeight = Infinity,
1516
onChange,
1617
}: Options = {}) => {
18+
// Use the useRafState hook to maintain the current window size (width and height)
1719
const [state, setState] = useRafState<{ width: number; height: number }>({
1820
width: isBrowser ? window.innerWidth : initialWidth,
1921
height: isBrowser ? window.innerHeight : initialHeight,
2022
});
2123

2224
useEffect((): (() => void) | void => {
25+
// Only run the effect on the browser (to avoid issues with SSR)
2326
if (isBrowser) {
2427
const handler = () => {
2528
const width = window.innerWidth;
2629
const height = window.innerHeight;
2730

31+
// Update the state with the new window size
2832
setState({
29-
width: window.innerWidth,
30-
height: window.innerHeight,
33+
width,
34+
height,
3135
});
3236

37+
// If an onChange callback is provided, call it with the new dimensions
3338
if (onChange) onChange(width, height);
3439
};
3540

41+
// Add event listener for the resize event
3642
on(window, 'resize', handler);
3743

44+
// Cleanup function to remove the event listener when the component is unmounted (it's for performance optimization)
3845
return () => {
3946
off(window, 'resize', handler);
4047
};
4148
}
4249
}, []);
4350

51+
// Return the current window size (width and height)
4452
return state;
4553
};
4654

stories/useWindowSize.story.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { storiesOf } from '@storybook/react';
22
import * as React from 'react';
33
import { useWindowSize } from '../src';
4+
import { action } from '@storybook/addon-actions'; // Import addon-actions
45
import ShowDocs from './util/ShowDocs';
56

67
const Demo = () => {
7-
const { width, height } = useWindowSize();
8+
const { width, height } = useWindowSize({
9+
// Log the resize event to the Storybook actions panel
10+
onChange: action('window resize'),
11+
});
812

913
return (
1014
<div>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy