glacia-native 0.3.5 → 0.5.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 (121) hide show
  1. package/lib/module/Bars.js +176 -0
  2. package/lib/module/Button.js +15 -2
  3. package/lib/module/Card.js +29 -14
  4. package/lib/module/Carousel.js +103 -0
  5. package/lib/module/Dialog.js +43 -32
  6. package/lib/module/Display.js +240 -0
  7. package/lib/module/Feedback.js +172 -0
  8. package/lib/module/Form.js +51 -12
  9. package/lib/module/Forms2.js +227 -0
  10. package/lib/module/Glass.js +11 -2
  11. package/lib/module/Input.js +19 -7
  12. package/lib/module/Overlays.js +166 -0
  13. package/lib/module/Pickers.js +243 -0
  14. package/lib/module/SegmentedControl.js +65 -21
  15. package/lib/module/Slider.js +121 -25
  16. package/lib/module/Switch.js +33 -15
  17. package/lib/module/TabBar.js +63 -25
  18. package/lib/module/Table.js +24 -6
  19. package/lib/module/Tabs.js +55 -33
  20. package/lib/module/Text.js +37 -0
  21. package/lib/module/ai.js +49 -20
  22. package/lib/module/charts/BarChart.js +4 -2
  23. package/lib/module/charts/FunnelChart.js +4 -2
  24. package/lib/module/charts/HeatmapChart.js +4 -2
  25. package/lib/module/charts/LineChart.js +4 -2
  26. package/lib/module/charts/PlotChart.js +11 -3
  27. package/lib/module/charts/RadarChart.js +4 -2
  28. package/lib/module/charts/RingChart.js +4 -2
  29. package/lib/module/charts/ScatterChart.js +20 -3
  30. package/lib/module/charts/WaterfallChart.js +12 -3
  31. package/lib/module/charts/advanced.js +12 -6
  32. package/lib/module/charts/extra.js +6 -3
  33. package/lib/module/index.js +58 -1
  34. package/lib/module/internal/GlassView.js +31 -17
  35. package/lib/module/internal/fallback.js +14 -3
  36. package/lib/module/internal/glass-routing.js +56 -0
  37. package/lib/module/internal/motion.js +55 -0
  38. package/lib/module/internal/primitives.js +413 -0
  39. package/lib/module/internal/tokens.js +55 -1
  40. package/lib/typescript/Bars.d.ts +121 -0
  41. package/lib/typescript/Button.d.ts +11 -1
  42. package/lib/typescript/Card.d.ts +20 -8
  43. package/lib/typescript/Carousel.d.ts +47 -0
  44. package/lib/typescript/Dialog.d.ts +19 -6
  45. package/lib/typescript/Display.d.ts +119 -0
  46. package/lib/typescript/Feedback.d.ts +138 -0
  47. package/lib/typescript/Form.d.ts +27 -11
  48. package/lib/typescript/Forms2.d.ts +128 -0
  49. package/lib/typescript/Glass.d.ts +10 -1
  50. package/lib/typescript/Input.d.ts +11 -1
  51. package/lib/typescript/Overlays.d.ts +105 -0
  52. package/lib/typescript/Pickers.d.ts +105 -0
  53. package/lib/typescript/SegmentedControl.d.ts +11 -1
  54. package/lib/typescript/Slider.d.ts +13 -1
  55. package/lib/typescript/Switch.d.ts +12 -1
  56. package/lib/typescript/TabBar.d.ts +13 -1
  57. package/lib/typescript/Table.d.ts +14 -5
  58. package/lib/typescript/Tabs.d.ts +8 -5
  59. package/lib/typescript/Text.d.ts +28 -0
  60. package/lib/typescript/ai.d.ts +12 -9
  61. package/lib/typescript/charts/BarChart.d.ts +1 -1
  62. package/lib/typescript/charts/FunnelChart.d.ts +1 -1
  63. package/lib/typescript/charts/HeatmapChart.d.ts +1 -1
  64. package/lib/typescript/charts/LineChart.d.ts +1 -1
  65. package/lib/typescript/charts/PlotChart.d.ts +1 -1
  66. package/lib/typescript/charts/RadarChart.d.ts +1 -1
  67. package/lib/typescript/charts/RingChart.d.ts +1 -1
  68. package/lib/typescript/charts/ScatterChart.d.ts +1 -1
  69. package/lib/typescript/charts/WaterfallChart.d.ts +1 -1
  70. package/lib/typescript/charts/advanced.d.ts +5 -5
  71. package/lib/typescript/charts/extra.d.ts +2 -2
  72. package/lib/typescript/index.d.ts +9 -1
  73. package/lib/typescript/internal/GlassView.d.ts +21 -4
  74. package/lib/typescript/internal/fallback.d.ts +10 -1
  75. package/lib/typescript/internal/glass-routing.d.ts +30 -0
  76. package/lib/typescript/internal/motion.d.ts +19 -0
  77. package/lib/typescript/internal/primitives.d.ts +41 -0
  78. package/lib/typescript/internal/tokens.d.ts +61 -0
  79. package/package.json +2 -2
  80. package/src/Bars.tsx +570 -0
  81. package/src/Button.tsx +47 -22
  82. package/src/Card.tsx +52 -31
  83. package/src/Carousel.tsx +233 -0
  84. package/src/Dialog.tsx +99 -64
  85. package/src/Display.tsx +550 -0
  86. package/src/Feedback.tsx +579 -0
  87. package/src/Form.tsx +90 -25
  88. package/src/Forms2.tsx +608 -0
  89. package/src/Glass.tsx +24 -1
  90. package/src/Input.tsx +33 -12
  91. package/src/Overlays.tsx +474 -0
  92. package/src/Pickers.tsx +590 -0
  93. package/src/SegmentedControl.tsx +104 -32
  94. package/src/Slider.tsx +151 -47
  95. package/src/Switch.tsx +56 -32
  96. package/src/TabBar.tsx +100 -30
  97. package/src/Table.tsx +38 -14
  98. package/src/Tabs.tsx +85 -32
  99. package/src/Text.tsx +83 -0
  100. package/src/ai.tsx +77 -31
  101. package/src/charts/BarChart.tsx +4 -2
  102. package/src/charts/FunnelChart.tsx +4 -2
  103. package/src/charts/HeatmapChart.tsx +4 -2
  104. package/src/charts/LineChart.tsx +4 -2
  105. package/src/charts/PlotChart.tsx +23 -3
  106. package/src/charts/RadarChart.tsx +4 -2
  107. package/src/charts/RingChart.tsx +4 -2
  108. package/src/charts/ScatterChart.tsx +24 -3
  109. package/src/charts/WaterfallChart.tsx +21 -3
  110. package/src/charts/advanced.tsx +12 -6
  111. package/src/charts/extra.tsx +6 -3
  112. package/src/index.ts +101 -1
  113. package/src/internal/GlassView.tsx +51 -19
  114. package/src/internal/fallback.tsx +23 -8
  115. package/src/internal/glass-routing.ts +68 -0
  116. package/src/internal/motion.ts +60 -0
  117. package/src/internal/primitives.ts +482 -0
  118. package/src/internal/tokens.ts +62 -0
  119. package/lib/module/charts/primitives.js +0 -212
  120. package/lib/typescript/charts/primitives.d.ts +0 -23
  121. package/src/charts/primitives.ts +0 -262
@@ -0,0 +1,243 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimePicker = exports.DatePicker = exports.Calendar = exports.MONTHS = exports.WEEKDAYS = void 0;
4
+ exports.startOfDay = startOfDay;
5
+ exports.sameDay = sameDay;
6
+ exports.monthGrid = monthGrid;
7
+ exports.parseTime = parseTime;
8
+ exports.formatTime = formatTime;
9
+ const jsx_runtime_1 = require("react/jsx-runtime");
10
+ const react_1 = require("react");
11
+ const Button_1 = require("./Button");
12
+ const Overlays_1 = require("./Overlays");
13
+ const tokens_1 = require("./internal/tokens");
14
+ const primitives_1 = require("./internal/primitives");
15
+ const motion_1 = require("./internal/motion");
16
+ // ---------------------------------------------------------------------------
17
+ // Date helpers
18
+ //
19
+ // Kept pure and exported so a caller (and the tests) can reason about the same
20
+ // calendar maths the grid uses. Dates are compared by day, never by instant,
21
+ // which is the bug that makes a picker select "yesterday" near midnight.
22
+ /** Midnight on the same calendar day, so two picks compare equal. */
23
+ function startOfDay(date) {
24
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate());
25
+ }
26
+ function sameDay(a, b) {
27
+ if (!a || !b)
28
+ return false;
29
+ return (a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate());
30
+ }
31
+ /**
32
+ * The six-row month grid.
33
+ *
34
+ * Always six rows so the calendar does not change height as months change,
35
+ * which would make the surrounding layout jump between February and March.
36
+ */
37
+ function monthGrid(year, month, weekStartsOn = 0) {
38
+ const first = new Date(year, month, 1);
39
+ // getDay() is 0 for Sunday; the offset is how many leading cells to add.
40
+ const offset = (first.getDay() - weekStartsOn + 7) % 7;
41
+ const cells = [];
42
+ for (let i = 0; i < 42; i += 1) {
43
+ const date = new Date(year, month, 1 - offset + i);
44
+ cells.push({
45
+ date,
46
+ outside: date.getMonth() !== month,
47
+ key: `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`,
48
+ });
49
+ }
50
+ return cells;
51
+ }
52
+ exports.WEEKDAYS = ["S", "M", "T", "W", "T", "F", "S"];
53
+ exports.MONTHS = [
54
+ "January",
55
+ "February",
56
+ "March",
57
+ "April",
58
+ "May",
59
+ "June",
60
+ "July",
61
+ "August",
62
+ "September",
63
+ "October",
64
+ "November",
65
+ "December",
66
+ ];
67
+ /**
68
+ * A month calendar, built from Glacia's own views.
69
+ *
70
+ * No native date picker anywhere: the grid, the weekday header and the month
71
+ * navigation are ours, so the calendar looks the same on iOS and Android and
72
+ * wears the library's tokens. The selected day is a `glassActive` fill, today
73
+ * carries a ring, and disabled days are dimmed.
74
+ */
75
+ exports.Calendar = (0, react_1.forwardRef)(function Calendar({ value, onChange, minDate, maxDate, initialMonth, weekStartsOn = 0, style, isDark: isDarkProp, testID, ...props }, ref) {
76
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
77
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
78
+ const { View, Text: RNText, Pressable } = (0, primitives_1.primitives)();
79
+ const anchor = value ?? initialMonth ?? new Date();
80
+ const [month, setMonth] = (0, react_1.useState)({ year: anchor.getFullYear(), month: anchor.getMonth() });
81
+ const cells = (0, react_1.useMemo)(() => monthGrid(month.year, month.month, weekStartsOn), [month, weekStartsOn]);
82
+ const today = startOfDay(new Date());
83
+ const isDisabled = (date) => {
84
+ const day = startOfDay(date);
85
+ if (minDate && day < startOfDay(minDate))
86
+ return true;
87
+ if (maxDate && day > startOfDay(maxDate))
88
+ return true;
89
+ return false;
90
+ };
91
+ const shift = (delta) => {
92
+ setMonth((current) => {
93
+ const next = new Date(current.year, current.month + delta, 1);
94
+ return { year: next.getFullYear(), month: next.getMonth() };
95
+ });
96
+ };
97
+ const cellSize = 38;
98
+ return ((0, jsx_runtime_1.jsxs)(View, { ref: ref, testID: testID, accessibilityRole: "none", style: [{ alignSelf: "stretch" }, style], ...props, children: [(0, jsx_runtime_1.jsxs)(View, { style: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingBottom: 8 }, children: [(0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: "Previous month", onPress: () => shift(-1), hitSlop: 8, style: { padding: 6 }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 17, color: theme.foreground }, children: "\u2039" }) }), (0, jsx_runtime_1.jsx)(RNText, { accessibilityRole: "heading", style: { fontSize: 16, fontWeight: "600", color: theme.foreground }, children: `${exports.MONTHS[month.month]} ${month.year}` }), (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: "Next month", onPress: () => shift(1), hitSlop: 8, style: { padding: 6 }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 17, color: theme.foreground }, children: "\u203A" }) })] }), (0, jsx_runtime_1.jsx)(View, { style: { flexDirection: "row" }, children: Array.from({ length: 7 }, (_, i) => ((0, jsx_runtime_1.jsx)(View, { style: { flex: 1, alignItems: "center", paddingVertical: 4 }, children: (0, jsx_runtime_1.jsx)(RNText, { accessibilityElementsHidden: true, style: { fontSize: 11, fontWeight: "600", color: theme.muted }, children: exports.WEEKDAYS[(i + weekStartsOn) % 7] }) }, i))) }), Array.from({ length: 6 }, (_, row) => ((0, jsx_runtime_1.jsx)(View, { style: { flexDirection: "row" }, children: cells.slice(row * 7, row * 7 + 7).map((cell) => {
99
+ const selected = sameDay(cell.date, value);
100
+ const isToday = sameDay(cell.date, today);
101
+ const disabled = isDisabled(cell.date);
102
+ return ((0, jsx_runtime_1.jsx)(View, { style: { flex: 1, alignItems: "center", paddingVertical: 2 }, children: (0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: cell.date.toDateString(), accessibilityState: { selected, disabled }, disabled: disabled, onPress: () => onChange?.(startOfDay(cell.date)), style: {
103
+ width: cellSize,
104
+ height: cellSize,
105
+ borderRadius: cellSize / 2,
106
+ alignItems: "center",
107
+ justifyContent: "center",
108
+ backgroundColor: selected ? theme.glassActive : "transparent",
109
+ borderWidth: isToday && !selected ? 0.5 : selected ? 1 : 0,
110
+ borderColor: isToday && !selected ? theme.fill : theme.edge,
111
+ opacity: disabled ? 0.28 : cell.outside ? 0.45 : 1,
112
+ }, children: (0, jsx_runtime_1.jsx)(RNText, { style: {
113
+ fontSize: 14,
114
+ fontWeight: selected ? "600" : "400",
115
+ color: selected ? theme.foreground : theme.foreground,
116
+ }, children: String(cell.date.getDate()) }) }) }, cell.key));
117
+ }) }, row)))] }));
118
+ });
119
+ /**
120
+ * A date picker: the Calendar presented in a Sheet.
121
+ *
122
+ * Browsing months does not commit anything — the pending date is local until
123
+ * the user confirms, so an accidental swipe of the calendar cannot change a
124
+ * saved value.
125
+ */
126
+ exports.DatePicker = (0, react_1.forwardRef)(function DatePicker({ open: openProp, defaultOpen = false, onOpenChange, value, onConfirm, onCancel, title = "Select date", confirmLabel = "Confirm", cancelLabel = "Cancel", minDate, maxDate, style, isDark: isDarkProp, testID, ...props }, ref) {
127
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
128
+ const [internalOpen, setInternalOpen] = (0, react_1.useState)(defaultOpen);
129
+ const open = openProp !== undefined ? openProp : internalOpen;
130
+ const [pending, setPending] = (0, react_1.useState)(value);
131
+ const setOpen = (next) => {
132
+ if (openProp === undefined)
133
+ setInternalOpen(next);
134
+ onOpenChange?.(next);
135
+ };
136
+ return ((0, jsx_runtime_1.jsx)(Overlays_1.Sheet, { ref: ref, open: open, onOpenChange: setOpen, title: title, isDark: isDark, testID: testID, style: style, footer: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "ghost", accessibilityLabel: cancelLabel, onPress: () => {
137
+ // Cancelling restores the committed value for the next open.
138
+ setPending(value);
139
+ onCancel?.();
140
+ setOpen(false);
141
+ }, children: cancelLabel }), (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "filled", accessibilityLabel: confirmLabel, disabled: pending === undefined, onPress: () => {
142
+ if (pending)
143
+ onConfirm?.(pending);
144
+ setOpen(false);
145
+ }, children: confirmLabel })] }), ...props, children: (0, jsx_runtime_1.jsx)(exports.Calendar, { value: pending, onChange: setPending, minDate: minDate, maxDate: maxDate, isDark: isDark, initialMonth: value }) }));
146
+ });
147
+ /** Splits "HH:MM" into parts, falling back to a sane default. */
148
+ function parseTime(value) {
149
+ const match = /^(\d{1,2}):(\d{2})$/.exec(value ?? "");
150
+ if (!match)
151
+ return { hour: 9, minute: 0 };
152
+ return {
153
+ hour: Math.min(Math.max(Number(match[1]), 0), 23),
154
+ minute: Math.min(Math.max(Number(match[2]), 0), 59),
155
+ };
156
+ }
157
+ /** Formats parts back to zero-padded "HH:MM". */
158
+ function formatTime(hour, minute) {
159
+ return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
160
+ }
161
+ /**
162
+ * A time picker: tappable hour/minute drums inside a Sheet.
163
+ *
164
+ * The drums are ordinary Pressables drawn by Glacia — not a native picker
165
+ * widget. Tapping a value selects it; the whole column scrolls in the Sheet.
166
+ */
167
+ exports.TimePicker = (0, react_1.forwardRef)(function TimePicker({ open: openProp, defaultOpen = false, onOpenChange, value, onConfirm, onCancel, use12Hour = false, title = "Select time", confirmLabel = "Confirm", cancelLabel = "Cancel", minuteStep = 1, style, isDark: isDarkProp, testID, ...props }, ref) {
168
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
169
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
170
+ const { View, Text: RNText, Pressable } = (0, primitives_1.primitives)();
171
+ const [internalOpen, setInternalOpen] = (0, react_1.useState)(defaultOpen);
172
+ const open = openProp !== undefined ? openProp : internalOpen;
173
+ const initial = parseTime(value);
174
+ const [hour, setHour] = (0, react_1.useState)(initial.hour);
175
+ const [minute, setMinute] = (0, react_1.useState)(initial.minute);
176
+ const setOpen = (next) => {
177
+ if (openProp === undefined)
178
+ setInternalOpen(next);
179
+ onOpenChange?.(next);
180
+ };
181
+ const hours = Array.from({ length: 24 }, (_, i) => i);
182
+ const minutes = Array.from({ length: Math.ceil(60 / minuteStep) }, (_, i) => i * minuteStep);
183
+ // In 12-hour mode the column shows 1–12; the stored hour stays 24-hour.
184
+ const displayHour = use12Hour ? (hour % 12 === 0 ? 12 : hour % 12) : hour;
185
+ const isPm = hour >= 12;
186
+ const column = (items, selected, onPick, label, pad = 2) => ((0, jsx_runtime_1.jsx)(View, { style: { height: 168, flex: 1 }, accessibilityRole: "none", accessibilityLabel: label, children: (0, jsx_runtime_1.jsx)(ScrollColumn, { items: items, selected: selected, onPick: onPick, isDark: isDark, pad: pad }) }));
187
+ return ((0, jsx_runtime_1.jsx)(Overlays_1.Sheet, { ref: ref, open: open, onOpenChange: setOpen, title: title, isDark: isDark, testID: testID, style: style, footer: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "ghost", accessibilityLabel: cancelLabel, onPress: () => {
188
+ setHour(initial.hour);
189
+ setMinute(initial.minute);
190
+ onCancel?.();
191
+ setOpen(false);
192
+ }, children: cancelLabel }), (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "filled", accessibilityLabel: confirmLabel, onPress: () => {
193
+ onConfirm?.(formatTime(hour, minute));
194
+ setOpen(false);
195
+ }, children: confirmLabel })] }), ...props, children: (0, jsx_runtime_1.jsxs)(View, { style: { flexDirection: "row", gap: 8, alignItems: "center" }, children: [column(use12Hour ? Array.from({ length: 12 }, (_, i) => i + 1) : hours, displayHour, (picked) => {
196
+ if (!use12Hour) {
197
+ setHour(picked);
198
+ return;
199
+ }
200
+ // Keep the current half of the day when the hour column changes.
201
+ setHour(picked % 12 === 0 ? (isPm ? 12 : 0) : isPm ? picked + 12 : picked);
202
+ }, "Hour"), (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 22, color: theme.muted }, children: ":" }), column(minutes, minute, setMinute, "Minute"), use12Hour && ((0, jsx_runtime_1.jsx)(View, { style: { flex: 1, gap: 6 }, children: ["AM", "PM"].map((label) => {
203
+ const active = label === "PM" ? isPm : !isPm;
204
+ return ((0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: label, accessibilityState: { selected: active }, onPress: () => {
205
+ const nextPm = label === "PM";
206
+ if (nextPm === isPm)
207
+ return;
208
+ setHour((current) => (nextPm ? (current % 12) + 12 : current % 12));
209
+ }, style: {
210
+ height: 34,
211
+ borderRadius: tokens_1.radii.sm,
212
+ alignItems: "center",
213
+ justifyContent: "center",
214
+ backgroundColor: active ? theme.glassActive : "transparent",
215
+ borderWidth: 0.5,
216
+ borderColor: active ? theme.edge : "transparent",
217
+ }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 14, fontWeight: active ? "600" : "500", color: active ? theme.foreground : theme.muted }, children: label }) }, label));
218
+ }) }))] }) }));
219
+ });
220
+ /** One scrollable column of the time drums. */
221
+ function ScrollColumn({ items, selected, onPick, isDark, pad, }) {
222
+ const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
223
+ const { getScrollView } = (0, primitives_1.primitives)();
224
+ const ScrollView = getScrollView();
225
+ const { View, Text: RNText, Pressable } = (0, primitives_1.primitives)();
226
+ const rows = items.map((item) => {
227
+ const active = item === selected;
228
+ return ((0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: String(item).padStart(pad, "0"), accessibilityState: { selected: active }, onPress: () => onPick(item), style: {
229
+ height: 34,
230
+ alignItems: "center",
231
+ justifyContent: "center",
232
+ borderRadius: tokens_1.radii.sm,
233
+ backgroundColor: active ? theme.glassActive : "transparent",
234
+ }, children: (0, jsx_runtime_1.jsx)(RNText, { style: { fontSize: 17, fontWeight: active ? "600" : "400", color: active ? theme.foreground : theme.muted }, children: String(item).padStart(pad, "0") }) }, item));
235
+ });
236
+ if (!ScrollView) {
237
+ // Off-device: no ScrollView, so the column is clipped to its height rather
238
+ // than truncated. Every value stays present and selectable, which is what
239
+ // the real drum shows once it can scroll.
240
+ return (0, jsx_runtime_1.jsx)(View, { style: { gap: 2, overflow: "hidden", height: 168 }, children: rows });
241
+ }
242
+ return ((0, jsx_runtime_1.jsx)(ScrollView, { showsVerticalScrollIndicator: false, style: { flex: 1 }, children: (0, jsx_runtime_1.jsx)(View, { style: { gap: 2 }, children: rows }) }));
243
+ }
@@ -5,42 +5,86 @@ const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const react_1 = require("react");
6
6
  const Glass_1 = require("./Glass");
7
7
  const tokens_1 = require("./internal/tokens");
8
- exports.SegmentedControl = (0, react_1.forwardRef)(function SegmentedControl({ values, selectedIndex: selectedProp, defaultIndex = 0, onValueChange, style, isDark = false, ...props }, ref) {
8
+ const primitives_1 = require("./internal/primitives");
9
+ const motion_1 = require("./internal/motion");
10
+ const SEGMENT_HEIGHT = 30;
11
+ /**
12
+ * A row of mutually exclusive segments on one glass container.
13
+ *
14
+ * The selected segment is marked by a pill that slides on the springs token.
15
+ * The pill's colours come from the theme tokens, so it reads correctly in both
16
+ * light and dark instead of assuming a white-on-light palette.
17
+ */
18
+ exports.SegmentedControl = (0, react_1.forwardRef)(function SegmentedControl({ values, selectedIndex: selectedProp, defaultIndex = 0, onValueChange, style, isDark: isDarkProp, testID, ...props }, ref) {
19
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
20
+ const themeColors = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
21
+ const { View, Text, Pressable, Animated } = (0, primitives_1.primitives)();
9
22
  const [internalIndex, setInternalIndex] = (0, react_1.useState)(defaultIndex);
10
23
  const activeIndex = selectedProp !== undefined ? selectedProp : internalIndex;
11
- const themeColors = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
24
+ const safeIndex = Math.min(Math.max(activeIndex, 0), Math.max(values.length - 1, 0));
25
+ // The pill slides between equal-width segments, so a ratio is enough: 0 is
26
+ // the first segment, values.length - 1 the last.
27
+ const { value: slide, animated } = (0, motion_1.useSpring)(safeIndex, "snappy");
12
28
  const handleSelect = (index) => {
13
- if (selectedProp === undefined) {
29
+ if (selectedProp === undefined)
14
30
  setInternalIndex(index);
15
- }
16
31
  const val = values[index];
17
- if (val !== undefined) {
32
+ if (val !== undefined)
18
33
  onValueChange?.(val, index);
19
- }
20
34
  };
35
+ const count = Math.max(values.length, 1);
36
+ const pillWidth = `${100 / count}%`;
21
37
  const containerStyle = {
22
38
  position: "relative",
23
- display: "inline-flex",
24
39
  flexDirection: "row",
25
40
  alignItems: "center",
41
+ alignSelf: "flex-start",
26
42
  padding: 2,
27
43
  borderRadius: tokens_1.radii.md,
28
44
  backgroundColor: "rgba(118, 118, 128, 0.12)",
29
45
  ...style,
30
46
  };
31
- return ((0, jsx_runtime_1.jsx)(Glass_1.Glass, { ref: ref, effect: "regular", style: containerStyle, ...props, children: values.map((label, idx) => {
32
- const isSelected = idx === activeIndex;
33
- return ((0, jsx_runtime_1.jsx)("button", { type: "button", onClick: () => handleSelect(idx), style: {
34
- padding: "6px 16px",
47
+ const pillTranslate = animated
48
+ ? slide.interpolate({
49
+ inputRange: [0, Math.max(count - 1, 1)],
50
+ // One segment's worth per step, measured against the pill's own width.
51
+ outputRange: ["0%", `${((count - 1) * 100) / count}%`],
52
+ })
53
+ : `${(safeIndex * 100) / count}%`;
54
+ return ((0, jsx_runtime_1.jsxs)(Glass_1.Glass, { ref: ref, effect: "regular", isDark: isDark, style: containerStyle, testID: testID, ...props, children: [animated ? ((0, jsx_runtime_1.jsx)(Animated.View, { style: {
55
+ position: "absolute",
56
+ top: 2,
57
+ left: 2,
58
+ width: pillWidth,
59
+ height: SEGMENT_HEIGHT,
60
+ borderRadius: tokens_1.radii.sm,
61
+ backgroundColor: themeColors.glassActive,
62
+ borderWidth: 0.5,
63
+ borderColor: themeColors.edge,
64
+ transform: [{ translateX: pillTranslate }],
65
+ }, pointerEvents: "none" })) : ((0, jsx_runtime_1.jsx)(View, { style: {
66
+ position: "absolute",
67
+ top: 2,
68
+ left: `${2 + (safeIndex * 100) / count}%`,
69
+ width: pillWidth,
70
+ height: SEGMENT_HEIGHT,
35
71
  borderRadius: tokens_1.radii.sm,
36
- border: 0,
37
- backgroundColor: isSelected ? "#FFFFFF" : "transparent",
38
- color: isSelected ? "#000000" : themeColors.muted,
39
- boxShadow: isSelected ? "0 1px 3px rgba(0, 0, 0, 0.12)" : "none",
40
- fontSize: 13,
41
- fontWeight: 500,
42
- cursor: "pointer",
43
- transition: "all 150ms ease",
44
- }, children: label }, label));
45
- }) }));
72
+ backgroundColor: themeColors.glassActive,
73
+ borderWidth: 0.5,
74
+ borderColor: themeColors.edge,
75
+ }, pointerEvents: "none" })), values.map((label, idx) => {
76
+ const isSelected = idx === safeIndex;
77
+ return ((0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "tab", accessibilityLabel: label, accessibilityState: { selected: isSelected }, onPress: () => handleSelect(idx), style: {
78
+ flex: 1,
79
+ height: SEGMENT_HEIGHT,
80
+ alignItems: "center",
81
+ justifyContent: "center",
82
+ paddingHorizontal: 16,
83
+ borderRadius: tokens_1.radii.sm,
84
+ }, children: (0, jsx_runtime_1.jsx)(Text, { numberOfLines: 1, style: {
85
+ fontSize: 13,
86
+ fontWeight: isSelected ? "600" : "500",
87
+ color: isSelected ? themeColors.foreground : themeColors.muted,
88
+ }, children: label }) }, label));
89
+ })] }));
46
90
  });
@@ -3,47 +3,143 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Slider = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const react_1 = require("react");
6
+ const primitives_1 = require("./internal/primitives");
6
7
  const Glass_1 = require("./Glass");
7
8
  const tokens_1 = require("./internal/tokens");
8
- exports.Slider = (0, react_1.forwardRef)(function Slider({ value: valueProp, defaultValue = 50, min = 0, max = 100, step = 1, onValueChange, disabled = false, accessibilityLabel, style, isDark = false, ...props }, ref) {
9
- const [internalValue, setInternalValue] = (0, react_1.useState)(defaultValue);
10
- const value = valueProp !== undefined ? valueProp : internalValue;
9
+ const primitives_2 = require("./internal/primitives");
10
+ const motion_1 = require("./internal/motion");
11
+ const TRACK_HEIGHT = 8;
12
+ const THUMB = 22;
13
+ const CONTAINER_HEIGHT = 40;
14
+ /** Rounds `value` onto the nearest step from `min`, clamped to the range. */
15
+ function quantize(value, min, max, step) {
16
+ if (!Number.isFinite(value))
17
+ return min;
18
+ const clamped = Math.min(max, Math.max(min, value));
19
+ if (!(step > 0))
20
+ return clamped;
21
+ const stepped = min + Math.round((clamped - min) / step) * step;
22
+ // Round away binary float noise introduced by the division above.
23
+ const decimals = (String(step).split(".")[1] ?? "").length;
24
+ return Number(Math.min(max, Math.max(min, stepped)).toFixed(decimals));
25
+ }
26
+ /**
27
+ * A continuous value chosen by dragging.
28
+ *
29
+ * The interaction is Glacia's own: the track and thumb are Views and dragging
30
+ * is a PanResponder reading the touch position against the track's measured
31
+ * width. React Native has no DOM `<input type="range">` and the platform
32
+ * sliders are not used, so the control looks and behaves the same on every
33
+ * platform; only the glass material changes.
34
+ */
35
+ exports.Slider = (0, react_1.forwardRef)(function Slider({ value: valueProp, defaultValue = 50, min = 0, max = 100, step = 1, onValueChange, disabled = false, accessibilityLabel, style, isDark: isDarkProp, testID, ...props }, ref) {
36
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
11
37
  const themeColors = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
12
- const percentage = Math.min(100, Math.max(0, ((value - min) / (max - min)) * 100));
13
- const handleChange = (e) => {
14
- const val = Number(e.target.value);
15
- if (valueProp === undefined) {
16
- setInternalValue(val);
17
- }
18
- onValueChange?.(val);
38
+ const { View, Pressable, PanResponder } = (0, primitives_2.primitives)();
39
+ const [internalValue, setInternalValue] = (0, react_1.useState)(() => quantize(defaultValue, min, max, step));
40
+ const value = valueProp !== undefined ? quantize(valueProp, min, max, step) : internalValue;
41
+ // Two actions can arrive before React re-renders (increment then decrement,
42
+ // or two drag moves in one frame). The ref holds the newest value so the
43
+ // second one starts from it instead of a stale closure.
44
+ const valueRef = (0, react_1.useRef)(value);
45
+ valueRef.current = value;
46
+ // Track width as measured on layout. A drag before layout is a no-op rather
47
+ // than a jump: without a width there is no honest mapping from x to value.
48
+ const widthRef = (0, react_1.useRef)(0);
49
+ /** Converts an x position relative to the track into a stepped value. */
50
+ const valueAtX = (0, react_1.useMemo)(() => (x) => {
51
+ const width = widthRef.current;
52
+ if (width <= 0 || max === min)
53
+ return null;
54
+ const ratio = Math.min(1, Math.max(0, x / width));
55
+ return quantize(min + ratio * (max - min), min, max, step);
56
+ }, [max, min, step]);
57
+ const report = (next) => {
58
+ if (next === valueRef.current)
59
+ return;
60
+ valueRef.current = next;
61
+ if (valueProp === undefined)
62
+ setInternalValue(next);
63
+ onValueChange?.(next);
19
64
  };
65
+ const panResponder = (0, react_1.useMemo)(() => {
66
+ if (!PanResponder?.create)
67
+ return null;
68
+ return PanResponder.create({
69
+ // Claim the gesture only for horizontal movement, so a Slider inside a
70
+ // vertical scroll view does not swallow the scroll.
71
+ onStartShouldSetPanResponder: () => !disabled,
72
+ onMoveShouldSetPanResponder: (_event, gesture) => !disabled && Math.abs(gesture.dx) > Math.abs(gesture.dy),
73
+ onPanResponderGrant: (event) => {
74
+ const next = valueAtX(event.nativeEvent.locationX);
75
+ if (next !== null)
76
+ report(next);
77
+ },
78
+ onPanResponderMove: (event) => {
79
+ const next = valueAtX(event.nativeEvent.locationX);
80
+ if (next !== null)
81
+ report(next);
82
+ },
83
+ onPanResponderTerminationRequest: () => false,
84
+ });
85
+ // `report` reads the value from a ref, so the responder does not need to be
86
+ // rebuilt on every value change and never acts on a stale render.
87
+ }, [disabled, valueAtX]);
88
+ /** Steps the value for accessibility actions and hardware keys. */
89
+ const nudge = (direction) => {
90
+ if (disabled)
91
+ return;
92
+ const from = valueRef.current;
93
+ report(quantize(from + direction * Math.max(step, (max - min) / 20), min, max, step));
94
+ };
95
+ const percentage = max === min ? 0 : ((value - min) / (max - min)) * 100;
20
96
  const containerStyle = {
21
- position: "relative",
22
- height: 40,
97
+ height: CONTAINER_HEIGHT,
23
98
  justifyContent: "center",
24
99
  opacity: disabled ? 0.38 : 1,
25
100
  ...style,
26
101
  };
27
102
  const trackStyle = {
28
- height: 8,
29
- borderRadius: 4,
103
+ height: TRACK_HEIGHT,
104
+ borderRadius: TRACK_HEIGHT / 2,
30
105
  backgroundColor: "rgba(120, 120, 128, 0.2)",
31
106
  overflow: "hidden",
32
107
  width: "100%",
33
108
  };
34
109
  const fillStyle = {
35
110
  height: "100%",
36
- width: `${percentage}%`,
111
+ width: `${Math.min(100, Math.max(0, percentage))}%`,
37
112
  backgroundColor: themeColors.fill,
38
113
  };
39
- return ((0, jsx_runtime_1.jsxs)("div", { ref: ref, style: containerStyle, ...props, children: [(0, jsx_runtime_1.jsx)(Glass_1.Glass, { effect: "regular", style: trackStyle, children: (0, jsx_runtime_1.jsx)("div", { style: fillStyle }) }), (0, jsx_runtime_1.jsx)("input", { type: "range", min: min, max: max, step: step, value: value, disabled: disabled, "aria-label": accessibilityLabel, onChange: handleChange, style: {
40
- position: "absolute",
41
- top: 0,
42
- left: 0,
43
- width: "100%",
44
- height: "100%",
45
- opacity: 0,
46
- cursor: disabled ? "default" : "pointer",
47
- margin: 0,
48
- } })] }));
114
+ const thumbStyle = {
115
+ position: "absolute",
116
+ top: (CONTAINER_HEIGHT - THUMB) / 2,
117
+ left: `${Math.min(100, Math.max(0, percentage))}%`,
118
+ width: THUMB,
119
+ height: THUMB,
120
+ marginLeft: -THUMB / 2,
121
+ borderRadius: THUMB / 2,
122
+ backgroundColor: "#FFFFFF",
123
+ borderWidth: 0.5,
124
+ borderColor: "rgba(0, 0, 0, 0.12)",
125
+ };
126
+ return ((0, jsx_runtime_1.jsxs)(Pressable, { testID: testID, accessibilityRole: "adjustable", accessibilityLabel: accessibilityLabel, accessibilityState: { disabled }, accessibilityValue: { min, max, now: value }, accessibilityActions: [{ name: "increment" }, { name: "decrement" }], onAccessibilityAction: (event) => {
127
+ if (event?.nativeEvent?.actionName === "increment")
128
+ nudge(1);
129
+ if (event?.nativeEvent?.actionName === "decrement")
130
+ nudge(-1);
131
+ }, disabled: disabled, onLayout: (event) => {
132
+ const width = event?.nativeEvent?.layout?.width;
133
+ if (typeof width === "number" && width > 0)
134
+ widthRef.current = width;
135
+ },
136
+ // The DOM preview has no layout events, so measure on mount there too;
137
+ // on a device this resolves to nothing and onLayout is the source.
138
+ ref: (node) => {
139
+ (0, primitives_1.measurePreviewWidth)(node, widthRef);
140
+ if (typeof ref === "function")
141
+ ref(node);
142
+ else if (ref)
143
+ ref.current = node;
144
+ }, style: containerStyle, ...(panResponder ? panResponder.panHandlers : {}), ...props, children: [(0, jsx_runtime_1.jsx)(Glass_1.Glass, { effect: "regular", isDark: isDark, style: trackStyle, children: (0, jsx_runtime_1.jsx)(View, { style: fillStyle }) }), (0, jsx_runtime_1.jsx)(View, { style: thumbStyle, pointerEvents: "none" })] }));
49
145
  });
@@ -5,38 +5,56 @@ const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const react_1 = require("react");
6
6
  const Glass_1 = require("./Glass");
7
7
  const tokens_1 = require("./internal/tokens");
8
- exports.Switch = (0, react_1.forwardRef)(function Switch({ checked: checkedProp, defaultChecked = false, onCheckedChange, disabled = false, accessibilityLabel, style, isDark = false, ...props }, ref) {
8
+ const primitives_1 = require("./internal/primitives");
9
+ const motion_1 = require("./internal/motion");
10
+ const TRACK_WIDTH = 51;
11
+ const TRACK_HEIGHT = 31;
12
+ const THUMB = 27;
13
+ const TRAVEL = TRACK_WIDTH - THUMB - 4;
14
+ /**
15
+ * An on/off switch.
16
+ *
17
+ * The track is Glacia's glass; the thumb is one of our own Views, animated on
18
+ * the springs token. The material changes by platform (Apple's glass on
19
+ * iOS 26+, Glacia's tint elsewhere) but the control is always ours — React
20
+ * Native's built-in Switch is never used.
21
+ */
22
+ exports.Switch = (0, react_1.forwardRef)(function Switch({ checked: checkedProp, defaultChecked = false, onCheckedChange, disabled = false, accessibilityLabel, style, isDark: isDarkProp, testID, ...props }, ref) {
23
+ const isDark = (0, motion_1.useIsDark)(isDarkProp);
24
+ const themeColors = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
25
+ const { View, Pressable, Animated } = (0, primitives_1.primitives)();
9
26
  const [internalChecked, setInternalChecked] = (0, react_1.useState)(defaultChecked);
10
27
  const isChecked = checkedProp !== undefined ? checkedProp : internalChecked;
11
- const themeColors = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
28
+ // The thumb's travel is a single number animated on the springs token.
29
+ const { value: offset, animated } = (0, motion_1.useSpring)(isChecked ? TRAVEL : 0, "snappy");
12
30
  const toggle = () => {
13
31
  if (disabled)
14
32
  return;
15
33
  const next = !isChecked;
16
- if (checkedProp === undefined) {
34
+ if (checkedProp === undefined)
17
35
  setInternalChecked(next);
18
- }
19
36
  onCheckedChange?.(next);
20
37
  };
21
38
  const trackStyle = {
22
- width: 51,
23
- height: 31,
24
- borderRadius: 15.5,
39
+ width: TRACK_WIDTH,
40
+ height: TRACK_HEIGHT,
41
+ borderRadius: TRACK_HEIGHT / 2,
25
42
  backgroundColor: isChecked ? themeColors.fill : "rgba(120, 120, 128, 0.16)",
26
43
  padding: 2,
27
44
  justifyContent: "center",
28
45
  opacity: disabled ? 0.38 : 1,
29
- transition: "background-color 200ms ease",
30
46
  ...style,
31
47
  };
32
48
  const thumbStyle = {
33
- width: 27,
34
- height: 27,
35
- borderRadius: 13.5,
49
+ width: THUMB,
50
+ height: THUMB,
51
+ borderRadius: THUMB / 2,
36
52
  backgroundColor: "#FFFFFF",
37
- boxShadow: "0 2px 4px rgba(0, 0, 0, 0.2)",
38
- transform: isChecked ? "translateX(20px)" : "translateX(0px)",
39
- transition: "transform 200ms cubic-bezier(0.25, 0.1, 0.25, 1)",
53
+ // A hairline edge instead of a drop shadow: RN shadows need per-platform
54
+ // props, and the edge reads correctly on a translucent track.
55
+ borderWidth: 0.5,
56
+ borderColor: "rgba(0, 0, 0, 0.12)",
57
+ transform: [{ translateX: animated ? offset : isChecked ? TRAVEL : 0 }],
40
58
  };
41
- return ((0, jsx_runtime_1.jsx)("div", { ref: ref, role: "switch", "aria-checked": isChecked, "aria-label": accessibilityLabel, "aria-disabled": disabled, tabIndex: disabled ? -1 : 0, onClick: toggle, style: { display: "inline-block", cursor: disabled ? "default" : "pointer" }, ...props, children: (0, jsx_runtime_1.jsx)(Glass_1.Glass, { effect: "regular", style: trackStyle, children: (0, jsx_runtime_1.jsx)("div", { style: thumbStyle }) }) }));
59
+ return ((0, jsx_runtime_1.jsx)(Pressable, { ref: ref, testID: testID, accessibilityRole: "switch", accessibilityLabel: accessibilityLabel, accessibilityState: { checked: isChecked, disabled }, disabled: disabled, onPress: toggle, style: { alignSelf: "flex-start" }, ...props, children: (0, jsx_runtime_1.jsx)(Glass_1.Glass, { effect: "regular", isDark: isDark, style: trackStyle, children: animated ? (0, jsx_runtime_1.jsx)(Animated.View, { style: thumbStyle }) : (0, jsx_runtime_1.jsx)(View, { style: thumbStyle }) }) }));
42
60
  });