@hozo/primitives 0.2.0 → 0.4.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 (187) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +18 -1
  3. package/dist/button-host-attributes.d.ts +20 -0
  4. package/dist/button-host-attributes.d.ts.map +1 -0
  5. package/dist/button-host-attributes.js +47 -0
  6. package/dist/button-host-attributes.js.map +1 -0
  7. package/dist/button-host-props.native.d.ts +8 -0
  8. package/dist/button-host-props.native.d.ts.map +1 -0
  9. package/dist/button-host-props.native.js +20 -0
  10. package/dist/button-host-props.native.js.map +1 -0
  11. package/dist/foundation.d.ts +27 -6
  12. package/dist/foundation.d.ts.map +1 -1
  13. package/dist/foundation.js +72 -19
  14. package/dist/foundation.js.map +1 -1
  15. package/dist/foundation.native.d.ts +27 -7
  16. package/dist/foundation.native.d.ts.map +1 -1
  17. package/dist/foundation.native.js +18 -3
  18. package/dist/foundation.native.js.map +1 -1
  19. package/dist/generated/button-dom-props.d.ts +11 -0
  20. package/dist/generated/button-dom-props.d.ts.map +1 -0
  21. package/dist/generated/button-dom-props.js +132 -0
  22. package/dist/generated/button-dom-props.js.map +1 -0
  23. package/dist/generated/button-props.d.ts +3 -0
  24. package/dist/generated/button-props.d.ts.map +1 -0
  25. package/dist/generated/button-props.js +5 -0
  26. package/dist/generated/button-props.js.map +1 -0
  27. package/dist/generated/button-props.native.d.ts +2 -0
  28. package/dist/generated/button-props.native.d.ts.map +1 -0
  29. package/dist/generated/button-props.native.js +2 -0
  30. package/dist/generated/button-props.native.js.map +1 -0
  31. package/dist/generated/inherited-label.d.ts +2 -0
  32. package/dist/generated/inherited-label.d.ts.map +1 -0
  33. package/dist/generated/inherited-label.js +3 -0
  34. package/dist/generated/inherited-label.js.map +1 -0
  35. package/dist/generated/inherited-pressable.d.ts +2 -0
  36. package/dist/generated/inherited-pressable.d.ts.map +1 -0
  37. package/dist/generated/inherited-pressable.js +3 -0
  38. package/dist/generated/inherited-pressable.js.map +1 -0
  39. package/dist/generated/inherited-relative-text.d.ts +2 -0
  40. package/dist/generated/inherited-relative-text.d.ts.map +1 -0
  41. package/dist/generated/inherited-relative-text.js +3 -0
  42. package/dist/generated/inherited-relative-text.js.map +1 -0
  43. package/dist/generated/inherited-text.d.ts +2 -0
  44. package/dist/generated/inherited-text.d.ts.map +1 -0
  45. package/dist/generated/inherited-text.js +3 -0
  46. package/dist/generated/inherited-text.js.map +1 -0
  47. package/dist/generated/inherited-view.d.ts +2 -0
  48. package/dist/generated/inherited-view.d.ts.map +1 -0
  49. package/dist/generated/inherited-view.js +3 -0
  50. package/dist/generated/inherited-view.js.map +1 -0
  51. package/dist/generated/native-disabled.d.ts +2 -0
  52. package/dist/generated/native-disabled.d.ts.map +1 -0
  53. package/dist/generated/native-disabled.js +2 -0
  54. package/dist/generated/native-disabled.js.map +1 -0
  55. package/dist/generated/text-input-dom-props.d.ts +7 -0
  56. package/dist/generated/text-input-dom-props.d.ts.map +1 -0
  57. package/dist/generated/text-input-dom-props.js +130 -0
  58. package/dist/generated/text-input-dom-props.js.map +1 -0
  59. package/dist/generated/text-input.native.d.ts +1 -1
  60. package/dist/generated/text-input.native.d.ts.map +1 -1
  61. package/dist/generated/text-input.native.js +1 -1
  62. package/dist/generated/text-input.native.js.map +1 -1
  63. package/dist/generated/view-dom-props.d.ts +8 -0
  64. package/dist/generated/view-dom-props.d.ts.map +1 -0
  65. package/dist/generated/view-dom-props.js +241 -0
  66. package/dist/generated/view-dom-props.js.map +1 -0
  67. package/dist/inherited-label.native.d.ts +8 -0
  68. package/dist/inherited-label.native.d.ts.map +1 -0
  69. package/dist/inherited-label.native.js +12 -0
  70. package/dist/inherited-label.native.js.map +1 -0
  71. package/dist/inherited-pressable.native.d.ts +11 -0
  72. package/dist/inherited-pressable.native.d.ts.map +1 -0
  73. package/dist/inherited-pressable.native.js +75 -0
  74. package/dist/inherited-pressable.native.js.map +1 -0
  75. package/dist/inherited-relative-text.native.d.ts +13 -0
  76. package/dist/inherited-relative-text.native.d.ts.map +1 -0
  77. package/dist/inherited-relative-text.native.js +16 -0
  78. package/dist/inherited-relative-text.native.js.map +1 -0
  79. package/dist/inherited-text-scope.d.ts +8 -0
  80. package/dist/inherited-text-scope.d.ts.map +1 -0
  81. package/dist/inherited-text-scope.js +10 -0
  82. package/dist/inherited-text-scope.js.map +1 -0
  83. package/dist/inherited-text-style.native.d.ts +5 -0
  84. package/dist/inherited-text-style.native.d.ts.map +1 -0
  85. package/dist/inherited-text-style.native.js +30 -0
  86. package/dist/inherited-text-style.native.js.map +1 -0
  87. package/dist/inherited-text.native.d.ts +5 -0
  88. package/dist/inherited-text.native.d.ts.map +1 -0
  89. package/dist/inherited-text.native.js +18 -0
  90. package/dist/inherited-text.native.js.map +1 -0
  91. package/dist/inherited-view.native.d.ts +9 -0
  92. package/dist/inherited-view.native.d.ts.map +1 -0
  93. package/dist/inherited-view.native.js +17 -0
  94. package/dist/inherited-view.native.js.map +1 -0
  95. package/dist/link.d.ts +4 -3
  96. package/dist/link.d.ts.map +1 -1
  97. package/dist/link.js +9 -1
  98. package/dist/link.js.map +1 -1
  99. package/dist/link.native.d.ts +16 -7
  100. package/dist/link.native.d.ts.map +1 -1
  101. package/dist/link.native.js +15 -5
  102. package/dist/link.native.js.map +1 -1
  103. package/dist/native-disabled.d.ts +13 -0
  104. package/dist/native-disabled.d.ts.map +1 -0
  105. package/dist/native-disabled.js +13 -0
  106. package/dist/native-disabled.js.map +1 -0
  107. package/dist/presence.native.d.ts.map +1 -1
  108. package/dist/presence.native.js +1 -1
  109. package/dist/presence.native.js.map +1 -1
  110. package/dist/pressable.d.ts +1 -1
  111. package/dist/pressable.d.ts.map +1 -1
  112. package/dist/pressable.js +15 -5
  113. package/dist/pressable.js.map +1 -1
  114. package/dist/pressable.native.d.ts +10 -1
  115. package/dist/pressable.native.d.ts.map +1 -1
  116. package/dist/pressable.native.js +206 -221
  117. package/dist/pressable.native.js.map +1 -1
  118. package/dist/runtime-container.native.d.ts +11 -9
  119. package/dist/runtime-container.native.d.ts.map +1 -1
  120. package/dist/runtime-container.native.js +8 -6
  121. package/dist/runtime-container.native.js.map +1 -1
  122. package/dist/text-color-transition.native.d.ts +12 -0
  123. package/dist/text-color-transition.native.d.ts.map +1 -0
  124. package/dist/text-color-transition.native.js +87 -0
  125. package/dist/text-color-transition.native.js.map +1 -0
  126. package/dist/text-input-props.native.d.ts +10 -0
  127. package/dist/text-input-props.native.d.ts.map +1 -0
  128. package/dist/text-input-props.native.js +15 -0
  129. package/dist/text-input-props.native.js.map +1 -0
  130. package/dist/text-input.native.d.ts +19 -0
  131. package/dist/text-input.native.d.ts.map +1 -0
  132. package/dist/text-input.native.js +32 -0
  133. package/dist/text-input.native.js.map +1 -0
  134. package/dist/view.d.ts +1 -285
  135. package/dist/view.d.ts.map +1 -1
  136. package/dist/view.js +5 -4
  137. package/dist/view.js.map +1 -1
  138. package/package.json +51 -7
  139. package/src/button-host-attributes.ts +56 -0
  140. package/src/button-host-props.native.ts +22 -0
  141. package/src/foundation.native.tsx +45 -6
  142. package/src/foundation.tsx +139 -28
  143. package/src/generated/button-dom-props.ts +127 -0
  144. package/src/generated/button-props.native.ts +1 -0
  145. package/src/generated/button-props.ts +4 -0
  146. package/src/generated/inherited-label.ts +2 -0
  147. package/src/generated/inherited-pressable.ts +2 -0
  148. package/src/generated/inherited-relative-text.ts +2 -0
  149. package/src/generated/inherited-text.ts +2 -0
  150. package/src/generated/inherited-view.ts +2 -0
  151. package/src/generated/native-disabled.ts +1 -0
  152. package/src/generated/text-input-dom-props.ts +128 -0
  153. package/src/generated/text-input.native.ts +1 -4
  154. package/src/generated/view-dom-props.ts +267 -0
  155. package/src/inherited-label.native.tsx +28 -0
  156. package/src/inherited-pressable.native.tsx +152 -0
  157. package/src/inherited-relative-text.native.tsx +22 -0
  158. package/src/inherited-text-scope.tsx +27 -0
  159. package/src/inherited-text-style.native.ts +35 -0
  160. package/src/inherited-text.native.tsx +32 -0
  161. package/src/inherited-view.native.tsx +37 -0
  162. package/src/link.native.tsx +43 -28
  163. package/src/link.ts +12 -0
  164. package/src/native-disabled.ts +23 -0
  165. package/src/presence.native.tsx +1 -1
  166. package/src/pressable.native.tsx +298 -262
  167. package/src/pressable.ts +34 -17
  168. package/src/runtime-container.native.tsx +30 -15
  169. package/src/text-color-transition.native.ts +96 -0
  170. package/src/text-input-props.native.ts +26 -0
  171. package/src/text-input.native.tsx +57 -0
  172. package/src/view.ts +34 -25
  173. package/dist/generated/animated.d.ts +0 -2
  174. package/dist/generated/animated.d.ts.map +0 -1
  175. package/dist/generated/animated.js +0 -3
  176. package/dist/generated/animated.js.map +0 -1
  177. package/dist/presence-context.d.ts +0 -15
  178. package/dist/presence-context.d.ts.map +0 -1
  179. package/dist/presence-context.js +0 -11
  180. package/dist/presence-context.js.map +0 -1
  181. package/dist/runtime-transition.native.d.ts +0 -30
  182. package/dist/runtime-transition.native.d.ts.map +0 -1
  183. package/dist/runtime-transition.native.js +0 -183
  184. package/dist/runtime-transition.native.js.map +0 -1
  185. package/src/generated/animated.ts +0 -6
  186. package/src/presence-context.ts +0 -27
  187. package/src/runtime-transition.native.tsx +0 -244
@@ -1,183 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- // A transition driven by the style changing, rather than by an
3
- // interaction.
4
- //
5
- // `./pressable.native.tsx` already animates, and it is built the other way
6
- // round: it knows the four interaction booleans, calls the author's `style`
7
- // function for each combination, and animates between the results. That
8
- // shape is right there and wrong here, because the conditions this has to
9
- // serve are ambient -- `dark:`, `md:`, `motion-reduce:` -- and the
10
- // compiler expresses those as a guarded array, `[base, __hozoDark &&
11
- // dark]`, which is already resolved by the time it arrives.
12
- //
13
- // So this one asks a simpler question: what did the style just become, and
14
- // what was it before? Everything that differs and can be interpolated is
15
- // animated to its new value; everything else is passed straight through.
16
- // That covers the interaction case too, in principle, which is why this is
17
- // not named after the conditions that prompted it.
18
- //
19
- // Colours run on the JavaScript driver and opacity and transforms do not.
20
- // React Native cannot interpolate a colour natively, so mixing the two in
21
- // one `Animated.timing` would drag the fast ones onto the slow driver.
22
- // They are separate animations for that reason and not for tidiness.
23
- import { useContext, useEffect, useMemo, useRef, useState } from 'react';
24
- import { Animated, Easing, StyleSheet } from 'react-native';
25
- import { PresenceContext } from './presence-context.js';
26
- /** The colour properties worth animating, in the order they are read. */
27
- const COLOR_KEYS = ['backgroundColor', 'borderColor', 'color'];
28
- /**
29
- * The transform entries, flattened to `{ key: value }`.
30
- *
31
- * React Native's `transform` is an array of one-key objects, which is a
32
- * shape nothing can be diffed against directly -- two arrays with the same
33
- * entries in a different order are the same transform and not equal.
34
- */
35
- function transformMap(style) {
36
- const out = new Map();
37
- const transform = style?.transform;
38
- if (!Array.isArray(transform))
39
- return out;
40
- for (const entry of transform) {
41
- for (const [key, value] of Object.entries(entry)) {
42
- if (typeof value === 'number')
43
- out.set(key, value);
44
- }
45
- }
46
- return out;
47
- }
48
- /** What a transform key means when nothing sets it. */
49
- function identity(key) {
50
- return key.startsWith('scale') ? 1 : 0;
51
- }
52
- export function HozoAnimated({ style, hozoTransition, hozoStarting, hozoExit, children, ...props }) {
53
- const presence = useContext(PresenceContext);
54
- // Only an element that has somewhere to leave to takes part. One without
55
- // `hozoExit` neither waits for `Presence` nor makes it wait.
56
- const exits = presence !== null && hozoExit != null;
57
- const leaving = exits && presence.state === 'closed';
58
- const flat = useMemo(() => StyleSheet.flatten(leaving ? [style, hozoExit] : style) ?? {}, [style, hozoExit, leaving]);
59
- // One progress value per animation rather than one per property. The
60
- // properties change together -- they are the same rule flipping -- so
61
- // animating a single 0 → 1 and interpolating each property from its old
62
- // value to its new one is both cheaper and impossible to get out of
63
- // step with itself.
64
- const progress = useRef(new Animated.Value(1)).current;
65
- // Without `hozoStarting` the first render has nothing to animate from --
66
- // `from` and `to` are the same style, and the mount effect below runs an
67
- // animation that moves nothing. With it, `from` is the starting frame
68
- // laid over the style, so that same mount effect is the enter animation.
69
- const [entersFrom] = useState(() => hozoStarting ? { ...flat, ...(StyleSheet.flatten(hozoStarting) ?? {}) } : flat);
70
- const from = useRef(entersFrom);
71
- const to = useRef(flat);
72
- // What changed, computed during render so the effect below has nothing
73
- // to work out. `JSON.stringify` on the two style objects is enough: they
74
- // are flat, their values are primitives, and the compiler emits the keys
75
- // in a stable order.
76
- const signature = JSON.stringify(flat);
77
- const previous = useRef(signature);
78
- if (previous.current !== signature) {
79
- // The *visible* style becomes the new starting point, not the
80
- // previous target. Interrupting a half-finished fade and restarting
81
- // from where it was going would make it jump backwards first.
82
- from.current = to.current;
83
- to.current = flat;
84
- previous.current = signature;
85
- }
86
- // Registered for as long as it can exit, so `Presence` knows to wait for
87
- // it and how long to wait before giving up.
88
- const [id] = useState(() => Symbol('HozoAnimated'));
89
- const register = exits ? presence.register : undefined;
90
- const exitMs = hozoTransition ? hozoTransition.duration + (hozoTransition.delay ?? 0) : 0;
91
- useEffect(() => register?.(id, exitMs), [register, id, exitMs]);
92
- // Read when the animation ends, which is after this render's values may
93
- // have changed.
94
- const leavingNow = useRef(leaving);
95
- leavingNow.current = leaving;
96
- const done = presence?.done;
97
- const doneNow = useRef(done);
98
- doneNow.current = done;
99
- // `signature` is read above rather than inside, and it is what says the
100
- // style moved -- `hozoTransition` does not change when only the values
101
- // do. Without it a style change starts no transition at all, which is
102
- // the whole feature. `biome check --unsafe` offers to remove it.
103
- // biome-ignore lint/correctness/useExhaustiveDependencies: it is what says the style changed
104
- useEffect(() => {
105
- if (!hozoTransition)
106
- return;
107
- progress.setValue(0);
108
- const animation = Animated.timing(progress, {
109
- toValue: 1,
110
- duration: hozoTransition.duration,
111
- delay: hozoTransition.delay ?? 0,
112
- easing: easingFor(hozoTransition.easing),
113
- // Colours are in here too, and React Native cannot interpolate one
114
- // on the native driver. One animation on the JavaScript driver is
115
- // the honest choice: two drivers running the same rule would drift
116
- // against each other on a busy frame, and a background that arrives
117
- // before the text that sits on it is worse than both arriving late.
118
- useNativeDriver: false,
119
- });
120
- animation.start(({ finished }) => {
121
- if (finished && leavingNow.current)
122
- doneNow.current?.(id);
123
- });
124
- return () => animation.stop();
125
- }, [progress, signature, hozoTransition, id]);
126
- // biome-ignore lint/correctness/useExhaustiveDependencies: as above -- `signature` is what changed
127
- const animated = useMemo(() => {
128
- if (!hozoTransition)
129
- return flat;
130
- const overrides = {};
131
- if (typeof from.current.opacity === 'number' || typeof flat.opacity === 'number') {
132
- const start = typeof from.current.opacity === 'number' ? from.current.opacity : 1;
133
- const end = typeof flat.opacity === 'number' ? flat.opacity : 1;
134
- if (start !== end) {
135
- overrides.opacity = progress.interpolate({ inputRange: [0, 1], outputRange: [start, end] });
136
- }
137
- }
138
- for (const key of COLOR_KEYS) {
139
- // Read through a record: `color` lives on `TextStyle` and the rest
140
- // on `ViewStyle`, and a component that may be either has to look at
141
- // both. The runtime check below is what makes it safe.
142
- const start = from.current[key];
143
- const end = flat[key];
144
- if (typeof start !== 'string' || typeof end !== 'string' || start === end)
145
- continue;
146
- overrides[key] = progress.interpolate({ inputRange: [0, 1], outputRange: [start, end] });
147
- }
148
- const before = transformMap(from.current);
149
- const after = transformMap(flat);
150
- const keys = [...new Set([...before.keys(), ...after.keys()])];
151
- const moved = keys.filter((key) => (before.get(key) ?? identity(key)) !== (after.get(key) ?? identity(key)));
152
- if (moved.length > 0) {
153
- // Every key, not only the moved ones: `transform` is replaced
154
- // wholesale, so leaving one out would reset it to identity halfway
155
- // through the animation of its neighbour.
156
- overrides.transform = keys.map((key) => ({
157
- [key]: progress.interpolate({
158
- inputRange: [0, 1],
159
- outputRange: [before.get(key) ?? identity(key), after.get(key) ?? identity(key)],
160
- }),
161
- }));
162
- }
163
- return [flat, overrides];
164
- }, [flat, hozoTransition, progress, signature]);
165
- return (_jsx(Animated.View, { style: animated, ...props, children: exits ? (_jsx(PresenceContext.Provider, { value: null, children: children })) : (children) }));
166
- }
167
- function easingFor(name) {
168
- // Built here rather than in a module-level table, for the reason
169
- // `hooks.native.ts` does the same: a table calls `Easing.bezier` on
170
- // import, for every project that loads the package and never
171
- // transitions. The import itself is free; the call is not.
172
- switch (name) {
173
- case 'linear':
174
- return Easing.linear;
175
- case 'ease-in':
176
- return Easing.bezier(0.4, 0, 1, 1);
177
- case 'ease-out':
178
- return Easing.bezier(0, 0, 0.2, 1);
179
- default:
180
- return Easing.bezier(0.4, 0, 0.2, 1);
181
- }
182
- }
183
- //# sourceMappingURL=runtime-transition.native.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime-transition.native.js","sourceRoot":"","sources":["../src/runtime-transition.native.tsx"],"names":[],"mappings":";AAAA,+DAA+D;AAC/D,eAAe;AACf,EAAE;AACF,2EAA2E;AAC3E,4EAA4E;AAC5E,wEAAwE;AACxE,0EAA0E;AAC1E,mEAAmE;AACnE,qEAAqE;AACrE,4DAA4D;AAC5D,EAAE;AACF,2EAA2E;AAC3E,yEAAyE;AACzE,yEAAyE;AACzE,2EAA2E;AAC3E,mDAAmD;AACnD,EAAE;AACF,0EAA0E;AAC1E,0EAA0E;AAC1E,uEAAuE;AACvE,qEAAqE;AAErE,OAAO,EAAkB,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACxF,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAkB,UAAU,EAAkB,MAAM,cAAc,CAAA;AAC3F,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAQvD,yEAAyE;AACzE,MAAM,UAAU,GAAG,CAAC,iBAAiB,EAAE,aAAa,EAAE,OAAO,CAAU,CAAA;AAEvE;;;;;;GAMG;AACH,SAAS,YAAY,CAAC,KAA4B;IAChD,MAAM,GAAG,GAAG,IAAI,GAAG,EAAkB,CAAA;IACrC,MAAM,SAAS,GAAG,KAAK,EAAE,SAAS,CAAA;IAClC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;QAAE,OAAO,GAAG,CAAA;IACzC,KAAK,M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AS,CAAC,CAAC,CAAA;IAE/C,OAAO,CACL,KAAC,QAAQ,CAAC,IAAI,IAAC,KAAK,EAAE,QAAgC,KAAM,KAAK,YAC9D,KAAK,CAAC,CAAC,CAAC,CAIP,KAAC,eAAe,CAAC,QAAQ,IAAC,KAAK,EAAE,IAAI,YAAG,QAAQ,GAA4B,CAC7E,CAAC,CAAC,CAAC,CACF,QAAQ,CACT,GACa,CACjB,CAAA;AACH,CAAC;AAED,SAAS,SAAS,CAAC,IAAkC;IACnD,iEAAiE;IACjE,oEAAoE;IACpE,6DAA6D;IAC7D,2DAA2D;IAC3D,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,QAAQ;YACX,OAAO,MAAM,CAAC,MAAM,CAAA;QACtB,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;QACpC,KAAK,UAAU;YACb,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAA;QACpC;YACE,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAA;IACxC,CAAC;AACH,CAAC"}
@@ -1,6 +0,0 @@
1
- // Native-only. See `./grid.ts`.
2
- export {
3
- HozoAnimated,
4
- type HozoAnimatedProps,
5
- type HozoTransitionSpec,
6
- } from '../runtime-transition.native.tsx'
@@ -1,27 +0,0 @@
1
- // What `Presence` tells the elements that animate out inside it, on
2
- // Native (decision 007, amendment 1).
3
- //
4
- // On Web the same thing travels as an attribute, `data-state`, and CSS
5
- // does the rest. React Native has no attributes to select on, so the
6
- // state is a context instead, and `HozoAnimated` -- the component the
7
- // compiler puts in place of an element with a `data-[state=closed]:`
8
- // class -- reads it.
9
-
10
- import { createContext } from 'react'
11
-
12
- export type PresenceState = 'open' | 'closed'
13
-
14
- export interface PresenceContextValue {
15
- state: PresenceState
16
- /**
17
- * Called by an element that will animate out, once, on mount. `Presence`
18
- * waits for every registered element before it removes its child, and
19
- * removes it at once when none registered. The returned function
20
- * unregisters.
21
- */
22
- register(id: symbol, durationMs: number): () => void
23
- /** Called by a registered element when its exit animation has ended. */
24
- done(id: symbol): void
25
- }
26
-
27
- export const PresenceContext = createContext<PresenceContextValue | null>(null)
@@ -1,244 +0,0 @@
1
- // A transition driven by the style changing, rather than by an
2
- // interaction.
3
- //
4
- // `./pressable.native.tsx` already animates, and it is built the other way
5
- // round: it knows the four interaction booleans, calls the author's `style`
6
- // function for each combination, and animates between the results. That
7
- // shape is right there and wrong here, because the conditions this has to
8
- // serve are ambient -- `dark:`, `md:`, `motion-reduce:` -- and the
9
- // compiler expresses those as a guarded array, `[base, __hozoDark &&
10
- // dark]`, which is already resolved by the time it arrives.
11
- //
12
- // So this one asks a simpler question: what did the style just become, and
13
- // what was it before? Everything that differs and can be interpolated is
14
- // animated to its new value; everything else is passed straight through.
15
- // That covers the interaction case too, in principle, which is why this is
16
- // not named after the conditions that prompted it.
17
- //
18
- // Colours run on the JavaScript driver and opacity and transforms do not.
19
- // React Native cannot interpolate a colour natively, so mixing the two in
20
- // one `Animated.timing` would drag the fast ones onto the slow driver.
21
- // They are separate animations for that reason and not for tidiness.
22
-
23
- import { type ReactNode, useContext, useEffect, useMemo, useRef, useState } from 'react'
24
- import { Animated, Easing, type StyleProp, StyleSheet, type ViewStyle } from 'react-native'
25
- import { PresenceContext } from './presence-context.ts'
26
-
27
- export interface HozoTransitionSpec {
28
- duration: number
29
- delay?: number
30
- easing: 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out'
31
- }
32
-
33
- /** The colour properties worth animating, in the order they are read. */
34
- const COLOR_KEYS = ['backgroundColor', 'borderColor', 'color'] as const
35
-
36
- /**
37
- * The transform entries, flattened to `{ key: value }`.
38
- *
39
- * React Native's `transform` is an array of one-key objects, which is a
40
- * shape nothing can be diffed against directly -- two arrays with the same
41
- * entries in a different order are the same transform and not equal.
42
- */
43
- function transformMap(style: ViewStyle | undefined): Map<string, number> {
44
- const out = new Map<string, number>()
45
- const transform = style?.transform
46
- if (!Array.isArray(transform)) return out
47
- for (const entry of transform) {
48
- for (const [key, value] of Object.entries(entry as Record<string, unknown>)) {
49
- if (typeof value === 'number') out.set(key, value)
50
- }
51
- }
52
- return out
53
- }
54
-
55
- /** What a transform key means when nothing sets it. */
56
- function identity(key: string): number {
57
- return key.startsWith('scale') ? 1 : 0
58
- }
59
-
60
- export interface HozoAnimatedProps {
61
- style?: StyleProp<ViewStyle>
62
- hozoTransition?: HozoTransitionSpec
63
- /**
64
- * The first frame of an enter animation, from a `starting:` class
65
- * (decision 007). The element mounts at these values and transitions to
66
- * `style`, which is what `@starting-style` does on Web. Read once, on
67
- * mount: it is where the element enters from, not a style it keeps.
68
- */
69
- hozoStarting?: StyleProp<ViewStyle>
70
- /**
71
- * The last frame of an exit animation, from a `data-[state=closed]:`
72
- * class (decision 007, amendment 1). Laid over `style` while the
73
- * enclosing `Presence` is closing, which is what the attribute selector
74
- * does on Web; `Presence` removes the element when the transition to it
75
- * ends.
76
- */
77
- hozoExit?: StyleProp<ViewStyle>
78
- children?: ReactNode
79
- [key: string]: unknown
80
- }
81
-
82
- export function HozoAnimated({
83
- style,
84
- hozoTransition,
85
- hozoStarting,
86
- hozoExit,
87
- children,
88
- ...props
89
- }: HozoAnimatedProps) {
90
- const presence = useContext(PresenceContext)
91
- // Only an element that has somewhere to leave to takes part. One without
92
- // `hozoExit` neither waits for `Presence` nor makes it wait.
93
- const exits = presence !== null && hozoExit != null
94
- const leaving = exits && presence.state === 'closed'
95
- const flat = useMemo(
96
- () => StyleSheet.flatten(leaving ? [style, hozoExit] : style) ?? {},
97
- [style, hozoExit, leaving],
98
- )
99
-
100
- // One progress value per animation rather than one per property. The
101
- // properties change together -- they are the same rule flipping -- so
102
- // animating a single 0 → 1 and interpolating each property from its old
103
- // value to its new one is both cheaper and impossible to get out of
104
- // step with itself.
105
- const progress = useRef(new Animated.Value(1)).current
106
- // Without `hozoStarting` the first render has nothing to animate from --
107
- // `from` and `to` are the same style, and the mount effect below runs an
108
- // animation that moves nothing. With it, `from` is the starting frame
109
- // laid over the style, so that same mount effect is the enter animation.
110
- const [entersFrom] = useState<ViewStyle>(() =>
111
- hozoStarting ? { ...flat, ...(StyleSheet.flatten(hozoStarting) ?? {}) } : flat,
112
- )
113
- const from = useRef<ViewStyle>(entersFrom)
114
- const to = useRef<ViewStyle>(flat)
115
-
116
- // What changed, computed during render so the effect below has nothing
117
- // to work out. `JSON.stringify` on the two style objects is enough: they
118
- // are flat, their values are primitives, and the compiler emits the keys
119
- // in a stable order.
120
- const signature = JSON.stringify(flat)
121
- const previous = useRef(signature)
122
- if (previous.current !== signature) {
123
- // The *visible* style becomes the new starting point, not the
124
- // previous target. Interrupting a half-finished fade and restarting
125
- // from where it was going would make it jump backwards first.
126
- from.current = to.current
127
- to.current = flat
128
- previous.current = signature
129
- }
130
-
131
- // Registered for as long as it can exit, so `Presence` knows to wait for
132
- // it and how long to wait before giving up.
133
- const [id] = useState(() => Symbol('HozoAnimated'))
134
- const register = exits ? presence.register : undefined
135
- const exitMs = hozoTransition ? hozoTransition.duration + (hozoTransition.delay ?? 0) : 0
136
- useEffect(() => register?.(id, exitMs), [register, id, exitMs])
137
- // Read when the animation ends, which is after this render's values may
138
- // have changed.
139
- const leavingNow = useRef(leaving)
140
- leavingNow.current = leaving
141
- const done = presence?.done
142
- const doneNow = useRef(done)
143
- doneNow.current = done
144
-
145
- // `signature` is read above rather than inside, and it is what says the
146
- // style moved -- `hozoTransition` does not change when only the values
147
- // do. Without it a style change starts no transition at all, which is
148
- // the whole feature. `biome check --unsafe` offers to remove it.
149
- // biome-ignore lint/correctness/useExhaustiveDependencies: it is what says the style changed
150
- useEffect(() => {
151
- if (!hozoTransition) return
152
- progress.setValue(0)
153
- const animation = Animated.timing(progress, {
154
- toValue: 1,
155
- duration: hozoTransition.duration,
156
- delay: hozoTransition.delay ?? 0,
157
- easing: easingFor(hozoTransition.easing),
158
- // Colours are in here too, and React Native cannot interpolate one
159
- // on the native driver. One animation on the JavaScript driver is
160
- // the honest choice: two drivers running the same rule would drift
161
- // against each other on a busy frame, and a background that arrives
162
- // before the text that sits on it is worse than both arriving late.
163
- useNativeDriver: false,
164
- })
165
- animation.start(({ finished }) => {
166
- if (finished && leavingNow.current) doneNow.current?.(id)
167
- })
168
- return () => animation.stop()
169
- }, [progress, signature, hozoTransition, id])
170
-
171
- // biome-ignore lint/correctness/useExhaustiveDependencies: as above -- `signature` is what changed
172
- const animated = useMemo(() => {
173
- if (!hozoTransition) return flat
174
- const overrides: Record<string, unknown> = {}
175
-
176
- if (typeof from.current.opacity === 'number' || typeof flat.opacity === 'number') {
177
- const start = typeof from.current.opacity === 'number' ? from.current.opacity : 1
178
- const end = typeof flat.opacity === 'number' ? flat.opacity : 1
179
- if (start !== end) {
180
- overrides.opacity = progress.interpolate({ inputRange: [0, 1], outputRange: [start, end] })
181
- }
182
- }
183
-
184
- for (const key of COLOR_KEYS) {
185
- // Read through a record: `color` lives on `TextStyle` and the rest
186
- // on `ViewStyle`, and a component that may be either has to look at
187
- // both. The runtime check below is what makes it safe.
188
- const start = (from.current as Record<string, unknown>)[key]
189
- const end = (flat as Record<string, unknown>)[key]
190
- if (typeof start !== 'string' || typeof end !== 'string' || start === end) continue
191
- overrides[key] = progress.interpolate({ inputRange: [0, 1], outputRange: [start, end] })
192
- }
193
-
194
- const before = transformMap(from.current)
195
- const after = transformMap(flat)
196
- const keys = [...new Set([...before.keys(), ...after.keys()])]
197
- const moved = keys.filter(
198
- (key) => (before.get(key) ?? identity(key)) !== (after.get(key) ?? identity(key)),
199
- )
200
- if (moved.length > 0) {
201
- // Every key, not only the moved ones: `transform` is replaced
202
- // wholesale, so leaving one out would reset it to identity halfway
203
- // through the animation of its neighbour.
204
- overrides.transform = keys.map((key) => ({
205
- [key]: progress.interpolate({
206
- inputRange: [0, 1],
207
- outputRange: [before.get(key) ?? identity(key), after.get(key) ?? identity(key)],
208
- }),
209
- }))
210
- }
211
-
212
- return [flat, overrides]
213
- }, [flat, hozoTransition, progress, signature])
214
-
215
- return (
216
- <Animated.View style={animated as StyleProp<ViewStyle>} {...props}>
217
- {exits ? (
218
- // The state is this element's, as `data-state` on Web belongs to
219
- // the one element `Presence` puts it on. Something nested further
220
- // in does not leave with it.
221
- <PresenceContext.Provider value={null}>{children}</PresenceContext.Provider>
222
- ) : (
223
- children
224
- )}
225
- </Animated.View>
226
- )
227
- }
228
-
229
- function easingFor(name: HozoTransitionSpec['easing']) {
230
- // Built here rather than in a module-level table, for the reason
231
- // `hooks.native.ts` does the same: a table calls `Easing.bezier` on
232
- // import, for every project that loads the package and never
233
- // transitions. The import itself is free; the call is not.
234
- switch (name) {
235
- case 'linear':
236
- return Easing.linear
237
- case 'ease-in':
238
- return Easing.bezier(0.4, 0, 1, 1)
239
- case 'ease-out':
240
- return Easing.bezier(0, 0, 0.2, 1)
241
- default:
242
- return Easing.bezier(0.4, 0, 0.2, 1)
243
- }
244
- }