Skip to content

Commit

Permalink
feat: Use ResizeObserver to listen to size changes (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
christiankaindl authored Aug 3, 2022
1 parent 7a180af commit 0eeeddf
Show file tree
Hide file tree
Showing 3 changed files with 5,451 additions and 5,468 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
},
"dependencies": {
"@babel/runtime": "^7.14.6",
"@reach/observe-rect": "^1.2.0",
"@types/d3-array": "^3.0.1",
"@types/d3-scale": "^4.0.1",
"@types/d3-shape": "^3.0.1",
Expand Down
9 changes: 4 additions & 5 deletions src/hooks/useRect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import observeRect from '@reach/observe-rect'
import React from 'react'

import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect'
Expand Down Expand Up @@ -39,14 +38,14 @@ export default function useRect(
return
}

const observer = observeRect(element as Element, (newRect: DOMRect) => {
setRect(newRect)
const observer = new ResizeObserver((entries) => {
setRect(entries[0]?.contentRect)
})

observer.observe()
observer.observe(element as Element)

return () => {
observer.unobserve()
observer.unobserve(element as Element)
}
}, [element, enabled])

Expand Down
Loading

0 comments on commit 0eeeddf

Please sign in to comment.