Skip to content

Commit

Permalink
feat: add debounce for web window resize to prevent re-renders
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Oct 4, 2023
1 parent b028ee3 commit b015d7b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/createUnistyles.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useContext } from 'react'
import { useWindowDimensions } from 'react-native'
import type { CreateStylesFactory, CustomNamedStyles, ScreenSize, ExtractBreakpoints, RemoveKeysWithPrefix } from './types'
import { UnistylesContext } from './UnistylesTheme'
import { getBreakpointFromScreenWidth, proxifyFunction, parseStyle, sortAndValidateBreakpoints } from './utils'
import { useDimensions } from './hooks'

export const createUnistyles = <B extends Record<string, number>, T = {}>(breakpoints: B) => {
const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints) as B
Expand All @@ -11,7 +11,7 @@ export const createUnistyles = <B extends Record<string, number>, T = {}>(breakp
createStyles: <S extends CustomNamedStyles<S, B>>(styles: S | CreateStylesFactory<S, T>) => styles as S,
useStyles: <ST extends CustomNamedStyles<ST, B>>(stylesheet?: ST | CreateStylesFactory<ST, T>) => {
const theme = useContext(UnistylesContext) as T
const dimensions = useWindowDimensions()
const dimensions = useDimensions()
const breakpoint = getBreakpointFromScreenWidth<B>(dimensions.width, sortedBreakpoints)
const screenSize: ScreenSize = {
width: dimensions.width,
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useDimensions } from './useDimensions'
11 changes: 11 additions & 0 deletions src/hooks/useDimensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useWindowDimensions } from 'react-native'
import type { ScreenSize } from '../types'

export const useDimensions = (): ScreenSize => {
const { width, height } = useWindowDimensions()

return {
width,
height
}
}
30 changes: 30 additions & 0 deletions src/hooks/useDimensions.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect, useRef, useState } from 'react'
import type { ScreenSize } from '../types'

export const useDimensions = (): ScreenSize => {
const timerRef = useRef<number>()
const [screenSize, setScreenSize] = useState<ScreenSize>({
width: window.innerWidth,
height: window.innerHeight
})

useEffect(() => {
const handleResize = () => {
clearTimeout(timerRef.current)

timerRef.current = setTimeout(() => setScreenSize({
width: window.innerWidth,
height: window.innerHeight
}), 100)
}

window.addEventListener('resize', handleResize)

return () => {
window.removeEventListener('resize', handleResize)
clearTimeout(timerRef.current)
}
}, [])

return screenSize
}
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"lib": ["esnext"],
"lib": [
"esnext",
"dom"
],
"module": "esnext",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3790,7 +3790,7 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:*":
"@types/node@npm:*, @types/node@npm:^20.8.2":
version: 20.8.2
resolution: "@types/node@npm:20.8.2"
checksum: 3da73e25d821bfcdb7de98589027e08bb4848e55408671c4a83ec0341e124b5313a0b20e1e4b4eff1168ea17a86f622ad73fcb04b761abd77496b9a27cbd5de5
Expand Down Expand Up @@ -15171,6 +15171,7 @@ __metadata:
dependencies:
"@babel/core": 7.23.0
"@expo/webpack-config": 19.0.0
"@types/node": ^20.8.2
babel-loader: 9.1.3
babel-plugin-module-resolver: 5.0.0
expo: 49.0.13
Expand Down

0 comments on commit b015d7b

Please sign in to comment.