UNPKG

recharts

Version:
86 lines (85 loc) • 3.6 kB
import { Size } from './types'; /** * Which box of an element to measure. * * - `content-box`: without padding, border, and scrollbar. A chart renders inside this box. * - `border-box`: including padding and border. This is how much space an element takes up, for example a Legend. * * @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing} */ export type MeasuredBox = 'content-box' | 'border-box'; /** * Measures the layout size of an element. * * Unlike `getBoundingClientRect()`, this reads the layout size, which CSS transforms do not affect. * An element that mounts inside a scaled parent (for example during an animation) gets its real size, * and the size stays right after the transform goes away, even though ResizeObserver does not fire then. * * The content box is in whole pixels, because charts render at whole pixel sizes. * The border box keeps fractional pixels, because other elements are positioned around it. * * @param element the element to measure * @param box which box to measure * @returns the width and height of the box */ export declare function getElementSize(element: HTMLElement, box: MeasuredBox): Size; export type UseElementSizeOptions = { /** * Which box of the element to measure. */ box: MeasuredBox; /** * The size to return before the first measurement, and when the element is not measured at all. * Only the value from the first render is used. */ initialSize: Size; /** * If true, measures again whenever ResizeObserver reports that the element changed size. * If false, measures once when the element attaches. */ observe: boolean; /** * Only applies when `observe` is true. If ResizeObserver is not available, for example in jsdom, * true measures once when the element attaches, and false keeps `initialSize`. * @default true */ measureWithoutResizeObserver?: boolean; /** * Measures at most once in this many milliseconds after a ResizeObserver notification. 0 measures on every one. * @default 0 */ throttleMs?: number; /** * Ignores size changes of this many pixels or fewer. Use this to break loops where the size of an element * changes the layout, which changes the size of the element again. * @default 0 */ threshold?: number; /** * Measures again when any of these change. Use this for values that change the content of the element, * when the content may change size without the element itself resizing. */ extraDependencies?: ReadonlyArray<unknown>; /** * Called with the measured size after every ResizeObserver notification, including the first one. */ onResize?: (size: Size) => void; }; /** * Callback ref setter returned by {@link useElementSize}. * * Pass this to a DOM element's `ref` prop to start measuring it. * * @param node - the DOM element to measure, or `null` when the element unmounts */ export type SetMeasuredElement = (node: HTMLElement | null) => void; /** * Measures the layout size of a DOM element, and optionally keeps measuring it as it resizes. * * Every measurement reads the layout size from the element itself, see {@link getElementSize}. * ResizeObserver only tells this hook when to measure again. * * @param options see {@link UseElementSizeOptions} * @returns [size, setMeasuredElement]. Pass the setter to a DOM element ref like this: `<div ref={setMeasuredElement}>` */ export declare function useElementSize(options: UseElementSizeOptions): [Size, SetMeasuredElement];