Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ yarn add @rehooks/component-size
## Usage

```js
import { useRef } from 'react'
import { useRef, useState } from 'react'
import useComponentSize from '@rehooks/component-size'

function MyComponent() {
let ref = useRef(null)
let size = useComponentSize(ref)
const [imgElem, setImageElem] = useState(null);
// callback ref to ensure re-render when ref is set, as per React docs
// https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node
let ref = useCallback(elem => {
if (elem !== null) {
setImgElem(elem)
}
}, [])
let size = useComponentSize(imgElem)
// size == { width: 100, height: 200 }
let { width, height } = size
let imgUrl = `https://via.placeholder.com/${width}x${height}`
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ interface ComponentSize {
height: number
}

export default function useComponentSize<T = any>(ref: React.RefObject<T>): ComponentSize
export default function useComponentSize<T = any>(ref: T | React.RefObject<T>): ComponentSize
Copy link
Author

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

25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,29 @@ function getSize(el) {
}
}

function useComponentSize(ref) {
var _useState = useState(getSize(ref ? ref.current : {}))
function useComponentSize(elemOrRef) {
const el = elemOrRef && (
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this statement altogether, and rename elemOrRef to el to make this a breaking change that drops support for RefObject inputs.

This would be better to make it clearer to users that passing in a RefObject is asking for trouble since it won't trigger a re-render

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()
Expand All @@ -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 {
Expand All @@ -58,7 +63,7 @@ function useComponentSize(ref) {
}
}
},
[ref.current]
[el]
)

return ComponentSize
Expand Down
2 changes: 1 addition & 1 deletion index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the React.Ref<T> type if we make this a breaking change

Copy link
Author

Choose a reason for hiding this comment

The 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 RefObject? shrug