@yamatozhen/design_system 1.0.0 → 1.1.0
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/LICENSE +201 -201
- package/README.md +101 -101
- package/package.json +50 -49
- package/scripts/postinstall.js +91 -105
- package/template/app/ClientNav.tsx +258 -310
- package/template/app/globals.css +2 -167
- package/template/app/layout.tsx +94 -128
- package/template/app/themes/dark-mc 2.css +51 -0
- package/template/app/themes/dark-mc.css +51 -51
- package/template/app/themes/dark.css +51 -51
- package/template/app/themes/light 2.css +51 -0
- package/template/app/themes/light-mc 2.css +51 -0
- package/template/app/themes/light-mc.css +51 -51
- package/template/app/themes/light.css +51 -51
- package/template/app/themes/theme 2.css +272 -0
- package/template/app/themes/theme.css +272 -82
- package/template/components/{layout/button → (buttons)}/Button.module.css +351 -305
- package/template/components/{layout/button → (buttons)}/ButtonGroup.tsx +414 -503
- package/template/components/{layout/button → (buttons)}/FAB.tsx +119 -118
- package/template/components/(buttons)/button.tsx +164 -0
- package/template/components/(controls)/slider/Slider.module.css +250 -0
- package/template/components/(controls)/slider/Slider.tsx +386 -0
- package/template/components/(controls)/switch/Switch.module.css +108 -0
- package/template/components/(controls)/switch/Switch.tsx +69 -0
- package/template/components/(date & time)/calendar/Calendar.module.css +125 -0
- package/template/components/(date & time)/calendar/Calendar.tsx +206 -0
- package/template/components/(date & time)/dateField/DateField.module.css +25 -0
- package/template/components/(date & time)/dateField/DateField.tsx +193 -0
- package/template/components/(date & time)/datePicker/DatePicker.tsx +118 -0
- package/template/components/(date & time)/dateRangePicker/DateRangePicker.tsx +144 -0
- package/template/components/(date & time)/rangeCalendar/RangeCalendar.module.css +203 -0
- package/template/components/(date & time)/rangeCalendar/RangeCalendar.tsx +263 -0
- package/template/components/(date & time)/shared/dateUtils.ts +107 -0
- package/template/components/(date & time)/timeField/TimeField.module.css +39 -0
- package/template/components/(date & time)/timeField/TimeField.tsx +158 -0
- package/template/components/(feedback)/circularProgress/CircularProgress.tsx +646 -0
- package/template/components/(feedback)/linearProgress/LinearProgress.tsx +337 -0
- package/template/components/(feedback)/meter/Meter.tsx +111 -0
- package/template/components/(feedback)/skeleton/Skeleton.module.css +49 -0
- package/template/components/(feedback)/skeleton/Skeleton.tsx +32 -0
- package/template/components/(feedback)/spinner/Spinner.module.css +27 -0
- package/template/components/(feedback)/spinner/Spinner.tsx +60 -0
- package/template/components/(forms)/checkbox/Checkbox.tsx +121 -0
- package/template/components/(forms)/checkboxGroup/CheckboxGroup.tsx +83 -0
- package/template/components/(forms)/description/Description.tsx +34 -0
- package/template/components/(forms)/errorMessage/ErrorMessage.tsx +31 -0
- package/template/components/(forms)/fieldError/FieldError.tsx +39 -0
- package/template/components/(forms)/fieldset/Fieldset.tsx +51 -0
- package/template/components/(forms)/form/Form.tsx +54 -0
- package/template/components/(forms)/input/Input.tsx +50 -0
- package/template/components/(forms)/inputGroup/InputGroup.tsx +65 -0
- package/template/components/(forms)/inputOtp/InputOtp.tsx +139 -0
- package/template/components/(forms)/label/Label.tsx +46 -0
- package/template/components/(forms)/numberField/NumberField.tsx +292 -0
- package/template/components/(forms)/radioGroup/RadioGroup.tsx +175 -0
- package/template/components/(forms)/searchField/SearchField.tsx +139 -0
- package/template/components/(forms)/shared/field.module.css +75 -0
- package/template/components/(forms)/shared/fieldContext.ts +28 -0
- package/template/components/(forms)/textArea/TextArea.tsx +55 -0
- package/template/components/(forms)/textField/TextField.tsx +91 -0
- package/template/components/{layout → (layout)}/Lightbox/LightboxCard.tsx +100 -100
- package/template/components/{layout → (layout)}/Lightbox/LightboxGallery.module.css +249 -249
- package/template/components/{layout → (layout)}/Lightbox/LightboxGallery.tsx +72 -72
- package/template/components/{layout → (layout)}/Lightbox/LightboxGrid.tsx +185 -186
- package/template/components/{layout → (layout)}/Lightbox/LightboxModal.module.css +395 -395
- package/template/components/{layout → (layout)}/Lightbox/LightboxModal.tsx +498 -500
- package/template/components/{layout → (layout)}/Lightbox/demo.tsx +396 -412
- package/template/components/{layout → (layout)}/Lightbox/index.ts +30 -30
- package/template/components/{layout → (layout)}/Lightbox/types.ts +101 -113
- package/template/components/{layout → (layout)}/Lightbox/useLightboxFilters.ts +140 -140
- package/template/components/(layout)/accordion/Accordion.module.css +108 -0
- package/template/components/(layout)/accordion/Accordion.tsx +251 -0
- package/template/components/(layout)/card/Card.module.css +136 -0
- package/template/components/(layout)/card/Card.tsx +371 -0
- package/template/components/(layout)/list/List.module.css +83 -0
- package/template/components/(layout)/list/List.tsx +332 -0
- package/template/components/{layout → (layout)}/navigation/Wrap.tsx +41 -41
- package/template/components/(layout)/navigation/breadcrumbs/Breadcrumbs.tsx +145 -0
- package/template/components/{layout/navigation/nav/Nav.module.css → (layout)/navigation/nav/Nav.module.css.old} +474 -508
- package/template/components/{layout → (layout)}/navigation/nav/Nav.tsx +186 -167
- package/template/components/{layout → (layout)}/navigation/nav/NavBar.tsx +396 -419
- package/template/components/{layout → (layout)}/navigation/nav/NavContext.tsx +259 -259
- package/template/components/{layout → (layout)}/navigation/nav/NavItem.tsx +195 -173
- package/template/components/{layout → (layout)}/navigation/nav/NavNew.module.css +406 -373
- package/template/components/{layout → (layout)}/navigation/nav/NavRail.tsx +317 -315
- package/template/components/{layout → (layout)}/navigation/nav/NavSection.tsx +101 -102
- package/template/components/{layout → (layout)}/navigation/nav/NavTab.tsx +25 -25
- package/template/components/{layout → (layout)}/navigation/nav/navftb/NavFTB.module.css +253 -252
- package/template/components/{layout → (layout)}/navigation/nav/navftb/NavFTB.tsx +448 -399
- package/template/components/{layout → (layout)}/navigation/nav/navftb/useTabIndicator.ts +498 -484
- package/template/components/{layout → (layout)}/navigation/nav/navheader/NavHeader.module.css +59 -59
- package/template/components/{layout → (layout)}/navigation/nav/navheader/NavHeader.tsx +465 -478
- package/template/components/(layout)/navigation/pagination/Pagination.tsx +261 -0
- package/template/components/{layout → (layout)}/pages/loadingScreen.tsx +117 -102
- package/template/components/{layout → (layout)}/pages/maintenance/Maintenance.tsx +52 -52
- package/template/components/{layout → (layout)}/pages/maintenance/maintenance.module.css +162 -162
- package/template/components/(layout)/scrollShadow/ScrollShadow.module.css +81 -0
- package/template/components/(layout)/scrollShadow/ScrollShadow.tsx +161 -0
- package/template/components/(layout)/scrollShadow/useScrollShadow.ts +130 -0
- package/template/components/(layout)/separator/Separator.tsx +112 -0
- package/template/components/(overlays)/accessibilityModal/AccessibilityModal.tsx +307 -0
- package/template/components/(overlays)/drawer/Drawer.tsx +953 -0
- package/template/components/(overlays)/modal/Modal.tsx +654 -0
- package/template/components/(overlays)/popover/Popover.tsx +420 -0
- package/template/components/(overlays)/toast/Toast.tsx +692 -0
- package/template/components/(pickers)/autocomplete/Autocomplete.tsx +24 -0
- package/template/components/(pickers)/comboBox/ComboBox.tsx +21 -0
- package/template/components/(pickers)/select/Select.tsx +188 -0
- package/template/components/(pickers)/shared/FilterableField.tsx +223 -0
- package/template/components/(pickers)/shared/PickerItem.ts +13 -0
- package/template/components/tools/CustomScrollbar 2.tsx +107 -0
- package/template/components/tools/CustomScrollbar.tsx +107 -0
- package/template/components/tools/Divider 2.tsx +11 -0
- package/template/components/tools/Divider.tsx +11 -11
- package/template/components/tools/ReduceMotionContext 2.tsx +5 -0
- package/template/components/tools/ReduceMotionContext.tsx +5 -5
- package/template/components/tools/Sinewave 2.tsx +206 -0
- package/template/components/tools/Sinewave.tsx +396 -199
- package/template/components/tools/SinewaveProgress 2.tsx +140 -0
- package/template/components/tools/SinewaveProgress.tsx +140 -135
- package/template/components/tools/badge/Badge 2.tsx +28 -0
- package/template/components/tools/badge/Badge.tsx +28 -28
- package/template/components/tools/dropdown/drop.module.css +196 -0
- package/template/components/tools/dropdown/drop.tsx +568 -0
- package/template/components/tools/easing.ts +118 -0
- package/template/components/tools/flubber.d 2.ts +7 -0
- package/template/components/tools/flubber.d.ts +6 -6
- package/template/components/tools/footer/Footer.tsx +20 -0
- package/template/components/tools/input/Input 2.tsx +99 -0
- package/template/components/tools/input/Input.module 2.css +45 -0
- package/template/components/tools/input/Input.module.css +45 -0
- package/template/components/tools/input/Input.tsx +99 -0
- package/template/components/tools/label/Icon 2.tsx +39 -0
- package/template/components/tools/label/Icon.tsx +39 -0
- package/template/components/tools/label/Label 2.tsx +150 -0
- package/template/components/tools/label/Label.tsx +156 -0
- package/template/components/tools/mobileViewProvider 2.tsx +53 -0
- package/template/components/tools/mobileViewProvider.tsx +53 -58
- package/template/components/tools/ripple/ripple 2.css +38 -0
- package/template/components/tools/ripple/ripple 2.tsx +78 -0
- package/template/components/tools/ripple/ripple.css +38 -41
- package/template/components/tools/ripple/ripple.tsx +78 -53
- package/template/components/tools/shape/{ShapeMorph.tsx → Shape 2.tsx } +313 -190
- package/template/components/tools/shape/Shape.module 2.css +21 -0
- package/template/components/tools/shape/Shape.module.css +25 -0
- package/template/components/tools/shape/Shape.tsx +316 -0
- package/template/components/tools/shape/ShapeLoader 2.tsx +104 -0
- package/template/components/tools/shape/ShapeLoader.tsx +104 -101
- package/template/components/tools/shape/ShapePaths 2.ts +116 -0
- package/template/components/tools/shape/ShapePaths.ts +116 -116
- package/template/components/{layout/switch/Switch.tsx → tools/switch/Switch 2.tsx } +48 -48
- package/template/components/{layout/switch/Switch.module.css → tools/switch/Switch.module 2.css } +102 -102
- package/template/components/tools/switch/Switch.module.css +102 -102
- package/template/components/tools/switch/Switch.tsx +48 -48
- package/template/components/tools/useBodyScrollLock 2.tsx +27 -0
- package/template/components/tools/useBodyScrollLock.tsx +27 -27
- package/template/components/tools/useImageColor 2.ts +47 -0
- package/template/components/tools/useImageColor.ts +47 -47
- package/template/components/tools/useNavSheetDrag 2.tsx +196 -0
- package/template/components/tools/useNavSheetDrag.tsx +196 -196
- package/template/public/fonts/material-symbols/MaterialSymbolsRounded-VariableFont_FILL,GRAD,opsz,wght 2.woff2 +0 -0
- package/template/components/layout/accordion/Accordion.module.css +0 -149
- package/template/components/layout/accordion/Accordion.tsx +0 -114
- package/template/components/layout/button/button.tsx +0 -128
- package/template/components/layout/button/drop.module.css +0 -229
- package/template/components/layout/button/drop.tsx +0 -516
- package/template/components/layout/hero/v1/heroSlideshow.module.css +0 -107
- package/template/components/layout/hero/v1/heroSlideshow.tsx +0 -233
- package/template/components/layout/modals/accessibilityModal/AccessibilityModal.module.css +0 -126
- package/template/components/layout/modals/accessibilityModal/AccessibilityModal.tsx +0 -290
- package/template/components/layout/modals/alert/Alert.tsx +0 -188
- package/template/components/layout/navigation/MioCard.module.css +0 -88
- package/template/components/layout/navigation/MioCard.tsx +0 -266
- package/template/components/tools/ProgressiveBlur.tsx +0 -73
- package/template/components/tools/icon/SocialIcons.tsx +0 -88
- package/template/components/tools/icon/icon.module.css +0 -0
- package/template/components/tools/icon/icon.tsx +0 -125
- package/template/components/tools/react-bits/ClickSpark.tsx +0 -177
- package/template/components/tools/react-bits/FadeContent.tsx +0 -90
- package/template/components/tools/react-bits/GlareHover.tsx +0 -73
- package/template/components/tools/react-bits/GradientText/GradientText.css +0 -70
- package/template/components/tools/react-bits/GradientText/GradientText.tsx +0 -44
- package/template/components/tools/react-bits/Magnet.tsx +0 -118
- package/template/components/tools/react-bits/Stepper/Stepper.css +0 -134
- package/template/components/tools/react-bits/Stepper/Stepper.tsx +0 -412
|
@@ -1,484 +1,498 @@
|
|
|
1
|
-
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
-
import type { FTBTab } from "../NavContext";
|
|
3
|
-
|
|
4
|
-
// ── Spring constants ────────────────────────────────────────────────────────
|
|
5
|
-
const PK = 200,
|
|
6
|
-
PD = 22;
|
|
7
|
-
const SK = 300,
|
|
8
|
-
SD = 10;
|
|
9
|
-
|
|
10
|
-
const SQUASH_X = 0.26;
|
|
11
|
-
const SQUASH_Y = 0.26;
|
|
12
|
-
|
|
13
|
-
const IDLE = 0;
|
|
14
|
-
const FLYING = 1;
|
|
15
|
-
const ARRIVED = 2;
|
|
16
|
-
|
|
17
|
-
export interface UseTabIndicatorArgs {
|
|
18
|
-
selected: number;
|
|
19
|
-
setSelected: (index: number) => void;
|
|
20
|
-
tabs: FTBTab[];
|
|
21
|
-
elevated: boolean;
|
|
22
|
-
isEmbedded: boolean;
|
|
23
|
-
reduceMotion: boolean;
|
|
24
|
-
onTabChange?: (index: number) => void;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface UseTabIndicatorReturn {
|
|
28
|
-
containerRef: React.RefObject<HTMLDivElement | null>;
|
|
29
|
-
indicatorRef: React.RefObject<HTMLDivElement | null>;
|
|
30
|
-
registerTab: (index: number) => (el: HTMLElement | null) => void;
|
|
31
|
-
remeasureAndSnap: () => void;
|
|
32
|
-
onTabPointerEnter: (index: number) => void;
|
|
33
|
-
onTabPointerLeave: () => void;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Owns the floating indicator: geometry cache, the three-spring physics
|
|
38
|
-
* integrator, touch-drag, and all the re-snap effects. No JSX — it writes
|
|
39
|
-
* transform/width straight to `indicatorRef` and reads tab geometry from the
|
|
40
|
-
* refs handed back via `registerTab`. The host component owns selection state
|
|
41
|
-
* and passes it in; the hook calls back through `setSelected`/`onTabChange`
|
|
42
|
-
* only when a drag settles on a new tab.
|
|
43
|
-
*/
|
|
44
|
-
export function useTabIndicator({
|
|
45
|
-
selected,
|
|
46
|
-
setSelected,
|
|
47
|
-
tabs,
|
|
48
|
-
elevated,
|
|
49
|
-
isEmbedded,
|
|
50
|
-
reduceMotion,
|
|
51
|
-
onTabChange,
|
|
52
|
-
}: UseTabIndicatorArgs): UseTabIndicatorReturn {
|
|
53
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
54
|
-
const indicatorRef = useRef<HTMLDivElement>(null);
|
|
55
|
-
const tabRefs = useRef<(HTMLElement | null)[]>([]);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
setSelectedRef
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}, []);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
//
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
};
|
|
484
|
-
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
+
import type { FTBTab } from "../NavContext";
|
|
3
|
+
|
|
4
|
+
// ── Spring constants ────────────────────────────────────────────────────────
|
|
5
|
+
const PK = 200,
|
|
6
|
+
PD = 22;
|
|
7
|
+
const SK = 300,
|
|
8
|
+
SD = 10;
|
|
9
|
+
|
|
10
|
+
const SQUASH_X = 0.26;
|
|
11
|
+
const SQUASH_Y = 0.26;
|
|
12
|
+
|
|
13
|
+
const IDLE = 0;
|
|
14
|
+
const FLYING = 1;
|
|
15
|
+
const ARRIVED = 2;
|
|
16
|
+
|
|
17
|
+
export interface UseTabIndicatorArgs {
|
|
18
|
+
selected: number;
|
|
19
|
+
setSelected: (index: number) => void;
|
|
20
|
+
tabs: FTBTab[];
|
|
21
|
+
elevated: boolean;
|
|
22
|
+
isEmbedded: boolean;
|
|
23
|
+
reduceMotion: boolean;
|
|
24
|
+
onTabChange?: (index: number) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface UseTabIndicatorReturn {
|
|
28
|
+
containerRef: React.RefObject<HTMLDivElement | null>;
|
|
29
|
+
indicatorRef: React.RefObject<HTMLDivElement | null>;
|
|
30
|
+
registerTab: (index: number) => (el: HTMLElement | null) => void;
|
|
31
|
+
remeasureAndSnap: () => void;
|
|
32
|
+
onTabPointerEnter: (index: number) => void;
|
|
33
|
+
onTabPointerLeave: () => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Owns the floating indicator: geometry cache, the three-spring physics
|
|
38
|
+
* integrator, touch-drag, and all the re-snap effects. No JSX — it writes
|
|
39
|
+
* transform/width straight to `indicatorRef` and reads tab geometry from the
|
|
40
|
+
* refs handed back via `registerTab`. The host component owns selection state
|
|
41
|
+
* and passes it in; the hook calls back through `setSelected`/`onTabChange`
|
|
42
|
+
* only when a drag settles on a new tab.
|
|
43
|
+
*/
|
|
44
|
+
export function useTabIndicator({
|
|
45
|
+
selected,
|
|
46
|
+
setSelected,
|
|
47
|
+
tabs,
|
|
48
|
+
elevated,
|
|
49
|
+
isEmbedded,
|
|
50
|
+
reduceMotion,
|
|
51
|
+
onTabChange,
|
|
52
|
+
}: UseTabIndicatorArgs): UseTabIndicatorReturn {
|
|
53
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
54
|
+
const indicatorRef = useRef<HTMLDivElement>(null);
|
|
55
|
+
const tabRefs = useRef<(HTMLElement | null)[]>([]);
|
|
56
|
+
|
|
57
|
+
// "Latest ref" pattern — lets the imperative drag/loop code below always
|
|
58
|
+
// read the freshest prop without adding it to effect dep arrays. Synced in
|
|
59
|
+
// a layout effect (not written directly during render) since ref writes
|
|
60
|
+
// aren't render-pure.
|
|
61
|
+
const selectedRef = useRef(selected);
|
|
62
|
+
const tabsRef = useRef(tabs);
|
|
63
|
+
const onTabChangeRef = useRef(onTabChange);
|
|
64
|
+
const setSelectedRef = useRef(setSelected);
|
|
65
|
+
useLayoutEffect(() => {
|
|
66
|
+
selectedRef.current = selected;
|
|
67
|
+
tabsRef.current = tabs;
|
|
68
|
+
onTabChangeRef.current = onTabChange;
|
|
69
|
+
setSelectedRef.current = setSelected;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const pxRef = useRef(0),
|
|
73
|
+
vxRef = useRef(0),
|
|
74
|
+
txRef = useRef(0);
|
|
75
|
+
|
|
76
|
+
const sxRef = useRef(1),
|
|
77
|
+
svxRef = useRef(0),
|
|
78
|
+
stxRef = useRef(1);
|
|
79
|
+
const syRef = useRef(1),
|
|
80
|
+
svyRef = useRef(0),
|
|
81
|
+
styRef = useRef(1);
|
|
82
|
+
|
|
83
|
+
const phaseRef = useRef(IDLE);
|
|
84
|
+
const dirRef = useRef(0);
|
|
85
|
+
const rafRef = useRef<number | null>(null);
|
|
86
|
+
const lastRef = useRef<number | null>(null);
|
|
87
|
+
const readyRef = useRef(false);
|
|
88
|
+
|
|
89
|
+
const tabRectsRef = useRef<{ x: number; width: number }[]>([]);
|
|
90
|
+
|
|
91
|
+
// Holds the current `loop` implementation so its own requestAnimationFrame
|
|
92
|
+
// recursion (below) doesn't reference the `loop` binding before its
|
|
93
|
+
// `const loop = useCallback(...)` assignment has finished evaluating.
|
|
94
|
+
const loopRef = useRef<(ts: number) => void>(() => {});
|
|
95
|
+
|
|
96
|
+
// Caches every tab's scroll-content position in one batched read pass.
|
|
97
|
+
// Geometry only changes on resize / tab-count change, so callers read the
|
|
98
|
+
// cache instead of forcing layout via getBoundingClientRect per frame/drag.
|
|
99
|
+
const measureTabs = useCallback(() => {
|
|
100
|
+
const container = containerRef.current;
|
|
101
|
+
if (!container) return;
|
|
102
|
+
const rects = tabRectsRef.current;
|
|
103
|
+
rects.length = tabRefs.current.length;
|
|
104
|
+
for (let i = 0; i < tabRefs.current.length; i++) {
|
|
105
|
+
const tab = tabRefs.current[i];
|
|
106
|
+
if (!tab) {
|
|
107
|
+
rects[i] = { x: 0, width: 0 };
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
// offsetLeft/offsetWidth are LAYOUT geometry — unaffected by the wrapper's
|
|
111
|
+
// `whileHover` scale or Framer `layout` transform, and final the instant
|
|
112
|
+
// the style commits. getBoundingClientRect returns the transformed,
|
|
113
|
+
// mid-animation box, so it reports the wrong width and never settles.
|
|
114
|
+
rects[i] = { x: tab.offsetLeft, width: tab.offsetWidth };
|
|
115
|
+
}
|
|
116
|
+
}, []);
|
|
117
|
+
|
|
118
|
+
const getTabRect = useCallback(
|
|
119
|
+
(index: number) => tabRectsRef.current[index] ?? null,
|
|
120
|
+
[],
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const commit = useCallback(() => {
|
|
124
|
+
const el = indicatorRef.current;
|
|
125
|
+
if (!el) return;
|
|
126
|
+
el.style.transform = `translateX(${pxRef.current}px) scaleX(${sxRef.current}) scaleY(${syRef.current})`;
|
|
127
|
+
}, []);
|
|
128
|
+
|
|
129
|
+
// Per-frame integrator. Three critically-damped springs (position, scaleX,
|
|
130
|
+
// scaleY) advanced with semi-implicit Euler, math inlined to keep the loop
|
|
131
|
+
// allocation-free. FLYING applies directional squash/stretch by velocity;
|
|
132
|
+
// ARRIVED eases the overshoot back to rest. Only touches refs + one transform
|
|
133
|
+
// write — never reads layout — so it stays on the compositor's good side.
|
|
134
|
+
const loop = useCallback(
|
|
135
|
+
(ts: number) => {
|
|
136
|
+
if (!lastRef.current) lastRef.current = ts;
|
|
137
|
+
const dt = Math.min((ts - lastRef.current) / 1000, 0.032);
|
|
138
|
+
lastRef.current = ts;
|
|
139
|
+
|
|
140
|
+
vxRef.current +=
|
|
141
|
+
(-PK * (pxRef.current - txRef.current) - PD * vxRef.current) * dt;
|
|
142
|
+
pxRef.current += vxRef.current * dt;
|
|
143
|
+
|
|
144
|
+
const speed = Math.abs(vxRef.current);
|
|
145
|
+
const dist = Math.abs(pxRef.current - txRef.current);
|
|
146
|
+
|
|
147
|
+
if (phaseRef.current === FLYING) {
|
|
148
|
+
const mag = Math.min(speed / 700, 1);
|
|
149
|
+
if (dirRef.current > 0) {
|
|
150
|
+
stxRef.current = 1 - mag * 0.06;
|
|
151
|
+
styRef.current = 1 + mag * SQUASH_Y;
|
|
152
|
+
} else {
|
|
153
|
+
stxRef.current = 1 + mag * SQUASH_X;
|
|
154
|
+
styRef.current = 1 - mag * 0.08;
|
|
155
|
+
}
|
|
156
|
+
if (dist < 6 && speed < 100) {
|
|
157
|
+
phaseRef.current = ARRIVED;
|
|
158
|
+
if (dirRef.current > 0) {
|
|
159
|
+
stxRef.current = 1 - 0.08;
|
|
160
|
+
styRef.current = 1 + SQUASH_Y * 0.6;
|
|
161
|
+
} else {
|
|
162
|
+
stxRef.current = 1 + SQUASH_X * 0.5;
|
|
163
|
+
styRef.current = 1 - 0.06;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (phaseRef.current === ARRIVED) {
|
|
169
|
+
stxRef.current += (1 - stxRef.current) * Math.min(dt * 6, 1);
|
|
170
|
+
styRef.current += (1 - styRef.current) * Math.min(dt * 6, 1);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const effectiveSK = reduceMotion ? 1 : SK;
|
|
174
|
+
|
|
175
|
+
svxRef.current +=
|
|
176
|
+
(-effectiveSK * (sxRef.current - stxRef.current) -
|
|
177
|
+
SD * svxRef.current) *
|
|
178
|
+
dt;
|
|
179
|
+
sxRef.current += svxRef.current * dt;
|
|
180
|
+
svyRef.current +=
|
|
181
|
+
(-effectiveSK * (syRef.current - styRef.current) -
|
|
182
|
+
SD * svyRef.current) *
|
|
183
|
+
dt;
|
|
184
|
+
syRef.current += svyRef.current * dt;
|
|
185
|
+
|
|
186
|
+
commit();
|
|
187
|
+
|
|
188
|
+
const settled =
|
|
189
|
+
dist < 0.15 &&
|
|
190
|
+
speed < 0.4 &&
|
|
191
|
+
Math.abs(sxRef.current - 1) < 0.003 &&
|
|
192
|
+
Math.abs(svxRef.current) < 0.01 &&
|
|
193
|
+
Math.abs(syRef.current - 1) < 0.003 &&
|
|
194
|
+
Math.abs(svyRef.current) < 0.01;
|
|
195
|
+
|
|
196
|
+
if (!settled) {
|
|
197
|
+
rafRef.current = requestAnimationFrame(loopRef.current);
|
|
198
|
+
} else {
|
|
199
|
+
sxRef.current = 1;
|
|
200
|
+
svxRef.current = 0;
|
|
201
|
+
stxRef.current = 1;
|
|
202
|
+
syRef.current = 1;
|
|
203
|
+
svyRef.current = 0;
|
|
204
|
+
styRef.current = 1;
|
|
205
|
+
phaseRef.current = IDLE;
|
|
206
|
+
rafRef.current = null;
|
|
207
|
+
lastRef.current = null;
|
|
208
|
+
commit();
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
[commit, reduceMotion],
|
|
212
|
+
);
|
|
213
|
+
useLayoutEffect(() => {
|
|
214
|
+
loopRef.current = loop;
|
|
215
|
+
}, [loop]);
|
|
216
|
+
|
|
217
|
+
// Retargets the indicator to a tab. First call snaps (no animation); after
|
|
218
|
+
// that it kicks off the spring loop toward the new tab's cached position.
|
|
219
|
+
const moveTo = useCallback(
|
|
220
|
+
(index: number) => {
|
|
221
|
+
const rect = getTabRect(index);
|
|
222
|
+
if (!rect) return;
|
|
223
|
+
|
|
224
|
+
const el = indicatorRef.current;
|
|
225
|
+
if (el) el.style.width = `${rect.width}px`;
|
|
226
|
+
|
|
227
|
+
if (!readyRef.current) {
|
|
228
|
+
pxRef.current = rect.x;
|
|
229
|
+
txRef.current = rect.x;
|
|
230
|
+
readyRef.current = true;
|
|
231
|
+
commit();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
|
|
236
|
+
|
|
237
|
+
dirRef.current = rect.x > txRef.current ? 1 : -1;
|
|
238
|
+
txRef.current = rect.x;
|
|
239
|
+
sxRef.current = 1;
|
|
240
|
+
syRef.current = 1;
|
|
241
|
+
svxRef.current = 0;
|
|
242
|
+
svyRef.current = 0;
|
|
243
|
+
stxRef.current = 1;
|
|
244
|
+
styRef.current = 1;
|
|
245
|
+
phaseRef.current = FLYING;
|
|
246
|
+
lastRef.current = null;
|
|
247
|
+
|
|
248
|
+
rafRef.current = requestAnimationFrame(loop);
|
|
249
|
+
},
|
|
250
|
+
[getTabRect, loop, commit],
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// Hard-snaps the indicator to the selected tab's CURRENT cached geometry
|
|
254
|
+
// (no animation). Assumes the cache is already warm — callers measureTabs()
|
|
255
|
+
// first if the layout just changed.
|
|
256
|
+
const snapToSelected = useCallback(() => {
|
|
257
|
+
if (!readyRef.current) return;
|
|
258
|
+
const rect = getTabRect(selectedRef.current);
|
|
259
|
+
if (!rect) return;
|
|
260
|
+
|
|
261
|
+
if (rafRef.current !== null) {
|
|
262
|
+
cancelAnimationFrame(rafRef.current);
|
|
263
|
+
rafRef.current = null;
|
|
264
|
+
}
|
|
265
|
+
const ind = indicatorRef.current;
|
|
266
|
+
if (ind) ind.style.width = `${rect.width}px`;
|
|
267
|
+
pxRef.current = rect.x;
|
|
268
|
+
txRef.current = rect.x;
|
|
269
|
+
sxRef.current = 1;
|
|
270
|
+
svxRef.current = 0;
|
|
271
|
+
stxRef.current = 1;
|
|
272
|
+
syRef.current = 1;
|
|
273
|
+
svyRef.current = 0;
|
|
274
|
+
styRef.current = 1;
|
|
275
|
+
phaseRef.current = IDLE;
|
|
276
|
+
lastRef.current = null;
|
|
277
|
+
commit();
|
|
278
|
+
}, [getTabRect, commit]);
|
|
279
|
+
|
|
280
|
+
// Warm the geometry cache before the first moveTo and on tab-count change.
|
|
281
|
+
useLayoutEffect(() => {
|
|
282
|
+
measureTabs();
|
|
283
|
+
}, [tabs, measureTabs]);
|
|
284
|
+
|
|
285
|
+
// Re-measure and re-snap whenever the floating/elevated state flips. Offset
|
|
286
|
+
// geometry is final at commit time regardless of the in-flight `layout`
|
|
287
|
+
// transition, so a plain snap here lands on the correct width; the host also
|
|
288
|
+
// calls remeasureAndSnap() on onLayoutAnimationComplete to clean up drift.
|
|
289
|
+
useLayoutEffect(() => {
|
|
290
|
+
measureTabs();
|
|
291
|
+
snapToSelected();
|
|
292
|
+
}, [elevated, isEmbedded, measureTabs, snapToSelected]);
|
|
293
|
+
|
|
294
|
+
useLayoutEffect(() => {
|
|
295
|
+
moveTo(selected);
|
|
296
|
+
}, [selected, moveTo]);
|
|
297
|
+
|
|
298
|
+
// Re-snaps when tab geometry ACTUALLY changes. Incidental reflows (a sibling
|
|
299
|
+
// hovering, a scrollbar toggling, a height-only change) fire the observer
|
|
300
|
+
// too; the guard prevents those from cancelling the animation.
|
|
301
|
+
useEffect(() => {
|
|
302
|
+
const el = containerRef.current;
|
|
303
|
+
if (!el) return;
|
|
304
|
+
const ro = new ResizeObserver(() => {
|
|
305
|
+
const prevRect = readyRef.current
|
|
306
|
+
? getTabRect(selectedRef.current)
|
|
307
|
+
: null;
|
|
308
|
+
measureTabs();
|
|
309
|
+
if (!readyRef.current) return;
|
|
310
|
+
const rect = getTabRect(selectedRef.current);
|
|
311
|
+
if (!rect) return;
|
|
312
|
+
|
|
313
|
+
if (prevRect && prevRect.x === rect.x && prevRect.width === rect.width) {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
snapToSelected();
|
|
318
|
+
});
|
|
319
|
+
ro.observe(el);
|
|
320
|
+
return () => ro.disconnect();
|
|
321
|
+
}, [getTabRect, measureTabs, snapToSelected]);
|
|
322
|
+
|
|
323
|
+
useEffect(
|
|
324
|
+
() => () => {
|
|
325
|
+
if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
|
|
326
|
+
},
|
|
327
|
+
[],
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
// Touch drag: grab the indicator, follow the finger within the tab bounds,
|
|
331
|
+
// then on release snap to the nearest tab (velocity carries into the spring).
|
|
332
|
+
useEffect(() => {
|
|
333
|
+
const el = containerRef.current;
|
|
334
|
+
if (!el) return;
|
|
335
|
+
|
|
336
|
+
type DragState = {
|
|
337
|
+
startX: number;
|
|
338
|
+
startPx: number;
|
|
339
|
+
lastX: number;
|
|
340
|
+
lastT: number;
|
|
341
|
+
velX: number;
|
|
342
|
+
active: boolean;
|
|
343
|
+
};
|
|
344
|
+
let drag: DragState | null = null;
|
|
345
|
+
|
|
346
|
+
const onTouchStart = (e: TouchEvent) => {
|
|
347
|
+
if (e.touches.length !== 1) return;
|
|
348
|
+
const touch = e.touches[0];
|
|
349
|
+
const ind = indicatorRef.current;
|
|
350
|
+
if (!ind) return;
|
|
351
|
+
const r = ind.getBoundingClientRect();
|
|
352
|
+
if (
|
|
353
|
+
touch.clientX < r.left ||
|
|
354
|
+
touch.clientX > r.right ||
|
|
355
|
+
touch.clientY < r.top ||
|
|
356
|
+
touch.clientY > r.bottom
|
|
357
|
+
)
|
|
358
|
+
return;
|
|
359
|
+
if (rafRef.current !== null) {
|
|
360
|
+
cancelAnimationFrame(rafRef.current);
|
|
361
|
+
rafRef.current = null;
|
|
362
|
+
lastRef.current = null;
|
|
363
|
+
}
|
|
364
|
+
drag = {
|
|
365
|
+
startX: touch.clientX,
|
|
366
|
+
startPx: pxRef.current,
|
|
367
|
+
lastX: touch.clientX,
|
|
368
|
+
lastT: e.timeStamp,
|
|
369
|
+
velX: 0,
|
|
370
|
+
active: false,
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const onTouchMove = (e: TouchEvent) => {
|
|
375
|
+
if (!drag || e.touches.length !== 1) return;
|
|
376
|
+
const touch = e.touches[0];
|
|
377
|
+
const dx = touch.clientX - drag.startX;
|
|
378
|
+
if (!drag.active) {
|
|
379
|
+
if (Math.abs(dx) < 6) return;
|
|
380
|
+
drag.active = true;
|
|
381
|
+
}
|
|
382
|
+
e.preventDefault();
|
|
383
|
+
|
|
384
|
+
const now = e.timeStamp;
|
|
385
|
+
const dt = now - drag.lastT;
|
|
386
|
+
if (dt > 0) {
|
|
387
|
+
const raw = ((touch.clientX - drag.lastX) / dt) * 1000;
|
|
388
|
+
drag.velX = drag.velX * 0.6 + raw * 0.4;
|
|
389
|
+
}
|
|
390
|
+
drag.lastX = touch.clientX;
|
|
391
|
+
drag.lastT = now;
|
|
392
|
+
|
|
393
|
+
const currentTabs = tabsRef.current;
|
|
394
|
+
const firstRect = getTabRect(0);
|
|
395
|
+
const lastRect = getTabRect(currentTabs.length - 1);
|
|
396
|
+
const minX = firstRect ? firstRect.x : 0;
|
|
397
|
+
const maxX = lastRect ? lastRect.x : 0;
|
|
398
|
+
|
|
399
|
+
pxRef.current = Math.max(minX, Math.min(maxX, drag.startPx + dx));
|
|
400
|
+
txRef.current = pxRef.current;
|
|
401
|
+
vxRef.current = 0;
|
|
402
|
+
|
|
403
|
+
const mag = Math.min(Math.abs(drag.velX) / 700, 1);
|
|
404
|
+
if (drag.velX > 2) {
|
|
405
|
+
sxRef.current = 1 - mag * 0.06;
|
|
406
|
+
syRef.current = 1 + mag * SQUASH_Y;
|
|
407
|
+
} else if (drag.velX < -2) {
|
|
408
|
+
sxRef.current = 1 + mag * SQUASH_X;
|
|
409
|
+
syRef.current = 1 - mag * 0.08;
|
|
410
|
+
} else {
|
|
411
|
+
sxRef.current = 1;
|
|
412
|
+
syRef.current = 1;
|
|
413
|
+
}
|
|
414
|
+
commit();
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
const onTouchEnd = () => {
|
|
418
|
+
if (!drag?.active) {
|
|
419
|
+
drag = null;
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
const { velX } = drag;
|
|
423
|
+
drag = null;
|
|
424
|
+
|
|
425
|
+
const currentTabs = tabsRef.current;
|
|
426
|
+
let nearestIndex = selectedRef.current;
|
|
427
|
+
let minDist = Infinity;
|
|
428
|
+
for (let i = 0; i < currentTabs.length; i++) {
|
|
429
|
+
const rect = getTabRect(i);
|
|
430
|
+
if (!rect) continue;
|
|
431
|
+
const dist = Math.abs(pxRef.current - rect.x);
|
|
432
|
+
if (dist < minDist) {
|
|
433
|
+
minDist = dist;
|
|
434
|
+
nearestIndex = i;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
vxRef.current = velX;
|
|
439
|
+
|
|
440
|
+
if (nearestIndex !== selectedRef.current) {
|
|
441
|
+
setSelectedRef.current(nearestIndex);
|
|
442
|
+
onTabChangeRef.current?.(nearestIndex);
|
|
443
|
+
} else {
|
|
444
|
+
moveTo(nearestIndex);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
el.addEventListener("touchstart", onTouchStart, { passive: true });
|
|
449
|
+
el.addEventListener("touchmove", onTouchMove, { passive: false });
|
|
450
|
+
el.addEventListener("touchend", onTouchEnd, { passive: true });
|
|
451
|
+
el.addEventListener("touchcancel", onTouchEnd, { passive: true });
|
|
452
|
+
return () => {
|
|
453
|
+
el.removeEventListener("touchstart", onTouchStart);
|
|
454
|
+
el.removeEventListener("touchmove", onTouchMove);
|
|
455
|
+
el.removeEventListener("touchend", onTouchEnd);
|
|
456
|
+
el.removeEventListener("touchcancel", onTouchEnd);
|
|
457
|
+
};
|
|
458
|
+
}, [commit, getTabRect, moveTo]);
|
|
459
|
+
|
|
460
|
+
// Hovering the selected tab brightens the indicator. Imperative rather than a
|
|
461
|
+
// CSS :has(:hover) rule — :has(:hover) forces a document-wide style recalc on
|
|
462
|
+
// every pointer move and flickers under rapid hovering.
|
|
463
|
+
const onTabPointerEnter = useCallback((index: number) => {
|
|
464
|
+
if (index === selectedRef.current && indicatorRef.current) {
|
|
465
|
+
indicatorRef.current.style.filter = "var(--filterBrightness)";
|
|
466
|
+
}
|
|
467
|
+
}, []);
|
|
468
|
+
|
|
469
|
+
const onTabPointerLeave = useCallback(() => {
|
|
470
|
+
if (indicatorRef.current) indicatorRef.current.style.filter = "";
|
|
471
|
+
}, []);
|
|
472
|
+
|
|
473
|
+
// Stable per-index ref callbacks so tabs don't get null-then-set on every
|
|
474
|
+
// render (which inline `(el) => ...` would cause).
|
|
475
|
+
const tabRefCallbacks = useRef<Array<(el: HTMLElement | null) => void>>([]);
|
|
476
|
+
const registerTab = useCallback((index: number) => {
|
|
477
|
+
if (!tabRefCallbacks.current[index]) {
|
|
478
|
+
tabRefCallbacks.current[index] = (el: HTMLElement | null) => {
|
|
479
|
+
tabRefs.current[index] = el;
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
return tabRefCallbacks.current[index];
|
|
483
|
+
}, []);
|
|
484
|
+
|
|
485
|
+
const remeasureAndSnap = useCallback(() => {
|
|
486
|
+
measureTabs();
|
|
487
|
+
snapToSelected();
|
|
488
|
+
}, [measureTabs, snapToSelected]);
|
|
489
|
+
|
|
490
|
+
return {
|
|
491
|
+
containerRef,
|
|
492
|
+
indicatorRef,
|
|
493
|
+
registerTab,
|
|
494
|
+
remeasureAndSnap,
|
|
495
|
+
onTabPointerEnter,
|
|
496
|
+
onTabPointerLeave,
|
|
497
|
+
};
|
|
498
|
+
}
|