@onekeyfe/react-native-pager-view 6.7.0-2 → 6.7.0-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/docs/SPEC.md +46 -0
- package/lib/module/CollapsiblePagerView.web.js +428 -37
- package/lib/module/NativeScroller.web.js +16 -3
- package/lib/module/WebCollapsiblePagerPage.js +182 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.web.js +2 -0
- package/lib/typescript/src/CollapsiblePagerView.web.d.ts +64 -3
- package/lib/typescript/src/WebCollapsiblePagerPage.d.ts +42 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.web.d.ts +4 -0
- package/package.json +2 -2
- package/src/CollapsiblePagerView.web.tsx +638 -49
- package/src/NativeScroller.web.tsx +24 -2
- package/src/WebCollapsiblePagerPage.tsx +258 -0
- package/src/index.tsx +4 -0
- package/src/index.web.tsx +4 -0
|
@@ -13,19 +13,41 @@ export const NativeScroller = React.forwardRef<
|
|
|
13
13
|
>(function NativeScroller(inputProps, ref) {
|
|
14
14
|
const {
|
|
15
15
|
dataSet,
|
|
16
|
-
pagerScrollKey
|
|
16
|
+
pagerScrollKey,
|
|
17
17
|
topOverlay: _topOverlay,
|
|
18
18
|
topScrollEdgeEffect: _topScrollEdgeEffect,
|
|
19
19
|
...props
|
|
20
20
|
} = inputProps as NativeScrollerWebProps;
|
|
21
|
+
const scrollRef = React.useRef<React.ElementRef<typeof ScrollView>>(null);
|
|
22
|
+
const setRef = React.useCallback(
|
|
23
|
+
(node: React.ElementRef<typeof ScrollView> | null) => {
|
|
24
|
+
scrollRef.current = node;
|
|
25
|
+
if (typeof ref === "function") ref(node);
|
|
26
|
+
else if (ref) ref.current = node;
|
|
27
|
+
},
|
|
28
|
+
[ref]
|
|
29
|
+
);
|
|
30
|
+
React.useLayoutEffect(() => {
|
|
31
|
+
const element = scrollRef.current?.getScrollableNode() as unknown as
|
|
32
|
+
| HTMLElement
|
|
33
|
+
| undefined;
|
|
34
|
+
const EventConstructor = element?.ownerDocument?.defaultView?.Event;
|
|
35
|
+
if (EventConstructor)
|
|
36
|
+
element.dispatchEvent(
|
|
37
|
+
new EventConstructor("ok-collapsible-pager-scroll-key-changed", {
|
|
38
|
+
bubbles: true,
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
}, [pagerScrollKey]);
|
|
21
42
|
const webDataProps = {
|
|
22
43
|
dataSet: {
|
|
23
44
|
...dataSet,
|
|
24
45
|
collapsiblePagerScroll: "true",
|
|
46
|
+
collapsiblePagerScrollKey: pagerScrollKey ?? "",
|
|
25
47
|
},
|
|
26
48
|
};
|
|
27
49
|
|
|
28
|
-
return <ScrollView {...props} ref={
|
|
50
|
+
return <ScrollView {...props} ref={setRef} {...webDataProps} />;
|
|
29
51
|
});
|
|
30
52
|
|
|
31
53
|
NativeScroller.displayName = "NativeScroller";
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
export type WebCollapsiblePagerPageState = Readonly<{
|
|
4
|
+
isFocused: boolean;
|
|
5
|
+
isRouteActive: boolean;
|
|
6
|
+
owner: HTMLElement | null;
|
|
7
|
+
content: HTMLElement | null;
|
|
8
|
+
contentHeight?: number;
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
scrollTop: number;
|
|
12
|
+
pendingRouteScrollTop: number | null;
|
|
13
|
+
registerContent: (element: HTMLElement, explicitHeight?: number) => () => void;
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
export type WebCollapsiblePagerPageBridge = Readonly<{
|
|
17
|
+
setOwner: (owner: HTMLElement | null) => void;
|
|
18
|
+
requestMeasure: () => void;
|
|
19
|
+
setPendingRouteScrollTop: (top: number | null) => void;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
const PageContext = React.createContext<WebCollapsiblePagerPageState | null>(null);
|
|
23
|
+
|
|
24
|
+
export function useWebCollapsiblePagerPage() {
|
|
25
|
+
return React.useContext(PageContext);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function WebCollapsiblePagerPage({
|
|
29
|
+
children,
|
|
30
|
+
pageKey,
|
|
31
|
+
isFocused,
|
|
32
|
+
isRouteActive,
|
|
33
|
+
headerHeight,
|
|
34
|
+
stickyHeaderHeight,
|
|
35
|
+
onBridge,
|
|
36
|
+
onGeometryChange,
|
|
37
|
+
onScroll,
|
|
38
|
+
onUserInput,
|
|
39
|
+
}: React.PropsWithChildren<{
|
|
40
|
+
pageKey: string;
|
|
41
|
+
isFocused: boolean;
|
|
42
|
+
isRouteActive: boolean;
|
|
43
|
+
headerHeight: number;
|
|
44
|
+
stickyHeaderHeight: number;
|
|
45
|
+
onBridge: (key: string, bridge: WebCollapsiblePagerPageBridge | null) => void;
|
|
46
|
+
onGeometryChange: (key: string) => void;
|
|
47
|
+
onScroll: (key: string, top: number) => void;
|
|
48
|
+
onUserInput: (key: string) => void;
|
|
49
|
+
}>) {
|
|
50
|
+
const [owner, setOwner] = React.useState<HTMLElement | null>(null);
|
|
51
|
+
const ownerRef = React.useRef<HTMLElement | null>(null);
|
|
52
|
+
const [registered, setRegistered] = React.useState<{
|
|
53
|
+
element: HTMLElement;
|
|
54
|
+
height?: number;
|
|
55
|
+
} | null>(null);
|
|
56
|
+
const registeredRef = React.useRef(registered);
|
|
57
|
+
const observerRef = React.useRef<ResizeObserver | null>(null);
|
|
58
|
+
const observedContentRef = React.useRef<HTMLElement | null>(null);
|
|
59
|
+
const frameRef = React.useRef<number | null>(null);
|
|
60
|
+
const disposedRef = React.useRef(false);
|
|
61
|
+
const [pendingRouteScrollTop, setPendingRouteScrollTop] = React.useState<
|
|
62
|
+
number | null
|
|
63
|
+
>(null);
|
|
64
|
+
const [metrics, setMetrics] = React.useState({
|
|
65
|
+
width: 0,
|
|
66
|
+
height: 0,
|
|
67
|
+
scrollTop: 0,
|
|
68
|
+
});
|
|
69
|
+
const inputsRef = React.useRef({
|
|
70
|
+
pageKey,
|
|
71
|
+
headerHeight,
|
|
72
|
+
stickyHeaderHeight,
|
|
73
|
+
onGeometryChange,
|
|
74
|
+
onScroll,
|
|
75
|
+
onUserInput,
|
|
76
|
+
});
|
|
77
|
+
inputsRef.current = {
|
|
78
|
+
pageKey,
|
|
79
|
+
headerHeight,
|
|
80
|
+
stickyHeaderHeight,
|
|
81
|
+
onGeometryChange,
|
|
82
|
+
onScroll,
|
|
83
|
+
onUserInput,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const measure = React.useCallback(() => {
|
|
87
|
+
frameRef.current = null;
|
|
88
|
+
if (disposedRef.current) return;
|
|
89
|
+
const current = ownerRef.current;
|
|
90
|
+
if (!current?.isConnected) return;
|
|
91
|
+
const ownerRect = current.getBoundingClientRect();
|
|
92
|
+
const candidate = registeredRef.current?.element;
|
|
93
|
+
const content = candidate?.isConnected && current.contains(candidate) ? candidate : null;
|
|
94
|
+
const contentRect = (content ?? current.firstElementChild)?.getBoundingClientRect();
|
|
95
|
+
const inputs = inputsRef.current;
|
|
96
|
+
const pinnedBottom =
|
|
97
|
+
ownerRect.top +
|
|
98
|
+
Math.max(0, inputs.headerHeight - current.scrollTop) +
|
|
99
|
+
inputs.stickyHeaderHeight;
|
|
100
|
+
const visibleTop = Math.max(
|
|
101
|
+
ownerRect.top,
|
|
102
|
+
pinnedBottom,
|
|
103
|
+
contentRect?.top ?? 0,
|
|
104
|
+
);
|
|
105
|
+
const next = {
|
|
106
|
+
width: current.clientWidth,
|
|
107
|
+
height: Math.max(0, ownerRect.bottom - visibleTop),
|
|
108
|
+
scrollTop: Math.max(0, pinnedBottom - (contentRect?.top ?? pinnedBottom)),
|
|
109
|
+
};
|
|
110
|
+
setMetrics((previous) =>
|
|
111
|
+
previous.width === next.width &&
|
|
112
|
+
previous.height === next.height &&
|
|
113
|
+
previous.scrollTop === next.scrollTop
|
|
114
|
+
? previous
|
|
115
|
+
: next,
|
|
116
|
+
);
|
|
117
|
+
inputs.onGeometryChange(inputs.pageKey);
|
|
118
|
+
}, []);
|
|
119
|
+
|
|
120
|
+
const requestMeasure = React.useCallback(() => {
|
|
121
|
+
if (!disposedRef.current && frameRef.current === null)
|
|
122
|
+
frameRef.current = requestAnimationFrame(measure);
|
|
123
|
+
}, [measure]);
|
|
124
|
+
|
|
125
|
+
const syncContentObservation = React.useCallback(() => {
|
|
126
|
+
const observer = observerRef.current;
|
|
127
|
+
const current = ownerRef.current;
|
|
128
|
+
if (!observer || !current) return;
|
|
129
|
+
const candidate = registeredRef.current?.element;
|
|
130
|
+
const content = candidate?.isConnected && current.contains(candidate)
|
|
131
|
+
? candidate
|
|
132
|
+
: current.firstElementChild;
|
|
133
|
+
const next =
|
|
134
|
+
typeof HTMLElement !== "undefined" && content instanceof HTMLElement
|
|
135
|
+
? content
|
|
136
|
+
: null;
|
|
137
|
+
if (observedContentRef.current === next) return;
|
|
138
|
+
if (observedContentRef.current) observer.unobserve(observedContentRef.current);
|
|
139
|
+
observedContentRef.current = next;
|
|
140
|
+
if (next) observer.observe(next);
|
|
141
|
+
}, []);
|
|
142
|
+
|
|
143
|
+
const registerContent = React.useCallback<
|
|
144
|
+
WebCollapsiblePagerPageState["registerContent"]
|
|
145
|
+
>(
|
|
146
|
+
(element, height) => {
|
|
147
|
+
const entry = {
|
|
148
|
+
element,
|
|
149
|
+
height: height !== undefined && Number.isFinite(height) && height >= 0
|
|
150
|
+
? height
|
|
151
|
+
: undefined,
|
|
152
|
+
};
|
|
153
|
+
registeredRef.current = entry;
|
|
154
|
+
setRegistered(entry);
|
|
155
|
+
syncContentObservation();
|
|
156
|
+
requestMeasure();
|
|
157
|
+
return () => {
|
|
158
|
+
if (registeredRef.current !== entry) return;
|
|
159
|
+
registeredRef.current = null;
|
|
160
|
+
setRegistered(null);
|
|
161
|
+
syncContentObservation();
|
|
162
|
+
requestMeasure();
|
|
163
|
+
};
|
|
164
|
+
},
|
|
165
|
+
[requestMeasure, syncContentObservation],
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const bridge = React.useMemo<WebCollapsiblePagerPageBridge>(
|
|
169
|
+
() => ({
|
|
170
|
+
setOwner: (next) => {
|
|
171
|
+
if (ownerRef.current === next) return;
|
|
172
|
+
ownerRef.current = next;
|
|
173
|
+
setOwner(next);
|
|
174
|
+
requestMeasure();
|
|
175
|
+
},
|
|
176
|
+
requestMeasure,
|
|
177
|
+
setPendingRouteScrollTop,
|
|
178
|
+
}),
|
|
179
|
+
[requestMeasure],
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
React.useLayoutEffect(() => {
|
|
183
|
+
disposedRef.current = false;
|
|
184
|
+
return () => {
|
|
185
|
+
disposedRef.current = true;
|
|
186
|
+
if (frameRef.current !== null) cancelAnimationFrame(frameRef.current);
|
|
187
|
+
frameRef.current = null;
|
|
188
|
+
};
|
|
189
|
+
}, []);
|
|
190
|
+
|
|
191
|
+
React.useLayoutEffect(() => {
|
|
192
|
+
onBridge(pageKey, bridge);
|
|
193
|
+
return () => onBridge(pageKey, null);
|
|
194
|
+
}, [bridge, onBridge, pageKey]);
|
|
195
|
+
|
|
196
|
+
React.useLayoutEffect(() => {
|
|
197
|
+
if (!owner) return undefined;
|
|
198
|
+
const observer = new ResizeObserver(requestMeasure);
|
|
199
|
+
observerRef.current = observer;
|
|
200
|
+
observer.observe(owner);
|
|
201
|
+
syncContentObservation();
|
|
202
|
+
const handleScroll = () => {
|
|
203
|
+
if (ownerRef.current !== owner) return;
|
|
204
|
+
const inputs = inputsRef.current;
|
|
205
|
+
inputs.onScroll(inputs.pageKey, owner.scrollTop);
|
|
206
|
+
requestMeasure();
|
|
207
|
+
};
|
|
208
|
+
const handleKey = (event: KeyboardEvent) => {
|
|
209
|
+
if (
|
|
210
|
+
["ArrowUp", "ArrowDown", "PageUp", "PageDown", "Home", "End", " "].includes(
|
|
211
|
+
event.key,
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
inputsRef.current.onUserInput(inputsRef.current.pageKey);
|
|
215
|
+
};
|
|
216
|
+
const handleUserInput = () =>
|
|
217
|
+
inputsRef.current.onUserInput(inputsRef.current.pageKey);
|
|
218
|
+
owner.addEventListener("scroll", handleScroll, { passive: true });
|
|
219
|
+
owner.addEventListener("touchstart", handleUserInput, { passive: true });
|
|
220
|
+
owner.addEventListener("pointerdown", handleUserInput);
|
|
221
|
+
owner.addEventListener("keydown", handleKey);
|
|
222
|
+
owner.addEventListener("ok-collapsible-pager-user-scroll", handleUserInput);
|
|
223
|
+
owner.addEventListener("ok-collapsible-pager-insets-changed", requestMeasure);
|
|
224
|
+
requestMeasure();
|
|
225
|
+
return () => {
|
|
226
|
+
observer.disconnect();
|
|
227
|
+
observerRef.current = null;
|
|
228
|
+
observedContentRef.current = null;
|
|
229
|
+
owner.removeEventListener("scroll", handleScroll);
|
|
230
|
+
owner.removeEventListener("touchstart", handleUserInput);
|
|
231
|
+
owner.removeEventListener("pointerdown", handleUserInput);
|
|
232
|
+
owner.removeEventListener("keydown", handleKey);
|
|
233
|
+
owner.removeEventListener("ok-collapsible-pager-user-scroll", handleUserInput);
|
|
234
|
+
owner.removeEventListener("ok-collapsible-pager-insets-changed", requestMeasure);
|
|
235
|
+
if (frameRef.current !== null) cancelAnimationFrame(frameRef.current);
|
|
236
|
+
frameRef.current = null;
|
|
237
|
+
};
|
|
238
|
+
}, [owner, requestMeasure, syncContentObservation]);
|
|
239
|
+
|
|
240
|
+
React.useLayoutEffect(requestMeasure, [isFocused, isRouteActive, headerHeight, stickyHeaderHeight, requestMeasure]);
|
|
241
|
+
|
|
242
|
+
const value = React.useMemo<WebCollapsiblePagerPageState>(
|
|
243
|
+
() => ({
|
|
244
|
+
isFocused,
|
|
245
|
+
isRouteActive,
|
|
246
|
+
owner,
|
|
247
|
+
content: registered?.element.isConnected && owner?.contains(registered.element)
|
|
248
|
+
? registered.element
|
|
249
|
+
: null,
|
|
250
|
+
contentHeight: registered?.height,
|
|
251
|
+
...metrics,
|
|
252
|
+
pendingRouteScrollTop,
|
|
253
|
+
registerContent,
|
|
254
|
+
}),
|
|
255
|
+
[isFocused, isRouteActive, owner, registered, metrics, pendingRouteScrollTop, registerContent],
|
|
256
|
+
);
|
|
257
|
+
return <PageContext.Provider value={value}>{children}</PageContext.Provider>;
|
|
258
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -4,6 +4,10 @@ export default PagerView;
|
|
|
4
4
|
export * from './usePagerView';
|
|
5
5
|
export * from './CollapsiblePagerView';
|
|
6
6
|
export * from './NativeScroller';
|
|
7
|
+
export { CollapsiblePagerView as WebCollapsiblePagerView } from './CollapsiblePagerView.web';
|
|
8
|
+
export type { CollapsiblePagerViewProps as WebCollapsiblePagerViewProps } from './CollapsiblePagerView.web';
|
|
9
|
+
export { useWebCollapsiblePagerPage } from './WebCollapsiblePagerPage';
|
|
10
|
+
export type { WebCollapsiblePagerPageState } from './WebCollapsiblePagerPage';
|
|
7
11
|
|
|
8
12
|
import type {
|
|
9
13
|
OnPageScrollEventData as PagerViewOnPageScrollEventData,
|
package/src/index.web.tsx
CHANGED
|
@@ -11,5 +11,9 @@ export type {
|
|
|
11
11
|
export * from "./usePagerView";
|
|
12
12
|
export * from "./CollapsiblePagerView.web";
|
|
13
13
|
export * from "./NativeScroller.web";
|
|
14
|
+
export { CollapsiblePagerView as WebCollapsiblePagerView } from "./CollapsiblePagerView.web";
|
|
15
|
+
export type { CollapsiblePagerViewProps as WebCollapsiblePagerViewProps } from "./CollapsiblePagerView.web";
|
|
16
|
+
export { useWebCollapsiblePagerPage } from "./WebCollapsiblePagerPage";
|
|
17
|
+
export type { WebCollapsiblePagerPageState } from "./WebCollapsiblePagerPage";
|
|
14
18
|
|
|
15
19
|
export { ScrollEdgeEffect } from "./ScrollEdgeEffect";
|