1 | import type { MutableRefObject } from 'react';
|
2 | export type Dimensions = {
|
3 | height?: number;
|
4 | width?: number;
|
5 | };
|
6 | /** If element is mounted, returns its dimensions and `ResizeObserverEntry`
|
7 | * If element is unmounted, returns null */
|
8 | export type ResizePayload = {
|
9 | width: number;
|
10 | height: number;
|
11 | entry: ResizeObserverEntry;
|
12 | } | {
|
13 | width: null;
|
14 | height: null;
|
15 | entry: null;
|
16 | };
|
17 | export type ResfreshModeType = 'throttle' | 'debounce';
|
18 | export type ResfreshOptionsType = {
|
19 | leading?: boolean;
|
20 | trailing?: boolean;
|
21 | };
|
22 | export type OnResizeCallback = (payload: ResizePayload) => void;
|
23 | export type Props = {
|
24 | /**
|
25 | * Function that will be invoked with observable element's width, height and ResizeObserverEntry.
|
26 | * If element is unmounted, width and height will be null.
|
27 | * Default: undefined
|
28 | */
|
29 | onResize?: OnResizeCallback;
|
30 | /**
|
31 | * Trigger update on height change.
|
32 | * Default: true
|
33 | */
|
34 | handleHeight?: boolean;
|
35 | /**
|
36 | * Trigger onResize on width change.
|
37 | * Default: true
|
38 | */
|
39 | handleWidth?: boolean;
|
40 | /**
|
41 | * Do not trigger update when a component mounts.
|
42 | * Default: false
|
43 | */
|
44 | skipOnMount?: boolean;
|
45 | /**
|
46 | * Changes the update strategy. Possible values: "throttle" and "debounce".
|
47 | * See `lodash` docs for more information https://lodash.com/docs/
|
48 | * undefined - callback will be fired for every frame.
|
49 | * Default: undefined
|
50 | */
|
51 | refreshMode?: ResfreshModeType;
|
52 | /**
|
53 | * Set the timeout/interval for `refreshMode` strategy
|
54 | * Default: undefined
|
55 | */
|
56 | refreshRate?: number;
|
57 | /**
|
58 | * Pass additional params to `refreshMode` according to lodash docs
|
59 | * Default: undefined
|
60 | */
|
61 | refreshOptions?: ResfreshOptionsType;
|
62 | /**
|
63 | * These options will be used as a second parameter of `resizeObserver.observe` method
|
64 | * @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/observe
|
65 | * Default: undefined
|
66 | */
|
67 | observerOptions?: ResizeObserverOptions;
|
68 | };
|
69 | export type OnRefChangeType<T = any> = {
|
70 | (node: T | null): void;
|
71 | current?: T | null;
|
72 | };
|
73 | export interface UseResizeDetectorReturn<T> extends Dimensions {
|
74 | ref: OnRefChangeType<T>;
|
75 | }
|
76 | export interface useResizeDetectorProps<T extends HTMLElement> extends Props {
|
77 | targetRef?: MutableRefObject<T | null>;
|
78 | }
|