@yamatozhen/design_system 1.0.1 → 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 (184) 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 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 -473
  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/icon.module.css +0 -0
  176. package/template/components/tools/icon/icon.tsx +0 -125
  177. package/template/components/tools/react-bits/ClickSpark.tsx +0 -177
  178. package/template/components/tools/react-bits/FadeContent.tsx +0 -90
  179. package/template/components/tools/react-bits/GlareHover.tsx +0 -73
  180. package/template/components/tools/react-bits/GradientText/GradientText.css +0 -70
  181. package/template/components/tools/react-bits/GradientText/GradientText.tsx +0 -44
  182. package/template/components/tools/react-bits/Magnet.tsx +0 -118
  183. package/template/components/tools/react-bits/Stepper/Stepper.css +0 -134
  184. package/template/components/tools/react-bits/Stepper/Stepper.tsx +0 -412
@@ -1,199 +1,396 @@
1
- "use client";
2
- import React from "react";
3
-
4
- type SinewaveProps = {
5
- /** Fixed pixel width. Ignored when `fluid` (the container's width is measured each frame instead). */
6
- width?: number;
7
- /** Fit to the container's current width, re-fitting every frame so an animating container width drives the wave. */
8
- fluid?: boolean;
9
- height?: number;
10
- frequency?: number;
11
- /** If set, frequency = currentWidth * freqPerPx — holds wavelength constant as width changes. Overrides `frequency`. */
12
- freqPerPx?: number;
13
- amplitude?: number;
14
- stroke?: string;
15
- strokeWidth?: number;
16
- /** Continuously shifts the wave's phase from right to left. */
17
- animate?: boolean;
18
- /** Seconds for the wave to shift exactly one full period. */
19
- duration?: number;
20
- style?: React.CSSProperties;
21
- className?: string;
22
- };
23
-
24
- const useIsoLayoutEffect =
25
- typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
26
-
27
- function buildPath(
28
- width: number,
29
- height: number,
30
- frequency: number,
31
- amplitude: number,
32
- phase: number,
33
- ) {
34
- const points: string[] = [];
35
- const step = 2;
36
- for (let x = 0; x <= width; x += step) {
37
- const y =
38
- height / 2 +
39
- amplitude * Math.sin((x / width) * frequency * 2 * Math.PI + phase);
40
- points.push(`${x},${y}`);
41
- }
42
- return "M" + points.join(" L ");
43
- }
44
-
45
- const Sinewave: React.FC<SinewaveProps> = ({
46
- width = 400,
47
- fluid = false,
48
- height = 20,
49
- frequency = width * 0.04,
50
- freqPerPx,
51
- amplitude = 4,
52
- stroke = "var(--md-sys-color-primary-container)",
53
- strokeWidth = 2,
54
- animate = false,
55
- duration = 1.6,
56
- style,
57
- className,
58
- }) => {
59
- const rootRef = React.useRef<HTMLDivElement>(null);
60
- const svgRef = React.useRef<SVGSVGElement>(null);
61
- const pathRef = React.useRef<SVGPathElement>(null);
62
- const phaseRef = React.useRef(0);
63
- const lastTimeRef = React.useRef<number | null>(null);
64
- const dispAmpRef = React.useRef(amplitude); // eased toward liveRef target
65
-
66
- // Geometry the rAF loop reads each frame. Refreshed on every render (not via
67
- // effect deps) so width/frequency/amplitude changes never restart the loop's
68
- // clock — phase keeps advancing while the width is being animated.
69
- const liveRef = React.useRef({
70
- width,
71
- fluid,
72
- height,
73
- frequency,
74
- freqPerPx,
75
- amplitude,
76
- duration,
77
- });
78
- liveRef.current = {
79
- width,
80
- fluid,
81
- height,
82
- frequency,
83
- freqPerPx,
84
- amplitude,
85
- duration,
86
- };
87
-
88
- // Resolve "now": measured width when fluid, derived freq when freqPerPx is
89
- // set, eased amplitude.
90
- const measure = React.useCallback(() => {
91
- const l = liveRef.current;
92
- const measured = rootRef.current?.clientWidth; // 0 is valid (progress 0)
93
- const w = l.fluid ? (measured ?? l.width) : l.width;
94
- const f = l.freqPerPx != null ? w * l.freqPerPx : l.frequency;
95
- return { w, f, h: l.height, a: dispAmpRef.current };
96
- }, []);
97
-
98
- const draw = React.useCallback(() => {
99
- const { w, f, h, a } = measure();
100
- svgRef.current?.setAttribute("width", String(w));
101
- svgRef.current?.setAttribute("viewBox", `0 0 ${w} ${h}`);
102
- pathRef.current?.setAttribute("d", buildPath(w, h, f, a, phaseRef.current));
103
- }, [measure]);
104
-
105
- // Animated phase + per-frame re-fit + amplitude easing.
106
- React.useEffect(() => {
107
- if (!animate) {
108
- lastTimeRef.current = null;
109
- return;
110
- }
111
- let frame: number;
112
- const tick = (now: number) => {
113
- if (lastTimeRef.current != null) {
114
- const dt = (now - lastTimeRef.current) / 1000;
115
- phaseRef.current =
116
- (phaseRef.current + (dt / liveRef.current.duration) * Math.PI * 2) %
117
- (Math.PI * 2);
118
- // ease amplitude toward target (drops to 0 at ≥98%)
119
- dispAmpRef.current +=
120
- (liveRef.current.amplitude - dispAmpRef.current) *
121
- (1 - Math.exp(-dt * 8));
122
- }
123
- lastTimeRef.current = now;
124
- draw();
125
- frame = requestAnimationFrame(tick);
126
- };
127
- frame = requestAnimationFrame(tick);
128
- return () => cancelAnimationFrame(frame);
129
- }, [animate, draw]);
130
-
131
- // Initial fit (pre-paint) + static/resize fit when not animating.
132
- useIsoLayoutEffect(() => {
133
- if (!animate) dispAmpRef.current = amplitude; // frozen frame is exact
134
- draw();
135
- if (animate || !fluid) return;
136
- const el = rootRef.current;
137
- if (!el) return;
138
- const ro = new ResizeObserver(() => draw());
139
- ro.observe(el);
140
- return () => ro.disconnect();
141
- }, [draw, animate, fluid, width, height, frequency, freqPerPx, amplitude]);
142
-
143
- // Fluid starts at 0 so nothing flashes full-width before the first fit.
144
- const initialW = fluid ? 0 : width;
145
- const initialFreq = freqPerPx != null ? initialW * freqPerPx : frequency;
146
- const initialPath = React.useMemo(
147
- () => buildPath(initialW, height, initialFreq, amplitude, 0),
148
- [initialW, height, initialFreq, amplitude],
149
- );
150
-
151
- return (
152
- <div
153
- ref={rootRef}
154
- className="sinewave-container"
155
- style={{
156
- minHeight: "20px",
157
- margin: "24px 0",
158
- display: "flex",
159
- justifyContent: "center",
160
- alignItems: "center",
161
- ...(fluid ? { width: "100%" } : null),
162
- ...style,
163
- }}
164
- >
165
- <svg
166
- ref={svgRef}
167
- width={initialW}
168
- height={height}
169
- viewBox={`0 0 ${initialW} ${height}`}
170
- fill="none"
171
- xmlns="http://www.w3.org/2000/svg"
172
- style={{
173
- stroke,
174
- transition: "stroke var(--transition-expressive-color)",
175
- position: "relative",
176
- margin: 0,
177
- padding: 0,
178
- overflow: "visible",
179
- }}
180
- className={className}
181
- >
182
- <path
183
- ref={pathRef}
184
- d={initialPath}
185
- stroke={stroke}
186
- strokeLinecap="round"
187
- fill="none"
188
- suppressHydrationWarning
189
- style={{
190
- strokeWidth,
191
- transition: "stroke-width var(--transition-expressive-color)",
192
- }}
193
- />
194
- </svg>
195
- </div>
196
- );
197
- };
198
-
199
- export default Sinewave;
1
+ "use client";
2
+ import React from "react";
3
+ import {
4
+ cubicBezier,
5
+ DEFAULT_EASE,
6
+ makeTween,
7
+ readTween,
8
+ snapTween,
9
+ } from "@/components/tools/easing";
10
+
11
+ export type SinewaveEasingName =
12
+ | "linear"
13
+ | "ease"
14
+ | "easeIn"
15
+ | "easeOut"
16
+ | "easeInOut";
17
+ export type SinewaveEasing = SinewaveEasingName | [number, number, number, number];
18
+
19
+ // Standard CSS easing-keyword equivalents, as cubic-bezier control points.
20
+ const NAMED_EASING: Record<SinewaveEasingName, [number, number, number, number]> = {
21
+ linear: [0, 0, 1, 1],
22
+ ease: [0.25, 0.1, 0.25, 1],
23
+ easeIn: [0.42, 0, 1, 1],
24
+ easeOut: [0, 0, 0.58, 1],
25
+ easeInOut: [0.42, 0, 0.58, 1],
26
+ };
27
+
28
+ function resolveEasing(easing: SinewaveEasing | undefined): (t: number) => number {
29
+ if (easing == null) return DEFAULT_EASE;
30
+ const [x1, y1, x2, y2] = Array.isArray(easing)
31
+ ? easing
32
+ : NAMED_EASING[easing];
33
+ return cubicBezier(x1, y1, x2, y2);
34
+ }
35
+
36
+ type SinewaveProps = {
37
+ /** Fixed pixel width. Ignored when `fluid` (the container's width is measured each frame instead) or `circular`. */
38
+ width?: number;
39
+ /** Fit to the container's current width, re-fitting every frame so an animating container width drives the wave. Ignored when `circular`. */
40
+ fluid?: boolean;
41
+ height?: number;
42
+ frequency?: number;
43
+ /** If set, frequency = currentWidth * freqPerPx — holds wavelength constant as width changes. Overrides `frequency`. Ignored when `circular`. */
44
+ freqPerPx?: number;
45
+ amplitude?: number;
46
+ stroke?: string;
47
+ strokeWidth?: number;
48
+ /** Continuously shifts the wave's phase from right to left (or around the ring, when `circular`). */
49
+ animate?: boolean;
50
+ /** Seconds for the wave to shift exactly one full period. */
51
+ duration?: number;
52
+ /** Easing for the amplitude/sweep/frequency tweens (not the phase crawl,
53
+ * which is a continuous loop with no start/end to ease between) — a named
54
+ * CSS-style keyword or a raw `[x1, y1, x2, y2]` cubic-bezier. Ignored when
55
+ * `instant`. @default the shared PROGRESS_TRANSITION curve (linear) */
56
+ easing?: SinewaveEasing;
57
+ /** Draw the wave along a ring instead of a straight line — `frequency` then
58
+ * means whole cycles per full 360° revolution. */
59
+ circular?: boolean;
60
+ /** Diameter in px. Required when `circular`. */
61
+ size?: number;
62
+ /** Resting radius in px (the wave oscillates around this). Required when `circular`. */
63
+ radius?: number;
64
+ /** Degrees where the arc starts, sweeping clockwise from there. 0 = top. @default 0 */
65
+ startAngle?: number;
66
+ /** Degrees of the ring to draw, starting at `startAngle` and sweeping clockwise. @default 360 */
67
+ sweepAngle?: number;
68
+ /** Skip the amplitude/sweep/frequency tweening and feed every frame's raw
69
+ * value straight into the path (phase still animates continuously). For
70
+ * callers already driving `startAngle`/`sweepAngle` from their own
71
+ * continuous animation loop (e.g. an indeterminate worm) — tweening a
72
+ * target that's already changing every frame just makes it lag, since the
73
+ * tween keeps getting retargeted before it can catch up. */
74
+ instant?: boolean;
75
+ /** Extra delay before `strokeWidth` starts transitioning, on top of
76
+ * `--transition-expressive-color`'s own duration. */
77
+ strokeWidthTransitionDelay?: string;
78
+ style?: React.CSSProperties;
79
+ className?: string;
80
+ };
81
+
82
+ const useIsoLayoutEffect =
83
+ typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
84
+
85
+ function buildPath(
86
+ width: number,
87
+ height: number,
88
+ frequency: number,
89
+ amplitude: number,
90
+ phase: number,
91
+ ) {
92
+ // width === 0 is a valid, intentional state (fluid mode starts at 0 before
93
+ // the first measurement) — but x / width would be 0 / 0 = NaN, poisoning
94
+ // the whole path string. Short-circuit to a degenerate flat point instead.
95
+ if (width <= 0) return `M0,${height / 2}`;
96
+ const points: string[] = [];
97
+ const step = 2;
98
+ for (let x = 0; x <= width; x += step) {
99
+ const y =
100
+ height / 2 +
101
+ amplitude * Math.sin((x / width) * frequency * 2 * Math.PI + phase);
102
+ points.push(`${x},${y}`);
103
+ }
104
+ return "M" + points.join(" L ");
105
+ }
106
+
107
+ // Polar counterpart of buildPath: walks the sweep angle (startAngle°=top,
108
+ // clockwise) instead of x, and offsets radius instead of y. Doesn't need to
109
+ // worry about the wave seaming with itself — it's an open arc, not a closed
110
+ // loop, even when startAngle/sweepAngle put it anywhere on the ring.
111
+ function buildCircularPath(
112
+ size: number,
113
+ radius: number,
114
+ frequency: number,
115
+ amplitude: number,
116
+ phase: number,
117
+ startAngle: number,
118
+ sweepAngle: number,
119
+ ) {
120
+ if (size <= 0 || sweepAngle <= 0) return "";
121
+ const cx = size / 2;
122
+ const cy = size / 2;
123
+ const startRad = (startAngle / 360) * Math.PI * 2;
124
+ const sweepRad = (sweepAngle / 360) * Math.PI * 2;
125
+ const steps = Math.max(2, Math.ceil(sweepAngle / 3));
126
+ const points: string[] = [];
127
+ for (let i = 0; i <= steps; i++) {
128
+ const t = i / steps;
129
+ // Angle measured from the top (12 o'clock), sweeping clockwise: SVG's
130
+ // y-axis points down, so a plain trig angle from the 3 o'clock origin
131
+ // is rotated -90deg via CSS on the <svg> rather than in this math.
132
+ const angle = startRad + t * sweepRad;
133
+ const r = radius + amplitude * Math.sin(t * frequency * 2 * Math.PI + phase);
134
+ const x = cx + r * Math.cos(angle);
135
+ const y = cy + r * Math.sin(angle);
136
+ points.push(`${x},${y}`);
137
+ }
138
+ return "M" + points.join(" L ");
139
+ }
140
+
141
+ const Sinewave: React.FC<SinewaveProps> = ({
142
+ width = 400,
143
+ fluid = false,
144
+ height = 20,
145
+ frequency = width * 0.04,
146
+ freqPerPx,
147
+ amplitude = 4,
148
+ stroke = "var(--md-sys-color-primary-container)",
149
+ strokeWidth = 2,
150
+ animate = false,
151
+ duration = 1.6,
152
+ circular = false,
153
+ size = 0,
154
+ radius = 0,
155
+ startAngle = 0,
156
+ sweepAngle = 360,
157
+ instant = false,
158
+ easing,
159
+ strokeWidthTransitionDelay,
160
+ style,
161
+ className,
162
+ }) => {
163
+ const rootRef = React.useRef<HTMLDivElement>(null);
164
+ const svgRef = React.useRef<SVGSVGElement>(null);
165
+ const pathRef = React.useRef<SVGPathElement>(null);
166
+ const phaseRef = React.useRef(0);
167
+ const lastTimeRef = React.useRef<number | null>(null);
168
+ const ampTweenRef = React.useRef(makeTween(amplitude));
169
+ const startTweenRef = React.useRef(makeTween(startAngle)); // circular only
170
+ const sweepTweenRef = React.useRef(makeTween(sweepAngle)); // circular only
171
+ const freqTweenRef = React.useRef(makeTween(frequency)); // circular only
172
+
173
+ // Recomputed only when the easing's actual VALUE changes, not on every
174
+ // render — a fresh `[x1,y1,x2,y2]` array literal from the caller shouldn't
175
+ // rebuild the bezier solver each time.
176
+ const easingKey = Array.isArray(easing) ? easing.join(",") : (easing ?? "");
177
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- keyed on easingKey, not `easing` itself
178
+ const resolvedEase = React.useMemo(() => resolveEasing(easing), [easingKey]);
179
+
180
+ // Geometry the rAF loop reads each frame. Refreshed every commit (via a
181
+ // no-deps layout effect, before paint — not during render, since ref
182
+ // writes aren't render-pure) so width/frequency/amplitude changes never
183
+ // restart the loop's clock — phase keeps advancing while width animates.
184
+ const liveRef = React.useRef({
185
+ width,
186
+ fluid,
187
+ height,
188
+ frequency,
189
+ freqPerPx,
190
+ amplitude,
191
+ ease: resolvedEase,
192
+ duration,
193
+ circular,
194
+ size,
195
+ radius,
196
+ startAngle,
197
+ sweepAngle,
198
+ instant,
199
+ });
200
+ useIsoLayoutEffect(() => {
201
+ liveRef.current = {
202
+ width,
203
+ fluid,
204
+ height,
205
+ frequency,
206
+ freqPerPx,
207
+ amplitude,
208
+ ease: resolvedEase,
209
+ duration,
210
+ circular,
211
+ size,
212
+ radius,
213
+ startAngle,
214
+ sweepAngle,
215
+ instant,
216
+ };
217
+ });
218
+
219
+ // Resolve "now": measured width when fluid, derived freq when freqPerPx is
220
+ // set, eased amplitude.
221
+ const measure = React.useCallback((now: number) => {
222
+ const l = liveRef.current;
223
+ const measured = rootRef.current?.clientWidth; // 0 is valid (progress 0)
224
+ const w = l.fluid ? (measured ?? l.width) : l.width;
225
+ const f = l.freqPerPx != null ? w * l.freqPerPx : l.frequency;
226
+ return {
227
+ w,
228
+ f,
229
+ h: l.height,
230
+ a: readTween(ampTweenRef.current, l.amplitude, now, undefined, l.ease),
231
+ };
232
+ }, []);
233
+
234
+ const draw = React.useCallback(
235
+ (now: number) => {
236
+ const l = liveRef.current;
237
+ if (l.circular) {
238
+ svgRef.current?.setAttribute("width", String(l.size));
239
+ svgRef.current?.setAttribute("height", String(l.size));
240
+ svgRef.current?.setAttribute("viewBox", `0 0 ${l.size} ${l.size}`);
241
+ const [freq, amp, start, sweep] = l.instant
242
+ ? [l.frequency, l.amplitude, l.startAngle, l.sweepAngle]
243
+ : [
244
+ readTween(freqTweenRef.current, l.frequency, now, undefined, l.ease),
245
+ readTween(ampTweenRef.current, l.amplitude, now, undefined, l.ease),
246
+ readTween(startTweenRef.current, l.startAngle, now, undefined, l.ease),
247
+ readTween(sweepTweenRef.current, l.sweepAngle, now, undefined, l.ease),
248
+ ];
249
+ pathRef.current?.setAttribute(
250
+ "d",
251
+ buildCircularPath(l.size, l.radius, freq, amp, phaseRef.current, start, sweep),
252
+ );
253
+ return;
254
+ }
255
+ const { w, f, h, a } = measure(now);
256
+ svgRef.current?.setAttribute("width", String(w));
257
+ svgRef.current?.setAttribute("viewBox", `0 0 ${w} ${h}`);
258
+ pathRef.current?.setAttribute(
259
+ "d",
260
+ buildPath(w, h, f, a, phaseRef.current),
261
+ );
262
+ },
263
+ [measure],
264
+ );
265
+
266
+ // Animated phase + per-frame re-fit. Amplitude/sweep/frequency ease inside
267
+ // draw() itself (time-boxed tweens keyed off `now`), not here.
268
+ React.useEffect(() => {
269
+ if (!animate) {
270
+ lastTimeRef.current = null;
271
+ return;
272
+ }
273
+ let frame: number;
274
+ const tick = (now: number) => {
275
+ if (lastTimeRef.current != null) {
276
+ const dt = (now - lastTimeRef.current) / 1000;
277
+ phaseRef.current =
278
+ (phaseRef.current + (dt / liveRef.current.duration) * Math.PI * 2) %
279
+ (Math.PI * 2);
280
+ }
281
+ lastTimeRef.current = now;
282
+ draw(now);
283
+ frame = requestAnimationFrame(tick);
284
+ };
285
+ frame = requestAnimationFrame(tick);
286
+ return () => cancelAnimationFrame(frame);
287
+ }, [animate, draw]);
288
+
289
+ // Initial fit (pre-paint) + static/resize fit when not animating.
290
+ useIsoLayoutEffect(() => {
291
+ const now = performance.now();
292
+ if (!animate) {
293
+ snapTween(ampTweenRef.current, amplitude); // frozen frame is exact
294
+ snapTween(startTweenRef.current, startAngle);
295
+ snapTween(sweepTweenRef.current, sweepAngle);
296
+ snapTween(freqTweenRef.current, frequency);
297
+ }
298
+ draw(now);
299
+ if (animate || !fluid || circular) return;
300
+ const el = rootRef.current;
301
+ if (!el) return;
302
+ const ro = new ResizeObserver(() => draw(performance.now()));
303
+ ro.observe(el);
304
+ return () => ro.disconnect();
305
+ }, [
306
+ draw,
307
+ animate,
308
+ fluid,
309
+ width,
310
+ height,
311
+ frequency,
312
+ freqPerPx,
313
+ amplitude,
314
+ circular,
315
+ size,
316
+ radius,
317
+ startAngle,
318
+ sweepAngle,
319
+ ]);
320
+
321
+ // Fluid starts at 0 so nothing flashes full-width before the first fit.
322
+ const initialW = fluid ? 0 : width;
323
+ const initialFreq = freqPerPx != null ? initialW * freqPerPx : frequency;
324
+ const initialPath = React.useMemo(
325
+ () =>
326
+ circular
327
+ ? buildCircularPath(size, radius, frequency, amplitude, 0, startAngle, sweepAngle)
328
+ : buildPath(initialW, height, initialFreq, amplitude, 0),
329
+ [
330
+ circular,
331
+ size,
332
+ radius,
333
+ startAngle,
334
+ sweepAngle,
335
+ initialW,
336
+ height,
337
+ initialFreq,
338
+ amplitude,
339
+ frequency,
340
+ ],
341
+ );
342
+
343
+ return (
344
+ <div
345
+ ref={rootRef}
346
+ className="sinewave-container"
347
+ style={{
348
+ minHeight: "20px",
349
+ margin: "24px 0",
350
+ display: "flex",
351
+ justifyContent: "center",
352
+ alignItems: "center",
353
+ ...(fluid && !circular ? { width: "100%" } : null),
354
+ ...style,
355
+ }}
356
+ >
357
+ <svg
358
+ ref={svgRef}
359
+ width={circular ? size : initialW}
360
+ height={circular ? size : height}
361
+ viewBox={
362
+ circular ? `0 0 ${size} ${size}` : `0 0 ${initialW} ${height}`
363
+ }
364
+ fill="none"
365
+ xmlns="http://www.w3.org/2000/svg"
366
+ style={{
367
+ stroke,
368
+ transition: "stroke var(--transition-expressive-color)",
369
+ position: "relative",
370
+ margin: 0,
371
+ padding: 0,
372
+ overflow: "visible",
373
+ ...(circular ? { transform: "rotate(-90deg)" } : null),
374
+ }}
375
+ className={className}
376
+ >
377
+ <path
378
+ ref={pathRef}
379
+ d={initialPath}
380
+ stroke={stroke}
381
+ strokeLinecap="round"
382
+ fill="none"
383
+ suppressHydrationWarning
384
+ style={{
385
+ strokeWidth,
386
+ transition: strokeWidthTransitionDelay
387
+ ? `stroke-width var(--transition-expressive-color) ${strokeWidthTransitionDelay}`
388
+ : "stroke-width var(--transition-expressive-color)",
389
+ }}
390
+ />
391
+ </svg>
392
+ </div>
393
+ );
394
+ };
395
+
396
+ export default Sinewave;