@fluxus-ui/native 0.1.0 → 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 (117) hide show
  1. package/README.md +16 -2
  2. package/lib/module/AudioPlayer.js +174 -0
  3. package/lib/module/AudioPlayer.js.map +1 -0
  4. package/lib/module/Bubble.js +182 -0
  5. package/lib/module/Bubble.js.map +1 -0
  6. package/lib/module/Button.js +87 -11
  7. package/lib/module/Button.js.map +1 -1
  8. package/lib/module/Card.js +10 -2
  9. package/lib/module/Card.js.map +1 -1
  10. package/lib/module/EmptyState.js +30 -2
  11. package/lib/module/EmptyState.js.map +1 -1
  12. package/lib/module/Link.js +97 -0
  13. package/lib/module/Link.js.map +1 -0
  14. package/lib/module/ListItem.js +9 -3
  15. package/lib/module/ListItem.js.map +1 -1
  16. package/lib/module/Skeleton.js +2 -1
  17. package/lib/module/Skeleton.js.map +1 -1
  18. package/lib/module/Swap.js +3 -3
  19. package/lib/module/Swap.js.map +1 -1
  20. package/lib/module/Toast.js +58 -9
  21. package/lib/module/Toast.js.map +1 -1
  22. package/lib/module/audio.js +54 -0
  23. package/lib/module/audio.js.map +1 -0
  24. package/lib/module/controls.js +61 -28
  25. package/lib/module/controls.js.map +1 -1
  26. package/lib/module/feel.js +24 -0
  27. package/lib/module/feel.js.map +1 -0
  28. package/lib/module/haptics.js +25 -0
  29. package/lib/module/haptics.js.map +1 -0
  30. package/lib/module/icon-host.js +42 -0
  31. package/lib/module/icon-host.js.map +1 -0
  32. package/lib/module/icon-shape.js +222 -0
  33. package/lib/module/icon-shape.js.map +1 -0
  34. package/lib/module/icon.js +293 -13
  35. package/lib/module/icon.js.map +1 -1
  36. package/lib/module/index.js +7 -1
  37. package/lib/module/index.js.map +1 -1
  38. package/lib/module/marks.js +99 -0
  39. package/lib/module/marks.js.map +1 -0
  40. package/lib/module/motion.js +71 -4
  41. package/lib/module/motion.js.map +1 -1
  42. package/lib/module/overlay.js +2 -2
  43. package/lib/module/overlay.js.map +1 -1
  44. package/lib/module/pending.js +11 -7
  45. package/lib/module/pending.js.map +1 -1
  46. package/lib/module/presence.js +8 -5
  47. package/lib/module/presence.js.map +1 -1
  48. package/lib/module/screen.js +6 -1
  49. package/lib/module/screen.js.map +1 -1
  50. package/lib/module/success.js +59 -0
  51. package/lib/module/success.js.map +1 -0
  52. package/lib/typescript/AudioPlayer.d.ts +26 -0
  53. package/lib/typescript/AudioPlayer.d.ts.map +1 -0
  54. package/lib/typescript/Bubble.d.ts +23 -0
  55. package/lib/typescript/Bubble.d.ts.map +1 -0
  56. package/lib/typescript/Button.d.ts +3 -1
  57. package/lib/typescript/Button.d.ts.map +1 -1
  58. package/lib/typescript/Card.d.ts.map +1 -1
  59. package/lib/typescript/EmptyState.d.ts +5 -3
  60. package/lib/typescript/EmptyState.d.ts.map +1 -1
  61. package/lib/typescript/Link.d.ts +23 -0
  62. package/lib/typescript/Link.d.ts.map +1 -0
  63. package/lib/typescript/ListItem.d.ts.map +1 -1
  64. package/lib/typescript/Skeleton.d.ts.map +1 -1
  65. package/lib/typescript/Toast.d.ts.map +1 -1
  66. package/lib/typescript/audio.d.ts +10 -0
  67. package/lib/typescript/audio.d.ts.map +1 -0
  68. package/lib/typescript/controls.d.ts.map +1 -1
  69. package/lib/typescript/feel.d.ts +10 -0
  70. package/lib/typescript/feel.d.ts.map +1 -0
  71. package/lib/typescript/haptics.d.ts +7 -0
  72. package/lib/typescript/haptics.d.ts.map +1 -0
  73. package/lib/typescript/icon-host.d.ts +30 -0
  74. package/lib/typescript/icon-host.d.ts.map +1 -0
  75. package/lib/typescript/icon-shape.d.ts +28 -0
  76. package/lib/typescript/icon-shape.d.ts.map +1 -0
  77. package/lib/typescript/icon.d.ts +13 -14
  78. package/lib/typescript/icon.d.ts.map +1 -1
  79. package/lib/typescript/index.d.ts +7 -1
  80. package/lib/typescript/index.d.ts.map +1 -1
  81. package/lib/typescript/marks.d.ts +23 -0
  82. package/lib/typescript/marks.d.ts.map +1 -0
  83. package/lib/typescript/motion.d.ts +23 -0
  84. package/lib/typescript/motion.d.ts.map +1 -1
  85. package/lib/typescript/pending.d.ts +3 -2
  86. package/lib/typescript/pending.d.ts.map +1 -1
  87. package/lib/typescript/presence.d.ts.map +1 -1
  88. package/lib/typescript/screen.d.ts +7 -2
  89. package/lib/typescript/screen.d.ts.map +1 -1
  90. package/lib/typescript/success.d.ts +13 -0
  91. package/lib/typescript/success.d.ts.map +1 -0
  92. package/package.json +26 -3
  93. package/src/AudioPlayer.tsx +106 -0
  94. package/src/Bubble.tsx +112 -0
  95. package/src/Button.tsx +65 -14
  96. package/src/Card.tsx +7 -2
  97. package/src/EmptyState.tsx +22 -3
  98. package/src/Link.tsx +61 -0
  99. package/src/ListItem.tsx +8 -3
  100. package/src/Skeleton.tsx +2 -1
  101. package/src/Swap.tsx +3 -3
  102. package/src/Toast.tsx +32 -4
  103. package/src/audio.tsx +56 -0
  104. package/src/controls.tsx +37 -9
  105. package/src/feel.ts +22 -0
  106. package/src/haptics.tsx +26 -0
  107. package/src/icon-host.tsx +46 -0
  108. package/src/icon-shape.ts +213 -0
  109. package/src/icon.tsx +252 -15
  110. package/src/index.ts +7 -1
  111. package/src/marks.tsx +79 -0
  112. package/src/motion.ts +76 -4
  113. package/src/overlay.tsx +2 -2
  114. package/src/pending.tsx +11 -7
  115. package/src/presence.tsx +10 -7
  116. package/src/screen.tsx +8 -3
  117. package/src/success.tsx +48 -0
package/src/Card.tsx CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { ComponentRef, ReactNode, Ref } from 'react';
2
2
  import { Pressable, View, type GestureResponderEvent, type PressableProps, type StyleProp, type ViewStyle } from 'react-native';
3
3
  import Animated from 'react-native-reanimated';
4
+ import { IconHostProvider, isOn, useHostFor } from './icon-host';
4
5
  import { useAsyncPress, usePending } from './pending';
5
6
  import { usePress } from './press';
6
7
  import { useTheme, type Theme } from './theme';
@@ -32,6 +33,7 @@ export function Card({ children, style, onPress, onPressIn, onPressOut, onLayout
32
33
  const press = usePress({ onPressIn, onPressOut });
33
34
  const [waiting, run, failed] = useAsyncPress(onPress);
34
35
  const pending = usePending(waiting, theme.color['text.default'], failed);
36
+ const icons = useHostFor(isOn(rest));
35
37
  if (!onPress) {
36
38
  const { ref, testID } = rest;
37
39
  return (
@@ -46,7 +48,10 @@ export function Card({ children, style, onPress, onPressIn, onPressOut, onLayout
46
48
  {...rest}
47
49
  disabled={!!rest.disabled || waiting}
48
50
  aria-busy={waiting}
49
- onPress={run}
51
+ onPress={(e) => {
52
+ icons.push('press');
53
+ run(e);
54
+ }}
50
55
  onLayout={(e) => {
51
56
  pending.onLayout(e);
52
57
  onLayout?.(e);
@@ -59,7 +64,7 @@ export function Card({ children, style, onPress, onPressIn, onPressOut, onLayout
59
64
  style={[cardStyle(theme), { overflow: 'hidden' }, style, press.style]}
60
65
  >
61
66
  {pending.fill}
62
- {children}
67
+ <IconHostProvider host={icons.host}>{children}</IconHostProvider>
63
68
  </AnimatedPressable>
64
69
  );
65
70
  }
@@ -1,5 +1,7 @@
1
- import type { ReactNode } from 'react';
1
+ import { useEffect, type ReactNode } from 'react';
2
2
  import { View, type ViewProps } from 'react-native';
3
+ import Animated, { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
4
+ import { spring } from './motion';
3
5
  import { Text } from './Text';
4
6
  import { useTheme } from './theme';
5
7
 
@@ -9,13 +11,16 @@ export interface EmptyStateProps extends ViewProps {
9
11
  children?: ReactNode;
10
12
  /** A button under the text. */
11
13
  action?: ReactNode;
14
+ /** Above the title: an icon, an image. It comes with a little gesture, once. */
15
+ icon?: ReactNode;
12
16
  }
13
17
 
14
- /** There is nothing here, and what to do about it. */
15
- export function EmptyState({ title, children, action, style, ...rest }: EmptyStateProps) {
18
+ /** There is nothing here, and what to do about it. (SPEC §15.14) */
19
+ export function EmptyState({ title, children, action, icon, style, ...rest }: EmptyStateProps) {
16
20
  const theme = useTheme();
17
21
  return (
18
22
  <View {...rest} style={[{ alignItems: 'center', gap: theme.size['space.2'], padding: theme.size['space.6'] }, style]}>
23
+ {icon !== undefined && <Arriving>{icon}</Arriving>}
19
24
  <Text variant="title" style={{ textAlign: 'center' }}>
20
25
  {title}
21
26
  </Text>
@@ -30,3 +35,17 @@ export function EmptyState({ title, children, action, style, ...rest }: EmptySta
30
35
  </View>
31
36
  );
32
37
  }
38
+
39
+ /** It rises 6 points and grows from 85 % as it comes, on the bouncy spring, once. */
40
+ function Arriving({ children }: { children: ReactNode }) {
41
+ const t = useSharedValue(0);
42
+ useEffect(() => {
43
+ t.value = spring(1, 'bouncy');
44
+ }, [t]);
45
+ const come = useAnimatedStyle(() => ({ transform: [{ translateY: 6 * (1 - t.value) }, { scale: 0.85 + 0.15 * t.value }] }));
46
+ return (
47
+ <Animated.View testID="empty-icon" style={come}>
48
+ {children}
49
+ </Animated.View>
50
+ );
51
+ }
package/src/Link.tsx ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * A link in text: the colour of the accent, underlined, and a link for a screen reader. Alone it is pressed
3
+ * with a target of 44 points; `inline`, it is part of a sentence. Pressed, it fades a little, and stays so
4
+ * while what its press returned is not settled: nothing moves. (SPEC §15.10, §15.11)
5
+ */
6
+ import { useState, type ReactNode } from 'react';
7
+ import { Linking, Pressable, View, type StyleProp, type TextStyle, type ViewStyle } from 'react-native';
8
+ import { useAsyncPress } from './pending';
9
+ import { Text, textStyle, type TextProps, type TextVariant } from './Text';
10
+ import { useTheme } from './theme';
11
+
12
+ export interface LinkProps extends Omit<TextProps, 'onPress' | 'style' | 'role'> {
13
+ children: ReactNode;
14
+ /** An address the system opens. For the browser of the app, or a screen of the app, use `onPress`. */
15
+ href?: string;
16
+ /** If it returns a promise, the link waits for it: faded, and taking no other press. */
17
+ onPress?: () => void | Promise<unknown>;
18
+ /** Inside a sentence: a nested text, with the line it is in. Default: false, an element of its own. */
19
+ inline?: boolean;
20
+ /** Default: 'body'. */
21
+ variant?: TextVariant;
22
+ /** The style of the text. */
23
+ style?: StyleProp<TextStyle>;
24
+ }
25
+
26
+ /** How much of it is seen while it is pressed, or waits. */
27
+ const FADED = 0.6;
28
+
29
+ export function Link({ children, href, onPress, inline = false, variant, style, testID, ...rest }: LinkProps) {
30
+ const theme = useTheme();
31
+ const [focused, setFocused] = useState(false);
32
+ // Opening an address is waited for too: two presses do not open it twice.
33
+ const [waiting, run] = useAsyncPress<void>(() => {
34
+ const result = onPress?.();
35
+ return href ? Promise.all([result, Linking.openURL(href)]) : result;
36
+ });
37
+ const go = (): void => {
38
+ if (!waiting) run();
39
+ };
40
+ const ink: TextStyle = { color: theme.color['text.accent'], textDecorationLine: 'underline' };
41
+ if (inline) {
42
+ return (
43
+ <Text role="link" aria-busy={waiting} disabled={waiting} suppressHighlighting variant={variant} testID={testID} {...rest} onPress={go} style={[ink, waiting ? { opacity: FADED } : null, style]}>
44
+ {children}
45
+ </Text>
46
+ );
47
+ }
48
+ // The text may be lower than a finger: the rest of the 44 points is around it, and takes no room.
49
+ const slop = Math.max(0, (theme.size['target.min'] - (textStyle(theme, variant).lineHeight ?? 0)) / 2);
50
+ const ring: ViewStyle | null = focused ? { outlineStyle: 'solid', outlineWidth: theme.size['focus.width'], outlineOffset: theme.size['focus.offset'], outlineColor: theme.color['focus.ring'] } : null;
51
+ return (
52
+ // In a column it would be as wide as the column, and pressed where there is no text: it stays as wide as its text, in a row of its own.
53
+ <View style={{ flexDirection: 'row' }}>
54
+ <Pressable role="link" aria-busy={waiting} disabled={waiting} testID={testID} hitSlop={slop} onPress={go} onFocus={() => setFocused(true)} onBlur={() => setFocused(false)} style={({ pressed }) => [{ flexShrink: 1, borderRadius: theme.size['radius.1'], opacity: pressed || waiting ? FADED : 1 }, ring]}>
55
+ <Text variant={variant} {...rest} style={[ink, style]}>
56
+ {children}
57
+ </Text>
58
+ </Pressable>
59
+ </View>
60
+ );
61
+ }
package/src/ListItem.tsx CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { ComponentRef, ReactNode, Ref } from 'react';
2
2
  import { Pressable, View, type GestureResponderEvent, type PressableProps, type StyleProp, type ViewStyle } from 'react-native';
3
3
  import { fill } from './layout';
4
+ import { IconHostProvider, isOn, useHostFor } from './icon-host';
4
5
  import { useAsyncPress, usePending } from './pending';
5
6
  import { Text } from './Text';
6
7
  import { useTheme } from './theme';
@@ -26,9 +27,10 @@ export function ListItem({ title, description, leading, trailing, style, onPress
26
27
  const theme = useTheme();
27
28
  const [waiting, run, failed] = useAsyncPress(onPress);
28
29
  const pending = usePending(waiting, theme.color['text.default'], failed);
30
+ const icons = useHostFor(isOn(rest));
29
31
  const row: ViewStyle = { minHeight: theme.size['target.min'], flexDirection: 'row', alignItems: 'center', gap: theme.size['space.3'], paddingInline: theme.size['space.4'], paddingBlock: theme.size['space.2'] };
30
32
  const body = (
31
- <>
33
+ <IconHostProvider host={icons.host}>
32
34
  {leading}
33
35
  <View style={fill}>
34
36
  <Text variant="label">{title}</Text>
@@ -39,7 +41,7 @@ export function ListItem({ title, description, leading, trailing, style, onPress
39
41
  )}
40
42
  </View>
41
43
  {trailing}
42
- </>
44
+ </IconHostProvider>
43
45
  );
44
46
  if (!onPress) {
45
47
  return (
@@ -54,7 +56,10 @@ export function ListItem({ title, description, leading, trailing, style, onPress
54
56
  aria-disabled={!!disabled}
55
57
  aria-busy={waiting}
56
58
  {...rest}
57
- onPress={run}
59
+ onPress={(e) => {
60
+ icons.push('press');
61
+ run(e);
62
+ }}
58
63
  onPressIn={(e) => {
59
64
  pending.onPressIn(e);
60
65
  onPressIn?.(e);
package/src/Skeleton.tsx CHANGED
@@ -2,6 +2,7 @@ import { useEffect } from 'react';
2
2
  import type { DimensionValue, StyleProp, ViewProps, ViewStyle } from 'react-native';
3
3
  import Animated, { ReduceMotion, cancelAnimation, useAnimatedStyle, useSharedValue, withRepeat, withTiming } from 'react-native-reanimated';
4
4
  import { motion } from './generated/tokens';
5
+ import { ms } from './motion';
5
6
  import { useTheme } from './theme';
6
7
 
7
8
  export interface SkeletonProps extends Omit<ViewProps, 'style' | 'children'> {
@@ -19,7 +20,7 @@ export function Skeleton({ width = '100%', height, radius, style, ...rest }: Ske
19
20
  const theme = useTheme();
20
21
  const light = useSharedValue(1);
21
22
  useEffect(() => {
22
- light.value = withRepeat(withTiming(0.5, { duration: motion.duration.slow * 2 }), -1, true, undefined, ReduceMotion.System);
23
+ light.value = withRepeat(withTiming(0.5, { duration: ms(motion.duration.slow * 2) }), -1, true, undefined, ReduceMotion.System);
23
24
  return () => cancelAnimation(light);
24
25
  }, [light]);
25
26
  const breath = useAnimatedStyle(() => ({ opacity: light.value }));
package/src/Swap.tsx CHANGED
@@ -7,7 +7,7 @@ import { useEffect, useRef, useState, type ReactNode } from 'react';
7
7
  import { Platform, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
8
8
  import Animated, { useAnimatedStyle, useReducedMotion, useSharedValue, type SharedValue } from 'react-native-reanimated';
9
9
  import { motion } from './generated/tokens';
10
- import { timing } from './motion';
10
+ import { ms, timing } from './motion';
11
11
  import { presence } from './presence';
12
12
 
13
13
  /** How small it gets as it goes, and how blurred in a browser. */
@@ -39,8 +39,8 @@ export function useSwap(id: string | number, children: ReactNode): { content: Re
39
39
  const timer = setTimeout(() => {
40
40
  setShown(id);
41
41
  t.value = timing(1, 'normal', 'decelerate');
42
- last = setTimeout(() => setMoving(false), motion.duration.normal);
43
- }, motion.duration.fast);
42
+ last = setTimeout(() => setMoving(false), ms(motion.duration.normal));
43
+ }, ms(motion.duration.fast));
44
44
  return () => {
45
45
  clearTimeout(timer);
46
46
  clearTimeout(last);
package/src/Toast.tsx CHANGED
@@ -1,7 +1,11 @@
1
1
  import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from 'react';
2
2
  import { AccessibilityInfo, Platform, Pressable, View } from 'react-native';
3
+ import Animated, { FadeInDown, FadeOutDown, ReduceMotion } from 'react-native-reanimated';
3
4
  import { toneColors } from './Badge';
4
- import { Appear } from './presence';
5
+ import { motion } from './generated/tokens';
6
+ import { BANG, Mark, TICK } from './marks';
7
+ import { ms } from './motion';
8
+ import { RISE, shift } from './presence';
5
9
  import { Text } from './Text';
6
10
  import { useTheme } from './theme';
7
11
 
@@ -56,7 +60,7 @@ export function ToastProvider({ children, inset, dismissLabel = 'Dismiss' }: Toa
56
60
  const layer = (
57
61
  <View pointerEvents="box-none" style={{ position: 'absolute', start: edge, end: edge, bottom: inset ?? edge, gap: theme.size['space.2'] }}>
58
62
  {toasts.map((t) => (
59
- <Appear key={t.id}>
63
+ <Animated.View key={t.id} {...coming()}>
60
64
  <Pressable
61
65
  role="alert"
62
66
  aria-live={t.tone === 'danger' ? 'assertive' : 'polite'}
@@ -64,7 +68,7 @@ export function ToastProvider({ children, inset, dismissLabel = 'Dismiss' }: Toa
64
68
  onPress={() => dismiss(t.id)}
65
69
  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
70
  >
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 }} />
71
+ <ToneMark tone={t.tone ?? 'info'} />
68
72
  <View style={{ flex: 1 }}>
69
73
  <Text variant="label">{t.title}</Text>
70
74
  {t.description !== undefined && (
@@ -74,7 +78,7 @@ export function ToastProvider({ children, inset, dismissLabel = 'Dismiss' }: Toa
74
78
  )}
75
79
  </View>
76
80
  </Pressable>
77
- </Appear>
81
+ </Animated.View>
78
82
  ))}
79
83
  </View>
80
84
  );
@@ -92,3 +96,27 @@ export function useToast(): (toast: ToastInput) => void {
92
96
  if (!toast) throw new Error('@fluxus-ui/native: no toasts. Wrap the app in <ToastProvider>, inside <ThemeProvider>.');
93
97
  return toast;
94
98
  }
99
+
100
+ /** Its mark, written as it comes: a tick, an exclamation, or a dot. (SPEC §15.14) */
101
+ function ToneMark({ tone }: { tone: NonNullable<ToastInput['tone']> }) {
102
+ const theme = useTheme();
103
+ const color = toneColors(theme, tone).solid;
104
+ const side = theme.size['icon.md'];
105
+ const [shown, setShown] = useState(false);
106
+ // Drawn after it is on the screen, not with it.
107
+ useEffect(() => setShown(true), []);
108
+ if (tone === 'info') return <View style={{ width: theme.size['space.2'], height: theme.size['space.2'], marginInline: (side - theme.size['space.2']) / 2, borderRadius: theme.size['space.2'], backgroundColor: color }} />;
109
+ return <Mark testID={`mark-${tone}`} segments={tone === 'success' ? TICK : BANG} shown={shown} size={side} color={color} stroke={2} />;
110
+ }
111
+
112
+ /** It comes rising and goes sinking, on the smooth spring. In a browser, as `presence`, nothing. */
113
+ const coming = () => {
114
+ if (Platform.OS === 'web') return {};
115
+ const { stiffness, damping, mass } = motion.spring.smooth;
116
+ const k = ms(1);
117
+ return {
118
+ entering: FadeInDown.springify().stiffness(stiffness / (k * k)).damping(damping / k).mass(mass).withInitialValues({ transform: [{ translateY: RISE }] }).reduceMotion(ReduceMotion.System),
119
+ exiting: FadeOutDown.springify().stiffness(stiffness / (k * k)).damping(damping / k).mass(mass).reduceMotion(ReduceMotion.System),
120
+ layout: shift(),
121
+ };
122
+ };
package/src/audio.tsx ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * `@fluxus-ui/native/audio`: the player, playing with expo-audio. Its own entry, so that an app with no sound
3
+ * does not need the library. (SPEC §15.10)
4
+ */
5
+ import { useEffect, useRef, useState } from 'react';
6
+ import { useAudioPlayer, useAudioPlayerStatus, type AudioSource } from 'expo-audio';
7
+ import { AudioPlayer, type AudioPlayerProps } from './AudioPlayer';
8
+
9
+ export interface AudioProps extends Pick<AudioPlayerProps, 'labels' | 'style' | 'testID'> {
10
+ /** What to play: an address, an asset, or null while it is not known (a signed address that is still coming). */
11
+ source: AudioSource;
12
+ /** It could not be played: the app says why. */
13
+ onError?: (message: string) => void;
14
+ }
15
+
16
+ export function Audio({ source, onError, ...rest }: AudioProps) {
17
+ const player = useAudioPlayer(source);
18
+ const status = useAudioPlayerStatus(player);
19
+ // Asked to play: the button waits until the sound runs, or until it is known that it will not.
20
+ // In a browser expo-audio says once more that it is not playing at second 0: it is asked until it has moved on.
21
+ const [asked, setAsked] = useState(false);
22
+ const running = status.playing && status.currentTime > 0;
23
+ useEffect(() => {
24
+ if (running || status.error) setAsked(false);
25
+ }, [running, status.error]);
26
+ // Told once for each error, whatever function the app passes on each render.
27
+ const tell = useRef(onError);
28
+ useEffect(() => {
29
+ tell.current = onError;
30
+ });
31
+ useEffect(() => {
32
+ if (status.error) tell.current?.(status.error);
33
+ }, [status.error]);
34
+ const ended = status.didJustFinish || (status.duration > 0 && status.currentTime >= status.duration);
35
+ return (
36
+ <AudioPlayer
37
+ {...rest}
38
+ disabled={source === null}
39
+ playing={status.playing || asked}
40
+ loading={asked && !status.playing}
41
+ position={status.currentTime}
42
+ duration={status.duration}
43
+ onToggle={() => {
44
+ if (status.playing || asked) {
45
+ setAsked(false);
46
+ return player.pause();
47
+ }
48
+ setAsked(true);
49
+ // Played to the end, it starts again: once it is back at the start.
50
+ if (ended) return void player.seekTo(0).then(() => player.play());
51
+ player.play();
52
+ }}
53
+ onSeek={(seconds) => void player.seekTo(seconds)}
54
+ />
55
+ );
56
+ }
package/src/controls.tsx CHANGED
@@ -5,7 +5,9 @@
5
5
  import { useEffect, useState, type ReactNode } from 'react';
6
6
  import { I18nManager, Pressable, View, type PressableProps, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
7
7
  import Animated, { interpolateColor, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
8
+ import { feel } from './feel';
8
9
  import { fill } from './layout';
10
+ import { DASH, Mark, TICK } from './marks';
9
11
  import { spring, timing } from './motion';
10
12
  import { Text } from './Text';
11
13
  import { useTheme } from './theme';
@@ -94,18 +96,21 @@ export function Checkbox({ checked, defaultChecked = false, mixed = false, onCha
94
96
  backgroundColor: interpolateColor(tint.value, [0, 1], [c['input.bg'], c['accent.default']]),
95
97
  borderColor: interpolateColor(tint.value, [0, 1], [c['border.control'], c['accent.default']]),
96
98
  }));
97
- const mark = useAnimatedStyle(() => ({ opacity: tint.value, transform: [{ scale: to.value }, { rotate: mixed ? '0deg' : '45deg' }] }));
98
99
  const ink = c['text.on-accent'];
99
100
  return (
100
101
  <Row
101
102
  role="checkbox"
102
103
  checked={mixed ? 'mixed' : on}
103
104
  {...rest}
104
- onPress={() => set(mixed ? true : !on)}
105
+ onPress={() => {
106
+ feel('selection');
107
+ set(mixed ? true : !on);
108
+ }}
105
109
  control={
106
110
  <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]} />
111
+ {/* The tick is written as the icon writes it; a dash when it is mixed. */}
112
+ <Mark testID="tick" segments={TICK} shown={on && !mixed} size={side * 0.8} color={ink} stroke={2} style={{ position: 'absolute' }} />
113
+ <Mark testID="dash" segments={DASH} shown={mixed} size={side * 0.8} color={ink} stroke={2} style={{ position: 'absolute' }} />
109
114
  </Animated.View>
110
115
  }
111
116
  />
@@ -125,17 +130,30 @@ export function Switch({ checked, defaultChecked = false, onChange, ...rest }: S
125
130
  const travel = height * 0.75;
126
131
  const way = I18nManager.isRTL ? -1 : 1;
127
132
  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 }] }));
133
+ // Pressed, the knob widens towards where it would go, and gives back when let go.
134
+ const press = useSharedValue(0);
135
+ const knob = useAnimatedStyle(() => ({ transform: [{ translateX: way * to.value * travel }, { scaleX: 1 + 0.15 * press.value }] }));
129
136
  return (
130
137
  <Row
131
138
  role="switch"
132
139
  after
133
140
  checked={on}
134
141
  {...rest}
135
- onPress={() => set(!on)}
142
+ onPressIn={(e) => {
143
+ press.value = spring(1, 'snappy');
144
+ rest.onPressIn?.(e);
145
+ }}
146
+ onPressOut={(e) => {
147
+ press.value = spring(0, 'snappy');
148
+ rest.onPressOut?.(e);
149
+ }}
150
+ onPress={() => {
151
+ feel('selection');
152
+ set(!on);
153
+ }}
136
154
  control={
137
155
  <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]} />
156
+ <Animated.View style={[{ width: thumb, height: thumb, borderRadius: thumb, backgroundColor: c['surface.base'], transformOrigin: (on ? way < 0 : way > 0) ? 'right' : 'left' }, knob]} />
139
157
  </Animated.View>
140
158
  }
141
159
  />
@@ -164,7 +182,8 @@ function Radio({ option, on, disabled, onPress }: { option: RadioOption; on: boo
164
182
  const c = theme.color;
165
183
  const { to, tint } = useOn(on);
166
184
  const side = theme.size['icon.md'];
167
- const ring = useAnimatedStyle(() => ({ borderColor: interpolateColor(tint.value, [0, 1], [c['border.control'], c['accent.default']]) }));
185
+ const press = useSharedValue(0);
186
+ const ring = useAnimatedStyle(() => ({ borderColor: interpolateColor(tint.value, [0, 1], [c['border.control'], c['accent.default']]), transform: [{ scale: 1 - 0.08 * press.value }] }));
168
187
  const dot = useAnimatedStyle(() => ({ transform: [{ scale: to.value }] }));
169
188
  return (
170
189
  <Row
@@ -174,6 +193,12 @@ function Radio({ option, on, disabled, onPress }: { option: RadioOption; on: boo
174
193
  hint={option.hint}
175
194
  disabled={disabled}
176
195
  onPress={onPress}
196
+ onPressIn={() => {
197
+ press.value = spring(1, 'snappy');
198
+ }}
199
+ onPressOut={() => {
200
+ press.value = spring(0, 'snappy');
201
+ }}
177
202
  control={
178
203
  <Animated.View style={[{ width: side, height: side, alignItems: 'center', justifyContent: 'center', borderWidth: 1.5, borderRadius: side, backgroundColor: c['input.bg'] }, ring]}>
179
204
  <Animated.View style={[{ width: side / 2, height: side / 2, borderRadius: side, backgroundColor: c['accent.default'] }, dot]} />
@@ -189,7 +214,10 @@ export function RadioGroup({ label, options, value, defaultValue = null, onChang
189
214
  return (
190
215
  <View role="radiogroup" aria-label={label} {...rest}>
191
216
  {options.map((option) => (
192
- <Radio key={option.value} option={option} on={option.value === chosen} disabled={disabled || !!option.disabled} onPress={() => set(option.value)} />
217
+ <Radio key={option.value} option={option} on={option.value === chosen} disabled={disabled || !!option.disabled} onPress={() => {
218
+ feel('selection');
219
+ set(option.value);
220
+ }} />
193
221
  ))}
194
222
  </View>
195
223
  );
package/src/feel.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The haptics of the main entry, by registration: it never imports the library of haptics. `enableHaptics()` of
3
+ * `@fluxus-ui/native/haptics` gives it the real one; without it, nothing is felt and nothing breaks. (SPEC §15.14)
4
+ */
5
+ export type HapticKind = 'success' | 'warning' | 'error' | 'light' | 'medium' | 'selection';
6
+
7
+ let felt: ((kind: HapticKind) => unknown) | null = null;
8
+
9
+ /** What makes a haptic; null for none. */
10
+ export const setHaptics = (fn: ((kind: HapticKind) => unknown) | null): void => {
11
+ felt = fn;
12
+ };
13
+
14
+ /** A haptic, if there is a way to make one. It never throws. */
15
+ export const feel = (kind: HapticKind): void => {
16
+ if (!felt) return;
17
+ try {
18
+ void Promise.resolve(felt(kind)).catch(() => undefined);
19
+ } catch {
20
+ // A haptic that fails is not felt: nothing else happens.
21
+ }
22
+ };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * `@fluxus-ui/native/haptics`: what the hand feels, with expo-haptics. Its own entry, so that an app without it does
3
+ * not need the library. `enableHaptics()` registers it for the pieces of the main entry, which never import it:
4
+ * a button that confirms, a switch, `Success`. (SPEC §15.14)
5
+ */
6
+ import * as Haptics from 'expo-haptics';
7
+ import { setHaptics, type HapticKind } from './feel';
8
+
9
+ export type { HapticKind };
10
+
11
+ /** A haptic of the system. It resolves when it has been asked for, and never rejects: a phone without one feels nothing. */
12
+ export function haptic(kind: HapticKind): Promise<void> {
13
+ const ask =
14
+ kind === 'selection'
15
+ ? Haptics.selectionAsync()
16
+ : kind === 'light' || kind === 'medium'
17
+ ? Haptics.impactAsync(kind === 'light' ? Haptics.ImpactFeedbackStyle.Light : Haptics.ImpactFeedbackStyle.Medium)
18
+ : Haptics.notificationAsync({ success: Haptics.NotificationFeedbackType.Success, warning: Haptics.NotificationFeedbackType.Warning, error: Haptics.NotificationFeedbackType.Error }[kind]);
19
+ return ask.catch(() => undefined);
20
+ }
21
+
22
+ /** Once, at the root of the app: from then on the pieces of Fluxus are felt. It returns how to stop. */
23
+ export function enableHaptics(): () => void {
24
+ setHaptics(haptic);
25
+ return () => setHaptics(null);
26
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * What a control tells the icons inside it: whether it is on (open, checked) and that it was pressed. The icon
3
+ * of `@fluxus-ui/native/icon` reads it, so that the app wires nothing. No SVG here. (SPEC §15.12)
4
+ */
5
+ import { createContext, useContext, useMemo, useRef, type ReactNode } from 'react';
6
+
7
+ export interface IconHost {
8
+ on: boolean;
9
+ /** The colour of the control's text: its icons take it unless they are given one. */
10
+ ink?: string;
11
+ /** Called with the name of a push: 'press'. It returns how to stop listening. */
12
+ listen: (fn: (name: string) => void) => () => void;
13
+ }
14
+
15
+ const Context = createContext<IconHost | null>(null);
16
+
17
+ export const useIconHost = (): IconHost | null => useContext(Context);
18
+
19
+ type Aria = { 'aria-expanded'?: boolean | null; 'aria-checked'?: boolean | 'mixed' | null };
20
+
21
+ /** On, as on the web: open, or checked. */
22
+ export const isOn = (props: Aria): boolean => props['aria-expanded'] === true || props['aria-checked'] === true;
23
+
24
+ /** For a control: the host its icons read, and the push to give them when it is pressed. */
25
+ export function useHostFor(on: boolean, ink?: string): { host: IconHost; push: (name: string) => void } {
26
+ const listeners = useRef(new Set<(name: string) => void>());
27
+ const host = useMemo<IconHost>(
28
+ () => ({
29
+ on,
30
+ ink,
31
+ listen: (fn) => {
32
+ listeners.current.add(fn);
33
+ return () => void listeners.current.delete(fn);
34
+ },
35
+ }),
36
+ [on, ink],
37
+ );
38
+ const push = useRef((name: string) => {
39
+ for (const fn of listeners.current) fn(name);
40
+ }).current;
41
+ return { host, push };
42
+ }
43
+
44
+ export function IconHostProvider({ host, children }: { host: IconHost; children: ReactNode }) {
45
+ return <Context.Provider value={host}>{children}</Context.Provider>;
46
+ }