Skip to content

Commit

Permalink
refactor: useWindowSize hook to re-render only after resize complet…
Browse files Browse the repository at this point in the history
…ion (#1704)

* refactor: useWindowSize hook

* chore: force min-width and tweak changeset

* chore: tweak changeset

---------

Co-authored-by: Daniel Sinclair <d@niel.nyc>
  • Loading branch information
magiziz and DanielSinclair committed Feb 1, 2024
1 parent 41616b9 commit c0a644a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-spoons-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rainbow-me/rainbowkit": patch
---

Fixed an issue that caused components to re-render on every window resize event. Now components will only re-render once the user has finished resizing their window.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { style } from '@vanilla-extract/css';
import { largeScreenMinWidth, sprinkles } from '../../css/sprinkles.css';
import { sprinkles } from '../../css/sprinkles.css';
export const QRCodeBackgroundClassName = style([
{
background: 'white',
Expand All @@ -18,12 +18,7 @@ export const ScrollClassName = style([

// biome-ignore format: design system keys
export const sidebar = style({
'@media': {
[`screen and (min-width: ${largeScreenMinWidth}px)`]: {
minWidth: '287px',
},
},
'minWidth': '246px',
minWidth: '287px',
});

export const sidebarCompactMode = style({
Expand Down
10 changes: 2 additions & 8 deletions packages/rainbowkit/src/components/Dialog/DialogContent.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ export const dialogContentWideMobile = style([
export const dialogContentWideDesktop = style([
dialogContent,
{
width: largeScreenMinWidth,
},
{
'@media': {
[`screen and (min-width: ${largeScreenMinWidth}px)`]: {
width: '720px',
},
},
minWidth: '720px',
width: '720px',
},
]);

Expand Down
6 changes: 4 additions & 2 deletions packages/rainbowkit/src/hooks/useWindowSize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react';
import { debounce } from '../utils/debounce';

export const useWindowSize = () => {
const [windowSize, setWindowSize] = useState<{
Expand All @@ -8,13 +9,14 @@ export const useWindowSize = () => {
height: undefined,
width: undefined,
});

useEffect(() => {
function handleResize() {
const handleResize = debounce(() => {
setWindowSize({
height: window.innerHeight,
width: window.innerWidth,
});
}
}, 500); // 500ms debounce by default
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
Expand Down
14 changes: 14 additions & 0 deletions packages/rainbowkit/src/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function debounce(fn: () => void, ms: number) {
let timer: NodeJS.Timeout | null;

return () => {
if (timer) {
clearTimeout(timer);
}

timer = setTimeout(() => {
timer = null;
fn();
}, ms);
};
}

0 comments on commit c0a644a

Please sign in to comment.