@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/Select.tsx ADDED
@@ -0,0 +1,118 @@
1
+ /**
2
+ * One of a few, chosen in a sheet. The field looks as a TextField does; the options are radios, which is what
3
+ * they are. If the app takes its time with the choice, the row waits and the sheet closes after. (SPEC §15.8)
4
+ */
5
+ import { useRef, useState, type ComponentRef } from 'react';
6
+ import { Pressable, View, type StyleProp, type ViewStyle } from 'react-native';
7
+ import { BottomSheet } from './BottomSheet';
8
+ import { Chevron } from './Disclosure';
9
+ import { fill } from './layout';
10
+ import { ListItem } from './ListItem';
11
+ import { focusOn } from './overlay';
12
+ import { settle } from './pending';
13
+ import { Text } from './Text';
14
+ import { useTheme } from './theme';
15
+
16
+ export interface SelectOption<T extends string = string> {
17
+ value: T;
18
+ label: string;
19
+ description?: string;
20
+ disabled?: boolean;
21
+ }
22
+
23
+ export interface SelectProps<T extends string = string> {
24
+ label: string;
25
+ options: readonly SelectOption<T>[];
26
+ value?: T | null;
27
+ /** It holds its own value from here. */
28
+ defaultValue?: T | null;
29
+ /** If it returns a promise, the chosen row waits for it and the sheet closes once it is fulfilled. */
30
+ onChange?: (value: T) => void | Promise<unknown>;
31
+ /** What the field says with nothing chosen. */
32
+ placeholder?: string;
33
+ /** Help under the field. */
34
+ hint?: string;
35
+ /** What is wrong, and how to fix it. It takes the place of the hint. */
36
+ error?: string;
37
+ disabled?: boolean;
38
+ /** For the sheet: what the bottom edge of the screen takes, and the name of its handle. */
39
+ inset?: number;
40
+ closeLabel?: string;
41
+ style?: StyleProp<ViewStyle>;
42
+ testID?: string;
43
+ }
44
+
45
+ export function Select<T extends string = string>({ label, options, value, defaultValue = null, onChange, placeholder, hint, error, disabled = false, inset, closeLabel, style, testID }: SelectProps<T>) {
46
+ const theme = useTheme();
47
+ const c = theme.color;
48
+ const [own, setOwn] = useState(defaultValue);
49
+ const [open, setOpen] = useState(false);
50
+ const [focused, setFocused] = useState(false);
51
+ const field = useRef<ComponentRef<typeof Pressable>>(null);
52
+ const now = value === undefined ? own : value;
53
+ const chosen = options.find((o) => o.value === now);
54
+ const message = error ?? hint;
55
+ const close = (): void => {
56
+ setOpen(false);
57
+ focusOn(field.current);
58
+ };
59
+ const choose = (next: T): void | Promise<void> => {
60
+ if (value === undefined) setOwn(next);
61
+ return settle(onChange?.(next), close);
62
+ };
63
+ const radius = theme.size['radius.control'];
64
+ return (
65
+ <View style={[{ gap: theme.size['space.1'], opacity: disabled ? 0.5 : 1 }, style]}>
66
+ <Text variant="label" aria-hidden>
67
+ {label}
68
+ </Text>
69
+ {/* The ring of the focus: a second line around the edge, always there, so that nothing moves when it shows. */}
70
+ <View style={{ borderWidth: 1, borderRadius: radius + 1, borderColor: focused ? c['focus.ring'] : 'transparent' }}>
71
+ <Pressable
72
+ ref={field}
73
+ role="combobox"
74
+ aria-expanded={open}
75
+ aria-disabled={disabled}
76
+ aria-label={[label, chosen?.label ?? placeholder].filter(Boolean).join(', ')}
77
+ accessibilityHint={message}
78
+ testID={testID}
79
+ disabled={disabled}
80
+ onPress={() => setOpen(true)}
81
+ onFocus={() => setFocused(true)}
82
+ onBlur={() => setFocused(false)}
83
+ style={{ minHeight: theme.size['target.min'], flexDirection: 'row', alignItems: 'center', gap: theme.size['space.2'], paddingInline: theme.size['pad.control-x'], borderWidth: 1, borderRadius: radius, borderColor: error ? c['state.danger.solid'] : focused ? c['accent.default'] : c['input.border'], backgroundColor: c['input.bg'] }}
84
+ >
85
+ <Text tone={chosen ? 'default' : 'muted'} numberOfLines={1} style={fill}>
86
+ {chosen?.label ?? placeholder}
87
+ </Text>
88
+ <Chevron />
89
+ </Pressable>
90
+ </View>
91
+ {message !== undefined && (
92
+ <Text variant="caption" tone={error ? 'default' : 'muted'} style={error ? { color: c['state.danger.text'] } : undefined}>
93
+ {message}
94
+ </Text>
95
+ )}
96
+ <BottomSheet open={open} onClose={close} title={label} inset={inset} closeLabel={closeLabel}>
97
+ <View role="radiogroup" aria-label={label} style={{ marginInline: -theme.size['space.4'] }}>
98
+ {options.map((option) => {
99
+ const on = option.value === now;
100
+ return (
101
+ <ListItem
102
+ key={option.value}
103
+ role="radio"
104
+ aria-checked={on}
105
+ title={option.label}
106
+ description={option.description}
107
+ disabled={option.disabled}
108
+ onPress={() => choose(option.value)}
109
+ // The mark of the chosen one: the two sides of a rectangle, turned.
110
+ trailing={on ? <View aria-hidden style={{ width: theme.size['space.2'], height: theme.size['space.3'] + 2, marginInlineEnd: theme.size['space.1'], borderColor: c['accent.default'], borderEndWidth: 2, borderBottomWidth: 2, transform: [{ rotate: '45deg' }] }} /> : null}
111
+ />
112
+ );
113
+ })}
114
+ </View>
115
+ </BottomSheet>
116
+ </View>
117
+ );
118
+ }
@@ -0,0 +1,27 @@
1
+ import { useEffect } from 'react';
2
+ import type { DimensionValue, StyleProp, ViewProps, ViewStyle } from 'react-native';
3
+ import Animated, { ReduceMotion, cancelAnimation, useAnimatedStyle, useSharedValue, withRepeat, withTiming } from 'react-native-reanimated';
4
+ import { motion } from './generated/tokens';
5
+ import { useTheme } from './theme';
6
+
7
+ export interface SkeletonProps extends Omit<ViewProps, 'style' | 'children'> {
8
+ /** Default: the whole width. */
9
+ width?: DimensionValue;
10
+ /** Default: a line of body text. */
11
+ height?: number;
12
+ /** Default: `radius.1`. For an avatar, half its height. */
13
+ radius?: number;
14
+ style?: StyleProp<ViewStyle>;
15
+ }
16
+
17
+ /** The shape of what is coming, breathing while it loads. Still with reduced motion; not there for a screen reader. */
18
+ export function Skeleton({ width = '100%', height, radius, style, ...rest }: SkeletonProps) {
19
+ const theme = useTheme();
20
+ const light = useSharedValue(1);
21
+ useEffect(() => {
22
+ light.value = withRepeat(withTiming(0.5, { duration: motion.duration.slow * 2 }), -1, true, undefined, ReduceMotion.System);
23
+ return () => cancelAnimation(light);
24
+ }, [light]);
25
+ const breath = useAnimatedStyle(() => ({ opacity: light.value }));
26
+ return <Animated.View aria-hidden importantForAccessibility="no-hide-descendants" {...rest} style={[{ width, height: height ?? theme.size['type.body.size'], borderRadius: radius ?? theme.size['radius.1'], backgroundColor: theme.color['surface.sunken'] }, style, breath]} />;
27
+ }
package/src/Slider.tsx ADDED
@@ -0,0 +1,107 @@
1
+ /**
2
+ * A number with a short run, set by sliding. It takes the touch with React Native's own responder, not with
3
+ * react-native-gesture-handler: its 2 and its 3 have different APIs, and this works with both and with none.
4
+ * A screen reader moves it a step at a time. (SPEC §15.7)
5
+ */
6
+ import { useEffect, useRef, useState } from 'react';
7
+ import { I18nManager, View, type GestureResponderEvent, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
8
+ import Animated, { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
9
+ import { useControlled } from './controls';
10
+ import { spring } from './motion';
11
+ import { useTheme } from './theme';
12
+
13
+ export interface SliderProps extends Omit<ViewProps, 'children' | 'style'> {
14
+ /** What it sets, for a screen reader. */
15
+ label: string;
16
+ /** Default: 0. */
17
+ min?: number;
18
+ /** Default: 100. */
19
+ max?: number;
20
+ /** Default: 1. */
21
+ step?: number;
22
+ value?: number;
23
+ defaultValue?: number;
24
+ /** On every step, while it slides too. */
25
+ onChange?: (value: number) => void;
26
+ /** What a screen reader says for a value: «0,8 ×», «3 de 5». Default: the number. */
27
+ valueText?: (value: number) => string;
28
+ disabled?: boolean;
29
+ style?: StyleProp<ViewStyle>;
30
+ }
31
+
32
+ export function Slider({ label, min = 0, max = 100, step = 1, value, defaultValue, onChange, valueText, disabled = false, style, ...rest }: SliderProps) {
33
+ const theme = useTheme();
34
+ const c = theme.color;
35
+ const [now, set] = useControlled(value, defaultValue ?? min, onChange);
36
+ const [width, setWidth] = useState(0);
37
+ const left = useRef<number | null>(null);
38
+ const dragging = useRef(false);
39
+ const part = max > min ? Math.min(1, Math.max(0, (now - min) / (max - min))) : 0;
40
+ const at = useSharedValue(part);
41
+ useEffect(() => {
42
+ // Under the finger it goes with it; a step from elsewhere travels on the spring.
43
+ at.value = dragging.current ? part : spring(part, 'snappy');
44
+ }, [part, at]);
45
+
46
+ const thumb = theme.size['control.h.sm'];
47
+ const rail = theme.size['space.1'];
48
+ const run = Math.max(0, width - thumb);
49
+ const way = I18nManager.isRTL ? -1 : 1;
50
+ const knob = useAnimatedStyle(() => ({ transform: [{ translateX: way * at.value * run }] }));
51
+ const fill = useAnimatedStyle(() => ({ transform: [{ translateX: `${-way * (1 - at.value) * 100}%` }] }));
52
+
53
+ const snap = (ratio: number): number => {
54
+ const raw = min + Math.min(1, Math.max(0, ratio)) * (max - min);
55
+ return Math.min(max, Math.max(min, Number((Math.round((raw - min) / step) * step + min).toFixed(6))));
56
+ };
57
+ const follow = (e: GestureResponderEvent): void => {
58
+ // The point is in the window; where the track starts in it is measured when the touch begins.
59
+ const x = left.current === null ? e.nativeEvent.locationX : e.nativeEvent.pageX - left.current;
60
+ const ratio = run ? (x - thumb / 2) / run : 0;
61
+ const next = snap(I18nManager.isRTL ? 1 - ratio : ratio);
62
+ if (next !== now) set(next);
63
+ };
64
+ const nudge = (by: number): void => set(snap((now + by * step - min) / (max - min)));
65
+
66
+ return (
67
+ <View
68
+ accessible
69
+ role="slider"
70
+ aria-label={label}
71
+ aria-disabled={disabled}
72
+ aria-valuemin={min}
73
+ aria-valuemax={max}
74
+ aria-valuenow={now}
75
+ aria-valuetext={valueText?.(now)}
76
+ accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
77
+ onAccessibilityAction={(e) => !disabled && nudge(e.nativeEvent.actionName === 'increment' ? 1 : -1)}
78
+ {...rest}
79
+ onLayout={(e) => setWidth(e.nativeEvent.layout.width)}
80
+ onStartShouldSetResponder={() => !disabled}
81
+ // Once it has the touch it keeps it: a list that scrolls does not take it away halfway.
82
+ onResponderTerminationRequest={() => false}
83
+ onResponderGrant={(e) => {
84
+ dragging.current = true;
85
+ left.current = null;
86
+ const first = e;
87
+ (e.currentTarget as unknown as { measureInWindow?: (done: (x: number) => void) => void } | null)?.measureInWindow?.((x) => {
88
+ left.current = x;
89
+ });
90
+ follow(first);
91
+ }}
92
+ onResponderMove={follow}
93
+ onResponderRelease={() => {
94
+ dragging.current = false;
95
+ }}
96
+ onResponderTerminate={() => {
97
+ dragging.current = false;
98
+ }}
99
+ style={[{ height: theme.size['target.min'], justifyContent: 'center', opacity: disabled ? 0.5 : 1 }, style]}
100
+ >
101
+ <View style={{ height: rail, marginInline: thumb / 2, borderRadius: rail, backgroundColor: c['surface.sunken'], overflow: 'hidden' }}>
102
+ <Animated.View style={[{ height: rail, borderRadius: rail, backgroundColor: c['accent.default'] }, fill]} />
103
+ </View>
104
+ <Animated.View style={[{ position: 'absolute', start: 0, width: thumb, height: thumb, borderRadius: thumb, borderWidth: 1, borderColor: c['border.control'], backgroundColor: c['surface.base'] }, knob]} />
105
+ </View>
106
+ );
107
+ }
@@ -0,0 +1,18 @@
1
+ import { ActivityIndicator, type ActivityIndicatorProps } from 'react-native';
2
+ import { useTheme } from './theme';
3
+
4
+ export interface SpinnerProps extends Omit<ActivityIndicatorProps, 'size' | 'color'> {
5
+ /** What is loading, for a screen reader. Without it the spinner is decoration and is hidden from one. */
6
+ label?: string;
7
+ /** Default: 'md'. The system draws two sizes: 'lg' is the large one. */
8
+ size?: 'sm' | 'md' | 'lg';
9
+ /** Default: 'muted'. */
10
+ tone?: 'muted' | 'accent';
11
+ }
12
+
13
+ /** Something is loading. Where there is a layout to show, a Skeleton instead. */
14
+ export function Spinner({ label, size = 'md', tone = 'muted', ...rest }: SpinnerProps) {
15
+ const theme = useTheme();
16
+ const a11y = label ? ({ accessible: true, role: 'status', 'aria-label': label, 'aria-busy': true } as const) : ({ 'aria-hidden': true, importantForAccessibility: 'no-hide-descendants' } as const);
17
+ return <ActivityIndicator {...a11y} {...rest} size={size === 'lg' ? 'large' : 'small'} color={theme.color[tone === 'accent' ? 'accent.default' : 'text.muted']} />;
18
+ }
package/src/Swap.tsx ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * What it shows gives way to what comes without a jump: the old dissolves, then the new condenses. In a browser
3
+ * it loses focus too (`filter: blur`); on a phone React Native cannot animate a blur, and the veil of
4
+ * `@fluxus-ui/native/blur` does it. With reduced motion the change is immediate. (SPEC §15.6)
5
+ */
6
+ import { useEffect, useRef, useState, type ReactNode } from 'react';
7
+ import { Platform, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
8
+ import Animated, { useAnimatedStyle, useReducedMotion, useSharedValue, type SharedValue } from 'react-native-reanimated';
9
+ import { motion } from './generated/tokens';
10
+ import { timing } from './motion';
11
+ import { presence } from './presence';
12
+
13
+ /** How small it gets as it goes, and how blurred in a browser. */
14
+ const SHRINK = 0.02;
15
+ const BLUR = 8;
16
+
17
+ /**
18
+ * `content` is what to paint now, `t` goes 1 → 0 → 1 across a change, and `moving` is true while it does.
19
+ * A change of `id` is a relay; a change of the children under the same `id` just shows.
20
+ */
21
+ export function useSwap(id: string | number, children: ReactNode): { content: ReactNode; t: SharedValue<number>; moving: boolean } {
22
+ const reduced = useReducedMotion();
23
+ const [shown, setShown] = useState(id);
24
+ const [moving, setMoving] = useState(false);
25
+ const kept = useRef(children);
26
+ const t = useSharedValue(1);
27
+ if (id === shown) kept.current = children;
28
+ const now = useRef(shown);
29
+ now.current = shown;
30
+ useEffect(() => {
31
+ if (id === now.current) return;
32
+ if (reduced) {
33
+ setShown(id);
34
+ return;
35
+ }
36
+ setMoving(true);
37
+ t.value = timing(0, 'fast', 'accelerate');
38
+ let last: ReturnType<typeof setTimeout>;
39
+ const timer = setTimeout(() => {
40
+ setShown(id);
41
+ t.value = timing(1, 'normal', 'decelerate');
42
+ last = setTimeout(() => setMoving(false), motion.duration.normal);
43
+ }, motion.duration.fast);
44
+ return () => {
45
+ clearTimeout(timer);
46
+ clearTimeout(last);
47
+ };
48
+ // Only a new `id` starts a relay: showing it must not cut short the one under way.
49
+ }, [id]);
50
+ return { content: id === shown ? children : kept.current, t, moving };
51
+ }
52
+
53
+ export interface SwapProps extends Omit<ViewProps, 'style' | 'id'> {
54
+ /** What is shown: when it changes, the old gives way to the new. */
55
+ id: string | number;
56
+ children?: ReactNode;
57
+ style?: StyleProp<ViewStyle>;
58
+ }
59
+
60
+ export function Swap({ id, children, style, ...rest }: SwapProps) {
61
+ const { content, t } = useSwap(id, children);
62
+ const web = Platform.OS === 'web';
63
+ const body = useAnimatedStyle(() => ({
64
+ opacity: t.value,
65
+ transform: [{ scale: 1 - SHRINK * (1 - t.value) }],
66
+ ...(web ? { filter: `blur(${(1 - t.value) * BLUR}px)` } : null),
67
+ }));
68
+ // Its height changes with what it shows: what is below glides, where layout animations exist.
69
+ return (
70
+ <Animated.View layout={presence().layout} {...rest} style={[style, body]}>
71
+ {content}
72
+ </Animated.View>
73
+ );
74
+ }
package/src/Tabs.tsx ADDED
@@ -0,0 +1,84 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { I18nManager, Pressable, View, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
3
+ import Animated, { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
4
+ import { spring } from './motion';
5
+ import { usePending } from './pending';
6
+ import { Text } from './Text';
7
+ import { useTheme } from './theme';
8
+
9
+ export interface TabItem {
10
+ key: string;
11
+ label: string;
12
+ }
13
+
14
+ export interface TabsProps extends Omit<ViewProps, 'style' | 'children'> {
15
+ items: readonly TabItem[];
16
+ /** The key of the chosen one. */
17
+ value: string;
18
+ /** If it returns a promise, the pressed tab waits for it: the mark goes to it at once, and it fills. If it fails, the mark goes back. */
19
+ onChange: (key: string) => void | Promise<unknown>;
20
+ /** The name of the group, for a screen reader. */
21
+ label?: string;
22
+ style?: StyleProp<ViewStyle>;
23
+ }
24
+
25
+ function Tab({ label, on, waiting, failed, height, onPress }: { label: string; on: boolean; waiting: boolean; failed: boolean; height: number; onPress: () => void }) {
26
+ const theme = useTheme();
27
+ const pending = usePending(waiting, theme.color['text.default'], failed);
28
+ return (
29
+ <Pressable
30
+ role="tab"
31
+ aria-selected={on}
32
+ aria-busy={waiting}
33
+ onPress={onPress}
34
+ onPressIn={pending.onPressIn}
35
+ onLayout={pending.onLayout}
36
+ hitSlop={Math.max(0, (theme.size['target.min'] - height) / 2)}
37
+ style={{ flex: 1, minHeight: height, alignItems: 'center', justifyContent: 'center', overflow: 'hidden', borderRadius: theme.size['radius.control'], paddingInline: theme.size['space.2'] }}
38
+ >
39
+ {pending.fill}
40
+ <Text variant="label" tone={on ? 'default' : 'muted'} numberOfLines={1}>
41
+ {label}
42
+ </Text>
43
+ </Pressable>
44
+ );
45
+ }
46
+
47
+ /** A few views of the same thing, one chosen. The mark slides to it on the smooth spring, moving only its transform. */
48
+ export function Tabs({ items, value, onChange, label, style, ...rest }: TabsProps) {
49
+ const theme = useTheme();
50
+ const pad = theme.size['space.1'] / 2;
51
+ const [waitingFor, setWaitingFor] = useState<string | null>(null);
52
+ const [failedFor, setFailedFor] = useState<string | null>(null);
53
+ // The pressed tab is marked at once, as on the web: it does not wait for the answer to look chosen.
54
+ const index = Math.max(0, items.findIndex((i) => i.key === (waitingFor ?? value)));
55
+ const choose = (key: string): void => {
56
+ const result = onChange(key) as PromiseLike<unknown> | undefined;
57
+ if (typeof result?.then !== 'function') return;
58
+ setWaitingFor(key);
59
+ setFailedFor(null);
60
+ const done = (): void => setWaitingFor((now) => (now === key ? null : now));
61
+ result.then(done, () => {
62
+ done();
63
+ setFailedFor(key);
64
+ });
65
+ };
66
+ const [width, setWidth] = useState(0);
67
+ const step = items.length ? (width - 2 * pad) / items.length : 0;
68
+ const at = useSharedValue(index);
69
+ useEffect(() => {
70
+ at.value = spring(index, 'smooth');
71
+ }, [index, at]);
72
+ // In a language written from the right, the first tab is at the right.
73
+ const way = I18nManager.isRTL ? -1 : 1;
74
+ const mark = useAnimatedStyle(() => ({ transform: [{ translateX: way * at.value * step }] }));
75
+ const height = theme.size['control.h.md'];
76
+ return (
77
+ <View role="tablist" aria-label={label} {...rest} onLayout={(e) => setWidth(e.nativeEvent.layout.width)} style={[{ flexDirection: 'row', padding: pad, borderRadius: theme.size['radius.control'], backgroundColor: theme.color['surface.sunken'] }, style]}>
78
+ {step > 0 && <Animated.View style={[{ position: 'absolute', top: pad, bottom: pad, start: pad, width: step, borderRadius: theme.size['radius.control'] - pad, backgroundColor: theme.color['surface.raised'] }, mark]} />}
79
+ {items.map((item) => (
80
+ <Tab key={item.key} label={item.label} on={item.key === (waitingFor ?? value)} waiting={item.key === waitingFor} failed={item.key === failedFor} height={height - 2 * pad} onPress={() => choose(item.key)} />
81
+ ))}
82
+ </View>
83
+ );
84
+ }
package/src/Text.tsx ADDED
@@ -0,0 +1,44 @@
1
+ import type { ComponentRef, Ref } from 'react';
2
+ import { Text as NativeText, type TextProps as NativeTextProps, type TextStyle } from 'react-native';
3
+ import { useTheme, type Theme } from './theme';
4
+
5
+ export type TextVariant = 'display' | 'heading' | 'title' | 'body' | 'label' | 'caption';
6
+ export type TextWeight = 'regular' | 'medium' | 'bold';
7
+ export type TextTone = 'default' | 'muted' | 'accent' | 'disabled' | 'on-accent' | 'inverse';
8
+
9
+ export interface TextProps extends Omit<NativeTextProps, 'ref'> {
10
+ /** Its place in the type scale. Default: 'body'. (`role` is taken: it is the ARIA role of React Native.) */
11
+ variant?: TextVariant;
12
+ /** Default: 'default'. */
13
+ tone?: TextTone;
14
+ /** Default: the one of its variant. */
15
+ weight?: TextWeight;
16
+ ref?: Ref<ComponentRef<typeof NativeText>>;
17
+ }
18
+
19
+ const TITLES: readonly TextVariant[] = ['display', 'heading', 'title'];
20
+
21
+ /** The family and the weight of a face: the app's own font if it loaded one for that weight. */
22
+ const face = (theme: Theme, weight: TextWeight): TextStyle => {
23
+ const own = theme.font?.[weight];
24
+ if (own) return { fontFamily: own };
25
+ return { fontFamily: theme.font?.regular, fontWeight: theme.weight[`font.weight.${weight}`] as TextStyle['fontWeight'] };
26
+ };
27
+
28
+ /** The style of a variant: its size, its line and its weight, from the theme. */
29
+ export function textStyle(theme: Theme, variant: TextVariant = 'body', tone: TextTone = 'default', weight?: TextWeight): TextStyle {
30
+ const size = theme.size[`type.${variant}.size`];
31
+ const title = TITLES.includes(variant);
32
+ return {
33
+ color: theme.color[`text.${tone}`],
34
+ fontSize: size,
35
+ lineHeight: Math.round(size * theme.number[title ? 'type.heading.line' : 'type.body.line']),
36
+ ...face(theme, weight ?? (title ? 'bold' : variant === 'label' ? 'medium' : 'regular')),
37
+ };
38
+ }
39
+
40
+ /** Text in one of the six variants of the type scale; a title is a heading for a screen reader. It scales with the system's text size. */
41
+ export function Text({ variant = 'body', tone = 'default', weight, style, ...rest }: TextProps) {
42
+ const theme = useTheme();
43
+ return <NativeText role={TITLES.includes(variant) ? 'heading' : undefined} {...rest} style={[textStyle(theme, variant, tone, weight), style]} />;
44
+ }
@@ -0,0 +1,137 @@
1
+ import { useEffect, useState, type ComponentRef, type ReactNode, type Ref } from 'react';
2
+ import { Pressable, TextInput, View, type StyleProp, type TextInputProps, type ViewStyle } from 'react-native';
3
+ import Animated, { interpolateColor, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
4
+ import { timing } from './motion';
5
+ import { presence } from './presence';
6
+ import { Text, textStyle } from './Text';
7
+ import { useTheme } from './theme';
8
+ import { announce } from './Toast';
9
+
10
+ export interface TextFieldProps extends Omit<TextInputProps, 'style' | 'ref'> {
11
+ label: string;
12
+ /** Help under the field. */
13
+ hint?: string;
14
+ /** What is wrong, and how to fix it. It takes the place of the hint. */
15
+ error?: string;
16
+ /** The label is only for a screen reader: a search field, whose place says what it is. */
17
+ labelHidden?: boolean;
18
+ /** Several lines: the field is this many lines tall at least, and grows with what is written. */
19
+ rows?: number;
20
+ /** At the end of the field, inside it: a button to clear it. */
21
+ trailing?: ReactNode;
22
+ /** A password: what is written is hidden, and a button in the field shows it. The two names of that button. */
23
+ reveal?: { show: string; hide: string };
24
+ /** The style of the whole field: its label, its box and its message. */
25
+ style?: StyleProp<ViewStyle>;
26
+ ref?: Ref<ComponentRef<typeof TextInput>>;
27
+ }
28
+
29
+ /** A text input with its label, its help and its error. A screen reader hears the three with the field. */
30
+ export function TextField({ label, hint, error, labelHidden, rows, trailing, reveal, style, editable = true, onFocus, onBlur, ...rest }: TextFieldProps) {
31
+ const theme = useTheme();
32
+ const c = theme.color;
33
+ const focus = useSharedValue(0);
34
+ const [hidden, setHidden] = useState(true);
35
+ const rest0 = error ? c['state.danger.solid'] : c['input.border'];
36
+ const edge = useAnimatedStyle(() => ({ borderColor: interpolateColor(focus.value, [0, 1], [rest0, c['accent.default']]) }));
37
+ // The ring of the focus: a second line around the edge, always there, so that nothing moves when it shows.
38
+ const lit = c['focus.ring'];
39
+ const ring = useAnimatedStyle(() => ({ borderColor: interpolateColor(focus.value, [0, 1], [`${lit.slice(0, 7)}00`, lit]) }));
40
+ const radius = theme.size['radius.control'];
41
+ const message = error ?? hint;
42
+ // Android reads a live region when it appears; iOS has to be told.
43
+ useEffect(() => {
44
+ if (error) announce(error);
45
+ }, [error]);
46
+ const { layout: _layout, ...coming } = presence();
47
+ return (
48
+ <View style={[{ gap: theme.size['space.1'], opacity: editable ? 1 : 0.5 }, style]}>
49
+ {!labelHidden && <Text variant="label">{label}</Text>}
50
+ <Animated.View style={[{ borderWidth: 1, borderRadius: radius + 1 }, ring]}>
51
+ <Animated.View style={[{ flexDirection: 'row', alignItems: 'center', borderWidth: 1, borderRadius: radius, backgroundColor: c['input.bg'] }, edge]}>
52
+ <TextInput
53
+ aria-label={label}
54
+ accessibilityHint={message}
55
+ placeholderTextColor={c['text.muted']}
56
+ {...rest}
57
+ {...(reveal ? { secureTextEntry: hidden } : null)}
58
+ {...(rows ? { multiline: true, textAlignVertical: 'top' as const } : null)}
59
+ editable={editable}
60
+ onFocus={(e) => {
61
+ focus.value = timing(1, 'fast');
62
+ onFocus?.(e);
63
+ }}
64
+ onBlur={(e) => {
65
+ focus.value = timing(0, 'fast');
66
+ onBlur?.(e);
67
+ }}
68
+ // A field is touched: at least target.min tall, and it grows with the system's text size.
69
+ style={[textStyle(theme), { flex: 1, minHeight: rows ? rows * (textStyle(theme).lineHeight ?? 0) + 2 * theme.size['space.2'] : theme.size['target.min'], paddingHorizontal: theme.size['pad.control-x'], paddingVertical: theme.size['space.2'] }]}
70
+ />
71
+ {trailing}
72
+ {reveal && (
73
+ <Pressable role="button" onPress={() => setHidden((h) => !h)} style={{ minWidth: theme.size['target.min'], minHeight: theme.size['target.min'], alignItems: 'center', justifyContent: 'center', paddingInline: theme.size['space.2'] }}>
74
+ <Text variant="label" tone="accent">
75
+ {hidden ? reveal.show : reveal.hide}
76
+ </Text>
77
+ </Pressable>
78
+ )}
79
+ </Animated.View>
80
+ </Animated.View>
81
+ {message !== undefined && (
82
+ // The key: an error takes the place of the help by coming in, not by changing its letters.
83
+ <Animated.View key={error ? 'error' : 'hint'} {...coming}>
84
+ <Text variant="caption" tone={error ? 'default' : 'muted'} style={error ? { color: c['state.danger.text'] } : undefined} accessibilityLiveRegion={error ? 'polite' : 'none'}>
85
+ {message}
86
+ </Text>
87
+ </Animated.View>
88
+ )}
89
+ </View>
90
+ );
91
+ }
92
+
93
+ /** A text input of several lines: three at least, unless `rows` says otherwise. */
94
+ export function TextArea({ rows = 3, ...rest }: Omit<TextFieldProps, 'reveal' | 'trailing'>) {
95
+ return <TextField rows={rows} {...rest} />;
96
+ }
97
+
98
+ export interface SearchFieldProps extends Omit<TextFieldProps, 'reveal' | 'rows' | 'trailing' | 'labelHidden' | 'onSubmitEditing'> {
99
+ /** With the key «search» of the keyboard. */
100
+ onSearch?: (text: string) => void;
101
+ /** The name of the button that empties it. Default: 'Clear'. */
102
+ clearLabel?: string;
103
+ }
104
+
105
+ /** A field to search with: its label is only for a screen reader, and a button empties it once it has text. */
106
+ export function SearchField({ value, defaultValue = '', onChangeText, onSearch, clearLabel = 'Clear', ...rest }: SearchFieldProps) {
107
+ const theme = useTheme();
108
+ const [own, setOwn] = useState(defaultValue);
109
+ const text = value ?? own;
110
+ const change = (next: string): void => {
111
+ if (value === undefined) setOwn(next);
112
+ onChangeText?.(next);
113
+ };
114
+ return (
115
+ <TextField
116
+ role="searchbox"
117
+ returnKeyType="search"
118
+ autoCapitalize="none"
119
+ autoCorrect={false}
120
+ {...rest}
121
+ labelHidden
122
+ value={text}
123
+ onChangeText={change}
124
+ onSubmitEditing={() => onSearch?.(text)}
125
+ trailing={
126
+ text ? (
127
+ <Pressable role="button" aria-label={clearLabel} onPress={() => change('')} style={{ minWidth: theme.size['target.min'], minHeight: theme.size['target.min'], alignItems: 'center', justifyContent: 'center' }}>
128
+ <Text variant="title" tone="muted" aria-hidden>
129
+ ×
130
+ </Text>
131
+ </Pressable>
132
+ ) : null
133
+ }
134
+ />
135
+ );
136
+ }
137
+