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