@jsb188/react 1.1.10 → 1.1.12
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 -20
- package/states/apiData.ts +18 -16
- package/states/appContext.ts +327 -312
- package/states/connected.ts +135 -11
- package/states/index.ts +2 -1
- package/states/keyDown.ts +148 -47
- package/states/keyboardLayer.ts +57 -0
- package/states/modal.ts +369 -268
- package/states/popOver.ts +374 -244
- package/states/reactiveEvents.ts +99 -0
- package/states/uploads.ts +39 -27
- package/types/PopOver.d.ts +552 -223
- package/utils/hooks.ts +387 -272
- package/utils/lazy.ts +183 -0
- package/states/toasts.ts +0 -30
- package/utils/schema.ts +0 -158
package/utils/hooks.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { areObjectsEqual } from '@jsb188/app/utils/object.ts';
|
|
2
|
+
import { releaseObjectUrl, retainObjectUrl } from '@jsb188/app/utils/url_client.ts';
|
|
2
3
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -7,65 +8,60 @@ import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
7
8
|
* NOTE: Do *NOT* export this function.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
|
-
function getDatabaseAction(
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return {
|
|
66
|
-
action,
|
|
67
|
-
documentData,
|
|
68
|
-
};
|
|
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
|
+
};
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
/**
|
|
@@ -73,28 +69,28 @@ function getDatabaseAction(
|
|
|
73
69
|
*/
|
|
74
70
|
|
|
75
71
|
export function formValuesAreDiff(currentValues: any, formValues: any): boolean {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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;
|
|
98
94
|
}
|
|
99
95
|
|
|
100
96
|
/**
|
|
@@ -102,18 +98,35 @@ export function formValuesAreDiff(currentValues: any, formValues: any): boolean
|
|
|
102
98
|
*/
|
|
103
99
|
|
|
104
100
|
export function useMounted() {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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;
|
|
117
130
|
}
|
|
118
131
|
|
|
119
132
|
/**
|
|
@@ -121,26 +134,26 @@ export function useMounted() {
|
|
|
121
134
|
*/
|
|
122
135
|
|
|
123
136
|
export function useWatchChange(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
137
|
+
currentValues: any,
|
|
138
|
+
formValues: any,
|
|
139
|
+
saving: boolean,
|
|
140
|
+
onChangeDiff: (hasChanges: boolean, saving: boolean) => void,
|
|
141
|
+
onDataUpdated: () => void,
|
|
129
142
|
) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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]);
|
|
144
157
|
}
|
|
145
158
|
|
|
146
159
|
/**
|
|
@@ -148,31 +161,27 @@ export function useWatchChange(
|
|
|
148
161
|
*/
|
|
149
162
|
|
|
150
163
|
export function checkPropsDiff(pp: any, np: any) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
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;
|
|
171
180
|
}
|
|
172
181
|
|
|
173
182
|
if (globalThis && globalThis.location?.origin?.indexOf('localhost') >= 0) {
|
|
174
|
-
|
|
175
|
-
|
|
183
|
+
// @ts-ignore - Dev only shim
|
|
184
|
+
globalThis.checkPropsDiff = checkPropsDiff;
|
|
176
185
|
}
|
|
177
186
|
|
|
178
187
|
/**
|
|
@@ -180,75 +189,83 @@ if (globalThis && globalThis.location?.origin?.indexOf('localhost') >= 0) {
|
|
|
180
189
|
*/
|
|
181
190
|
|
|
182
191
|
export function arePropsEqualExceptChildren(pp: any, np: any) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
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;
|
|
197
203
|
}
|
|
198
204
|
|
|
199
205
|
/**
|
|
200
206
|
* Keep track of visibility state change and copy props;
|
|
201
|
-
* 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')} />;
|
|
202
223
|
*/
|
|
203
224
|
|
|
204
|
-
type VisibilityValue = 0 | .5 | 1 | 2;
|
|
225
|
+
type VisibilityValue = 0 | 0.5 | 1 | 2;
|
|
205
226
|
|
|
206
227
|
export function useAnimationVisibility<T>(
|
|
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
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
}, [visibility, hasData]);
|
|
250
|
-
|
|
251
|
-
return [persistedProps, visibility, setVisibility];
|
|
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];
|
|
252
269
|
}
|
|
253
270
|
|
|
254
271
|
/**
|
|
@@ -257,94 +274,192 @@ export function useAnimationVisibility<T>(
|
|
|
257
274
|
*/
|
|
258
275
|
|
|
259
276
|
export function useImagesLoadStatus(
|
|
260
|
-
|
|
261
|
-
|
|
277
|
+
imageUrls: string[],
|
|
278
|
+
timeoutMS = 10000, // default timeout miliseconds
|
|
262
279
|
): boolean {
|
|
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
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
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]);
|
|
315
353
|
}
|
|
316
354
|
|
|
317
355
|
/*
|
|
318
356
|
* Track whether a referenced div's scroll position has passed a pixel offset.
|
|
319
357
|
*/
|
|
320
358
|
|
|
321
|
-
export function useScrollPastPosition(
|
|
322
|
-
|
|
323
|
-
)
|
|
324
|
-
const divRef = useRef<HTMLDivElement>(null);
|
|
325
|
-
const [hasScrolledPast, setHasScrolledPast] = useState(false);
|
|
359
|
+
export function useScrollPastPosition(pixels: number) {
|
|
360
|
+
const divRef = useRef<HTMLDivElement>(null);
|
|
361
|
+
const [hasScrolledPast, setHasScrolledPast] = useState(false);
|
|
326
362
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
const element = divRef.current;
|
|
365
|
+
if (!element) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
332
368
|
|
|
333
|
-
|
|
334
|
-
|
|
369
|
+
const handleScroll = () => {
|
|
370
|
+
const nextValue = element.scrollTop > pixels;
|
|
335
371
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
372
|
+
setHasScrolledPast((currentValue) => {
|
|
373
|
+
return currentValue === nextValue ? currentValue : nextValue;
|
|
374
|
+
});
|
|
375
|
+
};
|
|
340
376
|
|
|
341
|
-
|
|
342
|
-
|
|
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
|
+
}
|
|
343
387
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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.
|
|
402
|
+
*/
|
|
348
403
|
|
|
349
|
-
|
|
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;
|
|
350
465
|
}
|