feat: add onChange callback to useWindowSize

This commit is contained in:
Mahmoud El-Gammal 2024-12-02 17:02:44 +02:00
parent 3eb531ac9e
commit ea656f7e75
2 changed files with 18 additions and 6 deletions

View File

@ -3,10 +3,11 @@ import { useEffect } from 'react';
import useRafState from './useRafState';
import { isBrowser, off, on } from './misc/util';
// Define the type for options that can be passed to the hook
interface Options {
initialWidth?: number;
initialHeight?: number;
onChange?: (width: number, height: number) => void;
initialWidth?: number; // Initial width of the window (Default value is Infinity)
initialHeight?: number; // Initial height of the window (Default value is Infinity)
onChange?: (width: number, height: number) => void; // Callback function to execute on window resize (optional)
}
const useWindowSize = ({
@ -14,33 +15,40 @@ const useWindowSize = ({
initialHeight = Infinity,
onChange,
}: Options = {}) => {
// Use the useRafState hook to maintain the current window size (width and height)
const [state, setState] = useRafState<{ width: number; height: number }>({
width: isBrowser ? window.innerWidth : initialWidth,
height: isBrowser ? window.innerHeight : initialHeight,
});
useEffect((): (() => void) | void => {
// Only run the effect on the browser (to avoid issues with SSR)
if (isBrowser) {
const handler = () => {
const width = window.innerWidth;
const height = window.innerHeight;
// Update the state with the new window size
setState({
width: window.innerWidth,
height: window.innerHeight,
width,
height,
});
// If an onChange callback is provided, call it with the new dimensions
if (onChange) onChange(width, height);
};
// Add event listener for the resize event
on(window, 'resize', handler);
// Cleanup function to remove the event listener when the component is unmounted (it's for performance optimization)
return () => {
off(window, 'resize', handler);
};
}
}, []);
// Return the current window size (width and height)
return state;
};

View File

@ -1,10 +1,14 @@
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useWindowSize } from '../src';
import { action } from '@storybook/addon-actions'; // Import addon-actions
import ShowDocs from './util/ShowDocs';
const Demo = () => {
const { width, height } = useWindowSize();
const { width, height } = useWindowSize({
// Log the resize event to the Storybook actions panel
onChange: action('window resize'),
});
return (
<div>