@jsb188/react 1.1.1 → 1.1.2
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/libs/README.md +75 -0
- package/libs/page-filters.helpers.ts +65 -0
- package/libs/page-filters.ts +223 -0
- package/libs/use-page-filters.ts +147 -0
- package/package.json +24 -19
- package/states/apiData.ts +18 -14
- package/states/appContext.ts +423 -1
- package/states/connected.ts +135 -11
- package/states/index.ts +4 -0
- package/states/keyDown.ts +167 -0
- package/states/keyboardLayer.ts +57 -0
- package/states/modal.ts +403 -239
- package/states/popOver.ts +394 -178
- package/states/reactiveEvents.ts +99 -0
- package/states/uploads.ts +70 -0
- package/types/PopOver.d.ts +636 -88
- package/utils/hooks.ts +406 -234
- package/utils/lazy.ts +183 -0
package/utils/hooks.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { areObjectsEqual } from '@jsb188/app/utils/object.ts';
|
|
2
|
+
import { releaseObjectUrl, retainObjectUrl } from '@jsb188/app/utils/url_client.ts';
|
|
1
3
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
4
|
|
|
3
5
|
/**
|
|
@@ -6,57 +8,60 @@ import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
6
8
|
* NOTE: Do *NOT* export this function.
|
|
7
9
|
*/
|
|
8
10
|
|
|
9
|
-
function getDatabaseAction(
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
11
|
+
function getDatabaseAction(currentData: any, data: any, allowNull?: boolean, mergeCurrentData?: boolean): any {
|
|
12
|
+
if (!currentData) {
|
|
13
|
+
return {
|
|
14
|
+
action: 'INSERT',
|
|
15
|
+
documentData: data,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const documentData: Record<string, any> = {};
|
|
20
|
+
|
|
21
|
+
let hasChanges = false;
|
|
22
|
+
for (const key in data) {
|
|
23
|
+
if (Object.prototype.hasOwnProperty.call(data, key) && data[key] !== undefined) {
|
|
24
|
+
// trim() on all string databse fields should be OK;
|
|
25
|
+
// But if not, add an extra param.
|
|
26
|
+
const value = typeof data[key] === 'string' ? data[key].trim() : data[key];
|
|
27
|
+
|
|
28
|
+
const isNotNull = value !== null || allowNull;
|
|
29
|
+
if (isNotNull) {
|
|
30
|
+
documentData[key] = value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
// Doing loose != check instead of !== type check
|
|
35
|
+
currentData[key] != value &&
|
|
36
|
+
isNotNull
|
|
37
|
+
) {
|
|
38
|
+
if (value && typeof value === 'object') {
|
|
39
|
+
hasChanges = !areObjectsEqual(currentData[key], value);
|
|
40
|
+
} else {
|
|
41
|
+
hasChanges = true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (hasChanges) {
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (mergeCurrentData) {
|
|
52
|
+
for (const key in currentData) {
|
|
53
|
+
if (Object.prototype.hasOwnProperty.call(currentData, key) && documentData[key] === undefined) {
|
|
54
|
+
documentData[key] = currentData[key];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const action = hasChanges ? 'UPDATE' : 'NONE';
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
action,
|
|
63
|
+
documentData,
|
|
64
|
+
};
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
/**
|
|
@@ -64,28 +69,28 @@ function getDatabaseAction(
|
|
|
64
69
|
*/
|
|
65
70
|
|
|
66
71
|
export function formValuesAreDiff(currentValues: any, formValues: any): boolean {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
72
|
+
if (!formValues && formValues !== 0) {
|
|
73
|
+
// If formValues are null, it's not set yet
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (typeof formValues === 'object') {
|
|
78
|
+
// for (const key in currentValues) {
|
|
79
|
+
// if (
|
|
80
|
+
// currentValues[key] !== formValues[key] &&
|
|
81
|
+
// // This would not work for values with number 0, but I don't think that's an issue for any mutations right now
|
|
82
|
+
// (currentValues[key] !== null || formValues[key] !== undefined)
|
|
83
|
+
// ) {
|
|
84
|
+
// console.log('DIFF!:', key, currentValues[key], formValues[key]);
|
|
85
|
+
// return true;
|
|
86
|
+
// }
|
|
87
|
+
// }
|
|
88
|
+
|
|
89
|
+
const result = getDatabaseAction(currentValues, formValues, true);
|
|
90
|
+
return result.action !== 'NONE';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return currentValues !== formValues;
|
|
89
94
|
}
|
|
90
95
|
|
|
91
96
|
/**
|
|
@@ -93,18 +98,35 @@ export function formValuesAreDiff(currentValues: any, formValues: any): boolean
|
|
|
93
98
|
*/
|
|
94
99
|
|
|
95
100
|
export function useMounted() {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
101
|
+
const mounted = useRef(true);
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
// This is necessary, I don't know why, this hook unmounteds itself and remounts even though the dependencies are empty
|
|
105
|
+
// NOTE: This behavior was seen in [gqlClient.ts]
|
|
106
|
+
mounted.current = true;
|
|
107
|
+
return () => {
|
|
108
|
+
mounted.current = false;
|
|
109
|
+
};
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
return mounted;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Return the computer timezone after client hydration.
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
let cachedComputerTimeZone: string | null = null;
|
|
120
|
+
|
|
121
|
+
export function useComputerTimeZone() {
|
|
122
|
+
const [timeZone, setTimeZone] = useState<string | null>(cachedComputerTimeZone);
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
cachedComputerTimeZone ??= Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
126
|
+
setTimeZone(cachedComputerTimeZone);
|
|
127
|
+
}, []);
|
|
128
|
+
|
|
129
|
+
return timeZone;
|
|
108
130
|
}
|
|
109
131
|
|
|
110
132
|
/**
|
|
@@ -112,26 +134,26 @@ export function useMounted() {
|
|
|
112
134
|
*/
|
|
113
135
|
|
|
114
136
|
export function useWatchChange(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
137
|
+
currentValues: any,
|
|
138
|
+
formValues: any,
|
|
139
|
+
saving: boolean,
|
|
140
|
+
onChangeDiff: (hasChanges: boolean, saving: boolean) => void,
|
|
141
|
+
onDataUpdated: () => void,
|
|
120
142
|
) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
143
|
+
const isDiff = useMemo(() => {
|
|
144
|
+
return formValuesAreDiff(currentValues, formValues);
|
|
145
|
+
}, [currentValues, formValues]);
|
|
146
|
+
|
|
147
|
+
useEffect(() => {
|
|
148
|
+
onChangeDiff(isDiff, saving);
|
|
149
|
+
}, [isDiff, saving]);
|
|
150
|
+
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (!saving) {
|
|
153
|
+
// Always update, because isDiff memo() only listens to formValues (and not currentValues)
|
|
154
|
+
onDataUpdated();
|
|
155
|
+
}
|
|
156
|
+
}, [saving]);
|
|
135
157
|
}
|
|
136
158
|
|
|
137
159
|
/**
|
|
@@ -139,31 +161,27 @@ export function useWatchChange(
|
|
|
139
161
|
*/
|
|
140
162
|
|
|
141
163
|
export function checkPropsDiff(pp: any, np: any) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return isSame;
|
|
164
|
+
let isSame = true;
|
|
165
|
+
for (const k in pp) {
|
|
166
|
+
if (Object.prototype.hasOwnProperty.call(pp, k) && pp[k] !== np[k]) {
|
|
167
|
+
console.log('Diff:', k);
|
|
168
|
+
isSame = false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
for (const k in np) {
|
|
173
|
+
if (Object.prototype.hasOwnProperty.call(np, k) && !Object.prototype.hasOwnProperty.call(pp, k) && np[k] !== pp[k]) {
|
|
174
|
+
console.log('New Prop:', k);
|
|
175
|
+
isSame = false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return isSame;
|
|
162
180
|
}
|
|
163
181
|
|
|
164
182
|
if (globalThis && globalThis.location?.origin?.indexOf('localhost') >= 0) {
|
|
165
|
-
|
|
166
|
-
|
|
183
|
+
// @ts-ignore - Dev only shim
|
|
184
|
+
globalThis.checkPropsDiff = checkPropsDiff;
|
|
167
185
|
}
|
|
168
186
|
|
|
169
187
|
/**
|
|
@@ -171,123 +189,277 @@ if (globalThis && globalThis.location?.origin?.indexOf('localhost') >= 0) {
|
|
|
171
189
|
*/
|
|
172
190
|
|
|
173
191
|
export function arePropsEqualExceptChildren(pp: any, np: any) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return true;
|
|
192
|
+
for (const k in np) {
|
|
193
|
+
if (
|
|
194
|
+
Object.prototype.hasOwnProperty.call(np, k) &&
|
|
195
|
+
k !== 'children' &&
|
|
196
|
+
(!Object.prototype.hasOwnProperty.call(pp, k) || np[k] !== pp[k])
|
|
197
|
+
) {
|
|
198
|
+
// console.log('DIFF:', k, pp[k], np[k]);
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
188
203
|
}
|
|
189
204
|
|
|
190
205
|
/**
|
|
191
206
|
* Keep track of visibility state change and copy props;
|
|
192
|
-
* To be used in animation components
|
|
207
|
+
* To be used in animation components.
|
|
208
|
+
*
|
|
209
|
+
* You can use this hook to animate an element to fade out before returning
|
|
210
|
+
* null. Pass a truthy value while the element should be visible, render while
|
|
211
|
+
* visibility is not 0, and apply fade classes from the visibility value.
|
|
212
|
+
*
|
|
213
|
+
* Example:
|
|
214
|
+
*
|
|
215
|
+
* const [, visibility] = useAnimationVisibility(visible);
|
|
216
|
+
* const active = visibility >= 2;
|
|
217
|
+
*
|
|
218
|
+
* if (!visibility) {
|
|
219
|
+
* return null;
|
|
220
|
+
* }
|
|
221
|
+
*
|
|
222
|
+
* return <div className={cn('trans_op spd_2', active ? 'op_100' : 'op_0')} />;
|
|
193
223
|
*/
|
|
194
224
|
|
|
195
|
-
type VisibilityValue = 0 | .5 | 1 | 2;
|
|
196
|
-
|
|
197
|
-
export function useAnimationVisibility(
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
): [
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
}, [visible, hasData]);
|
|
241
|
-
|
|
242
|
-
return [persistedProps, visible, setVisible];
|
|
225
|
+
type VisibilityValue = 0 | 0.5 | 1 | 2;
|
|
226
|
+
|
|
227
|
+
export function useAnimationVisibility<T>(
|
|
228
|
+
props: T,
|
|
229
|
+
onHasProps?: (hasProps: boolean) => void,
|
|
230
|
+
): [any, VisibilityValue, (VisibilityValue: VisibilityValue) => void] {
|
|
231
|
+
const [visibility, setVisibility] = useState(0 as VisibilityValue);
|
|
232
|
+
const [persistedProps, setPersistedProps] = useState<T>(props as T);
|
|
233
|
+
const hasData = !!props;
|
|
234
|
+
|
|
235
|
+
useEffect(() => {
|
|
236
|
+
if (props) {
|
|
237
|
+
setPersistedProps(props);
|
|
238
|
+
}
|
|
239
|
+
onHasProps?.(hasData);
|
|
240
|
+
}, [props]);
|
|
241
|
+
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
if (visibility === 1) {
|
|
244
|
+
if (hasData) {
|
|
245
|
+
const timer = setTimeout(() => {
|
|
246
|
+
setVisibility(2);
|
|
247
|
+
}, 100);
|
|
248
|
+
return () => clearTimeout(timer);
|
|
249
|
+
} else {
|
|
250
|
+
const timer = setTimeout(() => {
|
|
251
|
+
setVisibility(0);
|
|
252
|
+
}, 300);
|
|
253
|
+
return () => clearTimeout(timer);
|
|
254
|
+
}
|
|
255
|
+
} else if (visibility === 0.5) {
|
|
256
|
+
const timer = setTimeout(() => {
|
|
257
|
+
setVisibility(0);
|
|
258
|
+
}, 300);
|
|
259
|
+
return () => clearTimeout(timer);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const shouldBeValue = hasData ? 2 : 0;
|
|
263
|
+
if (shouldBeValue !== visibility) {
|
|
264
|
+
setVisibility(hasData ? 1 : 0.5);
|
|
265
|
+
}
|
|
266
|
+
}, [visibility, hasData]);
|
|
267
|
+
|
|
268
|
+
return [persistedProps, visibility, setVisibility];
|
|
243
269
|
}
|
|
244
270
|
|
|
245
271
|
/**
|
|
246
|
-
*
|
|
272
|
+
* Check if image are loaded
|
|
273
|
+
* NOTE: This only checks on mount
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
export function useImagesLoadStatus(
|
|
277
|
+
imageUrls: string[],
|
|
278
|
+
timeoutMS = 10000, // default timeout miliseconds
|
|
279
|
+
): boolean {
|
|
280
|
+
const [loaded, setLoaded] = useState(false);
|
|
281
|
+
|
|
282
|
+
useEffect(() => {
|
|
283
|
+
if (!imageUrls || imageUrls.length === 0) {
|
|
284
|
+
setLoaded(true);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
let isCancelled = false;
|
|
289
|
+
let loadedCount = 0;
|
|
290
|
+
const timeout = setTimeout(() => {
|
|
291
|
+
if (!isCancelled) {
|
|
292
|
+
setLoaded(true);
|
|
293
|
+
}
|
|
294
|
+
}, timeoutMS);
|
|
295
|
+
|
|
296
|
+
const handleLoad = () => {
|
|
297
|
+
loadedCount += 1;
|
|
298
|
+
if (loadedCount === imageUrls.length && !isCancelled) {
|
|
299
|
+
clearTimeout(timeout);
|
|
300
|
+
setLoaded(true);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const handleError = () => {
|
|
305
|
+
loadedCount += 1; // count errors too, so they don’t block loading
|
|
306
|
+
if (loadedCount === imageUrls.length && !isCancelled) {
|
|
307
|
+
clearTimeout(timeout);
|
|
308
|
+
setLoaded(true);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const imageElements = imageUrls.map((url) => {
|
|
313
|
+
const img = new Image();
|
|
314
|
+
img.onload = handleLoad;
|
|
315
|
+
img.onerror = handleError;
|
|
316
|
+
img.src = url;
|
|
317
|
+
return img;
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
return () => {
|
|
321
|
+
isCancelled = true;
|
|
322
|
+
clearTimeout(timeout);
|
|
323
|
+
// Optional: clear handlers to prevent leaks
|
|
324
|
+
imageElements.forEach((img) => {
|
|
325
|
+
img.onload = null;
|
|
326
|
+
img.onerror = null;
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
}, []);
|
|
330
|
+
|
|
331
|
+
return loaded;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/*
|
|
335
|
+
* Hold local object URLs (e.g. image previews) while mounted, so they outlive the component that created them.
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
export function useRetainedObjectUrls(urls: (string | null | undefined)[]) {
|
|
339
|
+
const urlsKey = urls.filter(Boolean).join('\n');
|
|
340
|
+
|
|
341
|
+
useEffect(() => {
|
|
342
|
+
const retainedUrls = urlsKey ? urlsKey.split('\n') : [];
|
|
343
|
+
for (const url of retainedUrls) {
|
|
344
|
+
retainObjectUrl(url);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return () => {
|
|
348
|
+
for (const url of retainedUrls) {
|
|
349
|
+
releaseObjectUrl(url);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
}, [urlsKey]);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/*
|
|
356
|
+
* Track whether a referenced div's scroll position has passed a pixel offset.
|
|
357
|
+
*/
|
|
358
|
+
|
|
359
|
+
export function useScrollPastPosition(pixels: number) {
|
|
360
|
+
const divRef = useRef<HTMLDivElement>(null);
|
|
361
|
+
const [hasScrolledPast, setHasScrolledPast] = useState(false);
|
|
362
|
+
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
const element = divRef.current;
|
|
365
|
+
if (!element) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const handleScroll = () => {
|
|
370
|
+
const nextValue = element.scrollTop > pixels;
|
|
371
|
+
|
|
372
|
+
setHasScrolledPast((currentValue) => {
|
|
373
|
+
return currentValue === nextValue ? currentValue : nextValue;
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
element.addEventListener('scroll', handleScroll, { passive: true });
|
|
378
|
+
handleScroll();
|
|
379
|
+
|
|
380
|
+
return () => {
|
|
381
|
+
element.removeEventListener('scroll', handleScroll);
|
|
382
|
+
};
|
|
383
|
+
}, [pixels]);
|
|
384
|
+
|
|
385
|
+
return [divRef, hasScrolledPast] as const;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export type ScrollNavState = 'STATIC' | 'HIDDEN' | 'SHOWN';
|
|
389
|
+
|
|
390
|
+
interface ScrollNavValue {
|
|
391
|
+
navState: ScrollNavState;
|
|
392
|
+
// Whether the page is scrolled past `staticOffset` (independent of scroll direction)
|
|
393
|
+
pastOffset: boolean;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/*
|
|
397
|
+
* Track the state of a hide-on-scroll window nav.
|
|
398
|
+
* 'STATIC' from page load until the page scrolls down past `staticOffset` (the nav sits in the page flow),
|
|
399
|
+
* then 'HIDDEN' while scrolling down and 'SHOWN' as soon as the page scrolls back up. Once shown, the nav
|
|
400
|
+
* stays fixed all the way up and only returns to 'STATIC' at the very top, where the two line up.
|
|
401
|
+
* Scroll deltas smaller than `minDelta` are ignored so trackpad jitter does not flip the direction.
|
|
247
402
|
*/
|
|
248
403
|
|
|
249
|
-
export function
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
)
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
404
|
+
export function useScrollNavState(staticOffset = 0, minDelta = 4) {
|
|
405
|
+
const [value, setValue] = useState<ScrollNavValue>({ navState: 'STATIC', pastOffset: false });
|
|
406
|
+
|
|
407
|
+
useEffect(() => {
|
|
408
|
+
let lastScrollY = window.scrollY;
|
|
409
|
+
let frameRequested = false;
|
|
410
|
+
|
|
411
|
+
const update = () => {
|
|
412
|
+
frameRequested = false;
|
|
413
|
+
const scrollY = window.scrollY;
|
|
414
|
+
const delta = scrollY - lastScrollY;
|
|
415
|
+
const isDirectionChange = Math.abs(delta) >= minDelta;
|
|
416
|
+
|
|
417
|
+
if (isDirectionChange) {
|
|
418
|
+
lastScrollY = scrollY;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const pastOffset = scrollY > staticOffset;
|
|
422
|
+
|
|
423
|
+
const getNavState = (currentValue: ScrollNavState): ScrollNavState => {
|
|
424
|
+
// Negative scrollY happens during rubber-band overscroll; it counts as the top.
|
|
425
|
+
if (scrollY <= 0) {
|
|
426
|
+
return 'STATIC';
|
|
427
|
+
}
|
|
428
|
+
if (isDirectionChange) {
|
|
429
|
+
if (delta > 0) {
|
|
430
|
+
return pastOffset ? 'HIDDEN' : currentValue;
|
|
431
|
+
}
|
|
432
|
+
return currentValue === 'STATIC' ? 'STATIC' : 'SHOWN';
|
|
433
|
+
}
|
|
434
|
+
// Landed past the offset without a real scroll (e.g. restored scroll position on load)
|
|
435
|
+
return currentValue === 'STATIC' && pastOffset ? 'SHOWN' : currentValue;
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
setValue((currentValue) => {
|
|
439
|
+
const navState = getNavState(currentValue.navState);
|
|
440
|
+
// Keep the same object when nothing changed so scroll frames do not re-render the nav
|
|
441
|
+
if (navState === currentValue.navState && pastOffset === currentValue.pastOffset) {
|
|
442
|
+
return currentValue;
|
|
443
|
+
}
|
|
444
|
+
return { navState, pastOffset };
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// Coalesce bursts of scroll events into one read per frame
|
|
449
|
+
const handleScroll = () => {
|
|
450
|
+
if (!frameRequested) {
|
|
451
|
+
frameRequested = true;
|
|
452
|
+
window.requestAnimationFrame(update);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
window.addEventListener('scroll', handleScroll, { passive: true });
|
|
457
|
+
update();
|
|
458
|
+
|
|
459
|
+
return () => {
|
|
460
|
+
window.removeEventListener('scroll', handleScroll);
|
|
461
|
+
};
|
|
462
|
+
}, [staticOffset, minDelta]);
|
|
463
|
+
|
|
464
|
+
return value;
|
|
293
465
|
}
|