refactor: simpler logic for resize observer

This commit is contained in:
EnixCoda 2020-11-04 21:43:25 +08:00
parent b2150b9212
commit 6c433f1f17
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD

View file

@ -30,18 +30,17 @@ export function SizeObserver({ type = 'div', children, ...rest }: Props) {
}, [])
React.useEffect(() => {
if (features.resize) {
const observer = new window.ResizeObserver(entries => {
const entry = entries[0]
if (!entry) return
const rect = entry.contentRect
safeSetSize(rect)
})
if (ref.current) observer.observe(ref.current)
return () => observer.disconnect()
} else {
if (ref.current) {
if (ref.current) {
if (features.resize) {
const observer = new window.ResizeObserver(entries => {
const entry = entries[0]
if (!entry) return
const rect = entry.contentRect
safeSetSize(rect)
})
observer.observe(ref.current)
return () => observer.disconnect()
} else {
if ('getBoundingClientRect' in ref.current) {
const rect = ref.current.getBoundingClientRect()
setSize(rect)