@fluxus-ui/native 0.0.0-stage → 0.3.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 (230) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -2
  3. package/lib/module/AudioPlayer.js +174 -0
  4. package/lib/module/AudioPlayer.js.map +1 -0
  5. package/lib/module/Avatar.js +58 -0
  6. package/lib/module/Avatar.js.map +1 -0
  7. package/lib/module/Badge.js +72 -0
  8. package/lib/module/Badge.js.map +1 -0
  9. package/lib/module/BottomSheet.js +159 -0
  10. package/lib/module/BottomSheet.js.map +1 -0
  11. package/lib/module/Bubble.js +182 -0
  12. package/lib/module/Bubble.js.map +1 -0
  13. package/lib/module/Button.js +209 -0
  14. package/lib/module/Button.js.map +1 -0
  15. package/lib/module/Card.js +79 -0
  16. package/lib/module/Card.js.map +1 -0
  17. package/lib/module/Dialog.js +137 -0
  18. package/lib/module/Dialog.js.map +1 -0
  19. package/lib/module/Disclosure.js +153 -0
  20. package/lib/module/Disclosure.js.map +1 -0
  21. package/lib/module/EmptyState.js +71 -0
  22. package/lib/module/EmptyState.js.map +1 -0
  23. package/lib/module/Link.js +97 -0
  24. package/lib/module/Link.js.map +1 -0
  25. package/lib/module/ListItem.js +89 -0
  26. package/lib/module/ListItem.js.map +1 -0
  27. package/lib/module/Menu.js +76 -0
  28. package/lib/module/Menu.js.map +1 -0
  29. package/lib/module/Message.js +51 -0
  30. package/lib/module/Message.js.map +1 -0
  31. package/lib/module/Popover.js +153 -0
  32. package/lib/module/Popover.js.map +1 -0
  33. package/lib/module/Progress.js +54 -0
  34. package/lib/module/Progress.js.map +1 -0
  35. package/lib/module/Select.js +146 -0
  36. package/lib/module/Select.js.map +1 -0
  37. package/lib/module/Skeleton.js +40 -0
  38. package/lib/module/Skeleton.js.map +1 -0
  39. package/lib/module/Slider.js +137 -0
  40. package/lib/module/Slider.js.map +1 -0
  41. package/lib/module/Spinner.js +30 -0
  42. package/lib/module/Spinner.js.map +1 -0
  43. package/lib/module/Swap.js +87 -0
  44. package/lib/module/Swap.js.map +1 -0
  45. package/lib/module/Tabs.js +118 -0
  46. package/lib/module/Tabs.js.map +1 -0
  47. package/lib/module/Text.js +47 -0
  48. package/lib/module/Text.js.map +1 -0
  49. package/lib/module/TextField.js +190 -0
  50. package/lib/module/TextField.js.map +1 -0
  51. package/lib/module/Toast.js +157 -0
  52. package/lib/module/Toast.js.map +1 -0
  53. package/lib/module/audio.js +54 -0
  54. package/lib/module/audio.js.map +1 -0
  55. package/lib/module/blur.js +55 -0
  56. package/lib/module/blur.js.map +1 -0
  57. package/lib/module/controls.js +292 -0
  58. package/lib/module/controls.js.map +1 -0
  59. package/lib/module/feel.js +24 -0
  60. package/lib/module/feel.js.map +1 -0
  61. package/lib/module/generated/tokens.js +484 -0
  62. package/lib/module/generated/tokens.js.map +1 -0
  63. package/lib/module/haptics.js +25 -0
  64. package/lib/module/haptics.js.map +1 -0
  65. package/lib/module/icon-host.js +42 -0
  66. package/lib/module/icon-host.js.map +1 -0
  67. package/lib/module/icon-shape.js +222 -0
  68. package/lib/module/icon-shape.js.map +1 -0
  69. package/lib/module/icon.js +331 -0
  70. package/lib/module/icon.js.map +1 -0
  71. package/lib/module/index.js +39 -0
  72. package/lib/module/index.js.map +1 -0
  73. package/lib/module/layout.js +75 -0
  74. package/lib/module/layout.js.map +1 -0
  75. package/lib/module/marks.js +99 -0
  76. package/lib/module/marks.js.map +1 -0
  77. package/lib/module/motion.js +95 -0
  78. package/lib/module/motion.js.map +1 -0
  79. package/lib/module/overlay.js +99 -0
  80. package/lib/module/overlay.js.map +1 -0
  81. package/lib/module/package.json +1 -0
  82. package/lib/module/pending.js +181 -0
  83. package/lib/module/pending.js.map +1 -0
  84. package/lib/module/presence.js +71 -0
  85. package/lib/module/presence.js.map +1 -0
  86. package/lib/module/press.js +32 -0
  87. package/lib/module/press.js.map +1 -0
  88. package/lib/module/screen.js +50 -0
  89. package/lib/module/screen.js.map +1 -0
  90. package/lib/module/success.js +59 -0
  91. package/lib/module/success.js.map +1 -0
  92. package/lib/module/theme.js +41 -0
  93. package/lib/module/theme.js.map +1 -0
  94. package/lib/typescript/AudioPlayer.d.ts +26 -0
  95. package/lib/typescript/AudioPlayer.d.ts.map +1 -0
  96. package/lib/typescript/Avatar.d.ts +17 -0
  97. package/lib/typescript/Avatar.d.ts.map +1 -0
  98. package/lib/typescript/Badge.d.ts +21 -0
  99. package/lib/typescript/Badge.d.ts.map +1 -0
  100. package/lib/typescript/BottomSheet.d.ts +27 -0
  101. package/lib/typescript/BottomSheet.d.ts.map +1 -0
  102. package/lib/typescript/Bubble.d.ts +23 -0
  103. package/lib/typescript/Bubble.d.ts.map +1 -0
  104. package/lib/typescript/Button.d.ts +29 -0
  105. package/lib/typescript/Button.d.ts.map +1 -0
  106. package/lib/typescript/Card.d.ts +17 -0
  107. package/lib/typescript/Card.d.ts.map +1 -0
  108. package/lib/typescript/Dialog.d.ts +34 -0
  109. package/lib/typescript/Dialog.d.ts.map +1 -0
  110. package/lib/typescript/Disclosure.d.ts +39 -0
  111. package/lib/typescript/Disclosure.d.ts.map +1 -0
  112. package/lib/typescript/EmptyState.d.ts +14 -0
  113. package/lib/typescript/EmptyState.d.ts.map +1 -0
  114. package/lib/typescript/Link.d.ts +23 -0
  115. package/lib/typescript/Link.d.ts.map +1 -0
  116. package/lib/typescript/ListItem.d.ts +20 -0
  117. package/lib/typescript/ListItem.d.ts.map +1 -0
  118. package/lib/typescript/Menu.d.ts +23 -0
  119. package/lib/typescript/Menu.d.ts.map +1 -0
  120. package/lib/typescript/Message.d.ts +14 -0
  121. package/lib/typescript/Message.d.ts.map +1 -0
  122. package/lib/typescript/Popover.d.ts +71 -0
  123. package/lib/typescript/Popover.d.ts.map +1 -0
  124. package/lib/typescript/Progress.d.ts +12 -0
  125. package/lib/typescript/Progress.d.ts.map +1 -0
  126. package/lib/typescript/Select.d.ts +30 -0
  127. package/lib/typescript/Select.d.ts.map +1 -0
  128. package/lib/typescript/Skeleton.d.ts +13 -0
  129. package/lib/typescript/Skeleton.d.ts.map +1 -0
  130. package/lib/typescript/Slider.d.ts +21 -0
  131. package/lib/typescript/Slider.d.ts.map +1 -0
  132. package/lib/typescript/Spinner.d.ts +12 -0
  133. package/lib/typescript/Spinner.d.ts.map +1 -0
  134. package/lib/typescript/Swap.d.ts +25 -0
  135. package/lib/typescript/Swap.d.ts.map +1 -0
  136. package/lib/typescript/Tabs.d.ts +18 -0
  137. package/lib/typescript/Tabs.d.ts.map +1 -0
  138. package/lib/typescript/Text.d.ts +20 -0
  139. package/lib/typescript/Text.d.ts.map +1 -0
  140. package/lib/typescript/TextField.d.ts +36 -0
  141. package/lib/typescript/TextField.d.ts.map +1 -0
  142. package/lib/typescript/Toast.d.ts +25 -0
  143. package/lib/typescript/Toast.d.ts.map +1 -0
  144. package/lib/typescript/audio.d.ts +10 -0
  145. package/lib/typescript/audio.d.ts.map +1 -0
  146. package/lib/typescript/blur.d.ts +4 -0
  147. package/lib/typescript/blur.d.ts.map +1 -0
  148. package/lib/typescript/controls.d.ts +37 -0
  149. package/lib/typescript/controls.d.ts.map +1 -0
  150. package/lib/typescript/feel.d.ts +10 -0
  151. package/lib/typescript/feel.d.ts.map +1 -0
  152. package/lib/typescript/generated/tokens.d.ts +499 -0
  153. package/lib/typescript/generated/tokens.d.ts.map +1 -0
  154. package/lib/typescript/haptics.d.ts +7 -0
  155. package/lib/typescript/haptics.d.ts.map +1 -0
  156. package/lib/typescript/icon-host.d.ts +30 -0
  157. package/lib/typescript/icon-host.d.ts.map +1 -0
  158. package/lib/typescript/icon-shape.d.ts +28 -0
  159. package/lib/typescript/icon-shape.d.ts.map +1 -0
  160. package/lib/typescript/icon.d.ts +24 -0
  161. package/lib/typescript/icon.d.ts.map +1 -0
  162. package/lib/typescript/index.d.ts +37 -0
  163. package/lib/typescript/index.d.ts.map +1 -0
  164. package/lib/typescript/layout.d.ts +24 -0
  165. package/lib/typescript/layout.d.ts.map +1 -0
  166. package/lib/typescript/marks.d.ts +23 -0
  167. package/lib/typescript/marks.d.ts.map +1 -0
  168. package/lib/typescript/motion.d.ts +30 -0
  169. package/lib/typescript/motion.d.ts.map +1 -0
  170. package/lib/typescript/overlay.d.ts +33 -0
  171. package/lib/typescript/overlay.d.ts.map +1 -0
  172. package/lib/typescript/package.json +1 -0
  173. package/lib/typescript/pending.d.ts +26 -0
  174. package/lib/typescript/pending.d.ts.map +1 -0
  175. package/lib/typescript/presence.d.ts +37 -0
  176. package/lib/typescript/presence.d.ts.map +1 -0
  177. package/lib/typescript/press.d.ts +12 -0
  178. package/lib/typescript/press.d.ts.map +1 -0
  179. package/lib/typescript/screen.d.ts +21 -0
  180. package/lib/typescript/screen.d.ts.map +1 -0
  181. package/lib/typescript/success.d.ts +13 -0
  182. package/lib/typescript/success.d.ts.map +1 -0
  183. package/lib/typescript/theme.d.ts +32 -0
  184. package/lib/typescript/theme.d.ts.map +1 -0
  185. package/package.json +123 -3
  186. package/src/AudioPlayer.tsx +106 -0
  187. package/src/Avatar.tsx +44 -0
  188. package/src/Badge.tsx +45 -0
  189. package/src/BottomSheet.tsx +109 -0
  190. package/src/Bubble.tsx +112 -0
  191. package/src/Button.tsx +168 -0
  192. package/src/Card.tsx +70 -0
  193. package/src/Dialog.tsx +93 -0
  194. package/src/Disclosure.tsx +108 -0
  195. package/src/EmptyState.tsx +51 -0
  196. package/src/Link.tsx +61 -0
  197. package/src/ListItem.tsx +78 -0
  198. package/src/Menu.tsx +57 -0
  199. package/src/Message.tsx +43 -0
  200. package/src/Popover.tsx +145 -0
  201. package/src/Progress.tsx +33 -0
  202. package/src/Select.tsx +118 -0
  203. package/src/Skeleton.tsx +28 -0
  204. package/src/Slider.tsx +107 -0
  205. package/src/Spinner.tsx +18 -0
  206. package/src/Swap.tsx +74 -0
  207. package/src/Tabs.tsx +84 -0
  208. package/src/Text.tsx +44 -0
  209. package/src/TextField.tsx +137 -0
  210. package/src/Toast.tsx +122 -0
  211. package/src/audio.tsx +56 -0
  212. package/src/blur.tsx +33 -0
  213. package/src/controls.tsx +224 -0
  214. package/src/feel.ts +22 -0
  215. package/src/generated/tokens.ts +653 -0
  216. package/src/haptics.tsx +26 -0
  217. package/src/icon-host.tsx +46 -0
  218. package/src/icon-shape.ts +213 -0
  219. package/src/icon.tsx +276 -0
  220. package/src/index.ts +36 -0
  221. package/src/layout.tsx +40 -0
  222. package/src/marks.tsx +79 -0
  223. package/src/motion.ts +94 -0
  224. package/src/overlay.tsx +77 -0
  225. package/src/pending.tsx +135 -0
  226. package/src/presence.tsx +68 -0
  227. package/src/press.ts +23 -0
  228. package/src/screen.tsx +37 -0
  229. package/src/success.tsx +48 -0
  230. package/src/theme.tsx +49 -0
package/src/Badge.tsx ADDED
@@ -0,0 +1,45 @@
1
+ import type { ReactNode } from 'react';
2
+ import { View, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
3
+ import { Text } from './Text';
4
+ import { useTheme, type Theme } from './theme';
5
+
6
+ export type Tone = 'neutral' | 'accent' | 'success' | 'warning' | 'danger' | 'info';
7
+
8
+ export interface BadgeProps extends Omit<ViewProps, 'style'> {
9
+ children: ReactNode;
10
+ /** Default: 'neutral'. */
11
+ tone?: Tone;
12
+ /** 'dot': a point of the tone before the text, on a quiet ground. Default: 'tint'. */
13
+ look?: 'tint' | 'dot';
14
+ style?: StyleProp<ViewStyle>;
15
+ }
16
+
17
+ /** The ground, the ink and the solid colour of a tone. */
18
+ export function toneColors(theme: Theme, tone: Tone = 'neutral'): { subtle: string; ink: string; solid: string } {
19
+ const c = theme.color;
20
+ if (tone === 'neutral') return { subtle: c['surface.sunken'], ink: c['text.muted'], solid: c['text.muted'] };
21
+ if (tone === 'accent') return { subtle: c['accent.subtle'], ink: c['text.accent'], solid: c['accent.default'] };
22
+ return { subtle: c[`state.${tone}.subtle`], ink: c[`state.${tone}.text`], solid: c[`state.${tone}.solid`] };
23
+ }
24
+
25
+ /** A state in a word. The tone is never the only thing that says it: the text does. */
26
+ export function Badge({ children, tone = 'neutral', look = 'tint', style, ...rest }: BadgeProps) {
27
+ const theme = useTheme();
28
+ const { subtle, ink, solid } = toneColors(theme, tone);
29
+ const dot = look === 'dot';
30
+ const size = theme.size['space.2'];
31
+ return (
32
+ <View
33
+ {...rest}
34
+ style={[
35
+ { flexDirection: 'row', alignItems: 'center', alignSelf: 'flex-start', gap: theme.size['space.1'], paddingHorizontal: theme.size['space.2'], paddingVertical: theme.size['space.1'] / 2, borderRadius: theme.size['radius.pill'], backgroundColor: dot ? theme.color['surface.sunken'] : subtle },
36
+ style,
37
+ ]}
38
+ >
39
+ {dot && <View style={{ width: size, height: size, borderRadius: size, backgroundColor: solid }} />}
40
+ <Text variant="caption" weight="medium" style={{ color: dot ? theme.color['text.default'] : ink }}>
41
+ {children}
42
+ </Text>
43
+ </View>
44
+ );
45
+ }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * A sheet that rises from the bottom, moved only by its transform. As tall as what it holds, up to nine tenths
3
+ * of the screen. Dragged down from its header, it goes; so it does with its handle, which is a button with a
4
+ * name for whoever cannot drag. (SPEC §15.8)
5
+ */
6
+ import { useRef, useState, type ComponentRef, type ReactNode, type RefObject } from 'react';
7
+ import { PanResponder, Pressable, ScrollView, Text as NativeText, useWindowDimensions, View } from 'react-native';
8
+ import Animated, { useAnimatedStyle, useSharedValue, type SharedValue } from 'react-native-reanimated';
9
+ import { spring } from './motion';
10
+ import { floating, focusOn, Overlay } from './overlay';
11
+ import { Text } from './Text';
12
+ import { useTheme } from './theme';
13
+
14
+ /** How much of the screen it may take, how far down it must be let go to close, and how fast a flick is. */
15
+ const TALLEST = 0.9;
16
+ const CLOSE_AT = 0.3;
17
+ const FLICK = 0.8;
18
+
19
+ export interface BottomSheetProps {
20
+ open: boolean;
21
+ /** A press outside, back, the handle, or dragging it down. */
22
+ onClose: () => void;
23
+ /** Its name: at the top, and what a screen reader hears on opening. */
24
+ title: string;
25
+ /** The title is only for a screen reader. */
26
+ titleHidden?: boolean;
27
+ children?: ReactNode;
28
+ /** What the bottom edge of the screen takes: the safe area of the app. It is added to its own padding. Default: 0. */
29
+ inset?: number;
30
+ /** The name of the handle, for a screen reader. Default: 'Close'. */
31
+ closeLabel?: string;
32
+ /** A press outside or a drag closes it, and it has a handle. Default: true. Back always closes. */
33
+ dismissible?: boolean;
34
+ /** What does not fit scrolls. Default: true; false when what it holds scrolls by itself. */
35
+ scroll?: boolean;
36
+ testID?: string;
37
+ }
38
+
39
+ function Sheet({ t, onClose, title, titleHidden, children, inset, closeLabel = 'Close', dismissible = true, scroll = true, testID, heading }: Omit<BottomSheetProps, 'open'> & { t: SharedValue<number>; heading: RefObject<ComponentRef<typeof NativeText> | null> }) {
40
+ const theme = useTheme();
41
+ const { height: screen } = useWindowDimensions();
42
+ const [height, setHeight] = useState(0);
43
+ const drag = useSharedValue(0);
44
+ // Until it is measured it waits below the screen, wherever that is.
45
+ const far = height || screen;
46
+ const rise = useAnimatedStyle(() => ({ transform: [{ translateY: (1 - t.value) * far + drag.value }] }));
47
+ const latest = useRef({ height, onClose });
48
+ latest.current = { height, onClose };
49
+ const [pan] = useState(() =>
50
+ PanResponder.create({
51
+ // Only a drag down, and only from the header: what it holds may scroll.
52
+ onMoveShouldSetPanResponder: (_, g) => g.dy > 4 && g.dy > Math.abs(g.dx),
53
+ onPanResponderMove: (_, g) => {
54
+ drag.value = Math.max(0, g.dy);
55
+ },
56
+ onPanResponderRelease: (_, g) => {
57
+ if (g.dy > latest.current.height * CLOSE_AT || g.vy > FLICK) latest.current.onClose();
58
+ else drag.value = spring(0, 'snappy');
59
+ },
60
+ onPanResponderTerminate: () => {
61
+ drag.value = spring(0, 'snappy');
62
+ },
63
+ }),
64
+ );
65
+ const radius = theme.size['radius.card'];
66
+ const pad = theme.size['space.4'];
67
+ const body = <View style={{ paddingInline: pad, paddingBottom: pad + (inset ?? 0) }}>{children}</View>;
68
+ return (
69
+ <Animated.View
70
+ role="dialog"
71
+ aria-modal
72
+ accessibilityViewIsModal
73
+ aria-label={title}
74
+ testID={testID}
75
+ onLayout={(e) => setHeight(e.nativeEvent.layout.height)}
76
+ style={[floating(theme), { position: 'absolute', start: 0, end: 0, bottom: 0, maxHeight: screen * TALLEST, borderBottomWidth: 0, borderBottomStartRadius: 0, borderBottomEndRadius: 0, borderTopStartRadius: radius, borderTopEndRadius: radius }, rise]}
77
+ >
78
+ <View {...(dismissible ? pan.panHandlers : null)} style={{ paddingInline: pad, paddingBottom: theme.size['space.3'] }}>
79
+ {dismissible && (
80
+ <Pressable role="button" aria-label={closeLabel} onPress={onClose} hitSlop={theme.size['space.3']} style={{ alignSelf: 'center', paddingBlock: theme.size['space.2'] }}>
81
+ <View style={{ width: theme.size['space.10'], height: theme.size['space.1'], borderRadius: theme.size['space.1'], backgroundColor: theme.color['border.strong'] }} />
82
+ </Pressable>
83
+ )}
84
+ {!titleHidden && (
85
+ // Not selectable: in a browser, a drag that selects text is not a drag of the sheet.
86
+ <Text ref={heading} variant="title" selectable={false} style={dismissible ? null : { paddingTop: pad }}>
87
+ {title}
88
+ </Text>
89
+ )}
90
+ </View>
91
+ {scroll ? (
92
+ <ScrollView bounces={false} keyboardShouldPersistTaps="handled">
93
+ {body}
94
+ </ScrollView>
95
+ ) : (
96
+ body
97
+ )}
98
+ </Animated.View>
99
+ );
100
+ }
101
+
102
+ export function BottomSheet({ open, onClose, dismissible = true, ...rest }: BottomSheetProps) {
103
+ const heading = useRef<ComponentRef<typeof NativeText>>(null);
104
+ return (
105
+ <Overlay open={open} onClose={onClose} dismissible={dismissible} onShow={() => focusOn(heading.current)}>
106
+ {(t) => <Sheet t={t} onClose={onClose} dismissible={dismissible} heading={heading} {...rest} />}
107
+ </Overlay>
108
+ );
109
+ }
package/src/Bubble.tsx ADDED
@@ -0,0 +1,112 @@
1
+ /**
2
+ * A message of a chat: on the side of who wrote it, never wider than most of the screen, with who above and
3
+ * when below. A plain view: a chat has many, and one that arrives is wrapped in `Appear`. With `status`, one's
4
+ * own says how it went: sending, sent (a tick is written), read (a second one), failed (it shakes once).
5
+ * (SPEC §15.10, §15.14)
6
+ */
7
+ import { useEffect, useRef, type ReactNode } from 'react';
8
+ import { View, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
9
+ import Animated, { useAnimatedStyle, useReducedMotion, useSharedValue } from 'react-native-reanimated';
10
+ import { motion } from './generated/tokens';
11
+ import { BANG, Mark, TICK } from './marks';
12
+ import { withFluxSpring } from './motion';
13
+ import { Text } from './Text';
14
+ import { useTheme } from './theme';
15
+
16
+ export type BubbleStatus = 'sending' | 'sent' | 'read' | 'failed';
17
+
18
+ export interface BubbleProps extends Omit<ViewProps, 'style'> {
19
+ /** 'start': what arrives. 'end': one's own. */
20
+ side: 'start' | 'end';
21
+ /** Who wrote it, above. */
22
+ author?: string;
23
+ /** When, or whether it was read, below. Say the status in it too: the mark is not read out. */
24
+ meta?: string;
25
+ /** How a message of one's own went. With it, the bubble is animated; without, a plain view. */
26
+ status?: BubbleStatus;
27
+ children?: ReactNode;
28
+ style?: StyleProp<ViewStyle>;
29
+ }
30
+
31
+ /** How much of the width it may take. */
32
+ const WIDEST = '88%';
33
+
34
+ export function Bubble({ side, author, meta, status, children, style, ...rest }: BubbleProps) {
35
+ const theme = useTheme();
36
+ const own = side === 'end';
37
+ const radius = theme.size['radius.card'];
38
+ const corner = theme.size['radius.1'];
39
+ const box: StyleProp<ViewStyle> = [
40
+ { alignSelf: own ? 'flex-end' : 'flex-start', maxWidth: WIDEST, gap: theme.size['space.1'], padding: theme.size['space.3'], borderWidth: 1, borderRadius: radius },
41
+ // The corner of its side is tighter: it says whose it is without a tail.
42
+ own ? { borderBottomEndRadius: corner, borderColor: 'transparent', backgroundColor: theme.color['accent.subtle'] } : { borderBottomStartRadius: corner, borderColor: theme.color['border.default'], backgroundColor: theme.color['surface.raised'] },
43
+ style,
44
+ ];
45
+ const body = (
46
+ <>
47
+ {author !== undefined && (
48
+ <Text variant="caption" weight="medium" tone="muted">
49
+ {author}
50
+ </Text>
51
+ )}
52
+ {typeof children === 'string' ? <Text>{children}</Text> : children}
53
+ </>
54
+ );
55
+ if (status === undefined) {
56
+ return (
57
+ <View {...rest} style={box}>
58
+ {body}
59
+ {meta !== undefined && (
60
+ <Text variant="caption" tone="muted" style={{ alignSelf: 'flex-end' }}>
61
+ {meta}
62
+ </Text>
63
+ )}
64
+ </View>
65
+ );
66
+ }
67
+ return (
68
+ <Shaken {...rest} status={status} style={box}>
69
+ {body}
70
+ <View style={{ alignSelf: 'flex-end', flexDirection: 'row', alignItems: 'center', gap: theme.size['space.1'] }}>
71
+ {meta !== undefined && (
72
+ <Text variant="caption" tone="muted">
73
+ {meta}
74
+ </Text>
75
+ )}
76
+ <StatusMark status={status} />
77
+ </View>
78
+ </Shaken>
79
+ );
80
+ }
81
+
82
+ /** It shakes once when it fails: pushed sideways, brought back by the bouncy spring. */
83
+ function Shaken({ status, style, children, ...rest }: ViewProps & { status: BubbleStatus }) {
84
+ const reduced = useReducedMotion();
85
+ const x = useSharedValue(0);
86
+ const was = useRef(status);
87
+ useEffect(() => {
88
+ if (status === 'failed' && was.current !== 'failed' && !reduced) x.value = withFluxSpring(0, { ...motion.spring.bouncy, velocity: 160 });
89
+ was.current = status;
90
+ }, [status, reduced, x]);
91
+ const shake = useAnimatedStyle(() => ({ transform: [{ translateX: x.value }] }));
92
+ return (
93
+ <Animated.View {...rest} style={[style, shake]}>
94
+ {children}
95
+ </Animated.View>
96
+ );
97
+ }
98
+
99
+ /** Sending: a ring, still. Sent: a tick is written. Read: a second one. Failed: an exclamation. */
100
+ function StatusMark({ status }: { status: BubbleStatus }) {
101
+ const theme = useTheme();
102
+ const side = theme.size['icon.sm'];
103
+ const ink = theme.color['text.muted'];
104
+ return (
105
+ <View style={{ width: side * 1.4, height: side }}>
106
+ {status === 'sending' && <View testID="status-sending" style={{ position: 'absolute', left: side * 0.2, top: side * 0.2, width: side * 0.6, height: side * 0.6, borderRadius: side, borderWidth: 1.5, borderColor: ink }} />}
107
+ <Mark testID="status-sent" segments={TICK} shown={status === 'sent' || status === 'read'} size={side} color={ink} style={{ position: 'absolute' }} />
108
+ <Mark testID="status-read" segments={TICK} shown={status === 'read'} size={side} color={ink} style={{ position: 'absolute', left: side * 0.4 }} />
109
+ <Mark testID="status-failed" segments={BANG} shown={status === 'failed'} size={side} color={theme.color['state.danger.text']} style={{ position: 'absolute', left: side * 0.2 }} />
110
+ </View>
111
+ );
112
+ }
package/src/Button.tsx ADDED
@@ -0,0 +1,168 @@
1
+ import { useEffect, useRef, useState, type ComponentRef, type ReactNode, type Ref } from 'react';
2
+ import { ActivityIndicator, Pressable, type GestureResponderEvent, type PressableProps, type StyleProp, type ViewStyle } from 'react-native';
3
+ import Animated, { useAnimatedStyle, useReducedMotion, useSharedValue, withDelay } from 'react-native-reanimated';
4
+ import { feel } from './feel';
5
+ import { motion } from './generated/tokens';
6
+ import { IconHostProvider, isOn, useHostFor } from './icon-host';
7
+ import { Mark, TICK } from './marks';
8
+ import { ms, timing, withFluxSpring } from './motion';
9
+ import { useAsyncPress, usePending } from './pending';
10
+ import { presence } from './presence';
11
+ import { usePress } from './press';
12
+ import { Text } from './Text';
13
+ import { useTheme, type Theme } from './theme';
14
+
15
+ export type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'danger';
16
+ export type ButtonSize = 'sm' | 'md' | 'lg';
17
+
18
+ export interface ButtonProps extends Omit<PressableProps, 'children' | 'style' | 'ref' | 'onPress'> {
19
+ /** If it returns a promise, the button waits for it: it fills from where it was pressed and takes no other press. */
20
+ onPress?: (event: GestureResponderEvent) => void | Promise<unknown>;
21
+ children: ReactNode;
22
+ /** Default: 'secondary'. */
23
+ variant?: ButtonVariant;
24
+ /** Default: 'md'. */
25
+ size?: ButtonSize;
26
+ /** It is working: it shows a spinner, says so to a screen reader and takes no press. */
27
+ busy?: boolean;
28
+ /** When what its press returns is fulfilled, a tick is drawn where its text was, and the text comes back; if it fails, the text shakes once. */
29
+ feedback?: boolean;
30
+ style?: StyleProp<ViewStyle>;
31
+ ref?: Ref<ComponentRef<typeof Pressable>>;
32
+ }
33
+
34
+ const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
35
+
36
+ /** The surface, the ink and the edge of a variant, and the box of a size: only tokens. */
37
+ export function buttonStyle(theme: Theme, variant: ButtonVariant = 'secondary', size: ButtonSize = 'md'): { box: ViewStyle; ink: string; slop: number } {
38
+ const c = theme.color;
39
+ const [surface, ink, edge] = {
40
+ primary: [c['button.bg'], c['button.fg'], 'transparent'],
41
+ secondary: [c['surface.raised'], c['text.default'], c['border.control']],
42
+ ghost: ['transparent', c['text.default'], 'transparent'],
43
+ danger: [c['state.danger.subtle'], c['state.danger.text'], 'transparent'],
44
+ }[variant] as [string, string, string];
45
+ const height = theme.size[`control.h.${size}`];
46
+ return {
47
+ box: {
48
+ // A minimum, not a height: the text grows with the system's text size.
49
+ minHeight: height,
50
+ flexDirection: 'row',
51
+ alignItems: 'center',
52
+ justifyContent: 'center',
53
+ gap: theme.size['space.2'],
54
+ paddingHorizontal: theme.size['pad.control-x'],
55
+ borderWidth: 1,
56
+ borderColor: edge,
57
+ borderRadius: theme.size['button.radius'],
58
+ backgroundColor: surface,
59
+ },
60
+ ink,
61
+ // A small button is still a target of target.min: the rest is around it.
62
+ slop: Math.max(0, (theme.size['target.min'] - height) / 2),
63
+ };
64
+ }
65
+
66
+ /** A button with the variants and sizes of the web; pressed, it gives on the snappy spring. */
67
+ export function Button({ children, variant = 'secondary', size = 'md', busy = false, feedback = false, disabled, style, onPress, onPressIn, onPressOut, onLayout, onFocus, onBlur, ...rest }: ButtonProps) {
68
+ const theme = useTheme();
69
+ const { box, ink, slop } = buttonStyle(theme, variant, size);
70
+ const answer = useFeedback(feedback);
71
+ const [waiting, run, failed] = useAsyncPress(onPress, answer.ended);
72
+ const pending = usePending(waiting, ink, failed);
73
+ const off = !!disabled || busy || waiting;
74
+ const press = usePress({ onPressIn, onPressOut });
75
+ // An icon inside is open with the button, and is pushed when it is pressed.
76
+ const icons = useHostFor(isOn(rest), ink);
77
+ const { layout, ...coming } = presence();
78
+ // With a keyboard or a remote: a ring around it, drawn outside so that nothing moves.
79
+ const [focused, setFocused] = useState(false);
80
+ const ring: ViewStyle | null = focused ? { outlineStyle: 'solid', outlineWidth: theme.size['focus.width'], outlineOffset: theme.size['focus.offset'], outlineColor: theme.color['focus.ring'] } : null;
81
+ return (
82
+ <AnimatedPressable
83
+ role="button"
84
+ hitSlop={slop}
85
+ {...rest}
86
+ disabled={off}
87
+ aria-disabled={off}
88
+ aria-busy={busy || waiting}
89
+ onPress={(e) => {
90
+ icons.push('press');
91
+ run(e);
92
+ }}
93
+ onLayout={(e) => {
94
+ pending.onLayout(e);
95
+ onLayout?.(e);
96
+ }}
97
+ onPressIn={(e) => {
98
+ pending.onPressIn(e);
99
+ press.onPressIn(e);
100
+ }}
101
+ onPressOut={press.onPressOut}
102
+ onFocus={(e) => {
103
+ setFocused(true);
104
+ onFocus?.(e);
105
+ }}
106
+ onBlur={(e) => {
107
+ setFocused(false);
108
+ onBlur?.(e);
109
+ }}
110
+ style={[box, { overflow: 'hidden' }, disabled ? { opacity: 0.5 } : null, ring, style, press.style]}
111
+ >
112
+ {pending.fill}
113
+ {busy && (
114
+ <Animated.View {...coming}>
115
+ <ActivityIndicator size="small" color={ink} />
116
+ </Animated.View>
117
+ )}
118
+ {/* The label makes room for the spinner by gliding, not by jumping; an icon and its text sit in a row. */}
119
+ {feedback && <Mark testID="feedback" segments={TICK} shown={answer.done} size={theme.size['icon.md']} color={ink} style={{ position: 'absolute' }} />}
120
+ <Animated.View layout={layout} style={[{ flexDirection: 'row', alignItems: 'center', gap: theme.size['space.2'] }, feedback ? answer.label : null]}>
121
+ <IconHostProvider host={icons.host}>
122
+ {typeof children === 'string' ? (
123
+ <Text variant="label" style={{ color: ink }}>
124
+ {children}
125
+ </Text>
126
+ ) : (
127
+ children
128
+ )}
129
+ </IconHostProvider>
130
+ </Animated.View>
131
+ </AnimatedPressable>
132
+ );
133
+ }
134
+
135
+ /**
136
+ * Done: the text goes (fast), then a tick is drawn (snappy) and stays for `slow`; it is unwritten (snappy) and the
137
+ * text comes back (fast, after `normal`). Failed:
138
+ * the text is pushed sideways and the bouncy spring brings it back, once. (SPEC §15.14)
139
+ */
140
+ function useFeedback(on: boolean) {
141
+ const reduced = useReducedMotion();
142
+ const [done, setDone] = useState(false);
143
+ const seen = useSharedValue(1);
144
+ const x = useSharedValue(0);
145
+ const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
146
+ useEffect(() => () => clearTimeout(timer.current), []);
147
+ const ended = (failed: boolean): void => {
148
+ if (!on) return;
149
+ if (failed) {
150
+ feel('error');
151
+ if (!reduced) x.value = withFluxSpring(0, { ...motion.spring.bouncy, velocity: 160 });
152
+ return;
153
+ }
154
+ feel('success');
155
+ seen.value = timing(0, 'fast');
156
+ clearTimeout(timer.current);
157
+ // The tick is written once the text has gone, and unwritten before it comes back: never both at once.
158
+ timer.current = setTimeout(() => {
159
+ setDone(true);
160
+ timer.current = setTimeout(() => {
161
+ setDone(false);
162
+ seen.value = withDelay(ms(motion.duration.normal), timing(1, 'fast'));
163
+ }, ms(motion.duration.normal + motion.duration.slow));
164
+ }, ms(motion.duration.fast));
165
+ };
166
+ const label = useAnimatedStyle(() => ({ opacity: seen.value, transform: [{ translateX: x.value }] }));
167
+ return { done, label, ended };
168
+ }
package/src/Card.tsx ADDED
@@ -0,0 +1,70 @@
1
+ import type { ComponentRef, ReactNode, Ref } from 'react';
2
+ import { Pressable, View, type GestureResponderEvent, type PressableProps, type StyleProp, type ViewStyle } from 'react-native';
3
+ import Animated from 'react-native-reanimated';
4
+ import { IconHostProvider, isOn, useHostFor } from './icon-host';
5
+ import { useAsyncPress, usePending } from './pending';
6
+ import { usePress } from './press';
7
+ import { useTheme, type Theme } from './theme';
8
+
9
+ export interface CardProps extends Omit<PressableProps, 'children' | 'style' | 'ref' | 'onPress'> {
10
+ /** If it returns a promise, the card waits for it: it fills from where it was pressed. */
11
+ onPress?: (event: GestureResponderEvent) => void | Promise<unknown>;
12
+ children?: ReactNode;
13
+ style?: StyleProp<ViewStyle>;
14
+ ref?: Ref<ComponentRef<typeof Pressable>>;
15
+ }
16
+
17
+ const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
18
+
19
+ export const cardStyle = (theme: Theme): ViewStyle => ({
20
+ padding: theme.size['card.pad'],
21
+ borderWidth: 1,
22
+ borderColor: theme.color['card.border'],
23
+ borderRadius: theme.size['card.radius'],
24
+ backgroundColor: theme.color['card.bg'],
25
+ });
26
+
27
+ /**
28
+ * A surface. With `onPress` it is one button that gives when pressed, and a screen reader reads it whole.
29
+ * Without, it is a plain view: what it holds is reached piece by piece.
30
+ */
31
+ export function Card({ children, style, onPress, onPressIn, onPressOut, onLayout, ...rest }: CardProps) {
32
+ const theme = useTheme();
33
+ const press = usePress({ onPressIn, onPressOut });
34
+ const [waiting, run, failed] = useAsyncPress(onPress);
35
+ const pending = usePending(waiting, theme.color['text.default'], failed);
36
+ const icons = useHostFor(isOn(rest));
37
+ if (!onPress) {
38
+ const { ref, testID } = rest;
39
+ return (
40
+ <View ref={ref} testID={testID} style={[cardStyle(theme), style]}>
41
+ {children}
42
+ </View>
43
+ );
44
+ }
45
+ return (
46
+ <AnimatedPressable
47
+ role="button"
48
+ {...rest}
49
+ disabled={!!rest.disabled || waiting}
50
+ aria-busy={waiting}
51
+ onPress={(e) => {
52
+ icons.push('press');
53
+ run(e);
54
+ }}
55
+ onLayout={(e) => {
56
+ pending.onLayout(e);
57
+ onLayout?.(e);
58
+ }}
59
+ onPressIn={(e) => {
60
+ pending.onPressIn(e);
61
+ press.onPressIn(e);
62
+ }}
63
+ onPressOut={press.onPressOut}
64
+ style={[cardStyle(theme), { overflow: 'hidden' }, style, press.style]}
65
+ >
66
+ {pending.fill}
67
+ <IconHostProvider host={icons.host}>{children}</IconHostProvider>
68
+ </AnimatedPressable>
69
+ );
70
+ }
package/src/Dialog.tsx ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * A window in the middle of the screen: a title, what it says and its buttons. It comes growing a little as it
3
+ * shows. `ConfirmDialog` is the one that asks before acting, and waits for the answer of the app. (SPEC §15.8)
4
+ */
5
+ import { useRef, type ComponentRef, type ReactNode, type RefObject } from 'react';
6
+ import { ScrollView, StyleSheet, Text as NativeText, View } from 'react-native';
7
+ import Animated, { useAnimatedStyle, type SharedValue } from 'react-native-reanimated';
8
+ import { Button, type ButtonVariant } from './Button';
9
+ import { floating, focusOn, Overlay } from './overlay';
10
+ import { settle } from './pending';
11
+ import { Text } from './Text';
12
+ import { useTheme } from './theme';
13
+
14
+ /** As wide as on the web (ui/css/dialog.css), and how small it starts. */
15
+ const WIDEST = 512;
16
+ const SMALL = 0.96;
17
+
18
+ export interface DialogProps {
19
+ open: boolean;
20
+ /** A press outside (unless `alert`) and back. Its buttons close it by calling this too. */
21
+ onClose: () => void;
22
+ title: string;
23
+ /** Under the title, muted. */
24
+ description?: string;
25
+ children?: ReactNode;
26
+ /** The buttons, at the end. */
27
+ actions?: ReactNode;
28
+ /** It asks something that must be answered: a press outside does not close it, and it is an `alertdialog`. */
29
+ alert?: boolean;
30
+ testID?: string;
31
+ }
32
+
33
+ function Panel({ t, title, description, children, actions, alert, testID, heading }: Omit<DialogProps, 'open' | 'onClose'> & { t: SharedValue<number>; heading: RefObject<ComponentRef<typeof NativeText> | null> }) {
34
+ const theme = useTheme();
35
+ const grow = useAnimatedStyle(() => ({ opacity: t.value, transform: [{ scale: SMALL + (1 - SMALL) * t.value }] }));
36
+ const gap = theme.size['space.4'];
37
+ return (
38
+ <View pointerEvents="box-none" style={[StyleSheet.absoluteFill, { alignItems: 'center', justifyContent: 'center', padding: gap }]}>
39
+ <Animated.View role={alert ? 'alertdialog' : 'dialog'} aria-modal accessibilityViewIsModal aria-label={title} testID={testID} style={[floating(theme), { width: '100%', maxWidth: WIDEST, maxHeight: '100%', padding: theme.size['space.5'], gap }, grow]}>
40
+ {/* What it says scrolls if it does not fit, with a big text size; its buttons stay in sight. */}
41
+ <ScrollView bounces={false} keyboardShouldPersistTaps="handled" style={{ flexGrow: 0 }} contentContainerStyle={{ gap }}>
42
+ <View style={{ gap: theme.size['space.1'] }}>
43
+ <Text ref={heading} variant="title">
44
+ {title}
45
+ </Text>
46
+ {description !== undefined && <Text tone="muted">{description}</Text>}
47
+ </View>
48
+ {children}
49
+ </ScrollView>
50
+ {actions != null && <View style={{ flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'flex-end', gap: theme.size['space.2'] }}>{actions}</View>}
51
+ </Animated.View>
52
+ </View>
53
+ );
54
+ }
55
+
56
+ export function Dialog({ open, onClose, alert, ...rest }: DialogProps) {
57
+ const heading = useRef<ComponentRef<typeof NativeText>>(null);
58
+ return (
59
+ <Overlay open={open} onClose={onClose} dismissible={!alert} onShow={() => focusOn(heading.current)}>
60
+ {(t) => <Panel t={t} alert={alert} heading={heading} {...rest} />}
61
+ </Overlay>
62
+ );
63
+ }
64
+
65
+ export interface ConfirmDialogProps extends Pick<DialogProps, 'open' | 'onClose' | 'title' | 'description' | 'children' | 'testID'> {
66
+ /** What the button that confirms says. */
67
+ confirm: string;
68
+ /** What the other says. Default: 'Cancel'. */
69
+ cancel?: string;
70
+ /** Default: 'primary'; 'danger' to delete. */
71
+ confirmVariant?: ButtonVariant;
72
+ /** If it returns a promise, the button waits for it and the dialog closes once it is fulfilled. If it fails it stays: say why in `children`. */
73
+ onConfirm: () => void | Promise<unknown>;
74
+ }
75
+
76
+ /** It asks before acting, with the names of `ConfirmButton` on the web. */
77
+ export function ConfirmDialog({ confirm, cancel = 'Cancel', confirmVariant = 'primary', onConfirm, onClose, ...rest }: ConfirmDialogProps) {
78
+ return (
79
+ <Dialog
80
+ alert
81
+ onClose={onClose}
82
+ {...rest}
83
+ actions={
84
+ <>
85
+ <Button onPress={onClose}>{cancel}</Button>
86
+ <Button variant={confirmVariant} onPress={() => settle(onConfirm(), onClose)}>
87
+ {confirm}
88
+ </Button>
89
+ </>
90
+ }
91
+ />
92
+ );
93
+ }