@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
@@ -0,0 +1,213 @@
1
+ /**
2
+ * The geometry of a live icon on native, where SVG transforms are not reliable and `pathLength` does not exist:
3
+ * an outline becomes numbers that know what they are (point, radius, angle, flag), its length is measured once,
4
+ * and every frame writes the outline already moved, turned and scaled. (SPEC §15.12)
5
+ */
6
+ import { parse } from '@fluxus-ui/icons';
7
+
8
+ /** What each number of an outline is: the x or y of a point, a radius, the angle of an arc, or one of its flags. */
9
+ export const X = 0;
10
+ export const Y = 1;
11
+ export const RADIUS = 2;
12
+ export const ANGLE = 3;
13
+ export const FLAG = 4;
14
+
15
+ export interface Shape {
16
+ /** The letters, and '' where a number goes. */
17
+ tpl: string[];
18
+ /** What each number is, in order. */
19
+ kinds: number[];
20
+ }
21
+
22
+ export interface Outline {
23
+ nums: number[];
24
+ /** Its length, in units of the 16 × 16 grid. */
25
+ len: number;
26
+ }
27
+
28
+ const ARGS: Record<string, number> = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 };
29
+
30
+ type P = [number, number];
31
+ const dist = (a: P, b: P): number => Math.hypot(b[0] - a[0], b[1] - a[1]);
32
+
33
+ /** The length of a curve, by sampling it. */
34
+ const sampled = (at: (t: number) => P, steps: number): number => {
35
+ let len = 0;
36
+ let prev = at(0);
37
+ for (let i = 1; i <= steps; i++) {
38
+ const next = at(i / steps);
39
+ len += dist(prev, next);
40
+ prev = next;
41
+ }
42
+ return len;
43
+ };
44
+
45
+ const cubic = (a: P, b: P, c: P, d: P) => (t: number): P => {
46
+ const u = 1 - t;
47
+ return [u * u * u * a[0] + 3 * u * u * t * b[0] + 3 * u * t * t * c[0] + t * t * t * d[0], u * u * u * a[1] + 3 * u * u * t * b[1] + 3 * u * t * t * c[1] + t * t * t * d[1]];
48
+ };
49
+
50
+ const quad = (a: P, b: P, c: P) => (t: number): P => {
51
+ const u = 1 - t;
52
+ return [u * u * a[0] + 2 * u * t * b[0] + t * t * c[0], u * u * a[1] + 2 * u * t * b[1] + t * t * c[1]];
53
+ };
54
+
55
+ /** An arc of SVG from its end points to its centre (SVG 1.1, F.6.5), sampled. */
56
+ const arc = (from: P, rx0: number, ry0: number, phi: number, large: number, sweep: number, to: P): number => {
57
+ let rx = Math.abs(rx0);
58
+ let ry = Math.abs(ry0);
59
+ if (!rx || !ry) return dist(from, to);
60
+ const f = (phi * Math.PI) / 180;
61
+ const cos = Math.cos(f);
62
+ const sin = Math.sin(f);
63
+ const dx = (from[0] - to[0]) / 2;
64
+ const dy = (from[1] - to[1]) / 2;
65
+ const x1 = cos * dx + sin * dy;
66
+ const y1 = -sin * dx + cos * dy;
67
+ const fit = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry);
68
+ if (fit > 1) {
69
+ rx *= Math.sqrt(fit);
70
+ ry *= Math.sqrt(fit);
71
+ }
72
+ const num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1;
73
+ const k = (large === sweep ? -1 : 1) * Math.sqrt(Math.max(0, num / (rx * rx * y1 * y1 + ry * ry * x1 * x1)));
74
+ const cx1 = (k * rx * y1) / ry;
75
+ const cy1 = (-k * ry * x1) / rx;
76
+ const angle = (ux: number, uy: number, vx: number, vy: number): number => Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
77
+ const start = angle(1, 0, (x1 - cx1) / rx, (y1 - cy1) / ry);
78
+ let delta = angle((x1 - cx1) / rx, (y1 - cy1) / ry, (-x1 - cx1) / rx, (-y1 - cy1) / ry);
79
+ if (!sweep && delta > 0) delta -= 2 * Math.PI;
80
+ if (sweep && delta < 0) delta += 2 * Math.PI;
81
+ const mx = (from[0] + to[0]) / 2;
82
+ const my = (from[1] + to[1]) / 2;
83
+ return sampled((t) => {
84
+ const a = start + delta * t;
85
+ const px = rx * Math.cos(a);
86
+ const py = ry * Math.sin(a);
87
+ return [cos * px - sin * py + cos * cx1 - sin * cy1 + mx, sin * px + cos * py + sin * cx1 + cos * cy1 + my];
88
+ }, 32);
89
+ };
90
+
91
+ /** An outline of absolute commands as a shape and its numbers. `H` and `V` become `L`: the same drawing. */
92
+ export function outline(d: string): { shape: Shape; outline: Outline } {
93
+ const tokens = parse(d);
94
+ const tpl: string[] = [];
95
+ const kinds: number[] = [];
96
+ const nums: number[] = [];
97
+ let len = 0;
98
+ let cur: P = [0, 0];
99
+ let start: P = [0, 0];
100
+ /** The last control point, for the smooth curves. */
101
+ let ctrl: P | null = null;
102
+ let prevCmd = '';
103
+ const point = (p: P): void => {
104
+ tpl.push('', '');
105
+ kinds.push(X, Y);
106
+ nums.push(p[0], p[1]);
107
+ };
108
+ let i = 0;
109
+ while (i < tokens.length) {
110
+ const cmd = tokens[i++] as string;
111
+ const per = ARGS[cmd];
112
+ if (per === undefined) throw new Error(`An outline command that is not absolute: ${cmd}`);
113
+ if (per === 0) {
114
+ tpl.push('Z');
115
+ len += dist(cur, start);
116
+ cur = start;
117
+ ctrl = null;
118
+ prevCmd = 'Z';
119
+ continue;
120
+ }
121
+ let first = true;
122
+ while (i < tokens.length && typeof tokens[i] === 'number') {
123
+ const a = tokens.slice(i, i + per) as number[];
124
+ i += per;
125
+ // Pairs after the first of an M are lines.
126
+ const c = cmd === 'M' && !first ? 'L' : cmd;
127
+ first = false;
128
+ if (c === 'M') {
129
+ tpl.push('M');
130
+ point([a[0]!, a[1]!]);
131
+ cur = start = [a[0]!, a[1]!];
132
+ ctrl = null;
133
+ } else if (c === 'L' || c === 'H' || c === 'V') {
134
+ const to: P = c === 'L' ? [a[0]!, a[1]!] : c === 'H' ? [a[0]!, cur[1]] : [cur[0], a[0]!];
135
+ tpl.push('L');
136
+ point(to);
137
+ len += dist(cur, to);
138
+ cur = to;
139
+ ctrl = null;
140
+ } else if (c === 'C' || c === 'S') {
141
+ const reflected: P = ctrl && (prevCmd === 'C' || prevCmd === 'S') ? [2 * cur[0] - ctrl[0], 2 * cur[1] - ctrl[1]] : cur;
142
+ const [b, cc, to]: P[] = c === 'C' ? [[a[0]!, a[1]!], [a[2]!, a[3]!], [a[4]!, a[5]!]] : [reflected, [a[0]!, a[1]!], [a[2]!, a[3]!]];
143
+ tpl.push(c);
144
+ if (c === 'C') point(b!);
145
+ point(cc!);
146
+ point(to!);
147
+ len += sampled(cubic(cur, b!, cc!, to!), 24);
148
+ ctrl = cc!;
149
+ cur = to!;
150
+ } else if (c === 'Q' || c === 'T') {
151
+ const b: P = c === 'Q' ? [a[0]!, a[1]!] : ctrl && (prevCmd === 'Q' || prevCmd === 'T') ? [2 * cur[0] - ctrl[0], 2 * cur[1] - ctrl[1]] : cur;
152
+ const to: P = c === 'Q' ? [a[2]!, a[3]!] : [a[0]!, a[1]!];
153
+ tpl.push(c);
154
+ if (c === 'Q') point(b);
155
+ point(to);
156
+ len += sampled(quad(cur, b, to), 24);
157
+ ctrl = b;
158
+ cur = to;
159
+ } else {
160
+ const to: P = [a[5]!, a[6]!];
161
+ tpl.push('A', '', '', '', '', '');
162
+ kinds.push(RADIUS, RADIUS, ANGLE, FLAG, FLAG);
163
+ nums.push(a[0]!, a[1]!, a[2]!, a[3]!, a[4]!);
164
+ point(to);
165
+ len += arc(cur, a[0]!, a[1]!, a[2]!, a[3]!, a[4]!, to);
166
+ cur = to;
167
+ ctrl = null;
168
+ }
169
+ prevCmd = c;
170
+ }
171
+ }
172
+ return { shape: { tpl, kinds }, outline: { nums, len } };
173
+ }
174
+
175
+ const fmt = (v: number): string => {
176
+ 'worklet';
177
+ return String(Math.round(v * 100) / 100);
178
+ };
179
+
180
+ /**
181
+ * The outline between two sets of numbers, at `t`, moved by (x, y), turned by r degrees and scaled by s around
182
+ * (cx, cy): what the transform of the web does to the same outline.
183
+ */
184
+ export function written(shape: Shape, a: number[], b: number[], t: number, x: number, y: number, r: number, s: number, cx: number, cy: number): string {
185
+ 'worklet';
186
+ const rad = (r * Math.PI) / 180;
187
+ const cos = Math.cos(rad);
188
+ const sin = Math.sin(rad);
189
+ const { tpl, kinds } = shape;
190
+ let out = '';
191
+ let j = 0;
192
+ for (let i = 0; i < tpl.length; i++) {
193
+ const letter = tpl[i];
194
+ if (letter) {
195
+ out += out ? ` ${letter}` : letter;
196
+ continue;
197
+ }
198
+ const kind = kinds[j]!;
199
+ const v = a[j]! + (b[j]! - a[j]!) * t;
200
+ if (kind === X) {
201
+ const w = a[j + 1]! + (b[j + 1]! - a[j + 1]!) * t;
202
+ const dx = v - cx;
203
+ const dy = w - cy;
204
+ out += ` ${fmt(cx + s * (dx * cos - dy * sin) + x)} ${fmt(cy + s * (dx * sin + dy * cos) + y)}`;
205
+ j += 2;
206
+ i++;
207
+ continue;
208
+ }
209
+ out += ` ${fmt(kind === RADIUS ? v * s : kind === ANGLE ? v + r : v)}`;
210
+ j++;
211
+ }
212
+ return out;
213
+ }
package/src/icon.tsx CHANGED
@@ -1,14 +1,20 @@
1
1
  /**
2
- * `@fluxus-ui/native/icon`: an icon of @fluxus-ui/icons, drawn with react-native-svg. Its own entry, so that an app
3
- * that draws no icons does not need the library. Still: the motion of the icons is not here yet. (SPEC §15.5)
2
+ * `@fluxus-ui/native/icon`: an icon of @fluxus-ui/icons, drawn with react-native-svg and moved on the UI thread.
3
+ * The same data and the same engine as the web (icons/src/attach.ts): a state takes every part to its pose with
4
+ * a spring, a push springs back, and at rest it draws what the web draws. Its own entry, so that an app that
5
+ * draws no icons does not need the libraries. (SPEC §15.5, §15.12)
4
6
  */
7
+ import { memo, useEffect, useMemo, useRef } from 'react';
5
8
  import Svg, { Path } from 'react-native-svg';
9
+ import Animated, { cancelAnimation, makeMutable, useAnimatedProps, useReducedMotion, useSharedValue, type SharedValue } from 'react-native-reanimated';
10
+ import { ICON_SPRINGS, REST, poseOf, type Full, type IconDef, type IconPart } from '@fluxus-ui/icons';
11
+ import { useIconHost } from './icon-host';
12
+ import { outline, written, type Outline, type Shape } from './icon-shape';
13
+ import { withFluxSpring } from './motion';
6
14
  import { useTheme } from './theme';
7
15
 
8
- /** What an icon of @fluxus-ui/icons is, as far as drawing it at rest goes: outlines on a 16 × 16 grid. */
9
- export interface IconData {
10
- parts: readonly { id: string; d: string; fill?: boolean; rest?: { d?: string; o?: number; draw?: number | 'p' } }[];
11
- }
16
+ /** An icon of @fluxus-ui/icons. */
17
+ export type IconData = { name?: string; parts: IconDef['parts']; kicks?: IconDef['kicks'] };
12
18
 
13
19
  export interface IconProps {
14
20
  icon: IconData;
@@ -18,22 +24,253 @@ export interface IconProps {
18
24
  color?: string;
19
25
  /** What it means, for a screen reader. Without it the icon is decoration: the text beside it says it. */
20
26
  label?: string;
27
+ /** A state the icon names. Without it, the control it is in says: 'on' when open or checked, else 'rest'. */
28
+ state?: string;
29
+ /** 0–1, for the parts that follow it (`draw: 'p'`). */
30
+ progress?: number;
21
31
  testID?: string;
22
32
  }
23
33
 
24
- export function Icon({ icon, size = 'md', color, label, testID }: IconProps) {
34
+ /** A pose as numbers. `d` is the outline as written, when it is one of the icon's and not a pose caught between two. */
35
+ interface Num {
36
+ nums: number[];
37
+ len: number;
38
+ d: string | null;
39
+ x: number;
40
+ y: number;
41
+ r: number;
42
+ s: number;
43
+ draw: number;
44
+ o: number;
45
+ }
46
+
47
+ interface Prep {
48
+ part: IconPart;
49
+ shape: Shape;
50
+ cx: number;
51
+ cy: number;
52
+ lag: number;
53
+ outlines: Map<string, Outline>;
54
+ }
55
+
56
+ interface Live {
57
+ /** The pose it was made in: the props of its first frame, before the UI thread writes them. */
58
+ first: Num;
59
+ from: SharedValue<Num>;
60
+ to: SharedValue<Num>;
61
+ off: Record<'x' | 'y' | 'r' | 's', SharedValue<number>>;
62
+ }
63
+
64
+ const AnimatedPath = Animated.createAnimatedComponent(Path);
65
+ const STROKE = 1.5;
66
+
67
+ const prepare = (icon: IconData): Prep[] =>
68
+ icon.parts.map((part) => {
69
+ const { shape, outline: rest } = outline(part.d);
70
+ const [cx, cy] = part.c ?? [8, 8];
71
+ return { part, shape, cx, cy, lag: part.lag ?? 0, outlines: new Map([[part.d, rest]]) };
72
+ });
73
+
74
+ const numOf = (prep: Prep, pose: Full): Num => {
75
+ let o = prep.outlines.get(pose.d);
76
+ if (!o) prep.outlines.set(pose.d, (o = outline(pose.d).outline));
77
+ return { nums: o.nums, len: o.len, d: pose.d, x: pose.x, y: pose.y, r: pose.r, s: pose.s, draw: pose.draw, o: pose.o };
78
+ };
79
+
80
+ const clamp01 = (v: number): number => {
81
+ 'worklet';
82
+ return Math.min(1, Math.max(0, v));
83
+ };
84
+
85
+ /** Where a part is at `g` of the way between its two poses, as in attach.ts `now()`. */
86
+ const between = (a: Num, b: Num, g: number, lag: number): Num => {
87
+ 'worklet';
88
+ const t = lag ? Math.max(0, (g - lag) / (1 - lag)) : g;
89
+ const pose: Num = { ...b, d: t >= 1 ? b.d : t <= 0 ? a.d : a.d !== null && a.d === b.d ? a.d : null };
90
+ pose.nums = t >= 1 ? b.nums : t <= 0 ? a.nums : a.nums.map((v, i) => v + (b.nums[i]! - v) * t);
91
+ pose.len = a.len + (b.len - a.len) * clamp01(t);
92
+ pose.x = a.x + (b.x - a.x) * t;
93
+ pose.y = a.y + (b.y - a.y) * t;
94
+ pose.r = a.r + (b.r - a.r) * t;
95
+ pose.s = a.s + (b.s - a.s) * t;
96
+ pose.draw = clamp01(a.draw + (b.draw - a.draw) * t);
97
+ pose.o = clamp01(a.o + (b.o - a.o) * t);
98
+ return pose;
99
+ };
100
+
101
+ /** The props of a part in a pose, with what the pushes add: the attributes of attach.ts, without transforms. */
102
+ const propsOf = (shape: Shape, p: Num, cx: number, cy: number, ox: number, oy: number, or: number, os: number) => {
103
+ 'worklet';
104
+ const x = p.x + ox;
105
+ const y = p.y + oy;
106
+ const r = p.r + or;
107
+ const s = p.s + os;
108
+ const still = Math.abs(x) < 1e-6 && Math.abs(y) < 1e-6 && Math.abs(r) < 1e-6 && Math.abs(s - 1) < 1e-6;
109
+ const d = still && p.d !== null ? p.d : written(shape, p.nums, p.nums, 0, x, y, r, s, cx, cy);
110
+ const hidden = p.draw < 0.001 || p.o < 0.001;
111
+ const len = Math.max(0, p.len * s);
112
+ return {
113
+ d,
114
+ strokeWidth: STROKE * Math.max(0, s),
115
+ // A part not drawn at all is hidden: with round caps a dash of 0 would still paint a dot.
116
+ opacity: hidden ? 0 : p.o,
117
+ // Whole: a dash longer than the outline. Part of it: drawn from where the outline starts.
118
+ strokeDasharray: hidden || p.draw > 0.999 ? [len * 2 + 1, 1] : [Math.max(0.001, p.draw * len), len * 2 + 1],
119
+ };
120
+ };
121
+
122
+ const ids = new WeakMap<object, number>();
123
+ let lastId = 0;
124
+ const idOf = (icon: object): number => {
125
+ let id = ids.get(icon);
126
+ if (id === undefined) ids.set(icon, (id = ++lastId));
127
+ return id;
128
+ };
129
+
130
+ /** An icon is live when it has another state or a push; else it is drawn once, with nothing animated. */
131
+ const isLive = (icon: IconData): boolean => !!(icon.kicks && Object.keys(icon.kicks).length) || icon.parts.some((p) => p.on && Object.keys(p.on).length);
132
+
133
+ export function Icon({ icon, size = 'md', color, label, state, progress = 0, testID }: IconProps) {
25
134
  const theme = useTheme();
135
+ const host = useIconHost();
26
136
  const side = typeof size === 'number' ? size : theme.size[`icon.${size}`];
27
- const ink = color ?? theme.color['text.default'];
137
+ const ink = color ?? host?.ink ?? theme.color['text.default'];
28
138
  const a11y = label ? ({ accessible: true, accessibilityRole: 'image', accessibilityLabel: label } as const) : ({ accessible: false, accessibilityElementsHidden: true, importantForAccessibility: 'no-hide-descendants' } as const);
139
+ const preps = useMemo(() => prepare(icon), [icon]);
140
+ const now = state ?? (host?.on ? 'on' : REST);
29
141
  return (
30
- <Svg testID={testID} width={side} height={side} viewBox="0 0 16 16" fill="none" stroke={ink} strokeWidth={1.5} strokeLinecap="round" strokeLinejoin="round" {...a11y}>
31
- {icon.parts
32
- // A part that is hidden at rest only shows in another state of the icon.
33
- .filter((p) => p.rest?.o !== 0 && p.rest?.draw !== 0)
34
- .map((p) => (
35
- <Path key={p.id} d={p.rest?.d ?? p.d} opacity={p.rest?.o} fill={p.fill ? ink : undefined} />
36
- ))}
142
+ <Svg testID={testID} width={side} height={side} viewBox="0 0 16 16" fill="none" stroke={ink} strokeWidth={STROKE} strokeLinecap="round" strokeLinejoin="round" {...a11y}>
143
+ {isLive(icon) ? (
144
+ <LiveParts key={idOf(icon)} icon={icon} preps={preps} state={now} progress={progress} ink={ink} />
145
+ ) : (
146
+ preps.map((prep) => {
147
+ const pose = numOf(prep, poseOf(prep.part, now, progress));
148
+ const props = propsOf(prep.shape, pose, prep.cx, prep.cy, 0, 0, 0, 0);
149
+ // A part that is hidden at rest only shows in another state of the icon.
150
+ if (props.opacity === 0) return null;
151
+ return <Path key={prep.part.id} d={props.d} opacity={pose.o < 1 ? pose.o : undefined} strokeWidth={pose.s !== 1 ? props.strokeWidth : undefined} strokeDasharray={pose.draw < 1 ? props.strokeDasharray : undefined} fill={prep.part.fill ? ink : undefined} />;
152
+ })
153
+ )}
37
154
  </Svg>
38
155
  );
39
156
  }
157
+
158
+ function LiveParts({ icon, preps, state, progress, ink }: { icon: IconData; preps: Prep[]; state: string; progress: number; ink: string }) {
159
+ const host = useIconHost();
160
+ const reduced = useReducedMotion();
161
+ const g = useSharedValue(1);
162
+ const lives = useMemo<Live[]>(
163
+ () =>
164
+ preps.map((prep) => {
165
+ const pose = numOf(prep, poseOf(prep.part, state, progress));
166
+ return { first: pose, from: makeMutable(pose), to: makeMutable(pose), off: { x: makeMutable(0), y: makeMutable(0), r: makeMutable(0), s: makeMutable(0) } };
167
+ }),
168
+ // The poses it starts in; after that the engine moves them.
169
+ [preps],
170
+ );
171
+ const engine = useRef({ state, progress, ends: [state, state] as [string | null, string], aim: 1 as 0 | 1, timers: new Set<ReturnType<typeof setTimeout>>() });
172
+
173
+ const kick = (name: string): void => {
174
+ if (reduced) return;
175
+ for (const k of icon.kicks?.[name] ?? []) {
176
+ const start = (): void => {
177
+ preps.forEach((prep, i) => {
178
+ if (k.p !== '*' && k.p !== prep.part.id) return;
179
+ const sv = lives[i]!.off[k.k];
180
+ const to = k.to ?? 0;
181
+ // One push at a time on each property: a second one adds to the speed of the first.
182
+ sv.value = withFluxSpring(to, { ...(to ? ICON_SPRINGS.turn : ICON_SPRINGS.push), velocity: k.v ?? 0, restDelta: k.k === 's' ? 0.002 : 0.05, restSpeed: 0.05 }, (finished) => {
183
+ 'worklet';
184
+ // A full turn looks like none: it starts from 0 again.
185
+ if (finished) sv.value = 0;
186
+ });
187
+ });
188
+ };
189
+ if (k.wait) {
190
+ const timer = setTimeout(() => {
191
+ engine.current.timers.delete(timer);
192
+ start();
193
+ }, k.wait);
194
+ engine.current.timers.add(timer);
195
+ } else start();
196
+ }
197
+ };
198
+
199
+ const go = (next: string, force: boolean): void => {
200
+ const e = engine.current;
201
+ if (next === e.state && !force) return;
202
+ const at = g.value;
203
+ const running = Math.abs(at - e.aim) > 0.002;
204
+ if (reduced) {
205
+ cancelAnimation(g);
206
+ e.state = next;
207
+ e.aim = 1;
208
+ e.ends = [next, next];
209
+ preps.forEach((prep, i) => {
210
+ const pose = numOf(prep, poseOf(prep.part, next, e.progress));
211
+ lives[i]!.from.value = pose;
212
+ lives[i]!.to.value = pose;
213
+ });
214
+ g.value = 1;
215
+ return;
216
+ }
217
+ if (running && !force && next === e.ends[1 - e.aim]) {
218
+ // Back to where it came from: the same spring turns round with the speed it has.
219
+ e.aim = e.aim ? 0 : 1;
220
+ e.state = next;
221
+ g.value = withFluxSpring(e.aim, { ...ICON_SPRINGS.state, restDelta: 0.002 });
222
+ } else {
223
+ // Anywhere else: from the pose it is in.
224
+ preps.forEach((prep, i) => {
225
+ const live = lives[i]!;
226
+ live.from.value = between(live.from.value, live.to.value, at, prep.lag);
227
+ live.to.value = numOf(prep, poseOf(prep.part, next, e.progress));
228
+ });
229
+ e.ends = [running || force ? null : e.state, next];
230
+ e.state = next;
231
+ e.aim = 1;
232
+ g.value = 0;
233
+ g.value = withFluxSpring(1, { ...ICON_SPRINGS.state, restDelta: 0.002 });
234
+ }
235
+ if (!force) kick(next);
236
+ };
237
+ const latest = useRef({ go, kick });
238
+ latest.current = { go, kick };
239
+
240
+ useEffect(() => {
241
+ const e = engine.current;
242
+ const moved = progress !== e.progress;
243
+ e.progress = progress;
244
+ latest.current.go(state, moved);
245
+ }, [state, progress]);
246
+
247
+ useEffect(() => host?.listen((name) => latest.current.kick(name)), [host]);
248
+
249
+ useEffect(
250
+ () => () => {
251
+ for (const timer of engine.current.timers) clearTimeout(timer);
252
+ cancelAnimation(g);
253
+ for (const live of lives) for (const sv of Object.values(live.off)) cancelAnimation(sv);
254
+ },
255
+ [g, lives],
256
+ );
257
+
258
+ return (
259
+ <>
260
+ {preps.map((prep, i) => (
261
+ <LivePart key={prep.part.id} prep={prep} live={lives[i]!} g={g} ink={ink} />
262
+ ))}
263
+ </>
264
+ );
265
+ }
266
+
267
+ const LivePart = memo(function LivePart({ prep, live, g, ink }: { prep: Prep; live: Live; g: SharedValue<number>; ink: string }) {
268
+ const { shape, cx, cy, lag } = prep;
269
+ const { from, to, off } = live;
270
+ const animated = useAnimatedProps(() => propsOf(shape, between(from.value, to.value, g.value, lag), cx, cy, off.x.value, off.y.value, off.r.value, off.s.value));
271
+ // The same every render: React does not send it again over what the UI thread writes.
272
+ const first = useMemo(() => propsOf(shape, live.first, cx, cy, 0, 0, 0, 0), [shape, live, cx, cy]);
273
+ return <AnimatedPath {...first} animatedProps={animated} fill={prep.part.fill ? ink : undefined} />;
274
+ });
275
+
276
+ export { Success, type SuccessProps } from './success';
package/src/index.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export { ThemeProvider, useTheme, type Theme, type ThemeProviderProps, type Mode, type Fonts } from './theme';
2
2
  export { themes, motion, type ThemeTokens, type AppToken, type ColorToken, type SizeToken, type NumberToken, type WeightToken } from './generated/tokens';
3
- export { spring, timing, type SpringPreset, type DurationToken, type EasingToken } from './motion';
3
+ export { spring, timing, ms, setMotionScale, withFluxSpring, type SpringPreset, type DurationToken, type EasingToken, type FluxSpringConfig } from './motion';
4
+ export { IconHostProvider, useHostFor, useIconHost, isOn, type IconHost } from './icon-host';
5
+ export { Mark, TICK, DASH, BANG, type MarkProps } from './marks';
6
+ export { setHaptics, feel, type HapticKind } from './feel';
4
7
  export { Text, textStyle, type TextProps, type TextVariant, type TextTone, type TextWeight } from './Text';
5
8
  export { Button, buttonStyle, type ButtonProps, type ButtonVariant, type ButtonSize } from './Button';
6
9
  export { usePress } from './press';
@@ -28,3 +31,6 @@ export { Popover, place, type PopoverProps, type Placement, type Rect } from './
28
31
  export { Menu, MenuItem, MenuSeparator, type MenuProps, type MenuItemProps } from './Menu';
29
32
  export { Disclosure, Accordion, type DisclosureProps, type AccordionProps, type AccordionItem } from './Disclosure';
30
33
  export { Select, type SelectProps, type SelectOption } from './Select';
34
+ export { Link, type LinkProps } from './Link';
35
+ export { Bubble, type BubbleProps, type BubbleStatus } from './Bubble';
36
+ export { AudioPlayer, clock, type AudioPlayerProps } from './AudioPlayer';
package/src/marks.tsx ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Marks drawn with views, without SVG, for the pieces of the main entry: a tick, a dash and an exclamation. Each
3
+ * stroke is a bar that grows from where it starts, turned along it; the next one starts where it ends. Only
4
+ * `transform` and `opacity` move. (SPEC §15.14)
5
+ */
6
+ import { useEffect } from 'react';
7
+ import { View, type StyleProp, type ViewStyle } from 'react-native';
8
+ import Animated, { useAnimatedStyle, useSharedValue, type SharedValue } from 'react-native-reanimated';
9
+ import { spring } from './motion';
10
+
11
+ /** x1 y1 x2 y2, on the 16 × 16 grid of the icons. */
12
+ type Segment = readonly [number, number, number, number];
13
+
14
+ /** As the icon `tick`. */
15
+ export const TICK: readonly Segment[] = [
16
+ [3.5, 8.5, 6.5, 11.5],
17
+ [6.5, 11.5, 12.5, 4.5],
18
+ ];
19
+ export const DASH: readonly Segment[] = [[4, 8, 12, 8]];
20
+ /** A bar, then its dot. */
21
+ export const BANG: readonly Segment[] = [
22
+ [8, 3.5, 8, 9],
23
+ [8, 12, 8, 12],
24
+ ];
25
+
26
+ export interface MarkProps {
27
+ segments: readonly Segment[];
28
+ /** Drawn, or not; it draws and undraws on the snappy spring. */
29
+ shown: boolean;
30
+ /** Its side, in points. Default: 16. */
31
+ size?: number;
32
+ color: string;
33
+ /** The width of its stroke, in points. Default: 1.5 of every 16. */
34
+ stroke?: number;
35
+ style?: StyleProp<ViewStyle>;
36
+ testID?: string;
37
+ }
38
+
39
+ const length = ([x1, y1, x2, y2]: Segment): number => Math.hypot(x2 - x1, y2 - y1);
40
+
41
+ export function Mark({ segments, shown, size = 16, color, stroke, style, testID }: MarkProps) {
42
+ const t = useSharedValue(shown ? 1 : 0);
43
+ useEffect(() => {
44
+ t.value = spring(shown ? 1 : 0, 'snappy');
45
+ }, [shown, t]);
46
+ // A dot has no length: it takes a share of the way as if it were a short stroke.
47
+ const lengths = segments.map((s) => Math.max(length(s), 1));
48
+ const total = lengths.reduce((a, b) => a + b, 0);
49
+ let from = 0;
50
+ return (
51
+ <View testID={testID} aria-hidden pointerEvents="none" style={[{ width: size, height: size }, style]}>
52
+ {segments.map((segment, i) => {
53
+ const part = lengths[i]! / total;
54
+ const bar = <Bar key={i} segment={segment} from={from} part={part} t={t} k={size / 16} h={stroke ?? (1.5 * size) / 16} color={color} />;
55
+ from += part;
56
+ return bar;
57
+ })}
58
+ </View>
59
+ );
60
+ }
61
+
62
+ function Bar({ segment, from, part, t, k, h, color }: { segment: Segment; from: number; part: number; t: SharedValue<number>; k: number; h: number; color: string }) {
63
+ const [x1, y1, x2, y2] = segment;
64
+ const angle = Math.atan2(y2 - y1, x2 - x1);
65
+ const grow = useAnimatedStyle(() => {
66
+ const local = Math.min(1, Math.max(0, (t.value - from) / part));
67
+ // Below 2 % it is not there: the spring that unwrites it swings past 0 and would show a sliver.
68
+ return { opacity: local > 0.02 ? 1 : 0, transform: [{ rotate: `${angle}rad` }, { scaleX: Math.max(local, 0.001) }] };
69
+ });
70
+ // Round ends, as the stroke of the icons: the bar reaches half its width past each end.
71
+ return (
72
+ <Animated.View
73
+ style={[
74
+ { position: 'absolute', left: x1 * k - h / 2, top: y1 * k - h / 2, width: length(segment) * k + h, height: h, borderRadius: h / 2, backgroundColor: color, transformOrigin: [h / 2, h / 2, 0] },
75
+ grow,
76
+ ]}
77
+ />
78
+ );
79
+ }