@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,503 +1,414 @@
1
- "use client";
2
-
3
- import React, {
4
- createContext,
5
- useContext,
6
- useEffect,
7
- useRef,
8
- useState,
9
- } from "react";
10
- import Icon from "@/components/tools/icon/icon";
11
- import Button from "./button";
12
-
13
- // ---------------- config knobs ----------------
14
-
15
- const GAP = 4;
16
- const TRANSITION =
17
- "all var(--transition-expressive-color), border-radius var(--transition-expressive), padding-inline var(--transition-expressive)";
18
-
19
- const SIZE_TO_PADDING = {
20
- xs: 4,
21
- sm: 6,
22
- m: 8,
23
- l: 12,
24
- xl: 18,
25
- } as const;
26
-
27
- // ---------------- types ----------------
28
-
29
- type Variant =
30
- | "primary"
31
- | "secondary"
32
- | "tertiary"
33
- | "outlined"
34
- | "text"
35
- | "link";
36
-
37
- type VariantBools = {
38
- primary?: boolean;
39
- secondary?: boolean;
40
- tertiary?: boolean;
41
- outlined?: boolean;
42
- text?: boolean;
43
- link?: boolean;
44
- };
45
-
46
- type SizeBools = {
47
- xs?: boolean;
48
- sm?: boolean;
49
- m?: boolean;
50
- l?: boolean;
51
- xl?: boolean;
52
- };
53
-
54
- type LabelConfig = string | { text: string; hideWhenInactive?: boolean };
55
-
56
- type GroupContextValue = {
57
- variant: Variant;
58
- btnPadding: number;
59
- isExclusive: boolean;
60
- springLayout: boolean;
61
- isActive: (index: number) => boolean;
62
- onTabClick: (index: number, toggle: boolean | undefined) => void;
63
- };
64
-
65
- const GroupContext = createContext<GroupContextValue | null>(null);
66
-
67
- // ---------------- usePressed hook ----------------
68
-
69
- function usePressed(minDurationMs = 350) {
70
- const [pressed, setPressed] = useState(false);
71
- const pressedAtRef = useRef<number | null>(null);
72
- const releaseTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
73
-
74
- useEffect(
75
- () => () => {
76
- if (releaseTimerRef.current) clearTimeout(releaseTimerRef.current);
77
- },
78
- [],
79
- );
80
-
81
- const press = () => {
82
- if (releaseTimerRef.current) {
83
- clearTimeout(releaseTimerRef.current);
84
- releaseTimerRef.current = null;
85
- }
86
- pressedAtRef.current = Date.now();
87
- setPressed(true);
88
- };
89
-
90
- const release = () => {
91
- const heldFor = pressedAtRef.current
92
- ? Date.now() - pressedAtRef.current
93
- : minDurationMs;
94
- const remaining = Math.max(0, minDurationMs - heldFor);
95
- if (releaseTimerRef.current) clearTimeout(releaseTimerRef.current);
96
- releaseTimerRef.current = setTimeout(() => setPressed(false), remaining);
97
- };
98
-
99
- return { pressed, press, release };
100
- }
101
-
102
- function resolveVariant(p: VariantBools): Variant {
103
- if (p.primary) return "primary";
104
- if (p.secondary) return "secondary";
105
- if (p.tertiary) return "tertiary";
106
- if (p.outlined) return "outlined";
107
- if (p.text) return "text";
108
- if (p.link) return "link";
109
- return "primary";
110
- }
111
-
112
- function resolveSize(p: SizeBools): number {
113
- if (p.xs) return SIZE_TO_PADDING.xs;
114
- if (p.sm) return SIZE_TO_PADDING.sm;
115
- if (p.l) return SIZE_TO_PADDING.l;
116
- if (p.xl) return SIZE_TO_PADDING.xl;
117
- return SIZE_TO_PADDING.m;
118
- }
119
-
120
- // shared style derivation for Tab and CTA
121
- function deriveStyles(opts: {
122
- variant: Variant;
123
- toggle: boolean | undefined;
124
- isActive: boolean;
125
- pressed: boolean;
126
- hasLabel: boolean;
127
- hideWhenInactive?: boolean;
128
- square?: boolean;
129
- circle?: boolean;
130
- btnPadding: number;
131
- }) {
132
- const {
133
- variant,
134
- toggle,
135
- isActive,
136
- pressed,
137
- hasLabel,
138
- hideWhenInactive,
139
- square,
140
- circle,
141
- btnPadding,
142
- } = opts;
143
-
144
- // Padding shape selection:
145
- // - `square` or `circle` force square padding
146
- // - Buttons with an always-visible label → square
147
- // - Icon-only OR labels that hide when inactive → pill
148
- const useSquare = square || circle || (hasLabel && !hideWhenInactive);
149
-
150
- // Base settled padding (no press)
151
- // - Square: always btnPadding
152
- // - Pill: btnPadding when toggle && active, else btnPadding - btnPadding/1.6
153
- const basePadding = useSquare
154
- ? btnPadding
155
- : toggle && isActive
156
- ? btnPadding
157
- : btnPadding - btnPadding / 1.6;
158
-
159
- // Press bounce: always-positive expansion while pressed
160
- const bounceMag = Math.min(Math.max(btnPadding / 4, 8), 24);
161
- const paddingInline = basePadding + (pressed ? bounceMag : 0);
162
-
163
- // Border radius:
164
- // - `square` → 14 always
165
- // - `circle` → 36 always
166
- // - Else: 36 when toggle && active, 14 otherwise
167
- const borderRadius = square ? 14 : circle ? 36 : toggle && isActive ? 36 : 14;
168
-
169
- // Variant key:
170
- // - primary / secondary / tertiary → -bold when active
171
- // - outlined → secondary-bold when active
172
- // - text / link → no change when active
173
- let variantKey: string = variant;
174
- if (toggle && isActive) {
175
- if (
176
- variant === "primary" ||
177
- variant === "secondary" ||
178
- variant === "tertiary"
179
- ) {
180
- variantKey = `${variant}-bold`;
181
- } else if (variant === "outlined") {
182
- variantKey = "secondary-bold";
183
- }
184
- }
185
-
186
- return { paddingInline, borderRadius, variantKey };
187
- }
188
-
189
- // ---------------- Tab ----------------
190
-
191
- type TabProps = {
192
- default?: boolean;
193
- systemName?: string;
194
- label?: LabelConfig;
195
- toggle?: boolean;
196
- /** Force border-radius 14 and square padding regardless of active state */
197
- square?: boolean;
198
- /** Force border-radius 36 and square padding regardless of active state */
199
- circle?: boolean;
200
- onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void;
201
- } & Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onClick">;
202
-
203
- type TabInternalProps = TabProps & { _index?: number };
204
-
205
- function Tab({
206
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
207
- default: _isDefault,
208
- systemName,
209
- label,
210
- toggle,
211
- square = false,
212
- circle = false,
213
- onClick,
214
- _index,
215
- style,
216
- ...rest
217
- }: TabInternalProps) {
218
- const ctx = useContext(GroupContext);
219
- if (!ctx || _index === undefined) {
220
- throw new Error("Button.group.Tab must be a direct child of Button.group");
221
- }
222
-
223
- const { pressed, press, release } = usePressed();
224
- const isActive = ctx.isActive(_index);
225
-
226
- const labelText = typeof label === "string" ? label : label?.text;
227
- const hideWhenInactive =
228
- typeof label === "object" ? !!label.hideWhenInactive : false;
229
- const labelVisible =
230
- labelText !== undefined && (!hideWhenInactive || isActive);
231
- const iconLabel = labelVisible ? labelText : undefined;
232
- const hasLabel = label !== undefined;
233
-
234
- const { paddingInline, borderRadius, variantKey } = deriveStyles({
235
- variant: ctx.variant,
236
- toggle,
237
- isActive,
238
- pressed,
239
- hasLabel,
240
- hideWhenInactive,
241
- square,
242
- circle,
243
- btnPadding: ctx.btnPadding,
244
- });
245
-
246
- const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
247
- const isAlreadyActiveExclusive =
248
- ctx.isExclusive && toggle && ctx.isActive(_index);
249
- ctx.onTabClick(_index, toggle);
250
- if (!isAlreadyActiveExclusive) onClick?.(e);
251
- };
252
-
253
- return (
254
- <Button
255
- {...{ [variantKey]: true }}
256
- onClick={handleClick}
257
- onPointerDown={press}
258
- onPointerUp={release}
259
- onPointerLeave={release}
260
- onPointerCancel={release}
261
- style={{
262
- padding: ctx.btnPadding,
263
- paddingInline,
264
- margin: 0,
265
- borderRadius,
266
- transition: TRANSITION,
267
- ...(ctx.springLayout
268
- ? { flex: "1 1 auto", minWidth: 0, width: "max-content" }
269
- : {}),
270
- ...style,
271
- }}
272
- {...rest}
273
- >
274
- <Icon systemName={systemName} label={iconLabel} />
275
- </Button>
276
- );
277
- }
278
-
279
- // ---------------- CTA ----------------
280
-
281
- type CTAProps = {
282
- label?: string;
283
- systemName?: string;
284
- toggle?: boolean;
285
- square?: boolean;
286
- circle?: boolean;
287
- onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void;
288
- } & VariantBools &
289
- Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onClick">;
290
-
291
- type CTAInternalProps = CTAProps & {
292
- btnPadding: number;
293
- springLayout: boolean;
294
- };
295
-
296
- function CTAButton({
297
- label,
298
- systemName,
299
- toggle,
300
- square = false,
301
- circle = false,
302
- onClick,
303
- style,
304
- primary,
305
- secondary,
306
- tertiary,
307
- outlined,
308
- text,
309
- link,
310
- btnPadding,
311
- springLayout,
312
- ...rest
313
- }: CTAInternalProps) {
314
- const variant = resolveVariant({
315
- primary,
316
- secondary,
317
- tertiary,
318
- outlined,
319
- text,
320
- link,
321
- });
322
-
323
- const [isActive, setIsActive] = useState(false);
324
- const { pressed, press, release } = usePressed();
325
-
326
- const hasLabel = label !== undefined;
327
-
328
- const { paddingInline, borderRadius, variantKey } = deriveStyles({
329
- variant,
330
- toggle,
331
- isActive,
332
- pressed,
333
- hasLabel,
334
- square,
335
- circle,
336
- btnPadding,
337
- });
338
-
339
- const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
340
- if (toggle) setIsActive((a) => !a);
341
- onClick?.(e);
342
- };
343
-
344
- return (
345
- <Button
346
- {...{ [variantKey]: true }}
347
- onClick={handleClick}
348
- onPointerDown={press}
349
- onPointerUp={release}
350
- onPointerLeave={release}
351
- onPointerCancel={release}
352
- style={{
353
- padding: btnPadding,
354
- paddingInline,
355
- borderRadius,
356
- margin: 0,
357
- transition: TRANSITION,
358
- ...(springLayout ? { flex: "0 0 auto" } : {}),
359
- ...style,
360
- }}
361
- {...rest}
362
- >
363
- <Icon systemName={systemName} label={label} />
364
- </Button>
365
- );
366
- }
367
-
368
- // ---------------- Group ----------------
369
-
370
- type ButtonGroupProps = {
371
- children: React.ReactNode;
372
- /** When true (default), only one tab can be active at a time. */
373
- exclusive?: boolean;
374
- /** Inverse of `exclusive`. If both are set, this wins. */
375
- multiSelect?: boolean;
376
- cta?: CTAProps;
377
- /**
378
- * Fixed width for the group. When set, tabs share the width with `flex: 1 1 auto`
379
- * — pressing one tab visibly squeezes its siblings (spring effect).
380
- * When unset, the group sits at `max-content` (no spring, natural sizes).
381
- */
382
- width?: number | string | "fullWidth";
383
- } & VariantBools &
384
- SizeBools;
385
-
386
- function ButtonGroup({
387
- children,
388
- exclusive = true,
389
- multiSelect,
390
- cta,
391
- width,
392
- // size bools
393
- xs,
394
- sm,
395
- m,
396
- l,
397
- xl,
398
- // variant bools
399
- ...variantBools
400
- }: ButtonGroupProps) {
401
- const isExclusive = multiSelect !== undefined ? !multiSelect : exclusive;
402
- const variant = resolveVariant(variantBools);
403
- const btnPadding = resolveSize({ xs, sm, m, l, xl });
404
- const springLayout = width !== undefined;
405
-
406
- const tabs = React.Children.toArray(children).filter(React.isValidElement);
407
-
408
- const [activeIndices, setActiveIndices] = useState<Set<number>>(() => {
409
- const defaults = new Set<number>();
410
- tabs.forEach((child, idx) => {
411
- const props = (child as React.ReactElement<TabProps>).props;
412
- if (props.default) {
413
- if (isExclusive && defaults.size > 0) return;
414
- defaults.add(idx);
415
- }
416
- });
417
- return defaults;
418
- });
419
-
420
- const handleTabClick = (index: number, toggle: boolean | undefined) => {
421
- if (!toggle) return;
422
-
423
- const next = new Set(activeIndices);
424
- const wasActive = next.has(index);
425
-
426
- // In exclusive mode, can't deactivate the currently-active tab by clicking it
427
- if (wasActive && isExclusive) return;
428
-
429
- if (wasActive) {
430
- next.delete(index);
431
- } else {
432
- if (isExclusive) next.clear();
433
- next.add(index);
434
- }
435
- setActiveIndices(next);
436
- };
437
-
438
- const ctxValue: GroupContextValue = {
439
- variant,
440
- btnPadding,
441
- isExclusive,
442
- springLayout,
443
- isActive: (i) => activeIndices.has(i),
444
- onTabClick: handleTabClick,
445
- };
446
-
447
- const bounceMag = Math.min(Math.max(btnPadding / 4, 8), 24);
448
-
449
- const computedMaxWidth =
450
- width !== undefined ? `${Number(width) + bounceMag * 0}px` : "100%";
451
-
452
- return (
453
- <GroupContext.Provider value={ctxValue}>
454
- <div
455
- style={{
456
- display: "flex",
457
- flexDirection: "row",
458
- alignItems: "center",
459
- justifyContent: "center",
460
- overflow: "hidden",
461
- overflowX: "auto",
462
- width: width
463
- ? computedMaxWidth
464
- : width == "fullWidth"
465
- ? "100%"
466
- : "max-content",
467
- maxWidth: computedMaxWidth,
468
- gap: `${GAP * 2}px`,
469
- }}
470
- >
471
- <div
472
- style={{
473
- display: "flex",
474
- flexDirection: "row",
475
- overflow: "hidden",
476
- overflowX: "auto",
477
- flex: springLayout || width == "fullWidth" ? "1 1 auto" : undefined,
478
- minWidth: 0,
479
- gap: `${GAP}px`,
480
- }}
481
- >
482
- {tabs.map((tab, index) =>
483
- React.cloneElement(tab as React.ReactElement<TabInternalProps>, {
484
- _index: index,
485
- key: index,
486
- }),
487
- )}
488
- </div>
489
- {cta && (
490
- <CTAButton
491
- {...cta}
492
- btnPadding={btnPadding}
493
- springLayout={springLayout}
494
- />
495
- )}
496
- </div>
497
- </GroupContext.Provider>
498
- );
499
- }
500
-
501
- ButtonGroup.Tab = Tab;
502
-
503
- export default ButtonGroup;
1
+ "use client";
2
+
3
+ import React, {
4
+ createContext,
5
+ useContext,
6
+ useEffect,
7
+ useRef,
8
+ useState,
9
+ } from "react";
10
+ import Label from "@/components/tools/label/Label";
11
+ import BaseButton from "./button";
12
+
13
+ // ---------------- config knobs ----------------
14
+
15
+ const GAP = 4;
16
+ const TRANSITION =
17
+ "all var(--transition-expressive-color), border-radius var(--transition-expressive), padding-inline var(--transition-expressive)";
18
+
19
+ const SIZE_TO_PADDING = {
20
+ xs: 4,
21
+ sm: 6,
22
+ m: 8,
23
+ l: 12,
24
+ xl: 18,
25
+ } as const;
26
+
27
+ // ---------------- types ----------------
28
+
29
+ type Variant =
30
+ | "primary"
31
+ | "secondary"
32
+ | "tertiary"
33
+ | "outlined"
34
+ | "text"
35
+ | "link";
36
+
37
+ type VariantBools = {
38
+ primary?: boolean;
39
+ secondary?: boolean;
40
+ tertiary?: boolean;
41
+ outlined?: boolean;
42
+ text?: boolean;
43
+ link?: boolean;
44
+ };
45
+
46
+ type SizeBools = {
47
+ xs?: boolean;
48
+ sm?: boolean;
49
+ m?: boolean;
50
+ l?: boolean;
51
+ xl?: boolean;
52
+ };
53
+
54
+ type LabelConfig = string | { text: string; hideWhenInactive?: boolean };
55
+
56
+ type GroupContextValue = {
57
+ variant: Variant;
58
+ btnPadding: number;
59
+ isExclusive: boolean;
60
+ springLayout: boolean;
61
+ isActive: (index: number) => boolean;
62
+ onTabClick: (index: number, toggle: boolean | undefined) => void;
63
+ };
64
+
65
+ const GroupContext = createContext<GroupContextValue | null>(null);
66
+
67
+ // ---------------- usePressed hook ----------------
68
+
69
+ function usePressed(minDurationMs = 350) {
70
+ const [pressed, setPressed] = useState(false);
71
+ const pressedAtRef = useRef<number | null>(null);
72
+ const releaseTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
73
+
74
+ useEffect(
75
+ () => () => {
76
+ if (releaseTimerRef.current) clearTimeout(releaseTimerRef.current);
77
+ },
78
+ [],
79
+ );
80
+
81
+ const press = () => {
82
+ if (releaseTimerRef.current) {
83
+ clearTimeout(releaseTimerRef.current);
84
+ releaseTimerRef.current = null;
85
+ }
86
+ pressedAtRef.current = Date.now();
87
+ setPressed(true);
88
+ };
89
+
90
+ const release = () => {
91
+ const heldFor = pressedAtRef.current
92
+ ? Date.now() - pressedAtRef.current
93
+ : minDurationMs;
94
+ const remaining = Math.max(0, minDurationMs - heldFor);
95
+ if (releaseTimerRef.current) clearTimeout(releaseTimerRef.current);
96
+ releaseTimerRef.current = setTimeout(() => setPressed(false), remaining);
97
+ };
98
+
99
+ return { pressed, press, release };
100
+ }
101
+
102
+ function resolveVariant(p: VariantBools): Variant {
103
+ if (p.primary) return "primary";
104
+ if (p.secondary) return "secondary";
105
+ if (p.tertiary) return "tertiary";
106
+ if (p.outlined) return "outlined";
107
+ if (p.text) return "text";
108
+ if (p.link) return "link";
109
+ return "primary";
110
+ }
111
+
112
+ function resolveSize(p: SizeBools): number {
113
+ if (p.xs) return SIZE_TO_PADDING.xs;
114
+ if (p.sm) return SIZE_TO_PADDING.sm;
115
+ if (p.l) return SIZE_TO_PADDING.l;
116
+ if (p.xl) return SIZE_TO_PADDING.xl;
117
+ return SIZE_TO_PADDING.m;
118
+ }
119
+
120
+ // shared style derivation for Tab and CTA
121
+ function deriveStyles(opts: {
122
+ variant: Variant;
123
+ toggle: boolean | undefined;
124
+ isActive: boolean;
125
+ pressed: boolean;
126
+ hasLabel: boolean;
127
+ hideWhenInactive?: boolean;
128
+ square?: boolean;
129
+ circle?: boolean;
130
+ btnPadding: number;
131
+ }) {
132
+ const {
133
+ variant,
134
+ toggle,
135
+ isActive,
136
+ pressed,
137
+ hasLabel,
138
+ hideWhenInactive,
139
+ square,
140
+ circle,
141
+ btnPadding,
142
+ } = opts;
143
+
144
+ // Padding shape selection:
145
+ // - `square` or `circle` force square padding
146
+ // - Buttons with an always-visible label → square
147
+ // - Icon-only OR labels that hide when inactive → pill
148
+ const useSquare = square || circle || (hasLabel && !hideWhenInactive);
149
+
150
+ // Base settled padding (no press)
151
+ // - Square: always btnPadding
152
+ // - Pill: btnPadding when toggle && active, else btnPadding - btnPadding/1.6
153
+ const basePadding = useSquare
154
+ ? btnPadding
155
+ : toggle && isActive
156
+ ? btnPadding
157
+ : btnPadding - btnPadding / 1.6;
158
+
159
+ // Press bounce: always-positive expansion while pressed
160
+ const bounceMag = Math.min(Math.max(btnPadding / 4, 8), 24);
161
+ const paddingInline = basePadding + (pressed ? bounceMag : 0);
162
+
163
+ // Border radius:
164
+ // - `square` → 14 always
165
+ // - `circle` → 36 always
166
+ // - Else: 36 when toggle && active, 14 otherwise
167
+ const borderRadius = square ? 14 : circle ? 36 : toggle && isActive ? 36 : 14;
168
+
169
+ // Variant key:
170
+ // - primary / secondary / tertiary → -bold when active
171
+ // - outlined → secondary-bold when active
172
+ // - text / link → no change when active
173
+ let variantKey: string = variant;
174
+ if (toggle && isActive) {
175
+ if (
176
+ variant === "primary" ||
177
+ variant === "secondary" ||
178
+ variant === "tertiary"
179
+ ) {
180
+ variantKey = `${variant}-bold`;
181
+ } else if (variant === "outlined") {
182
+ variantKey = "secondary-bold";
183
+ }
184
+ }
185
+
186
+ return { paddingInline, borderRadius, variantKey };
187
+ }
188
+
189
+ // ---------------- Tab ----------------
190
+
191
+ type TabProps = {
192
+ default?: boolean;
193
+ systemName?: string;
194
+ label?: LabelConfig;
195
+ toggle?: boolean;
196
+ /** Force border-radius 14 and square padding regardless of active state */
197
+ square?: boolean;
198
+ /** Force border-radius 36 and square padding regardless of active state */
199
+ circle?: boolean;
200
+ onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void;
201
+ } & Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onClick">;
202
+
203
+ type TabInternalProps = TabProps & { _index?: number };
204
+
205
+ function Tab({
206
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
207
+ default: _isDefault,
208
+ systemName,
209
+ label,
210
+ toggle,
211
+ square = false,
212
+ circle = false,
213
+ onClick,
214
+ _index,
215
+ style,
216
+ ...rest
217
+ }: TabInternalProps) {
218
+ const ctx = useContext(GroupContext);
219
+ if (!ctx || _index === undefined) {
220
+ throw new Error("Button.Tab must be a direct child of Button.Group");
221
+ }
222
+
223
+ const { pressed, press, release } = usePressed();
224
+ const isActive = ctx.isActive(_index);
225
+
226
+ const labelText = typeof label === "string" ? label : label?.text;
227
+ const hideWhenInactive =
228
+ typeof label === "object" ? !!label.hideWhenInactive : false;
229
+ const labelVisible =
230
+ labelText !== undefined && (!hideWhenInactive || isActive);
231
+ const iconLabel = labelVisible ? labelText : "";
232
+ const hasLabel = label !== undefined;
233
+
234
+ const { paddingInline, borderRadius, variantKey } = deriveStyles({
235
+ variant: ctx.variant,
236
+ toggle,
237
+ isActive,
238
+ pressed,
239
+ hasLabel,
240
+ hideWhenInactive,
241
+ square,
242
+ circle,
243
+ btnPadding: ctx.btnPadding,
244
+ });
245
+
246
+ const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
247
+ const isAlreadyActiveExclusive =
248
+ ctx.isExclusive && toggle && ctx.isActive(_index);
249
+ ctx.onTabClick(_index, toggle);
250
+ if (!isAlreadyActiveExclusive) onClick?.(e);
251
+ };
252
+
253
+ return (
254
+ <BaseButton
255
+ {...{ [variantKey]: true }}
256
+ onClick={handleClick}
257
+ onPointerDown={press}
258
+ onPointerUp={release}
259
+ onPointerLeave={release}
260
+ onPointerCancel={release}
261
+ style={{
262
+ padding: ctx.btnPadding,
263
+ paddingInline,
264
+ margin: 0,
265
+ borderRadius,
266
+ transition: TRANSITION,
267
+ ...(ctx.springLayout
268
+ ? { flex: "1 1 auto", minWidth: 0, width: "max-content" }
269
+ : {}),
270
+ ...style,
271
+ }}
272
+ {...rest}
273
+ >
274
+ <Label systemName={systemName} reverse label={iconLabel} />
275
+ </BaseButton>
276
+ );
277
+ }
278
+
279
+ // MARK: Group
280
+
281
+ type ButtonGroupProps = {
282
+ children: React.ReactNode;
283
+ /** When true (default), only one tab can be active at a time. */
284
+ exclusive?: boolean;
285
+ /** Inverse of `exclusive`. If both are set, this wins. */
286
+ multiSelect?: boolean;
287
+ /**
288
+ * Fixed width for the group. When set, tabs share the width with `flex: 1 1 auto`
289
+ * — pressing one tab visibly squeezes its siblings (spring effect).
290
+ * When unset, the group sits at `max-content` (no spring, natural sizes).
291
+ */
292
+ width?: number | string | "fullWidth";
293
+ } & VariantBools &
294
+ SizeBools;
295
+
296
+ function Group({
297
+ children,
298
+ exclusive = true,
299
+ multiSelect,
300
+ width,
301
+ // size bools
302
+ xs,
303
+ sm,
304
+ m,
305
+ l,
306
+ xl,
307
+ // variant bools
308
+ ...variantBools
309
+ }: ButtonGroupProps) {
310
+ const isExclusive = multiSelect !== undefined ? !multiSelect : exclusive;
311
+ const variant = resolveVariant(variantBools);
312
+ const btnPadding = resolveSize({ xs, sm, m, l, xl });
313
+ const isFullWidth = width === "fullWidth";
314
+ const springLayout = width !== undefined && !isFullWidth;
315
+
316
+ const childArray = React.Children.toArray(children).filter(
317
+ React.isValidElement,
318
+ );
319
+ const tabs = childArray.filter((child) => child.type === Tab);
320
+ const otherChildren = childArray.filter((child) => child.type !== Tab);
321
+
322
+ const [activeIndices, setActiveIndices] = useState<Set<number>>(() => {
323
+ const defaults = new Set<number>();
324
+ tabs.forEach((child, idx) => {
325
+ const props = (child as React.ReactElement<TabProps>).props;
326
+ if (props.default) {
327
+ if (isExclusive && defaults.size > 0) return;
328
+ defaults.add(idx);
329
+ }
330
+ });
331
+ return defaults;
332
+ });
333
+
334
+ const handleTabClick = (index: number, toggle: boolean | undefined) => {
335
+ if (!toggle) return;
336
+
337
+ const next = new Set(activeIndices);
338
+ const wasActive = next.has(index);
339
+
340
+ // In exclusive mode, can't deactivate the currently-active tab by clicking it
341
+ if (wasActive && isExclusive) return;
342
+
343
+ if (wasActive) {
344
+ next.delete(index);
345
+ } else {
346
+ if (isExclusive) next.clear();
347
+ next.add(index);
348
+ }
349
+ setActiveIndices(next);
350
+ };
351
+
352
+ const ctxValue: GroupContextValue = {
353
+ variant,
354
+ btnPadding,
355
+ isExclusive,
356
+ springLayout,
357
+ isActive: (i) => activeIndices.has(i),
358
+ onTabClick: handleTabClick,
359
+ };
360
+
361
+ const computedMaxWidth = springLayout ? `${Number(width)}px` : "100%";
362
+
363
+ return (
364
+ <GroupContext.Provider value={ctxValue}>
365
+ <div
366
+ style={{
367
+ display: "flex",
368
+ flexDirection: "row",
369
+ alignItems: "center",
370
+ justifyContent: "center",
371
+ overflow: "hidden",
372
+ overflowX: "auto",
373
+ width: springLayout
374
+ ? computedMaxWidth
375
+ : isFullWidth
376
+ ? "100%"
377
+ : "max-content",
378
+ maxWidth: computedMaxWidth,
379
+ gap: `${GAP * 2}px`,
380
+ }}
381
+ >
382
+ <div
383
+ style={{
384
+ display: "flex",
385
+ flexDirection: "row",
386
+ overflow: "hidden",
387
+ overflowX: "auto",
388
+ flex: springLayout || isFullWidth ? "1 1 auto" : undefined,
389
+ minWidth: 0,
390
+ gap: `${GAP}px`,
391
+ }}
392
+ >
393
+ {tabs.map((tab, index) =>
394
+ React.cloneElement(tab as React.ReactElement<TabInternalProps>, {
395
+ _index: index,
396
+ key: index,
397
+ }),
398
+ )}
399
+ </div>
400
+ {otherChildren}
401
+ </div>
402
+ </GroupContext.Provider>
403
+ );
404
+ }
405
+
406
+ Object.assign(BaseButton, { Group, Tab });
407
+
408
+ type ButtonWithStatics = typeof BaseButton & {
409
+ Group: typeof Group;
410
+ Tab: typeof Tab;
411
+ };
412
+
413
+ export { Group, Tab };
414
+ export default BaseButton as ButtonWithStatics;