-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow passing DOM elem directly #29
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,24 +18,29 @@ function getSize(el) { | |
} | ||
} | ||
|
||
function useComponentSize(ref) { | ||
var _useState = useState(getSize(ref ? ref.current : {})) | ||
function useComponentSize(elemOrRef) { | ||
const el = elemOrRef && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this statement altogether, and rename This would be better to make it clearer to users that passing in a |
||
elemOrRef instanceof Element || elemOrRef instanceof HTMLDocument | ||
? elemOrRef | ||
: elemOrRef.current | ||
); | ||
var _useState = useState(getSize(el)) | ||
var ComponentSize = _useState[0] | ||
var setComponentSize = _useState[1] | ||
|
||
var handleResize = useCallback( | ||
function handleResize() { | ||
if (ref.current) { | ||
setComponentSize(getSize(ref.current)) | ||
if (el) { | ||
setComponentSize(getSize(el)) | ||
} | ||
}, | ||
[ref] | ||
[el] | ||
) | ||
|
||
useLayoutEffect( | ||
function() { | ||
if (!ref.current) { | ||
return | ||
if (!el) { | ||
return; | ||
} | ||
|
||
handleResize() | ||
|
@@ -44,10 +49,10 @@ function useComponentSize(ref) { | |
var resizeObserver = new ResizeObserver(function() { | ||
handleResize() | ||
}) | ||
resizeObserver.observe(ref.current) | ||
resizeObserver.observe(el) | ||
|
||
return function() { | ||
resizeObserver.disconnect(ref.current) | ||
resizeObserver.disconnect(el) | ||
resizeObserver = null | ||
} | ||
} else { | ||
|
@@ -58,7 +63,7 @@ function useComponentSize(ref) { | |
} | ||
} | ||
}, | ||
[ref.current] | ||
[el] | ||
) | ||
|
||
return ComponentSize | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ interface ComponentSize { | |
height: number, | ||
} | ||
|
||
declare export default function useComponentSize<T>(ref: React.Ref<T>): ComponentSize; | ||
declare export default function useComponentSize<T>(ref: T | React.Ref<T>): ComponentSize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. separately, idk where the flow types are since idk how flow works, but is this supposed to be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the
React.RefObject<T>
type if we make this a breaking change