The useRect
hook is a custom React hook that provides functionality for measuring the dimensions and position of a DOM element. It can be particularly useful for scenarios where you need to dynamically adjust layout or styles based on the size or position of a DOM element.
import { useRect } from 'hamo'
function App() {
// Basic usage
const [setRectRef, rect] = useRect()
console.log(rect)
// Lazy usage
const [setRectRef, getRect] = useRect({
lazy: true,
callback: (rect) => {
console.log(rect)
},
})
return (
<div ref={setRectRef} />
)
}
The useRect
hook accepts an options object with the following optional parameters:
parameters
: (object) An object containing the following optional parameters:ignoreTransform
: (boolean, default:false
) Iftrue
, ignores CSS transform applied to the element and its parents. It's useful for animations such as parallax.ignoreSticky
: (boolean, default:true
) Iftrue
, ignores sticky positioning of the element and its parents. See the difference with and without.debounce
: (number, default:500
) Delay in milliseconds for debouncing measurement updates. Alternatively, you can set the globaluseResizeObserver.setDebounce
function to change the default debounce delay.lazy
: (boolean, default:false
) Iftrue
, doesn't trigger state update and return a getter instead.callback
: (function) A callback function to be invoked whenever the dimensions or position of the element change.
deps
: (array) An array of dependencies.
useRect.setDebounce(500)
The useRect
hook returns an array containing the following elements:
setRef
: A function that should be passed as theref
prop to the target DOM element.rect
: An object representing the current dimensions and position of the element. iflazy
istrue
,rect
is a function that returns the current dimensions and position of the element. The object has the following properties:element
: The DOM element being measured.resize
: A function to trigger manual resizing when needed.width
: The width of the element.height
: The height of the element.top
,y
: The distance from the top of the document to the top of the element.left
,x
: The distance from the left of the document to the left of the element.right
: The distance from the left of the document to the right of the element.bottom
: The distance from the top of the document to the bottom of the element.
setWrapperRef
: A function to set a reference to the wrapper element. Default wrapper element isdocument.body
. Any time the wrapper element is resized, the dimensions and position of the target element are recalculated.
The useRect
hook provides additional functionality through its resize
method, which can be used to trigger manual resizing when needed for all useRect instances.
useRect.resize()
import { useRect } from 'hamo'
function App() {
const [setRectRef, rect, setWrapperRef] = useRect()
console.log(rect)
return (
<div ref={setWrapperRef} />
)
}