@fluxus-ui/native 0.0.0-stage → 0.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 (180) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +61 -2
  3. package/lib/module/Avatar.js +58 -0
  4. package/lib/module/Avatar.js.map +1 -0
  5. package/lib/module/Badge.js +72 -0
  6. package/lib/module/Badge.js.map +1 -0
  7. package/lib/module/BottomSheet.js +159 -0
  8. package/lib/module/BottomSheet.js.map +1 -0
  9. package/lib/module/Button.js +133 -0
  10. package/lib/module/Button.js.map +1 -0
  11. package/lib/module/Card.js +71 -0
  12. package/lib/module/Card.js.map +1 -0
  13. package/lib/module/Dialog.js +137 -0
  14. package/lib/module/Dialog.js.map +1 -0
  15. package/lib/module/Disclosure.js +153 -0
  16. package/lib/module/Disclosure.js.map +1 -0
  17. package/lib/module/EmptyState.js +43 -0
  18. package/lib/module/EmptyState.js.map +1 -0
  19. package/lib/module/ListItem.js +83 -0
  20. package/lib/module/ListItem.js.map +1 -0
  21. package/lib/module/Menu.js +76 -0
  22. package/lib/module/Menu.js.map +1 -0
  23. package/lib/module/Message.js +51 -0
  24. package/lib/module/Message.js.map +1 -0
  25. package/lib/module/Popover.js +153 -0
  26. package/lib/module/Popover.js.map +1 -0
  27. package/lib/module/Progress.js +54 -0
  28. package/lib/module/Progress.js.map +1 -0
  29. package/lib/module/Select.js +146 -0
  30. package/lib/module/Select.js.map +1 -0
  31. package/lib/module/Skeleton.js +39 -0
  32. package/lib/module/Skeleton.js.map +1 -0
  33. package/lib/module/Slider.js +137 -0
  34. package/lib/module/Slider.js.map +1 -0
  35. package/lib/module/Spinner.js +30 -0
  36. package/lib/module/Spinner.js.map +1 -0
  37. package/lib/module/Swap.js +87 -0
  38. package/lib/module/Swap.js.map +1 -0
  39. package/lib/module/Tabs.js +118 -0
  40. package/lib/module/Tabs.js.map +1 -0
  41. package/lib/module/Text.js +47 -0
  42. package/lib/module/Text.js.map +1 -0
  43. package/lib/module/TextField.js +190 -0
  44. package/lib/module/TextField.js.map +1 -0
  45. package/lib/module/Toast.js +108 -0
  46. package/lib/module/Toast.js.map +1 -0
  47. package/lib/module/blur.js +55 -0
  48. package/lib/module/blur.js.map +1 -0
  49. package/lib/module/controls.js +259 -0
  50. package/lib/module/controls.js.map +1 -0
  51. package/lib/module/generated/tokens.js +484 -0
  52. package/lib/module/generated/tokens.js.map +1 -0
  53. package/lib/module/icon.js +51 -0
  54. package/lib/module/icon.js.map +1 -0
  55. package/lib/module/index.js +33 -0
  56. package/lib/module/index.js.map +1 -0
  57. package/lib/module/layout.js +75 -0
  58. package/lib/module/layout.js.map +1 -0
  59. package/lib/module/motion.js +28 -0
  60. package/lib/module/motion.js.map +1 -0
  61. package/lib/module/overlay.js +99 -0
  62. package/lib/module/overlay.js.map +1 -0
  63. package/lib/module/package.json +1 -0
  64. package/lib/module/pending.js +177 -0
  65. package/lib/module/pending.js.map +1 -0
  66. package/lib/module/presence.js +68 -0
  67. package/lib/module/presence.js.map +1 -0
  68. package/lib/module/press.js +32 -0
  69. package/lib/module/press.js.map +1 -0
  70. package/lib/module/screen.js +45 -0
  71. package/lib/module/screen.js.map +1 -0
  72. package/lib/module/theme.js +41 -0
  73. package/lib/module/theme.js.map +1 -0
  74. package/lib/typescript/Avatar.d.ts +17 -0
  75. package/lib/typescript/Avatar.d.ts.map +1 -0
  76. package/lib/typescript/Badge.d.ts +21 -0
  77. package/lib/typescript/Badge.d.ts.map +1 -0
  78. package/lib/typescript/BottomSheet.d.ts +27 -0
  79. package/lib/typescript/BottomSheet.d.ts.map +1 -0
  80. package/lib/typescript/Button.d.ts +27 -0
  81. package/lib/typescript/Button.d.ts.map +1 -0
  82. package/lib/typescript/Card.d.ts +17 -0
  83. package/lib/typescript/Card.d.ts.map +1 -0
  84. package/lib/typescript/Dialog.d.ts +34 -0
  85. package/lib/typescript/Dialog.d.ts.map +1 -0
  86. package/lib/typescript/Disclosure.d.ts +39 -0
  87. package/lib/typescript/Disclosure.d.ts.map +1 -0
  88. package/lib/typescript/EmptyState.d.ts +12 -0
  89. package/lib/typescript/EmptyState.d.ts.map +1 -0
  90. package/lib/typescript/ListItem.d.ts +20 -0
  91. package/lib/typescript/ListItem.d.ts.map +1 -0
  92. package/lib/typescript/Menu.d.ts +23 -0
  93. package/lib/typescript/Menu.d.ts.map +1 -0
  94. package/lib/typescript/Message.d.ts +14 -0
  95. package/lib/typescript/Message.d.ts.map +1 -0
  96. package/lib/typescript/Popover.d.ts +71 -0
  97. package/lib/typescript/Popover.d.ts.map +1 -0
  98. package/lib/typescript/Progress.d.ts +12 -0
  99. package/lib/typescript/Progress.d.ts.map +1 -0
  100. package/lib/typescript/Select.d.ts +30 -0
  101. package/lib/typescript/Select.d.ts.map +1 -0
  102. package/lib/typescript/Skeleton.d.ts +13 -0
  103. package/lib/typescript/Skeleton.d.ts.map +1 -0
  104. package/lib/typescript/Slider.d.ts +21 -0
  105. package/lib/typescript/Slider.d.ts.map +1 -0
  106. package/lib/typescript/Spinner.d.ts +12 -0
  107. package/lib/typescript/Spinner.d.ts.map +1 -0
  108. package/lib/typescript/Swap.d.ts +25 -0
  109. package/lib/typescript/Swap.d.ts.map +1 -0
  110. package/lib/typescript/Tabs.d.ts +18 -0
  111. package/lib/typescript/Tabs.d.ts.map +1 -0
  112. package/lib/typescript/Text.d.ts +20 -0
  113. package/lib/typescript/Text.d.ts.map +1 -0
  114. package/lib/typescript/TextField.d.ts +36 -0
  115. package/lib/typescript/TextField.d.ts.map +1 -0
  116. package/lib/typescript/Toast.d.ts +25 -0
  117. package/lib/typescript/Toast.d.ts.map +1 -0
  118. package/lib/typescript/blur.d.ts +4 -0
  119. package/lib/typescript/blur.d.ts.map +1 -0
  120. package/lib/typescript/controls.d.ts +37 -0
  121. package/lib/typescript/controls.d.ts.map +1 -0
  122. package/lib/typescript/generated/tokens.d.ts +499 -0
  123. package/lib/typescript/generated/tokens.d.ts.map +1 -0
  124. package/lib/typescript/icon.d.ts +25 -0
  125. package/lib/typescript/icon.d.ts.map +1 -0
  126. package/lib/typescript/index.d.ts +31 -0
  127. package/lib/typescript/index.d.ts.map +1 -0
  128. package/lib/typescript/layout.d.ts +24 -0
  129. package/lib/typescript/layout.d.ts.map +1 -0
  130. package/lib/typescript/motion.d.ts +7 -0
  131. package/lib/typescript/motion.d.ts.map +1 -0
  132. package/lib/typescript/overlay.d.ts +33 -0
  133. package/lib/typescript/overlay.d.ts.map +1 -0
  134. package/lib/typescript/package.json +1 -0
  135. package/lib/typescript/pending.d.ts +25 -0
  136. package/lib/typescript/pending.d.ts.map +1 -0
  137. package/lib/typescript/presence.d.ts +37 -0
  138. package/lib/typescript/presence.d.ts.map +1 -0
  139. package/lib/typescript/press.d.ts +12 -0
  140. package/lib/typescript/press.d.ts.map +1 -0
  141. package/lib/typescript/screen.d.ts +16 -0
  142. package/lib/typescript/screen.d.ts.map +1 -0
  143. package/lib/typescript/theme.d.ts +32 -0
  144. package/lib/typescript/theme.d.ts.map +1 -0
  145. package/package.json +100 -3
  146. package/src/Avatar.tsx +44 -0
  147. package/src/Badge.tsx +45 -0
  148. package/src/BottomSheet.tsx +109 -0
  149. package/src/Button.tsx +117 -0
  150. package/src/Card.tsx +65 -0
  151. package/src/Dialog.tsx +93 -0
  152. package/src/Disclosure.tsx +108 -0
  153. package/src/EmptyState.tsx +32 -0
  154. package/src/ListItem.tsx +73 -0
  155. package/src/Menu.tsx +57 -0
  156. package/src/Message.tsx +43 -0
  157. package/src/Popover.tsx +145 -0
  158. package/src/Progress.tsx +33 -0
  159. package/src/Select.tsx +118 -0
  160. package/src/Skeleton.tsx +27 -0
  161. package/src/Slider.tsx +107 -0
  162. package/src/Spinner.tsx +18 -0
  163. package/src/Swap.tsx +74 -0
  164. package/src/Tabs.tsx +84 -0
  165. package/src/Text.tsx +44 -0
  166. package/src/TextField.tsx +137 -0
  167. package/src/Toast.tsx +94 -0
  168. package/src/blur.tsx +33 -0
  169. package/src/controls.tsx +196 -0
  170. package/src/generated/tokens.ts +653 -0
  171. package/src/icon.tsx +39 -0
  172. package/src/index.ts +30 -0
  173. package/src/layout.tsx +40 -0
  174. package/src/motion.ts +22 -0
  175. package/src/overlay.tsx +77 -0
  176. package/src/pending.tsx +131 -0
  177. package/src/presence.tsx +65 -0
  178. package/src/press.ts +23 -0
  179. package/src/screen.tsx +32 -0
  180. package/src/theme.tsx +49 -0
package/src/Toast.tsx ADDED
@@ -0,0 +1,94 @@
1
+ import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from 'react';
2
+ import { AccessibilityInfo, Platform, Pressable, View } from 'react-native';
3
+ import { toneColors } from './Badge';
4
+ import { Appear } from './presence';
5
+ import { Text } from './Text';
6
+ import { useTheme } from './theme';
7
+
8
+ export interface ToastInput {
9
+ title: string;
10
+ description?: string;
11
+ /** Default: 'info'. */
12
+ tone?: 'info' | 'success' | 'warning' | 'danger';
13
+ /** How long it stays, in ms; 0 stays until it is pressed. Default: 5000, and 0 for a danger. */
14
+ duration?: number;
15
+ }
16
+
17
+ const Context = createContext<((toast: ToastInput) => void) | null>(null);
18
+ /** The notices themselves: the provider paints them, and so does every window open above it. */
19
+ const Layer = createContext<ReactNode>(null);
20
+
21
+ /** Inside a window that opens above the app (`Modal`), where the notices of the app would be behind. */
22
+ export const ToastLayer = (): ReactNode => useContext(Layer);
23
+
24
+ export interface ToastProviderProps {
25
+ children?: ReactNode;
26
+ /** How far from the bottom: the app's safe area, or what a tab bar takes. Default: `space.4`. */
27
+ inset?: number;
28
+ /** What pressing one does, for a screen reader. Default: 'Dismiss'. */
29
+ dismissLabel?: string;
30
+ }
31
+
32
+ /** Said aloud on both platforms: Android reads a live region by itself, iOS has to be told. */
33
+ export const announce = (text: string): void => {
34
+ if (Platform.OS === 'ios') AccessibilityInfo.announceForAccessibility(text);
35
+ };
36
+
37
+ /** Holds the notices of the app above everything it wraps: put it once, near the root. */
38
+ export function ToastProvider({ children, inset, dismissLabel = 'Dismiss' }: ToastProviderProps) {
39
+ const theme = useTheme();
40
+ const [toasts, setToasts] = useState<readonly (ToastInput & { id: number })[]>([]);
41
+ const next = useRef(0);
42
+ const timers = useRef(new Set<ReturnType<typeof setTimeout>>());
43
+ useEffect(() => () => timers.current.forEach(clearTimeout), []);
44
+ const dismiss = useCallback((id: number) => setToasts((list) => list.filter((t) => t.id !== id)), []);
45
+ const toast = useCallback(
46
+ (input: ToastInput) => {
47
+ const id = ++next.current;
48
+ setToasts((list) => [...list, { ...input, id }]);
49
+ announce(input.description ? `${input.title}. ${input.description}` : input.title);
50
+ const ms = input.duration ?? (input.tone === 'danger' ? 0 : 5000);
51
+ if (ms) timers.current.add(setTimeout(() => dismiss(id), ms));
52
+ },
53
+ [dismiss],
54
+ );
55
+ const edge = theme.size['space.4'];
56
+ const layer = (
57
+ <View pointerEvents="box-none" style={{ position: 'absolute', start: edge, end: edge, bottom: inset ?? edge, gap: theme.size['space.2'] }}>
58
+ {toasts.map((t) => (
59
+ <Appear key={t.id}>
60
+ <Pressable
61
+ role="alert"
62
+ aria-live={t.tone === 'danger' ? 'assertive' : 'polite'}
63
+ accessibilityHint={dismissLabel}
64
+ onPress={() => dismiss(t.id)}
65
+ style={{ minHeight: theme.size['target.min'], flexDirection: 'row', alignItems: 'center', gap: theme.size['space.3'], padding: theme.size['space.3'], borderWidth: 1, borderColor: theme.color['border.default'], borderRadius: theme.size['radius.card'], backgroundColor: theme.color['surface.raised'] }}
66
+ >
67
+ <View style={{ width: theme.size['space.2'], height: theme.size['space.2'], borderRadius: theme.size['space.2'], backgroundColor: toneColors(theme, t.tone ?? 'info').solid }} />
68
+ <View style={{ flex: 1 }}>
69
+ <Text variant="label">{t.title}</Text>
70
+ {t.description !== undefined && (
71
+ <Text variant="caption" tone="muted">
72
+ {t.description}
73
+ </Text>
74
+ )}
75
+ </View>
76
+ </Pressable>
77
+ </Appear>
78
+ ))}
79
+ </View>
80
+ );
81
+ return (
82
+ <Context.Provider value={toast}>
83
+ <Layer.Provider value={layer}>{children}</Layer.Provider>
84
+ {layer}
85
+ </Context.Provider>
86
+ );
87
+ }
88
+
89
+ /** `toast({ title })` shows a notice. */
90
+ export function useToast(): (toast: ToastInput) => void {
91
+ const toast = useContext(Context);
92
+ if (!toast) throw new Error('@fluxus-ui/native: no toasts. Wrap the app in <ToastProvider>, inside <ThemeProvider>.');
93
+ return toast;
94
+ }
package/src/blur.tsx ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * `@fluxus-ui/native/blur`: the Swap that loses focus on a phone too, with the veil of expo-blur over what changes.
3
+ * Its own entry, so that an app without it does not need expo-blur. On Android the blur needs Android 12;
4
+ * below, it only dissolves. (SPEC §15.6)
5
+ */
6
+ import { useRef, type ComponentRef } from 'react';
7
+ import { StyleSheet, type View } from 'react-native';
8
+ import { BlurTargetView, BlurView } from 'expo-blur';
9
+ import Animated, { useAnimatedProps, useAnimatedStyle } from 'react-native-reanimated';
10
+ import { presence } from './presence';
11
+ import { useSwap, type SwapProps } from './Swap';
12
+
13
+ const Veil = Animated.createAnimatedComponent(BlurView);
14
+ /** How blurred it gets at the middle of the change, of the 100 of expo-blur. */
15
+ const DEPTH = 40;
16
+
17
+ export function Swap({ id, children, style, ...rest }: SwapProps) {
18
+ const { content, t, moving } = useSwap(id, children);
19
+ const target = useRef<ComponentRef<typeof View>>(null);
20
+ const body = useAnimatedStyle(() => ({ opacity: 0.4 + 0.6 * t.value }));
21
+ const veil = useAnimatedProps(() => ({ intensity: (1 - t.value) * DEPTH }));
22
+ return (
23
+ <Animated.View layout={presence().layout} {...rest} style={style}>
24
+ <BlurTargetView ref={target}>
25
+ <Animated.View style={body}>{content}</Animated.View>
26
+ </BlurTargetView>
27
+ {/* Only while it changes: at rest there is no veil over the content. */}
28
+ {moving && <Veil pointerEvents="none" blurTarget={target} blurMethod="dimezisBlurViewSdk31Plus" animatedProps={veil} style={StyleSheet.absoluteFill} />}
29
+ </Animated.View>
30
+ );
31
+ }
32
+
33
+ export type { SwapProps };
@@ -0,0 +1,196 @@
1
+ /**
2
+ * What is switched: a checkbox, a switch and a group of radios. Each is one row to press, of 44 points at least,
3
+ * with its label and its help; the mark moves by its transform and the colours fade. (SPEC §15.7)
4
+ */
5
+ import { useEffect, useState, type ReactNode } from 'react';
6
+ import { I18nManager, Pressable, View, type PressableProps, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
7
+ import Animated, { interpolateColor, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
8
+ import { fill } from './layout';
9
+ import { spring, timing } from './motion';
10
+ import { Text } from './Text';
11
+ import { useTheme } from './theme';
12
+
13
+ /** A value the app holds, or one the piece holds by itself from `defaultValue`. */
14
+ export function useControlled<T>(value: T | undefined, initial: T, onChange?: (next: T) => void): [T, (next: T) => void] {
15
+ const [own, setOwn] = useState(initial);
16
+ return [
17
+ value === undefined ? own : value,
18
+ (next) => {
19
+ if (value === undefined) setOwn(next);
20
+ onChange?.(next);
21
+ },
22
+ ];
23
+ }
24
+
25
+ interface RowProps extends Omit<PressableProps, 'children' | 'style' | 'role'> {
26
+ role: 'checkbox' | 'switch' | 'radio';
27
+ checked: boolean | 'mixed';
28
+ label: string;
29
+ hint?: string;
30
+ /** The box, the track or the ring. */
31
+ control: ReactNode;
32
+ /** A switch goes after its text; the others before. */
33
+ after?: boolean;
34
+ style?: StyleProp<ViewStyle>;
35
+ }
36
+
37
+ function Row({ role, checked, label, hint, control, after, disabled, style, ...rest }: RowProps) {
38
+ const theme = useTheme();
39
+ return (
40
+ <Pressable
41
+ role={role}
42
+ aria-checked={checked}
43
+ aria-disabled={!!disabled}
44
+ aria-label={hint ? `${label}. ${hint}` : label}
45
+ {...rest}
46
+ disabled={disabled}
47
+ style={[{ minHeight: theme.size['target.min'], flexDirection: after ? 'row-reverse' : 'row', alignItems: 'center', gap: theme.size['space.3'], paddingBlock: theme.size['space.2'], opacity: disabled ? 0.5 : 1 }, style]}
48
+ >
49
+ {control}
50
+ <View style={fill}>
51
+ <Text>{label}</Text>
52
+ {hint !== undefined && (
53
+ <Text variant="caption" tone="muted">
54
+ {hint}
55
+ </Text>
56
+ )}
57
+ </View>
58
+ </Pressable>
59
+ );
60
+ }
61
+
62
+ /** 0 → 1 as it goes on: `to` for what moves, on the snappy spring; `tint` for the colours, on the fast curve. */
63
+ function useOn(on: boolean) {
64
+ const to = useSharedValue(on ? 1 : 0);
65
+ const tint = useSharedValue(on ? 1 : 0);
66
+ useEffect(() => {
67
+ to.value = spring(on ? 1 : 0, 'snappy');
68
+ tint.value = timing(on ? 1 : 0, 'fast');
69
+ }, [on, to, tint]);
70
+ return { to, tint };
71
+ }
72
+
73
+ interface ToggleProps extends Omit<PressableProps, 'children' | 'style' | 'onPress' | 'role'> {
74
+ label: string;
75
+ hint?: string;
76
+ checked?: boolean;
77
+ defaultChecked?: boolean;
78
+ onChange?: (checked: boolean) => void;
79
+ style?: StyleProp<ViewStyle>;
80
+ }
81
+
82
+ export interface CheckboxProps extends ToggleProps {
83
+ /** Neither on nor off: some of what it stands for is chosen. */
84
+ mixed?: boolean;
85
+ }
86
+
87
+ export function Checkbox({ checked, defaultChecked = false, mixed = false, onChange, ...rest }: CheckboxProps) {
88
+ const theme = useTheme();
89
+ const c = theme.color;
90
+ const [on, set] = useControlled(checked, defaultChecked, onChange);
91
+ const { to, tint } = useOn(on || mixed);
92
+ const side = theme.size['icon.md'];
93
+ const box = useAnimatedStyle(() => ({
94
+ backgroundColor: interpolateColor(tint.value, [0, 1], [c['input.bg'], c['accent.default']]),
95
+ borderColor: interpolateColor(tint.value, [0, 1], [c['border.control'], c['accent.default']]),
96
+ }));
97
+ const mark = useAnimatedStyle(() => ({ opacity: tint.value, transform: [{ scale: to.value }, { rotate: mixed ? '0deg' : '45deg' }] }));
98
+ const ink = c['text.on-accent'];
99
+ return (
100
+ <Row
101
+ role="checkbox"
102
+ checked={mixed ? 'mixed' : on}
103
+ {...rest}
104
+ onPress={() => set(mixed ? true : !on)}
105
+ control={
106
+ <Animated.View style={[{ width: side, height: side, alignItems: 'center', justifyContent: 'center', borderWidth: 1.5, borderRadius: theme.size['radius.1'] }, box]}>
107
+ {/* The tick is two sides of a rectangle, turned; a dash when it is mixed. */}
108
+ <Animated.View style={[mixed ? { width: side / 2, height: 2, backgroundColor: ink } : { width: side * 0.3, height: side * 0.55, marginBlockEnd: side * 0.1, borderColor: ink, borderEndWidth: 2, borderBottomWidth: 2 }, mark]} />
109
+ </Animated.View>
110
+ }
111
+ />
112
+ );
113
+ }
114
+
115
+ export type SwitchProps = ToggleProps;
116
+
117
+ export function Switch({ checked, defaultChecked = false, onChange, ...rest }: SwitchProps) {
118
+ const theme = useTheme();
119
+ const c = theme.color;
120
+ const [on, set] = useControlled(checked, defaultChecked, onChange);
121
+ const { to, tint } = useOn(on);
122
+ const height = theme.size['control.h.sm'];
123
+ const pad = theme.size['space.1'] / 2;
124
+ const thumb = height - 2 * pad;
125
+ const travel = height * 0.75;
126
+ const way = I18nManager.isRTL ? -1 : 1;
127
+ const track = useAnimatedStyle(() => ({ backgroundColor: interpolateColor(tint.value, [0, 1], [c['border.control'], c['accent.default']]) }));
128
+ const knob = useAnimatedStyle(() => ({ transform: [{ translateX: way * to.value * travel }] }));
129
+ return (
130
+ <Row
131
+ role="switch"
132
+ after
133
+ checked={on}
134
+ {...rest}
135
+ onPress={() => set(!on)}
136
+ control={
137
+ <Animated.View style={[{ width: height + travel, height, padding: pad, borderRadius: height }, track]}>
138
+ <Animated.View style={[{ width: thumb, height: thumb, borderRadius: thumb, backgroundColor: c['surface.base'] }, knob]} />
139
+ </Animated.View>
140
+ }
141
+ />
142
+ );
143
+ }
144
+
145
+ export interface RadioOption {
146
+ value: string;
147
+ label: string;
148
+ hint?: string;
149
+ disabled?: boolean;
150
+ }
151
+
152
+ export interface RadioGroupProps extends Omit<ViewProps, 'children'> {
153
+ /** The name of the group, for a screen reader. */
154
+ label: string;
155
+ options: readonly RadioOption[];
156
+ value?: string | null;
157
+ defaultValue?: string | null;
158
+ onChange?: (value: string) => void;
159
+ disabled?: boolean;
160
+ }
161
+
162
+ function Radio({ option, on, disabled, onPress }: { option: RadioOption; on: boolean; disabled: boolean; onPress: () => void }) {
163
+ const theme = useTheme();
164
+ const c = theme.color;
165
+ const { to, tint } = useOn(on);
166
+ const side = theme.size['icon.md'];
167
+ const ring = useAnimatedStyle(() => ({ borderColor: interpolateColor(tint.value, [0, 1], [c['border.control'], c['accent.default']]) }));
168
+ const dot = useAnimatedStyle(() => ({ transform: [{ scale: to.value }] }));
169
+ return (
170
+ <Row
171
+ role="radio"
172
+ checked={on}
173
+ label={option.label}
174
+ hint={option.hint}
175
+ disabled={disabled}
176
+ onPress={onPress}
177
+ control={
178
+ <Animated.View style={[{ width: side, height: side, alignItems: 'center', justifyContent: 'center', borderWidth: 1.5, borderRadius: side, backgroundColor: c['input.bg'] }, ring]}>
179
+ <Animated.View style={[{ width: side / 2, height: side / 2, borderRadius: side, backgroundColor: c['accent.default'] }, dot]} />
180
+ </Animated.View>
181
+ }
182
+ />
183
+ );
184
+ }
185
+
186
+ /** One of a few, all in sight. */
187
+ export function RadioGroup({ label, options, value, defaultValue = null, onChange, disabled = false, ...rest }: RadioGroupProps) {
188
+ const [chosen, set] = useControlled<string | null>(value, defaultValue, onChange as (next: string | null) => void);
189
+ return (
190
+ <View role="radiogroup" aria-label={label} {...rest}>
191
+ {options.map((option) => (
192
+ <Radio key={option.value} option={option} on={option.value === chosen} disabled={disabled || !!option.disabled} onPress={() => set(option.value)} />
193
+ ))}
194
+ </View>
195
+ );
196
+ }