@yamatozhen/design_system 1.0.1 → 1.2.1

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 (156) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +101 -101
  3. package/package.json +50 -50
  4. package/scripts/postinstall.js +91 -91
  5. package/template/app/ClientNav.tsx +258 -310
  6. package/template/app/globals.css +2 -167
  7. package/template/app/layout.tsx +94 -82
  8. package/template/app/themes/dark-mc.css +51 -51
  9. package/template/app/themes/dark.css +51 -51
  10. package/template/app/themes/light-mc.css +51 -51
  11. package/template/app/themes/light.css +51 -51
  12. package/template/app/themes/theme.css +272 -82
  13. package/template/components/{layout/button → (buttons)}/Button.module.css +351 -305
  14. package/template/components/{layout/button → (buttons)}/ButtonGroup.tsx +414 -503
  15. package/template/components/{layout/button → (buttons)}/FAB.tsx +119 -118
  16. package/template/components/(buttons)/button.tsx +164 -0
  17. package/template/components/(controls)/slider/Slider.module.css +250 -0
  18. package/template/components/(controls)/slider/Slider.tsx +386 -0
  19. package/template/components/{layout → (controls)}/switch/Switch.module.css +108 -102
  20. package/template/components/{layout → (controls)}/switch/Switch.tsx +69 -48
  21. package/template/components/(date & time)/calendar/Calendar.module.css +125 -0
  22. package/template/components/(date & time)/calendar/Calendar.tsx +206 -0
  23. package/template/components/(date & time)/dateField/DateField.module.css +25 -0
  24. package/template/components/(date & time)/dateField/DateField.tsx +193 -0
  25. package/template/components/(date & time)/datePicker/DatePicker.tsx +118 -0
  26. package/template/components/(date & time)/dateRangePicker/DateRangePicker.tsx +144 -0
  27. package/template/components/(date & time)/rangeCalendar/RangeCalendar.module.css +203 -0
  28. package/template/components/(date & time)/rangeCalendar/RangeCalendar.tsx +263 -0
  29. package/template/components/(date & time)/shared/dateUtils.ts +107 -0
  30. package/template/components/(date & time)/timeField/TimeField.module.css +39 -0
  31. package/template/components/(date & time)/timeField/TimeField.tsx +158 -0
  32. package/template/components/(feedback)/circularProgress/CircularProgress.tsx +646 -0
  33. package/template/components/(feedback)/linearProgress/LinearProgress.tsx +337 -0
  34. package/template/components/(feedback)/meter/Meter.tsx +111 -0
  35. package/template/components/(feedback)/skeleton/Skeleton.module.css +49 -0
  36. package/template/components/(feedback)/skeleton/Skeleton.tsx +32 -0
  37. package/template/components/(feedback)/spinner/Spinner.module.css +27 -0
  38. package/template/components/(feedback)/spinner/Spinner.tsx +60 -0
  39. package/template/components/(forms)/checkbox/Checkbox.tsx +121 -0
  40. package/template/components/(forms)/checkboxGroup/CheckboxGroup.tsx +83 -0
  41. package/template/components/(forms)/description/Description.tsx +34 -0
  42. package/template/components/(forms)/errorMessage/ErrorMessage.tsx +31 -0
  43. package/template/components/(forms)/fieldError/FieldError.tsx +39 -0
  44. package/template/components/(forms)/fieldset/Fieldset.tsx +51 -0
  45. package/template/components/(forms)/form/Form.tsx +54 -0
  46. package/template/components/(forms)/input/Input.tsx +50 -0
  47. package/template/components/(forms)/inputGroup/InputGroup.tsx +65 -0
  48. package/template/components/(forms)/inputOtp/InputOtp.tsx +139 -0
  49. package/template/components/(forms)/label/Label.tsx +46 -0
  50. package/template/components/(forms)/numberField/NumberField.tsx +292 -0
  51. package/template/components/(forms)/radioGroup/RadioGroup.tsx +175 -0
  52. package/template/components/(forms)/searchField/SearchField.tsx +139 -0
  53. package/template/components/(forms)/shared/field.module.css +75 -0
  54. package/template/components/(forms)/shared/fieldContext.ts +28 -0
  55. package/template/components/(forms)/textArea/TextArea.tsx +55 -0
  56. package/template/components/(forms)/textField/TextField.tsx +91 -0
  57. package/template/components/{layout → (layout)}/Lightbox/LightboxCard.tsx +100 -100
  58. package/template/components/{layout → (layout)}/Lightbox/LightboxGallery.module.css +249 -249
  59. package/template/components/{layout → (layout)}/Lightbox/LightboxGallery.tsx +72 -72
  60. package/template/components/{layout → (layout)}/Lightbox/LightboxGrid.tsx +185 -186
  61. package/template/components/{layout → (layout)}/Lightbox/LightboxModal.module.css +395 -395
  62. package/template/components/{layout → (layout)}/Lightbox/LightboxModal.tsx +498 -500
  63. package/template/components/{layout → (layout)}/Lightbox/demo.tsx +396 -412
  64. package/template/components/{layout → (layout)}/Lightbox/index.ts +30 -30
  65. package/template/components/{layout → (layout)}/Lightbox/types.ts +101 -113
  66. package/template/components/{layout → (layout)}/Lightbox/useLightboxFilters.ts +140 -140
  67. package/template/components/(layout)/accordion/Accordion.module.css +108 -0
  68. package/template/components/(layout)/accordion/Accordion.tsx +251 -0
  69. package/template/components/(layout)/card/Card.module.css +136 -0
  70. package/template/components/(layout)/card/Card.tsx +371 -0
  71. package/template/components/(layout)/list/List.module.css +83 -0
  72. package/template/components/(layout)/list/List.tsx +332 -0
  73. package/template/components/{layout → (layout)}/navigation/Wrap.tsx +41 -41
  74. package/template/components/(layout)/navigation/breadcrumbs/Breadcrumbs.tsx +145 -0
  75. package/template/components/{layout/navigation/nav/Nav.module.css → (layout)/navigation/nav/Nav.module.css.old} +474 -508
  76. package/template/components/{layout → (layout)}/navigation/nav/Nav.tsx +186 -167
  77. package/template/components/{layout → (layout)}/navigation/nav/NavBar.tsx +396 -419
  78. package/template/components/{layout → (layout)}/navigation/nav/NavContext.tsx +259 -259
  79. package/template/components/{layout → (layout)}/navigation/nav/NavItem.tsx +195 -173
  80. package/template/components/{layout → (layout)}/navigation/nav/NavNew.module.css +406 -373
  81. package/template/components/{layout → (layout)}/navigation/nav/NavRail.tsx +317 -315
  82. package/template/components/{layout → (layout)}/navigation/nav/NavSection.tsx +101 -102
  83. package/template/components/{layout → (layout)}/navigation/nav/NavTab.tsx +25 -25
  84. package/template/components/{layout → (layout)}/navigation/nav/navftb/NavFTB.module.css +253 -252
  85. package/template/components/{layout → (layout)}/navigation/nav/navftb/NavFTB.tsx +448 -399
  86. package/template/components/{layout → (layout)}/navigation/nav/navftb/useTabIndicator.ts +498 -484
  87. package/template/components/{layout → (layout)}/navigation/nav/navheader/NavHeader.module.css +59 -59
  88. package/template/components/{layout → (layout)}/navigation/nav/navheader/NavHeader.tsx +465 -473
  89. package/template/components/(layout)/navigation/pagination/Pagination.tsx +261 -0
  90. package/template/components/{layout → (layout)}/pages/loadingScreen.tsx +117 -102
  91. package/template/components/{layout → (layout)}/pages/maintenance/Maintenance.tsx +52 -52
  92. package/template/components/{layout → (layout)}/pages/maintenance/maintenance.module.css +162 -162
  93. package/template/components/(layout)/scrollShadow/ScrollShadow.module.css +81 -0
  94. package/template/components/(layout)/scrollShadow/ScrollShadow.tsx +161 -0
  95. package/template/components/(layout)/scrollShadow/useScrollShadow.ts +130 -0
  96. package/template/components/(layout)/separator/Separator.tsx +112 -0
  97. package/template/components/(overlays)/accessibilityModal/AccessibilityModal.tsx +307 -0
  98. package/template/components/(overlays)/drawer/Drawer.tsx +953 -0
  99. package/template/components/(overlays)/modal/Modal.tsx +654 -0
  100. package/template/components/(overlays)/popover/Popover.tsx +420 -0
  101. package/template/components/(overlays)/toast/Toast.tsx +692 -0
  102. package/template/components/(pickers)/autocomplete/Autocomplete.tsx +24 -0
  103. package/template/components/(pickers)/comboBox/ComboBox.tsx +21 -0
  104. package/template/components/(pickers)/select/Select.tsx +188 -0
  105. package/template/components/(pickers)/shared/FilterableField.tsx +223 -0
  106. package/template/components/(pickers)/shared/PickerItem.ts +13 -0
  107. package/template/components/tools/CustomScrollbar.tsx +107 -0
  108. package/template/components/tools/Divider.tsx +11 -11
  109. package/template/components/tools/ReduceMotionContext.tsx +5 -5
  110. package/template/components/tools/Sinewave.tsx +396 -199
  111. package/template/components/tools/SinewaveProgress.tsx +140 -135
  112. package/template/components/tools/badge/Badge.tsx +28 -28
  113. package/template/components/tools/dropdown/drop.module.css +196 -0
  114. package/template/components/tools/dropdown/drop.tsx +568 -0
  115. package/template/components/tools/easing.ts +118 -0
  116. package/template/components/tools/flubber.d.ts +6 -6
  117. package/template/components/tools/footer/Footer.tsx +20 -0
  118. package/template/components/tools/input/Input.module.css +45 -0
  119. package/template/components/tools/input/Input.tsx +99 -0
  120. package/template/components/tools/label/Icon.tsx +39 -0
  121. package/template/components/tools/label/Label.tsx +156 -0
  122. package/template/components/tools/mobileViewProvider.tsx +53 -58
  123. package/template/components/tools/ripple/ripple.css +38 -41
  124. package/template/components/tools/ripple/ripple.tsx +78 -53
  125. package/template/components/tools/shape/Shape.module.css +25 -0
  126. package/template/components/tools/shape/{ShapeMorph.tsx → Shape.tsx} +316 -190
  127. package/template/components/tools/shape/ShapeLoader.tsx +104 -101
  128. package/template/components/tools/shape/ShapePaths.ts +116 -116
  129. package/template/components/tools/switch/Switch.module.css +102 -102
  130. package/template/components/tools/switch/Switch.tsx +48 -48
  131. package/template/components/tools/useBodyScrollLock.tsx +27 -27
  132. package/template/components/tools/useImageColor.ts +47 -47
  133. package/template/components/tools/useNavSheetDrag.tsx +196 -196
  134. package/template/components/layout/accordion/Accordion.module.css +0 -149
  135. package/template/components/layout/accordion/Accordion.tsx +0 -114
  136. package/template/components/layout/button/button.tsx +0 -128
  137. package/template/components/layout/button/drop.module.css +0 -229
  138. package/template/components/layout/button/drop.tsx +0 -516
  139. package/template/components/layout/hero/v1/heroSlideshow.module.css +0 -107
  140. package/template/components/layout/hero/v1/heroSlideshow.tsx +0 -233
  141. package/template/components/layout/modals/accessibilityModal/AccessibilityModal.module.css +0 -126
  142. package/template/components/layout/modals/accessibilityModal/AccessibilityModal.tsx +0 -290
  143. package/template/components/layout/modals/alert/Alert.tsx +0 -188
  144. package/template/components/layout/navigation/MioCard.module.css +0 -88
  145. package/template/components/layout/navigation/MioCard.tsx +0 -266
  146. package/template/components/tools/ProgressiveBlur.tsx +0 -73
  147. package/template/components/tools/icon/icon.module.css +0 -0
  148. package/template/components/tools/icon/icon.tsx +0 -125
  149. package/template/components/tools/react-bits/ClickSpark.tsx +0 -177
  150. package/template/components/tools/react-bits/FadeContent.tsx +0 -90
  151. package/template/components/tools/react-bits/GlareHover.tsx +0 -73
  152. package/template/components/tools/react-bits/GradientText/GradientText.css +0 -70
  153. package/template/components/tools/react-bits/GradientText/GradientText.tsx +0 -44
  154. package/template/components/tools/react-bits/Magnet.tsx +0 -118
  155. package/template/components/tools/react-bits/Stepper/Stepper.css +0 -134
  156. package/template/components/tools/react-bits/Stepper/Stepper.tsx +0 -412
@@ -1,315 +1,317 @@
1
- "use client";
2
-
3
- import React, {
4
- useContext,
5
- useMemo,
6
- useState,
7
- useRef,
8
- useEffect,
9
- useCallback,
10
- } from "react";
11
- import { useRouter, usePathname } from "next/navigation";
12
- import styles from "./NavNew.module.css";
13
- import Icon from "@/components/tools/icon/icon";
14
- import Button from "../../button/button";
15
- import {
16
- NavRailProps,
17
- NavItemProps,
18
- NavTabInfo,
19
- NavRailContext,
20
- NavRailMenuContext,
21
- NavRailMenuContextValue,
22
- useNav,
23
- useCollapseOnOutsideInteraction,
24
- } from "./NavContext";
25
- import NavItem from "./NavItem";
26
- import FAB from "../../button/FAB";
27
-
28
- const NavRail: React.FC<NavRailProps> = function NavRail({
29
- accessibilityAriaLabel,
30
- accessibilityOnClick,
31
- themeOnClick,
32
- theme,
33
- children,
34
- isExpanded,
35
- onToggleExpanded,
36
- style,
37
- }) {
38
- useNav();
39
- const railCtx = useContext(NavRailContext);
40
- const isMobileCtx = railCtx?.isMobile ?? false;
41
- const onCollapseCtx = railCtx?.onCollapse;
42
-
43
- const router = useRouter();
44
- const pathname = usePathname();
45
-
46
- // TypeScript-safe child narrowing — compares against NavItem directly,
47
- // same reference as Nav.Item after Object.assign in Nav.tsx.
48
- type NavItemElement = React.ReactElement<NavItemProps, typeof NavItem>;
49
-
50
- const navItems = useMemo(
51
- () =>
52
- React.Children.toArray(
53
- React.isValidElement(children) && children.type === React.Fragment
54
- ? (children.props as { children: React.ReactNode }).children
55
- : children,
56
- ).filter(
57
- (child): child is NavItemElement =>
58
- React.isValidElement(child) && child.type === NavItem,
59
- ),
60
- [children],
61
- );
62
-
63
- // Derive defaultPath inside useMemo so the hook count is always consistent.
64
- const defaultPath = useMemo(() => {
65
- const defaultItems = navItems.filter((item) => item.props.default === true);
66
- if (process.env.NODE_ENV !== "production") {
67
- if (defaultItems.length === 0)
68
- console.error(
69
- "Nav.Rail: exactly one Nav.Item must have default={true}.",
70
- );
71
- else if (defaultItems.length > 1)
72
- console.error("Nav.Rail: only one Nav.Item may have default={true}.");
73
- const item = defaultItems[0];
74
- if (item && !item.props.path)
75
- console.error(
76
- "Nav.Rail: the default Nav.Item must have a 'path' prop for root redirect.",
77
- );
78
- }
79
- return defaultItems[0]?.props.path ?? "";
80
- }, [navItems]);
81
-
82
- // Only redirect when landing at root AND the default path is not itself "/",
83
- // preventing an infinite redirect loop when home IS the root route.
84
- useEffect(() => {
85
- if (pathname === "/" && defaultPath && defaultPath !== "/")
86
- router.replace(defaultPath);
87
- }, [pathname, defaultPath, router]);
88
-
89
- const wrapperRef = useRef<HTMLDivElement>(null);
90
- // enabled=!isMobileCtx: only register when Rail is actually visible.
91
- useCollapseOnOutsideInteraction(
92
- isExpanded,
93
- onCollapseCtx ?? (() => {}),
94
- wrapperRef,
95
- !isMobileCtx,
96
- );
97
-
98
- // ── Sub-tab menu state ─────────────────────────────────────────────────
99
- const [menuState, setMenuState] = useState<{
100
- activeItemPath: string | null;
101
- activeMenuItemId: string | null;
102
- tabs: NavTabInfo[];
103
- expanded: boolean;
104
- }>({
105
- activeItemPath: null,
106
- activeMenuItemId: null,
107
- tabs: [],
108
- expanded: false,
109
- });
110
-
111
- // Stable refs so the callback never captures stale values.
112
- const menuStateRef = useRef(menuState);
113
- useEffect(() => {
114
- menuStateRef.current = menuState;
115
- }, [menuState]);
116
-
117
- const isExpandedRef = useRef(isExpanded);
118
- useEffect(() => {
119
- isExpandedRef.current = isExpanded;
120
- }, [isExpanded]);
121
-
122
- const pendingMenuOpenRef = useRef<{
123
- itemId: string;
124
- itemPath: string | null;
125
- tabs: NavTabInfo[];
126
- } | null>(null);
127
-
128
- useEffect(() => {
129
- if (!isExpanded) return;
130
- const pending = pendingMenuOpenRef.current;
131
- if (!pending) return;
132
-
133
- const rafId = window.requestAnimationFrame(() => {
134
- setMenuState({
135
- activeItemPath: pending.itemPath,
136
- activeMenuItemId: pending.itemId,
137
- tabs: pending.tabs,
138
- expanded: true,
139
- });
140
- pendingMenuOpenRef.current = null;
141
- });
142
-
143
- return () => window.cancelAnimationFrame(rafId);
144
- }, [isExpanded]);
145
-
146
- // When the rail collapses externally, also close the sub-tab menu.
147
- useEffect(() => {
148
- if (!isExpanded) {
149
- pendingMenuOpenRef.current = null;
150
- setMenuState((prev) =>
151
- prev.expanded
152
- ? { ...prev, expanded: false, activeMenuItemId: null }
153
- : prev,
154
- );
155
- }
156
- }, [isExpanded]);
157
-
158
- const onItemActivated = useCallback(
159
- (itemId: string, itemPath: string | null, tabs: NavTabInfo[]) => {
160
- if (tabs.length > 0) {
161
- if (!menuStateRef.current.expanded) {
162
- // Menu was closed → open it; also expand rail if collapsed.
163
- if (!isExpandedRef.current) {
164
- pendingMenuOpenRef.current = { itemId, itemPath, tabs };
165
- onToggleExpanded();
166
- return;
167
- }
168
- setMenuState({
169
- activeItemPath: itemPath,
170
- activeMenuItemId: itemId,
171
- tabs,
172
- expanded: true,
173
- });
174
- } else {
175
- // Menu already open → swap tab list only (no CSS transition fires).
176
- setMenuState({
177
- activeItemPath: itemPath,
178
- activeMenuItemId: itemId,
179
- tabs,
180
- expanded: true,
181
- });
182
- }
183
- } else {
184
- // Item carries no tabs → collapse the menu.
185
- pendingMenuOpenRef.current = null;
186
- setMenuState({
187
- activeItemPath: null,
188
- activeMenuItemId: null,
189
- tabs: [],
190
- expanded: false,
191
- });
192
- }
193
- },
194
- [onToggleExpanded],
195
- );
196
-
197
- const railMenuContextValue = useMemo<NavRailMenuContextValue>(
198
- () => ({
199
- onItemActivated,
200
- activeMenuItemId: menuState.activeMenuItemId,
201
- }),
202
- [onItemActivated, menuState.activeMenuItemId],
203
- );
204
-
205
- return (
206
- <NavRailMenuContext.Provider value={railMenuContextValue}>
207
- <div ref={wrapperRef} style={{ ...style }}>
208
- <div
209
- className={`${styles.navRailMenu} ${menuState.expanded ? styles.navRailExpanded : ""}`}
210
- >
211
- <ul>
212
- {menuState.tabs.map((tab) => (
213
- <li
214
- key={tab.path}
215
- className={
216
- pathname === tab.path ? styles.navTabActive : undefined
217
- }
218
- onClick={() => router.push(tab.path)}
219
- onKeyDown={(e) => {
220
- if (e.key === "Enter" || e.key === " ") {
221
- e.preventDefault();
222
- router.push(tab.path);
223
- }
224
- }}
225
- role="button"
226
- tabIndex={0}
227
- >
228
- {tab.label}
229
- </li>
230
- ))}
231
- </ul>
232
- </div>
233
- <aside
234
- className={`${styles.navRail} ${isExpanded ? styles.navRailExpanded : ""}`}
235
- >
236
- <div
237
- style={{
238
- display: "flex",
239
- flexDirection: "column",
240
- gap: 12,
241
- padding: "0 12px",
242
- }}
243
- >
244
- <div
245
- className={`${styles.navRailMenuButtonWrapper} ${isExpanded ? styles.navRailExpanded : ""}`}
246
- >
247
- <Button
248
- onClick={onToggleExpanded}
249
- type="button"
250
- style={{ width: 44, marginRight: isExpanded ? 0 : 4 }}
251
- text
252
- aria-label={
253
- isExpanded ? "Collapse navigation" : "Expand navigation"
254
- }
255
- >
256
- <Icon systemName={isExpanded ? "menu_open" : "menu"} />
257
- </Button>
258
- </div>
259
- <FAB
260
- systemName="search"
261
- label={isExpanded ? "Search" : undefined}
262
- sm
263
- primary-bold
264
- />
265
- </div>
266
- <div
267
- className={`${styles.navRailScrollContainer} ${isExpanded ? styles.navRailExpanded : ""}`}
268
- >
269
- <nav
270
- className={`${styles.navItemsContainer} ${isExpanded ? styles.navRailExpanded : ""}`}
271
- >
272
- {children}
273
- </nav>
274
-
275
- <div className={styles.navRailBottomButtons}>
276
- {accessibilityAriaLabel && accessibilityOnClick && (
277
- <Button
278
- outlined
279
- aria-label={accessibilityAriaLabel}
280
- onClick={accessibilityOnClick}
281
- style={{ padding: 6, minWidth: 44, height: 44 }}
282
- >
283
- <Icon
284
- systemName="settings_accessibility"
285
- label={isExpanded ? "Accessibility" : undefined}
286
- />
287
- </Button>
288
- )}
289
-
290
- <Button
291
- outlined
292
- aria-label="Light / Dark mode toggle"
293
- onClick={themeOnClick}
294
- style={{ padding: 6, minWidth: 44, height: 44 }}
295
- >
296
- <Icon
297
- systemName={theme === "dark" ? "light_mode" : "dark_mode"}
298
- label={
299
- isExpanded
300
- ? theme === "dark"
301
- ? "Light mode"
302
- : "Dark mode"
303
- : undefined
304
- }
305
- />
306
- </Button>
307
- </div>
308
- </div>
309
- </aside>
310
- </div>
311
- </NavRailMenuContext.Provider>
312
- );
313
- };
314
-
315
- export default NavRail;
1
+ "use client";
2
+
3
+ import React, {
4
+ useContext,
5
+ useMemo,
6
+ useState,
7
+ useRef,
8
+ useEffect,
9
+ useCallback,
10
+ } from "react";
11
+ import { useRouter, usePathname } from "next/navigation";
12
+ import styles from "./NavNew.module.css";
13
+ import Icon from "@/components/tools/label/Icon";
14
+ import Label from "@/components/tools/label/Label";
15
+ import Button from "../../../(buttons)/button";
16
+ import {
17
+ NavRailProps,
18
+ NavItemProps,
19
+ NavTabInfo,
20
+ NavRailContext,
21
+ NavRailMenuContext,
22
+ NavRailMenuContextValue,
23
+ useNav,
24
+ useCollapseOnOutsideInteraction,
25
+ } from "./NavContext";
26
+ import NavItem from "./NavItem";
27
+ import FAB from "../../../(buttons)/FAB";
28
+
29
+ const NavRail: React.FC<NavRailProps> = function NavRail({
30
+ accessibilityAriaLabel,
31
+ accessibilityOnClick,
32
+ themeOnClick,
33
+ theme,
34
+ children,
35
+ isExpanded,
36
+ onToggleExpanded,
37
+ style,
38
+ }) {
39
+ useNav();
40
+ const railCtx = useContext(NavRailContext);
41
+ const isMobileCtx = railCtx?.isMobile ?? false;
42
+ const onCollapseCtx = railCtx?.onCollapse;
43
+
44
+ const router = useRouter();
45
+ const pathname = usePathname();
46
+
47
+ // TypeScript-safe child narrowing — compares against NavItem directly,
48
+ // same reference as Nav.Item after Object.assign in Nav.tsx.
49
+ type NavItemElement = React.ReactElement<NavItemProps, typeof NavItem>;
50
+
51
+ const navItems = useMemo(
52
+ () =>
53
+ React.Children.toArray(
54
+ React.isValidElement(children) && children.type === React.Fragment
55
+ ? (children.props as { children: React.ReactNode }).children
56
+ : children,
57
+ ).filter(
58
+ (child): child is NavItemElement =>
59
+ React.isValidElement(child) && child.type === NavItem,
60
+ ),
61
+ [children],
62
+ );
63
+
64
+ // Derive defaultPath inside useMemo so the hook count is always consistent.
65
+ const defaultPath = useMemo(() => {
66
+ const defaultItems = navItems.filter((item) => item.props.default === true);
67
+ if (process.env.NODE_ENV !== "production") {
68
+ if (defaultItems.length === 0)
69
+ console.error(
70
+ "Nav.Rail: exactly one Nav.Item must have default={true}.",
71
+ );
72
+ else if (defaultItems.length > 1)
73
+ console.error("Nav.Rail: only one Nav.Item may have default={true}.");
74
+ const item = defaultItems[0];
75
+ if (item && !item.props.path)
76
+ console.error(
77
+ "Nav.Rail: the default Nav.Item must have a 'path' prop for root redirect.",
78
+ );
79
+ }
80
+ return defaultItems[0]?.props.path ?? "";
81
+ }, [navItems]);
82
+
83
+ // Only redirect when landing at root AND the default path is not itself "/",
84
+ // preventing an infinite redirect loop when home IS the root route.
85
+ useEffect(() => {
86
+ if (pathname === "/" && defaultPath && defaultPath !== "/")
87
+ router.replace(defaultPath);
88
+ }, [pathname, defaultPath, router]);
89
+
90
+ const wrapperRef = useRef<HTMLDivElement>(null);
91
+ // enabled=!isMobileCtx: only register when Rail is actually visible.
92
+ useCollapseOnOutsideInteraction(
93
+ isExpanded,
94
+ onCollapseCtx ?? (() => {}),
95
+ wrapperRef,
96
+ !isMobileCtx,
97
+ );
98
+
99
+ // ── Sub-tab menu state ─────────────────────────────────────────────────
100
+ const [menuState, setMenuState] = useState<{
101
+ activeItemPath: string | null;
102
+ activeMenuItemId: string | null;
103
+ tabs: NavTabInfo[];
104
+ expanded: boolean;
105
+ }>({
106
+ activeItemPath: null,
107
+ activeMenuItemId: null,
108
+ tabs: [],
109
+ expanded: false,
110
+ });
111
+
112
+ // Stable refs so the callback never captures stale values.
113
+ const menuStateRef = useRef(menuState);
114
+ useEffect(() => {
115
+ menuStateRef.current = menuState;
116
+ }, [menuState]);
117
+
118
+ const isExpandedRef = useRef(isExpanded);
119
+ useEffect(() => {
120
+ isExpandedRef.current = isExpanded;
121
+ }, [isExpanded]);
122
+
123
+ const pendingMenuOpenRef = useRef<{
124
+ itemId: string;
125
+ itemPath: string | null;
126
+ tabs: NavTabInfo[];
127
+ } | null>(null);
128
+
129
+ useEffect(() => {
130
+ if (!isExpanded) return;
131
+ const pending = pendingMenuOpenRef.current;
132
+ if (!pending) return;
133
+
134
+ const rafId = window.requestAnimationFrame(() => {
135
+ setMenuState({
136
+ activeItemPath: pending.itemPath,
137
+ activeMenuItemId: pending.itemId,
138
+ tabs: pending.tabs,
139
+ expanded: true,
140
+ });
141
+ pendingMenuOpenRef.current = null;
142
+ });
143
+
144
+ return () => window.cancelAnimationFrame(rafId);
145
+ }, [isExpanded]);
146
+
147
+ // When the rail collapses externally, also close the sub-tab menu.
148
+ /* eslint-disable react-hooks/set-state-in-effect */
149
+ useEffect(() => {
150
+ if (!isExpanded) {
151
+ pendingMenuOpenRef.current = null;
152
+ setMenuState((prev) =>
153
+ prev.expanded
154
+ ? { ...prev, expanded: false, activeMenuItemId: null }
155
+ : prev,
156
+ );
157
+ }
158
+ }, [isExpanded]);
159
+ /* eslint-enable react-hooks/set-state-in-effect */
160
+
161
+ const onItemActivated = useCallback(
162
+ (itemId: string, itemPath: string | null, tabs: NavTabInfo[]) => {
163
+ if (tabs.length > 0) {
164
+ if (!menuStateRef.current.expanded) {
165
+ // Menu was closed → open it; also expand rail if collapsed.
166
+ if (!isExpandedRef.current) {
167
+ pendingMenuOpenRef.current = { itemId, itemPath, tabs };
168
+ onToggleExpanded();
169
+ return;
170
+ }
171
+ setMenuState({
172
+ activeItemPath: itemPath,
173
+ activeMenuItemId: itemId,
174
+ tabs,
175
+ expanded: true,
176
+ });
177
+ } else {
178
+ // Menu already open → swap tab list only (no CSS transition fires).
179
+ setMenuState({
180
+ activeItemPath: itemPath,
181
+ activeMenuItemId: itemId,
182
+ tabs,
183
+ expanded: true,
184
+ });
185
+ }
186
+ } else {
187
+ // Item carries no tabs → collapse the menu.
188
+ pendingMenuOpenRef.current = null;
189
+ setMenuState({
190
+ activeItemPath: null,
191
+ activeMenuItemId: null,
192
+ tabs: [],
193
+ expanded: false,
194
+ });
195
+ }
196
+ },
197
+ [onToggleExpanded],
198
+ );
199
+
200
+ const railMenuContextValue = useMemo<NavRailMenuContextValue>(
201
+ () => ({
202
+ onItemActivated,
203
+ activeMenuItemId: menuState.activeMenuItemId,
204
+ }),
205
+ [onItemActivated, menuState.activeMenuItemId],
206
+ );
207
+
208
+ return (
209
+ <NavRailMenuContext.Provider value={railMenuContextValue}>
210
+ <div ref={wrapperRef} style={{ ...style }}>
211
+ <div
212
+ className={`${styles.navRailMenu} ${menuState.expanded ? styles.navRailExpanded : ""}`}
213
+ >
214
+ <ul>
215
+ {menuState.tabs.map((tab) => (
216
+ <li
217
+ key={tab.path}
218
+ className={
219
+ pathname === tab.path ? styles.navTabActive : undefined
220
+ }
221
+ onClick={() => router.push(tab.path)}
222
+ onKeyDown={(e) => {
223
+ if (e.key === "Enter" || e.key === " ") {
224
+ e.preventDefault();
225
+ router.push(tab.path);
226
+ }
227
+ }}
228
+ role="button"
229
+ tabIndex={0}
230
+ >
231
+ {tab.label}
232
+ </li>
233
+ ))}
234
+ </ul>
235
+ </div>
236
+ <aside
237
+ className={`${styles.navRail} ${isExpanded ? styles.navRailExpanded : ""}`}
238
+ >
239
+ <div
240
+ style={{
241
+ display: "flex",
242
+ flexDirection: "column",
243
+ gap: 12,
244
+ padding: "0 12px",
245
+ }}
246
+ >
247
+ <div
248
+ className={`${styles.navRailMenuButtonWrapper} ${isExpanded ? styles.navRailExpanded : ""}`}
249
+ >
250
+ <Button
251
+ onClick={onToggleExpanded}
252
+ type="button"
253
+ style={{ width: 44, marginRight: isExpanded ? 0 : 4 }}
254
+ text
255
+ aria-label={
256
+ isExpanded ? "Collapse navigation" : "Expand navigation"
257
+ }
258
+ >
259
+ <Icon systemName={isExpanded ? "menu_open" : "menu"} />
260
+ </Button>
261
+ </div>
262
+ <FAB
263
+ systemName="search"
264
+ label={isExpanded ? "Search" : undefined}
265
+ sm
266
+ primary-bold
267
+ style={{
268
+ aspectRatio: isExpanded ? "unset" : "initial",
269
+ }}
270
+ />
271
+ </div>
272
+ <div
273
+ className={`${styles.navRailScrollContainer} ${isExpanded ? styles.navRailExpanded : ""}`}
274
+ >
275
+ <nav
276
+ className={`${styles.navItemsContainer} ${isExpanded ? styles.navRailExpanded : ""}`}
277
+ >
278
+ {children}
279
+ </nav>
280
+
281
+ <div className={styles.navRailBottomButtons}>
282
+ {accessibilityAriaLabel && accessibilityOnClick && (
283
+ <Button
284
+ outlined
285
+ aria-label={accessibilityAriaLabel}
286
+ onClick={accessibilityOnClick}
287
+ style={{ padding: 6, minWidth: 44, height: 44 }}
288
+ >
289
+ <Icon systemName="settings_accessibility" />
290
+ {isExpanded && <Label label="Accessibility" />}
291
+ </Button>
292
+ )}
293
+
294
+ <Button
295
+ outlined
296
+ aria-label="Light / Dark mode toggle"
297
+ onClick={themeOnClick}
298
+ style={{ padding: 6, minWidth: 44, height: 44 }}
299
+ >
300
+ <Icon
301
+ systemName={theme === "dark" ? "light_mode" : "dark_mode"}
302
+ />
303
+ {isExpanded && (
304
+ <Label
305
+ label={theme === "dark" ? "Light mode" : "Dark mode"}
306
+ />
307
+ )}
308
+ </Button>
309
+ </div>
310
+ </div>
311
+ </aside>
312
+ </div>
313
+ </NavRailMenuContext.Provider>
314
+ );
315
+ };
316
+
317
+ export default NavRail;