Skip to content

Commit

Permalink
improve types and docs for useAutoupdatingClientRect
Browse files Browse the repository at this point in the history
  • Loading branch information
oatkiller committed Dec 20, 2019
1 parent c4896ac commit 1bcdbde
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
import { useCallback, useState, useEffect, useRef } from 'react';
import ResizeObserver from 'resize-observer-polyfill';

/** Built in typescript DOM libs and the ResizeObserver polyfill have incompatible definitions of DOMRectReadOnly so we use this basic one
*/
interface BasicDOMRect {
x: number;
y: number;
width: number;
height: number;
}

/**
* Returns a DOMRect sometimes, and a `ref` callback. Put the `ref` as the `ref` property of an element, and
* DOMRect will be the result of getBoundingClientRect on it.
* Updates automatically when the window resizes. TODO: better Englishe here
* Returns a nullable DOMRect and a ref callback. Pass the refCallback to the
* `ref` property of a native element and this hook will return a DOMRect for
* it by calling `getBoundingClientRect`. This hook will observe the element
* with a resize observer and call getBoundingClientRect again after resizes.
*
* Note that the changes to the position of the element aren't automatically
* tracked. So if the element's position moves for some reason, be sure to
* handle that.
*/
export function useAutoUpdatingClientRect(): [BasicDOMRect | null, (node: Element | null) => void] {
const [rect, setRect] = useState<BasicDOMRect | null>(null);
export function useAutoUpdatingClientRect(): [DOMRect | null, (node: Element | null) => void] {
const [rect, setRect] = useState<DOMRect | null>(null);
const nodeRef = useRef<Element | null>(null);
const ref = useCallback((node: Element | null) => {
nodeRef.current = node;
Expand Down

0 comments on commit 1bcdbde

Please sign in to comment.