glacia-native 0.3.4 → 0.4.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.
- package/lib/module/Button.js +15 -2
- package/lib/module/Card.js +29 -14
- package/lib/module/Dialog.js +43 -32
- package/lib/module/Form.js +51 -12
- package/lib/module/Glass.js +11 -2
- package/lib/module/Input.js +19 -7
- package/lib/module/SegmentedControl.js +65 -21
- package/lib/module/Slider.js +121 -25
- package/lib/module/Switch.js +33 -15
- package/lib/module/TabBar.js +63 -25
- package/lib/module/Table.js +24 -6
- package/lib/module/Tabs.js +55 -33
- package/lib/module/ai.js +49 -20
- package/lib/module/charts/BarChart.js +4 -2
- package/lib/module/charts/FunnelChart.js +4 -2
- package/lib/module/charts/HeatmapChart.js +4 -2
- package/lib/module/charts/LineChart.js +4 -2
- package/lib/module/charts/PlotChart.js +11 -3
- package/lib/module/charts/RadarChart.js +4 -2
- package/lib/module/charts/RingChart.js +4 -2
- package/lib/module/charts/ScatterChart.js +20 -3
- package/lib/module/charts/WaterfallChart.js +12 -3
- package/lib/module/charts/advanced.js +12 -6
- package/lib/module/charts/extra.js +6 -3
- package/lib/module/internal/GlassView.js +17 -19
- package/lib/module/internal/fallback.js +14 -3
- package/lib/module/internal/glass-routing.js +56 -0
- package/lib/module/internal/motion.js +55 -0
- package/lib/module/internal/primitives.js +408 -0
- package/lib/typescript/Button.d.ts +11 -1
- package/lib/typescript/Card.d.ts +20 -8
- package/lib/typescript/Dialog.d.ts +19 -6
- package/lib/typescript/Form.d.ts +27 -11
- package/lib/typescript/Glass.d.ts +9 -0
- package/lib/typescript/Input.d.ts +11 -1
- package/lib/typescript/SegmentedControl.d.ts +11 -1
- package/lib/typescript/Slider.d.ts +13 -1
- package/lib/typescript/Switch.d.ts +12 -1
- package/lib/typescript/TabBar.d.ts +13 -1
- package/lib/typescript/Table.d.ts +14 -5
- package/lib/typescript/Tabs.d.ts +8 -5
- package/lib/typescript/ai.d.ts +12 -9
- package/lib/typescript/charts/BarChart.d.ts +1 -1
- package/lib/typescript/charts/FunnelChart.d.ts +1 -1
- package/lib/typescript/charts/HeatmapChart.d.ts +1 -1
- package/lib/typescript/charts/LineChart.d.ts +1 -1
- package/lib/typescript/charts/PlotChart.d.ts +1 -1
- package/lib/typescript/charts/RadarChart.d.ts +1 -1
- package/lib/typescript/charts/RingChart.d.ts +1 -1
- package/lib/typescript/charts/ScatterChart.d.ts +1 -1
- package/lib/typescript/charts/WaterfallChart.d.ts +1 -1
- package/lib/typescript/charts/advanced.d.ts +5 -5
- package/lib/typescript/charts/extra.d.ts +2 -2
- package/lib/typescript/internal/GlassView.d.ts +11 -3
- package/lib/typescript/internal/fallback.d.ts +10 -1
- package/lib/typescript/internal/glass-routing.d.ts +30 -0
- package/lib/typescript/internal/motion.d.ts +19 -0
- package/lib/typescript/internal/primitives.d.ts +41 -0
- package/package.json +2 -2
- package/src/Button.tsx +47 -22
- package/src/Card.tsx +52 -31
- package/src/Dialog.tsx +99 -64
- package/src/Form.tsx +90 -25
- package/src/Glass.tsx +24 -1
- package/src/Input.tsx +33 -12
- package/src/SegmentedControl.tsx +104 -32
- package/src/Slider.tsx +151 -47
- package/src/Switch.tsx +56 -32
- package/src/TabBar.tsx +100 -30
- package/src/Table.tsx +38 -14
- package/src/Tabs.tsx +85 -32
- package/src/ai.tsx +77 -31
- package/src/charts/BarChart.tsx +4 -2
- package/src/charts/FunnelChart.tsx +4 -2
- package/src/charts/HeatmapChart.tsx +4 -2
- package/src/charts/LineChart.tsx +4 -2
- package/src/charts/PlotChart.tsx +23 -3
- package/src/charts/RadarChart.tsx +4 -2
- package/src/charts/RingChart.tsx +4 -2
- package/src/charts/ScatterChart.tsx +24 -3
- package/src/charts/WaterfallChart.tsx +21 -3
- package/src/charts/advanced.tsx +12 -6
- package/src/charts/extra.tsx +6 -3
- package/src/internal/GlassView.tsx +31 -21
- package/src/internal/fallback.tsx +23 -8
- package/src/internal/glass-routing.ts +68 -0
- package/src/internal/motion.ts +60 -0
- package/src/internal/primitives.ts +477 -0
- package/lib/module/charts/primitives.js +0 -212
- package/lib/typescript/charts/primitives.d.ts +0 -23
- package/src/charts/primitives.ts +0 -262
package/src/Form.tsx
CHANGED
|
@@ -1,38 +1,44 @@
|
|
|
1
1
|
import { forwardRef, useState, type ReactNode } from "react";
|
|
2
|
+
import type { StyleProp, TextStyle, ViewStyle } from "react-native";
|
|
2
3
|
|
|
3
4
|
import { colors, radii } from "./internal/tokens";
|
|
4
|
-
import { primitives } from "./
|
|
5
|
+
import { primitives } from "./internal/primitives";
|
|
6
|
+
import { useIsDark } from "./internal/motion";
|
|
5
7
|
|
|
6
8
|
export interface LabelProps {
|
|
7
9
|
children?: ReactNode;
|
|
8
10
|
/** Marks the field required with a visible asterisk. */
|
|
9
11
|
required?: boolean;
|
|
10
12
|
disabled?: boolean;
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
+
/**
|
|
14
|
+
* The id of the control this label describes. Same meaning and prop name as
|
|
15
|
+
* React Native's `nativeID`.
|
|
16
|
+
*/
|
|
17
|
+
nativeID?: string;
|
|
13
18
|
isDark?: boolean;
|
|
14
|
-
style?:
|
|
19
|
+
style?: StyleProp<TextStyle>;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
/** The text above a control. */
|
|
18
23
|
export const Label = forwardRef<any, LabelProps>(function Label(
|
|
19
|
-
{ children, required = false, disabled = false,
|
|
24
|
+
{ children, required = false, disabled = false, nativeID, isDark: isDarkProp, style },
|
|
20
25
|
ref,
|
|
21
26
|
) {
|
|
27
|
+
const isDark = useIsDark(isDarkProp);
|
|
22
28
|
const theme = isDark ? colors.dark : colors.light;
|
|
23
29
|
const { Text } = primitives();
|
|
24
30
|
|
|
25
31
|
return (
|
|
26
32
|
<Text
|
|
27
33
|
ref={ref}
|
|
28
|
-
nativeID={
|
|
34
|
+
nativeID={nativeID}
|
|
29
35
|
accessibilityRole="text"
|
|
30
36
|
style={{
|
|
31
37
|
fontSize: 13,
|
|
32
38
|
fontWeight: "600",
|
|
33
39
|
color: disabled ? theme.muted : theme.foreground,
|
|
34
40
|
opacity: disabled ? 0.6 : 1,
|
|
35
|
-
...style,
|
|
41
|
+
...(style as TextStyle),
|
|
36
42
|
}}
|
|
37
43
|
>
|
|
38
44
|
{children}
|
|
@@ -51,7 +57,7 @@ export interface FieldProps {
|
|
|
51
57
|
disabled?: boolean;
|
|
52
58
|
isDark?: boolean;
|
|
53
59
|
children?: ReactNode;
|
|
54
|
-
style?:
|
|
60
|
+
style?: StyleProp<ViewStyle>;
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
/**
|
|
@@ -64,16 +70,17 @@ export function Field({
|
|
|
64
70
|
error,
|
|
65
71
|
required = false,
|
|
66
72
|
disabled = false,
|
|
67
|
-
isDark
|
|
73
|
+
isDark: isDarkProp,
|
|
68
74
|
children,
|
|
69
75
|
style,
|
|
70
76
|
}: FieldProps) {
|
|
77
|
+
const isDark = useIsDark(isDarkProp);
|
|
71
78
|
const theme = isDark ? colors.dark : colors.light;
|
|
72
79
|
const { View, Text } = primitives();
|
|
73
80
|
const message = error ?? description;
|
|
74
81
|
|
|
75
82
|
return (
|
|
76
|
-
<View style={{ gap: 6, ...style }}>
|
|
83
|
+
<View style={{ gap: 6, ...(style as ViewStyle) }}>
|
|
77
84
|
{label && (
|
|
78
85
|
<Label required={required} disabled={disabled} isDark={isDark}>
|
|
79
86
|
{label}
|
|
@@ -99,44 +106,63 @@ export function Field({
|
|
|
99
106
|
|
|
100
107
|
export interface CheckboxProps {
|
|
101
108
|
checked?: boolean;
|
|
109
|
+
/** Starting state for uncontrolled use. */
|
|
110
|
+
defaultChecked?: boolean;
|
|
102
111
|
onCheckedChange?: (checked: boolean) => void;
|
|
103
112
|
label?: string;
|
|
104
113
|
description?: string;
|
|
105
114
|
indeterminate?: boolean;
|
|
106
115
|
disabled?: boolean;
|
|
107
116
|
isDark?: boolean;
|
|
108
|
-
style?:
|
|
117
|
+
style?: StyleProp<ViewStyle>;
|
|
118
|
+
/** Passed through to the pressable row so it is testable. */
|
|
119
|
+
testID?: string;
|
|
109
120
|
}
|
|
110
121
|
|
|
111
122
|
/** A tick box for a single boolean, with an optional label and description. */
|
|
112
123
|
export function Checkbox({
|
|
113
|
-
checked
|
|
124
|
+
checked: checkedProp,
|
|
125
|
+
defaultChecked = false,
|
|
114
126
|
onCheckedChange,
|
|
115
127
|
label,
|
|
116
128
|
description,
|
|
117
129
|
indeterminate = false,
|
|
118
130
|
disabled = false,
|
|
119
|
-
isDark
|
|
131
|
+
isDark: isDarkProp,
|
|
120
132
|
style,
|
|
133
|
+
testID,
|
|
121
134
|
}: CheckboxProps) {
|
|
135
|
+
const isDark = useIsDark(isDarkProp);
|
|
122
136
|
const theme = isDark ? colors.dark : colors.light;
|
|
123
137
|
const { View, Text, Pressable } = primitives();
|
|
124
138
|
|
|
139
|
+
// Controlled when `checked` is given, otherwise the box owns its state.
|
|
140
|
+
const [internalChecked, setInternalChecked] = useState(defaultChecked);
|
|
141
|
+
const checked = checkedProp !== undefined ? checkedProp : internalChecked;
|
|
142
|
+
|
|
143
|
+
const toggle = () => {
|
|
144
|
+
if (disabled) return;
|
|
145
|
+
const next = !checked;
|
|
146
|
+
if (checkedProp === undefined) setInternalChecked(next);
|
|
147
|
+
onCheckedChange?.(next);
|
|
148
|
+
};
|
|
149
|
+
|
|
125
150
|
const filled = checked || indeterminate;
|
|
126
151
|
|
|
127
152
|
return (
|
|
128
153
|
<Pressable
|
|
154
|
+
testID={testID}
|
|
129
155
|
accessibilityRole="checkbox"
|
|
130
156
|
accessibilityState={{ checked, disabled }}
|
|
131
157
|
accessibilityLabel={label}
|
|
132
158
|
disabled={disabled}
|
|
133
|
-
onPress={
|
|
159
|
+
onPress={toggle}
|
|
134
160
|
style={{
|
|
135
161
|
flexDirection: "row",
|
|
136
162
|
alignItems: "flex-start",
|
|
137
163
|
gap: 10,
|
|
138
164
|
opacity: disabled ? 0.45 : 1,
|
|
139
|
-
...style,
|
|
165
|
+
...(style as ViewStyle),
|
|
140
166
|
}}
|
|
141
167
|
>
|
|
142
168
|
<View
|
|
@@ -185,32 +211,54 @@ export interface RadioOption {
|
|
|
185
211
|
|
|
186
212
|
export interface RadioGroupProps {
|
|
187
213
|
value?: string;
|
|
214
|
+
/** Starting selection for uncontrolled use. */
|
|
215
|
+
defaultValue?: string;
|
|
188
216
|
onValueChange?: (value: string) => void;
|
|
189
217
|
options: RadioOption[];
|
|
190
218
|
/** Lay the options out in a row instead of a column. */
|
|
191
219
|
horizontal?: boolean;
|
|
192
220
|
disabled?: boolean;
|
|
193
221
|
isDark?: boolean;
|
|
194
|
-
style?:
|
|
222
|
+
style?: StyleProp<ViewStyle>;
|
|
223
|
+
/** Passed through to the group so it is testable. */
|
|
224
|
+
testID?: string;
|
|
195
225
|
}
|
|
196
226
|
|
|
197
227
|
/** One choice from a small, always-visible set. */
|
|
198
228
|
export function RadioGroup({
|
|
199
|
-
value,
|
|
229
|
+
value: valueProp,
|
|
230
|
+
defaultValue,
|
|
200
231
|
onValueChange,
|
|
201
232
|
options,
|
|
202
233
|
horizontal = false,
|
|
203
234
|
disabled = false,
|
|
204
|
-
isDark
|
|
235
|
+
isDark: isDarkProp,
|
|
205
236
|
style,
|
|
237
|
+
testID,
|
|
206
238
|
}: RadioGroupProps) {
|
|
239
|
+
const isDark = useIsDark(isDarkProp);
|
|
207
240
|
const theme = isDark ? colors.dark : colors.light;
|
|
208
241
|
const { View, Text, Pressable } = primitives();
|
|
209
242
|
|
|
243
|
+
// Controlled when `value` is given, otherwise the group owns its selection.
|
|
244
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
245
|
+
const value = valueProp !== undefined ? valueProp : internalValue;
|
|
246
|
+
|
|
247
|
+
const select = (next: string, isDisabled: boolean | undefined) => {
|
|
248
|
+
if (isDisabled) return;
|
|
249
|
+
if (valueProp === undefined) setInternalValue(next);
|
|
250
|
+
onValueChange?.(next);
|
|
251
|
+
};
|
|
252
|
+
|
|
210
253
|
return (
|
|
211
254
|
<View
|
|
255
|
+
testID={testID}
|
|
212
256
|
accessibilityRole="radiogroup"
|
|
213
|
-
style={{
|
|
257
|
+
style={{
|
|
258
|
+
gap: horizontal ? 14 : 10,
|
|
259
|
+
flexDirection: horizontal ? "row" : "column",
|
|
260
|
+
...(style as ViewStyle),
|
|
261
|
+
}}
|
|
214
262
|
>
|
|
215
263
|
{options.map((option) => {
|
|
216
264
|
const isSelected = option.value === value;
|
|
@@ -223,7 +271,7 @@ export function RadioGroup({
|
|
|
223
271
|
accessibilityState={{ checked: isSelected, disabled: isDisabled }}
|
|
224
272
|
accessibilityLabel={option.label}
|
|
225
273
|
disabled={isDisabled}
|
|
226
|
-
onPress={() =>
|
|
274
|
+
onPress={() => select(option.value, isDisabled)}
|
|
227
275
|
style={{
|
|
228
276
|
flexDirection: "row",
|
|
229
277
|
alignItems: "flex-start",
|
|
@@ -263,12 +311,16 @@ export interface SelectOption {
|
|
|
263
311
|
|
|
264
312
|
export interface SelectProps {
|
|
265
313
|
value?: string;
|
|
314
|
+
/** Starting selection for uncontrolled use. */
|
|
315
|
+
defaultValue?: string;
|
|
266
316
|
onValueChange?: (value: string) => void;
|
|
267
317
|
options: SelectOption[];
|
|
268
318
|
placeholder?: string;
|
|
269
319
|
disabled?: boolean;
|
|
270
320
|
isDark?: boolean;
|
|
271
|
-
style?:
|
|
321
|
+
style?: StyleProp<ViewStyle>;
|
|
322
|
+
/** Passed through to the trigger so it is testable. */
|
|
323
|
+
testID?: string;
|
|
272
324
|
}
|
|
273
325
|
|
|
274
326
|
/**
|
|
@@ -279,23 +331,36 @@ export interface SelectProps {
|
|
|
279
331
|
* modal. A long list scrolls naturally with the page.
|
|
280
332
|
*/
|
|
281
333
|
export function Select({
|
|
282
|
-
value,
|
|
334
|
+
value: valueProp,
|
|
335
|
+
defaultValue,
|
|
283
336
|
onValueChange,
|
|
284
337
|
options,
|
|
285
338
|
placeholder = "Select an option",
|
|
286
339
|
disabled = false,
|
|
287
|
-
isDark
|
|
340
|
+
isDark: isDarkProp,
|
|
288
341
|
style,
|
|
342
|
+
testID,
|
|
289
343
|
}: SelectProps) {
|
|
344
|
+
const isDark = useIsDark(isDarkProp);
|
|
290
345
|
const theme = isDark ? colors.dark : colors.light;
|
|
291
346
|
const { View, Text, Pressable } = primitives();
|
|
292
347
|
const [open, setOpen] = useState(false);
|
|
293
348
|
|
|
349
|
+
// Controlled when `value` is given, otherwise the select owns its choice.
|
|
350
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
351
|
+
const value = valueProp !== undefined ? valueProp : internalValue;
|
|
352
|
+
|
|
353
|
+
const choose = (next: string) => {
|
|
354
|
+
if (valueProp === undefined) setInternalValue(next);
|
|
355
|
+
onValueChange?.(next);
|
|
356
|
+
};
|
|
357
|
+
|
|
294
358
|
const selected = options.find((option) => option.value === value);
|
|
295
359
|
|
|
296
360
|
return (
|
|
297
|
-
<View style={style}>
|
|
361
|
+
<View style={style as StyleProp<ViewStyle>}>
|
|
298
362
|
<Pressable
|
|
363
|
+
testID={testID}
|
|
299
364
|
accessibilityRole="button"
|
|
300
365
|
accessibilityState={{ expanded: open, disabled }}
|
|
301
366
|
accessibilityLabel={selected?.label ?? placeholder}
|
|
@@ -340,7 +405,7 @@ export function Select({
|
|
|
340
405
|
accessibilityState={{ selected: isSelected }}
|
|
341
406
|
accessibilityLabel={option.label}
|
|
342
407
|
onPress={() => {
|
|
343
|
-
|
|
408
|
+
choose(option.value);
|
|
344
409
|
setOpen(false);
|
|
345
410
|
}}
|
|
346
411
|
style={{
|
package/src/Glass.tsx
CHANGED
|
@@ -1,20 +1,43 @@
|
|
|
1
1
|
import { forwardRef } from "react";
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
|
|
2
4
|
import { GlassView, type NativeGlassProps } from "./internal/GlassView";
|
|
5
|
+
import { useIsDark } from "./internal/motion";
|
|
3
6
|
|
|
4
7
|
export interface GlassProps extends NativeGlassProps {
|
|
5
8
|
testID?: string;
|
|
9
|
+
style?: StyleProp<ViewStyle>;
|
|
6
10
|
}
|
|
7
11
|
|
|
12
|
+
/**
|
|
13
|
+
* A glass surface.
|
|
14
|
+
*
|
|
15
|
+
* Routes through the package's single switch: Apple's real `UIGlassEffect` on
|
|
16
|
+
* iOS 26+, Glacia's translucent fallback of identical shape on older iOS and
|
|
17
|
+
* Android. `testID` is passed to whichever surface renders.
|
|
18
|
+
*/
|
|
8
19
|
export const Glass = forwardRef<any, GlassProps>(function Glass(
|
|
9
|
-
{
|
|
20
|
+
{
|
|
21
|
+
effect = "regular",
|
|
22
|
+
tintColor,
|
|
23
|
+
interactive = false,
|
|
24
|
+
testID,
|
|
25
|
+
style,
|
|
26
|
+
children,
|
|
27
|
+
isDark: isDarkProp,
|
|
28
|
+
...props
|
|
29
|
+
},
|
|
10
30
|
ref,
|
|
11
31
|
) {
|
|
32
|
+
const isDark = useIsDark(isDarkProp);
|
|
12
33
|
return (
|
|
13
34
|
<GlassView
|
|
14
35
|
ref={ref}
|
|
15
36
|
effect={effect}
|
|
16
37
|
tintColor={tintColor}
|
|
17
38
|
interactive={interactive}
|
|
39
|
+
testID={testID}
|
|
40
|
+
isDark={isDark}
|
|
18
41
|
style={style}
|
|
19
42
|
{...props}
|
|
20
43
|
>
|
package/src/Input.tsx
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { forwardRef, type ReactNode } from "react";
|
|
1
|
+
import { forwardRef, useState, type ReactNode } from "react";
|
|
2
|
+
import type { StyleProp, TextStyle, ViewStyle } from "react-native";
|
|
2
3
|
|
|
3
4
|
import { colors, radii } from "./internal/tokens";
|
|
4
|
-
import { primitives } from "./
|
|
5
|
+
import { primitives } from "./internal/primitives";
|
|
6
|
+
import { useIsDark } from "./internal/motion";
|
|
5
7
|
|
|
6
8
|
export interface InputProps {
|
|
7
9
|
value?: string;
|
|
@@ -21,10 +23,14 @@ export interface InputProps {
|
|
|
21
23
|
accessibilityLabel?: string;
|
|
22
24
|
returnKeyType?: "done" | "next" | "go" | "search" | "send";
|
|
23
25
|
onSubmitEditing?: () => void;
|
|
26
|
+
/** Starting text for uncontrolled use, like React Native's `defaultValue`. */
|
|
27
|
+
defaultValue?: string;
|
|
24
28
|
prefix?: ReactNode;
|
|
25
29
|
suffix?: ReactNode;
|
|
26
30
|
isDark?: boolean;
|
|
27
|
-
style?:
|
|
31
|
+
style?: StyleProp<ViewStyle>;
|
|
32
|
+
/** Passed through to the text field so it is testable. */
|
|
33
|
+
testID?: string;
|
|
28
34
|
// Standard TextInput props a caller may still want to pass through.
|
|
29
35
|
maxLength?: number;
|
|
30
36
|
editable?: boolean;
|
|
@@ -41,6 +47,11 @@ const fontSizes = { sm: 14, md: 15, lg: 16 };
|
|
|
41
47
|
* The native TextInput has no placeholder styling prop, so the placeholder is
|
|
42
48
|
* drawn as a sibling Text: that keeps its colour tied to the theme instead of
|
|
43
49
|
* the OS default, which changes between iOS versions.
|
|
50
|
+
*
|
|
51
|
+
* The field works controlled (`value` + `onChangeText`) or uncontrolled (no
|
|
52
|
+
* `value` prop). In uncontrolled use it tracks its own text so the themed
|
|
53
|
+
* placeholder disappears as soon as the user types; without that state the
|
|
54
|
+
* overlay would sit on top of the typed text until the parent re-rendered.
|
|
44
55
|
*/
|
|
45
56
|
export const Input = forwardRef<any, InputProps>(function Input(
|
|
46
57
|
{
|
|
@@ -59,10 +70,12 @@ export const Input = forwardRef<any, InputProps>(function Input(
|
|
|
59
70
|
accessibilityLabel,
|
|
60
71
|
returnKeyType,
|
|
61
72
|
onSubmitEditing,
|
|
73
|
+
defaultValue,
|
|
62
74
|
prefix,
|
|
63
75
|
suffix,
|
|
64
|
-
isDark
|
|
76
|
+
isDark: isDarkProp,
|
|
65
77
|
style,
|
|
78
|
+
testID,
|
|
66
79
|
maxLength,
|
|
67
80
|
editable,
|
|
68
81
|
onFocus,
|
|
@@ -70,20 +83,27 @@ export const Input = forwardRef<any, InputProps>(function Input(
|
|
|
70
83
|
},
|
|
71
84
|
ref,
|
|
72
85
|
) {
|
|
86
|
+
const isDark = useIsDark(isDarkProp);
|
|
73
87
|
const theme = isDark ? colors.dark : colors.light;
|
|
74
88
|
const { View, Text, TextInput } = primitives();
|
|
75
89
|
|
|
90
|
+
// Uncontrolled support: with no `value` prop the field owns its text.
|
|
91
|
+
const [internalText, setInternalText] = useState(defaultValue ?? "");
|
|
92
|
+
const text = value !== undefined ? value : internalText;
|
|
93
|
+
const handleChangeText = (next: string) => {
|
|
94
|
+
if (value === undefined) setInternalText(next);
|
|
95
|
+
onChangeText?.(next);
|
|
96
|
+
};
|
|
97
|
+
|
|
76
98
|
const isEditable = editable !== undefined ? editable : !disabled;
|
|
77
|
-
const showPlaceholder = !
|
|
99
|
+
const showPlaceholder = !text && Boolean(placeholder);
|
|
78
100
|
|
|
79
|
-
const fieldStyle:
|
|
101
|
+
const fieldStyle: TextStyle = {
|
|
80
102
|
flex: 1,
|
|
81
103
|
paddingVertical: height_padding(size),
|
|
82
104
|
paddingHorizontal: 0,
|
|
83
105
|
fontSize: fontSizes[size],
|
|
84
106
|
color: theme.foreground,
|
|
85
|
-
// RN draws its own underline on Android; the border is ours.
|
|
86
|
-
outlineStyle: "none",
|
|
87
107
|
};
|
|
88
108
|
|
|
89
109
|
return (
|
|
@@ -99,7 +119,7 @@ export const Input = forwardRef<any, InputProps>(function Input(
|
|
|
99
119
|
backgroundColor: theme.glass,
|
|
100
120
|
paddingHorizontal: 12,
|
|
101
121
|
opacity: disabled ? 0.5 : 1,
|
|
102
|
-
...style,
|
|
122
|
+
...(style as ViewStyle),
|
|
103
123
|
}}
|
|
104
124
|
>
|
|
105
125
|
{prefix}
|
|
@@ -113,8 +133,8 @@ export const Input = forwardRef<any, InputProps>(function Input(
|
|
|
113
133
|
top: multiline ? 0 : undefined,
|
|
114
134
|
fontSize: fontSizes[size],
|
|
115
135
|
color: theme.muted,
|
|
116
|
-
pointerEvents: "none",
|
|
117
136
|
}}
|
|
137
|
+
pointerEvents="none"
|
|
118
138
|
numberOfLines={1}
|
|
119
139
|
>
|
|
120
140
|
{placeholder}
|
|
@@ -123,8 +143,9 @@ export const Input = forwardRef<any, InputProps>(function Input(
|
|
|
123
143
|
|
|
124
144
|
<TextInput
|
|
125
145
|
ref={ref}
|
|
126
|
-
|
|
127
|
-
|
|
146
|
+
testID={testID}
|
|
147
|
+
value={text}
|
|
148
|
+
onChangeText={handleChangeText}
|
|
128
149
|
editable={isEditable}
|
|
129
150
|
secureTextEntry={secureTextEntry}
|
|
130
151
|
keyboardType={keyboardType}
|
package/src/SegmentedControl.tsx
CHANGED
|
@@ -1,16 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { forwardRef, useState } from "react";
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
|
|
2
4
|
import { Glass } from "./Glass";
|
|
3
5
|
import { colors, radii } from "./internal/tokens";
|
|
6
|
+
import { primitives } from "./internal/primitives";
|
|
7
|
+
import { useIsDark, useSpring } from "./internal/motion";
|
|
4
8
|
|
|
5
9
|
export interface SegmentedControlProps {
|
|
6
10
|
values: string[];
|
|
7
11
|
selectedIndex?: number;
|
|
8
12
|
defaultIndex?: number;
|
|
9
13
|
onValueChange?: (value: string, index: number) => void;
|
|
10
|
-
style?:
|
|
14
|
+
style?: StyleProp<ViewStyle>;
|
|
11
15
|
isDark?: boolean;
|
|
16
|
+
/** Passed through to the control so it is testable. */
|
|
17
|
+
testID?: string;
|
|
12
18
|
}
|
|
13
19
|
|
|
20
|
+
const SEGMENT_HEIGHT = 30;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A row of mutually exclusive segments on one glass container.
|
|
24
|
+
*
|
|
25
|
+
* The selected segment is marked by a pill that slides on the springs token.
|
|
26
|
+
* The pill's colours come from the theme tokens, so it reads correctly in both
|
|
27
|
+
* light and dark instead of assuming a white-on-light palette.
|
|
28
|
+
*/
|
|
14
29
|
export const SegmentedControl = forwardRef<any, SegmentedControlProps>(function SegmentedControl(
|
|
15
30
|
{
|
|
16
31
|
values,
|
|
@@ -18,60 +33,117 @@ export const SegmentedControl = forwardRef<any, SegmentedControlProps>(function
|
|
|
18
33
|
defaultIndex = 0,
|
|
19
34
|
onValueChange,
|
|
20
35
|
style,
|
|
21
|
-
isDark
|
|
36
|
+
isDark: isDarkProp,
|
|
37
|
+
testID,
|
|
22
38
|
...props
|
|
23
39
|
},
|
|
24
40
|
ref,
|
|
25
41
|
) {
|
|
42
|
+
const isDark = useIsDark(isDarkProp);
|
|
43
|
+
const themeColors = isDark ? colors.dark : colors.light;
|
|
44
|
+
const { View, Text, Pressable, Animated } = primitives();
|
|
45
|
+
|
|
26
46
|
const [internalIndex, setInternalIndex] = useState(defaultIndex);
|
|
27
47
|
const activeIndex = selectedProp !== undefined ? selectedProp : internalIndex;
|
|
28
|
-
const
|
|
48
|
+
const safeIndex = Math.min(Math.max(activeIndex, 0), Math.max(values.length - 1, 0));
|
|
49
|
+
|
|
50
|
+
// The pill slides between equal-width segments, so a ratio is enough: 0 is
|
|
51
|
+
// the first segment, values.length - 1 the last.
|
|
52
|
+
const { value: slide, animated } = useSpring(safeIndex, "snappy");
|
|
29
53
|
|
|
30
54
|
const handleSelect = (index: number) => {
|
|
31
|
-
if (selectedProp === undefined)
|
|
32
|
-
setInternalIndex(index);
|
|
33
|
-
}
|
|
55
|
+
if (selectedProp === undefined) setInternalIndex(index);
|
|
34
56
|
const val = values[index];
|
|
35
|
-
if (val !== undefined)
|
|
36
|
-
onValueChange?.(val, index);
|
|
37
|
-
}
|
|
57
|
+
if (val !== undefined) onValueChange?.(val, index);
|
|
38
58
|
};
|
|
39
59
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
60
|
+
const count = Math.max(values.length, 1);
|
|
61
|
+
const pillWidth = `${100 / count}%`;
|
|
62
|
+
|
|
63
|
+
const containerStyle: ViewStyle = {
|
|
64
|
+
position: "relative",
|
|
65
|
+
flexDirection: "row",
|
|
66
|
+
alignItems: "center",
|
|
67
|
+
alignSelf: "flex-start",
|
|
45
68
|
padding: 2,
|
|
46
69
|
borderRadius: radii.md,
|
|
47
70
|
backgroundColor: "rgba(118, 118, 128, 0.12)",
|
|
48
|
-
...style,
|
|
71
|
+
...(style as ViewStyle),
|
|
49
72
|
};
|
|
50
73
|
|
|
74
|
+
const pillTranslate = animated
|
|
75
|
+
? slide.interpolate({
|
|
76
|
+
inputRange: [0, Math.max(count - 1, 1)],
|
|
77
|
+
// One segment's worth per step, measured against the pill's own width.
|
|
78
|
+
outputRange: ["0%", `${((count - 1) * 100) / count}%`],
|
|
79
|
+
})
|
|
80
|
+
: `${(safeIndex * 100) / count}%`;
|
|
81
|
+
|
|
51
82
|
return (
|
|
52
|
-
<Glass ref={ref} effect="regular" style={containerStyle} {...props}>
|
|
83
|
+
<Glass ref={ref} effect="regular" isDark={isDark} style={containerStyle} testID={testID} {...props}>
|
|
84
|
+
{/* The sliding pill: one View behind the labels, coloured by the theme. */}
|
|
85
|
+
{animated ? (
|
|
86
|
+
<Animated.View
|
|
87
|
+
style={{
|
|
88
|
+
position: "absolute",
|
|
89
|
+
top: 2,
|
|
90
|
+
left: 2,
|
|
91
|
+
width: pillWidth,
|
|
92
|
+
height: SEGMENT_HEIGHT,
|
|
93
|
+
borderRadius: radii.sm,
|
|
94
|
+
backgroundColor: themeColors.glassActive,
|
|
95
|
+
borderWidth: 0.5,
|
|
96
|
+
borderColor: themeColors.edge,
|
|
97
|
+
transform: [{ translateX: pillTranslate }],
|
|
98
|
+
}}
|
|
99
|
+
pointerEvents="none"
|
|
100
|
+
/>
|
|
101
|
+
) : (
|
|
102
|
+
<View
|
|
103
|
+
style={{
|
|
104
|
+
position: "absolute",
|
|
105
|
+
top: 2,
|
|
106
|
+
left: `${2 + (safeIndex * 100) / count}%`,
|
|
107
|
+
width: pillWidth,
|
|
108
|
+
height: SEGMENT_HEIGHT,
|
|
109
|
+
borderRadius: radii.sm,
|
|
110
|
+
backgroundColor: themeColors.glassActive,
|
|
111
|
+
borderWidth: 0.5,
|
|
112
|
+
borderColor: themeColors.edge,
|
|
113
|
+
}}
|
|
114
|
+
pointerEvents="none"
|
|
115
|
+
/>
|
|
116
|
+
)}
|
|
117
|
+
|
|
53
118
|
{values.map((label, idx) => {
|
|
54
|
-
const isSelected = idx ===
|
|
119
|
+
const isSelected = idx === safeIndex;
|
|
55
120
|
return (
|
|
56
|
-
<
|
|
121
|
+
<Pressable
|
|
57
122
|
key={label}
|
|
58
|
-
|
|
59
|
-
|
|
123
|
+
accessibilityRole="tab"
|
|
124
|
+
accessibilityLabel={label}
|
|
125
|
+
accessibilityState={{ selected: isSelected }}
|
|
126
|
+
onPress={() => handleSelect(idx)}
|
|
60
127
|
style={{
|
|
61
|
-
|
|
128
|
+
flex: 1,
|
|
129
|
+
height: SEGMENT_HEIGHT,
|
|
130
|
+
alignItems: "center",
|
|
131
|
+
justifyContent: "center",
|
|
132
|
+
paddingHorizontal: 16,
|
|
62
133
|
borderRadius: radii.sm,
|
|
63
|
-
border: 0,
|
|
64
|
-
backgroundColor: isSelected ? "#FFFFFF" : "transparent",
|
|
65
|
-
color: isSelected ? "#000000" : themeColors.muted,
|
|
66
|
-
boxShadow: isSelected ? "0 1px 3px rgba(0, 0, 0, 0.12)" : "none",
|
|
67
|
-
fontSize: 13,
|
|
68
|
-
fontWeight: 500,
|
|
69
|
-
cursor: "pointer",
|
|
70
|
-
transition: "all 150ms ease",
|
|
71
134
|
}}
|
|
72
135
|
>
|
|
73
|
-
|
|
74
|
-
|
|
136
|
+
<Text
|
|
137
|
+
numberOfLines={1}
|
|
138
|
+
style={{
|
|
139
|
+
fontSize: 13,
|
|
140
|
+
fontWeight: isSelected ? "600" : "500",
|
|
141
|
+
color: isSelected ? themeColors.foreground : themeColors.muted,
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
{label}
|
|
145
|
+
</Text>
|
|
146
|
+
</Pressable>
|
|
75
147
|
);
|
|
76
148
|
})}
|
|
77
149
|
</Glass>
|