Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'
let { useCallback, useState, useLayoutEffect } = require('react')
var _require = require('react')
var useCallback = _require.useCallback
var useState = _require.useState
var useLayoutEffect = _require.useLayoutEffect

function getSize(el) {
if (!el) {
Expand All @@ -16,11 +19,11 @@ function getSize(el) {
}

function useComponentSize(ref) {
let [ComponentSize, setComponentSize] = useState(
getSize(ref ? ref.current : {})
)
var _useState = useState(getSize(ref ? ref.current : {}))
var ComponentSize = _useState[0]
var setComponentSize = _useState[1]

const handleResize = useCallback(
var handleResize = useCallback(
function handleResize() {
if (ref.current) {
setComponentSize(getSize(ref.current))
Expand All @@ -30,25 +33,25 @@ function useComponentSize(ref) {
)

useLayoutEffect(
() => {
function () {
if (!ref.current) {
return
}

handleResize()

if (typeof ResizeObserver === 'function') {
let resizeObserver = new ResizeObserver(() => handleResize())
var resizeObserver = new ResizeObserver(function () { handleResize() })
resizeObserver.observe(ref.current)

return () => {
return function () {
resizeObserver.disconnect(ref.current)
resizeObserver = null
}
} else {
window.addEventListener('resize', handleResize)

return () => {
return function () {
window.removeEventListener('resize', handleResize)
}
}
Expand Down