1 | import * as React from 'react';
|
2 |
|
3 | type State = {
|
4 | inView: boolean;
|
5 | entry?: IntersectionObserverEntry;
|
6 | };
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | declare class InView extends React.Component<IntersectionObserverProps | PlainChildrenProps, State> {
|
57 | node: Element | null;
|
58 | _unobserveCb: (() => void) | null;
|
59 | constructor(props: IntersectionObserverProps | PlainChildrenProps);
|
60 | componentDidMount(): void;
|
61 | componentDidUpdate(prevProps: IntersectionObserverProps): void;
|
62 | componentWillUnmount(): void;
|
63 | observeNode(): void;
|
64 | unobserve(): void;
|
65 | handleNode: (node?: Element | null) => void;
|
66 | handleChange: (inView: boolean, entry: IntersectionObserverEntry) => void;
|
67 | render(): React.ReactNode;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | declare function useInView({ threshold, delay, trackVisibility, rootMargin, root, triggerOnce, skip, initialInView, fallbackInView, onChange, }?: IntersectionOptions): InViewHookResponse;
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | declare function defaultFallbackInView(inView: boolean | undefined): void;
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | declare function observe(element: Element, callback: ObserverInstanceCallback, options?: IntersectionObserverInit, fallbackInView?: boolean | undefined): () => void;
|
113 |
|
114 | type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
115 | type ObserverInstanceCallback = (inView: boolean, entry: IntersectionObserverEntry) => void;
|
116 | interface RenderProps {
|
117 | inView: boolean;
|
118 | entry: IntersectionObserverEntry | undefined;
|
119 | ref: React.RefObject<any> | ((node?: Element | null) => void);
|
120 | }
|
121 | interface IntersectionOptions extends IntersectionObserverInit {
|
122 | /** The IntersectionObserver interface's read-only `root` property identifies the Element or Document whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. If the `root` is null, then the bounds of the actual document viewport are used.*/
|
123 | root?: Element | Document | null;
|
124 | /** Margin around the root. Can have values similar to the CSS margin property, e.g. `10px 20px 30px 40px` (top, right, bottom, left). */
|
125 | rootMargin?: string;
|
126 | /** Number between `0` and `1` indicating the percentage that should be visible before triggering. Can also be an `array` of numbers, to create multiple trigger points. */
|
127 | threshold?: number | number[];
|
128 | /** Only trigger the inView callback once */
|
129 | triggerOnce?: boolean;
|
130 | /** Skip assigning the observer to the `ref` */
|
131 | skip?: boolean;
|
132 | /** Set the initial value of the `inView` boolean. This can be used if you expect the element to be in the viewport to start with, and you want to trigger something when it leaves. */
|
133 | initialInView?: boolean;
|
134 | /** Fallback to this inView state if the IntersectionObserver is unsupported, and a polyfill wasn't loaded */
|
135 | fallbackInView?: boolean;
|
136 | /** IntersectionObserver v2 - Track the actual visibility of the element */
|
137 | trackVisibility?: boolean;
|
138 | /** IntersectionObserver v2 - Set a minimum delay between notifications */
|
139 | delay?: number;
|
140 | /** Call this function whenever the in view state changes */
|
141 | onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void;
|
142 | }
|
143 | interface IntersectionObserverProps extends IntersectionOptions {
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 | children: (fields: RenderProps) => React.ReactNode;
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 | type PlainChildrenProps = IntersectionOptions & {
|
155 | children?: React.ReactNode;
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | as?: keyof JSX.IntrinsicElements;
|
164 |
|
165 | onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void;
|
166 | } & Omit<React.HTMLProps<HTMLElement>, "onChange">;
|
167 |
|
168 |
|
169 |
|
170 | type InViewHookResponse = [
|
171 | (node?: Element | null) => void,
|
172 | boolean,
|
173 | IntersectionObserverEntry | undefined
|
174 | ] & {
|
175 | ref: (node?: Element | null) => void;
|
176 | inView: boolean;
|
177 | entry?: IntersectionObserverEntry;
|
178 | };
|
179 |
|
180 | export { InView, type InViewHookResponse, type IntersectionObserverProps, type IntersectionOptions, type ObserverInstanceCallback, type PlainChildrenProps, defaultFallbackInView, observe, useInView };
|