Skip to content

Commit

Permalink
Merge pull request #2427 from habx/fix/APP-32572
Browse files Browse the repository at this point in the history
APP-32572: fix(Drawer): update initial drawer step on device resize
  • Loading branch information
habxtech authored Oct 21, 2022
2 parents f9bec89 + 3fdd46f commit b8deec1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Drawer/Drawer.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export const reducer: React.Reducer<DrawerReducerState, DrawerActions> = (
const drawerSteps = getStepHeights(action.containerHeight)
const stepEntries = Object.entries(drawerSteps)
const currentPos = action.containerHeight - (state.position ?? 0)
const closestStep = stepEntries.reduce((prev, curr) => {
return Math.abs(curr[1] - currentPos) < Math.abs(prev[1] - currentPos)
const closestStep = stepEntries.reduce((prev, curr) =>
Math.abs(curr[1] - currentPos) < Math.abs(prev[1] - currentPos)
? curr
: prev
})
)

return {
...state,
Expand Down
22 changes: 17 additions & 5 deletions src/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { debounce } from 'lodash'
import * as React from 'react'

import { useMergedRef } from '../_internal/useMergedRef'
Expand Down Expand Up @@ -35,12 +36,23 @@ export const Drawer = React.forwardRef<HTMLDivElement, DrawerProps>(
}, [state.drawerStep]) // eslint-disable-line react-hooks/exhaustive-deps
React.useEffect(() => {
if (stateProp && stateProp !== state.drawerStep) {
dispatch({
type: DrawerActionTypes.ChangeStep,
value: stateProp,
containerHeight: mergedRef.current?.clientHeight ?? 0,
})
const initializedStep = debounce(
() => {
dispatch({
type: DrawerActionTypes.ChangeStep,
value: stateProp,
containerHeight: mergedRef.current?.clientHeight ?? 0,
})
},
200,
{ leading: true, trailing: false }
)
initializedStep()
initialized.current = true
window.addEventListener('resize', initializedStep)
return () => {
window.removeEventListener('resize', initializedStep)
}
}
}, [stateProp]) // eslint-disable-line react-hooks/exhaustive-deps

Expand Down

0 comments on commit b8deec1

Please sign in to comment.