Skip to content

Commit

Permalink
use ResizeObserver replace window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
lerte committed Nov 6, 2024
1 parent 0be6da9 commit b2abf09
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions packages/actify/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@ const Select = <T extends object>(props: SelectProps<T>) => {
Tag = OutlinedField
}

React.useLayoutEffect(() => {
const width = ref?.current?.getBoundingClientRect().width
setReferenceWidth(width)
}, [])

React.useEffect(() => {
const updateWidth = () => {
const resizeObserver = new ResizeObserver(() => {
const width = ref?.current?.getBoundingClientRect().width
setReferenceWidth(width)
}
window.addEventListener('resize', updateWidth)
})
resizeObserver.observe(ref?.current as Element)
return () => {
window.removeEventListener('resize', updateWidth)
resizeObserver.disconnect()
}
}, [])

Expand Down

0 comments on commit b2abf09

Please sign in to comment.