@hozo/primitives 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +54 -0
- package/dist/flat-list.native.d.ts +17 -0
- package/dist/flat-list.native.d.ts.map +1 -0
- package/dist/flat-list.native.js +89 -0
- package/dist/flat-list.native.js.map +1 -0
- package/dist/foundation.d.ts +104 -0
- package/dist/foundation.d.ts.map +1 -0
- package/dist/foundation.js +115 -0
- package/dist/foundation.js.map +1 -0
- package/dist/foundation.native.d.ts +59 -0
- package/dist/foundation.native.d.ts.map +1 -0
- package/dist/foundation.native.js +40 -0
- package/dist/foundation.native.js.map +1 -0
- package/dist/generated/animated.d.ts +2 -0
- package/dist/generated/animated.d.ts.map +1 -0
- package/dist/generated/animated.js +3 -0
- package/dist/generated/animated.js.map +1 -0
- package/dist/generated/backdrop-filter.d.ts +2 -0
- package/dist/generated/backdrop-filter.d.ts.map +1 -0
- package/dist/generated/backdrop-filter.js +3 -0
- package/dist/generated/backdrop-filter.js.map +1 -0
- package/dist/generated/container.d.ts +2 -0
- package/dist/generated/container.d.ts.map +1 -0
- package/dist/generated/container.js +3 -0
- package/dist/generated/container.js.map +1 -0
- package/dist/generated/flat-list.d.ts +2 -0
- package/dist/generated/flat-list.d.ts.map +1 -0
- package/dist/generated/flat-list.js +2 -0
- package/dist/generated/flat-list.js.map +1 -0
- package/dist/generated/flat-list.native.d.ts +3 -0
- package/dist/generated/flat-list.native.d.ts.map +1 -0
- package/dist/generated/flat-list.native.js +2 -0
- package/dist/generated/flat-list.native.js.map +1 -0
- package/dist/generated/grid.d.ts +3 -0
- package/dist/generated/grid.d.ts.map +1 -0
- package/dist/generated/grid.js +4 -0
- package/dist/generated/grid.js.map +1 -0
- package/dist/generated/link.d.ts +2 -0
- package/dist/generated/link.d.ts.map +1 -0
- package/dist/generated/link.js +2 -0
- package/dist/generated/link.js.map +1 -0
- package/dist/generated/link.native.d.ts +2 -0
- package/dist/generated/link.native.d.ts.map +1 -0
- package/dist/generated/link.native.js +2 -0
- package/dist/generated/link.native.js.map +1 -0
- package/dist/generated/pressable.d.ts +2 -0
- package/dist/generated/pressable.d.ts.map +1 -0
- package/dist/generated/pressable.js +2 -0
- package/dist/generated/pressable.js.map +1 -0
- package/dist/generated/pressable.native.d.ts +2 -0
- package/dist/generated/pressable.native.d.ts.map +1 -0
- package/dist/generated/pressable.native.js +2 -0
- package/dist/generated/pressable.native.js.map +1 -0
- package/dist/generated/refresh-control.d.ts +2 -0
- package/dist/generated/refresh-control.d.ts.map +1 -0
- package/dist/generated/refresh-control.js +2 -0
- package/dist/generated/refresh-control.js.map +1 -0
- package/dist/generated/refresh-control.native.d.ts +2 -0
- package/dist/generated/refresh-control.native.d.ts.map +1 -0
- package/dist/generated/refresh-control.native.js +2 -0
- package/dist/generated/refresh-control.native.js.map +1 -0
- package/dist/generated/scroll-view.d.ts +2 -0
- package/dist/generated/scroll-view.d.ts.map +1 -0
- package/dist/generated/scroll-view.js +2 -0
- package/dist/generated/scroll-view.js.map +1 -0
- package/dist/generated/scroll-view.native.d.ts +2 -0
- package/dist/generated/scroll-view.native.d.ts.map +1 -0
- package/dist/generated/scroll-view.native.js +2 -0
- package/dist/generated/scroll-view.native.js.map +1 -0
- package/dist/generated/spaced.d.ts +2 -0
- package/dist/generated/spaced.d.ts.map +1 -0
- package/dist/generated/spaced.js +3 -0
- package/dist/generated/spaced.js.map +1 -0
- package/dist/generated/text-input.d.ts +2 -0
- package/dist/generated/text-input.d.ts.map +1 -0
- package/dist/generated/text-input.js +2 -0
- package/dist/generated/text-input.js.map +1 -0
- package/dist/generated/text-input.native.d.ts +2 -0
- package/dist/generated/text-input.native.d.ts.map +1 -0
- package/dist/generated/text-input.native.js +2 -0
- package/dist/generated/text-input.native.js.map +1 -0
- package/dist/generated/text-size.d.ts +2 -0
- package/dist/generated/text-size.d.ts.map +1 -0
- package/dist/generated/text-size.js +3 -0
- package/dist/generated/text-size.js.map +1 -0
- package/dist/generated/text.d.ts +2 -0
- package/dist/generated/text.d.ts.map +1 -0
- package/dist/generated/text.js +5 -0
- package/dist/generated/text.js.map +1 -0
- package/dist/generated/view.d.ts +2 -0
- package/dist/generated/view.d.ts.map +1 -0
- package/dist/generated/view.js +2 -0
- package/dist/generated/view.js.map +1 -0
- package/dist/generated/view.native.d.ts +2 -0
- package/dist/generated/view.native.d.ts.map +1 -0
- package/dist/generated/view.native.js +2 -0
- package/dist/generated/view.native.js.map +1 -0
- package/dist/index.d.ts +188 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +718 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.d.ts +22 -0
- package/dist/index.native.d.ts.map +1 -0
- package/dist/index.native.js +8 -0
- package/dist/index.native.js.map +1 -0
- package/dist/link.d.ts +320 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +69 -0
- package/dist/link.js.map +1 -0
- package/dist/link.native.d.ts +33 -0
- package/dist/link.native.d.ts.map +1 -0
- package/dist/link.native.js +44 -0
- package/dist/link.native.js.map +1 -0
- package/dist/pressable.d.ts +65 -0
- package/dist/pressable.d.ts.map +1 -0
- package/dist/pressable.js +202 -0
- package/dist/pressable.js.map +1 -0
- package/dist/pressable.native.d.ts +55 -0
- package/dist/pressable.native.d.ts.map +1 -0
- package/dist/pressable.native.js +405 -0
- package/dist/pressable.native.js.map +1 -0
- package/dist/runtime-backdrop.native.d.ts +8 -0
- package/dist/runtime-backdrop.native.d.ts.map +1 -0
- package/dist/runtime-backdrop.native.js +12 -0
- package/dist/runtime-backdrop.native.js.map +1 -0
- package/dist/runtime-container.native.d.ts +41 -0
- package/dist/runtime-container.native.d.ts.map +1 -0
- package/dist/runtime-container.native.js +61 -0
- package/dist/runtime-container.native.js.map +1 -0
- package/dist/runtime-grid.d.ts +37 -0
- package/dist/runtime-grid.d.ts.map +1 -0
- package/dist/runtime-grid.js +139 -0
- package/dist/runtime-grid.js.map +1 -0
- package/dist/runtime-grid.native.d.ts +26 -0
- package/dist/runtime-grid.native.d.ts.map +1 -0
- package/dist/runtime-grid.native.js +76 -0
- package/dist/runtime-grid.native.js.map +1 -0
- package/dist/runtime-measured.d.ts +20 -0
- package/dist/runtime-measured.d.ts.map +1 -0
- package/dist/runtime-measured.js +42 -0
- package/dist/runtime-measured.js.map +1 -0
- package/dist/runtime-spacing.d.ts +9 -0
- package/dist/runtime-spacing.d.ts.map +1 -0
- package/dist/runtime-spacing.js +35 -0
- package/dist/runtime-spacing.js.map +1 -0
- package/dist/runtime-spacing.native.d.ts +12 -0
- package/dist/runtime-spacing.native.d.ts.map +1 -0
- package/dist/runtime-spacing.native.js +64 -0
- package/dist/runtime-spacing.native.js.map +1 -0
- package/dist/runtime-text-size.native.d.ts +29 -0
- package/dist/runtime-text-size.native.d.ts.map +1 -0
- package/dist/runtime-text-size.native.js +51 -0
- package/dist/runtime-text-size.native.js.map +1 -0
- package/dist/runtime-transition.native.d.ts +15 -0
- package/dist/runtime-transition.native.d.ts.map +1 -0
- package/dist/runtime-transition.native.js +156 -0
- package/dist/runtime-transition.native.js.map +1 -0
- package/dist/text-input.d.ts +40 -0
- package/dist/text-input.d.ts.map +1 -0
- package/dist/text-input.js +54 -0
- package/dist/text-input.js.map +1 -0
- package/dist/view.d.ts +42 -0
- package/dist/view.d.ts.map +1 -0
- package/dist/view.js +84 -0
- package/dist/view.js.map +1 -0
- package/dist/view.native.d.ts +8 -0
- package/dist/view.native.d.ts.map +1 -0
- package/dist/view.native.js +5 -0
- package/dist/view.native.js.map +1 -0
- package/dist/windowing.d.ts +149 -0
- package/dist/windowing.d.ts.map +1 -0
- package/dist/windowing.js +336 -0
- package/dist/windowing.js.map +1 -0
- package/package.json +160 -0
- package/src/flat-list.native.tsx +158 -0
- package/src/foundation.native.tsx +179 -0
- package/src/foundation.tsx +323 -0
- package/src/generated/animated.ts +6 -0
- package/src/generated/backdrop-filter.ts +5 -0
- package/src/generated/container.ts +7 -0
- package/src/generated/flat-list.native.ts +6 -0
- package/src/generated/flat-list.ts +7 -0
- package/src/generated/grid.ts +4 -0
- package/src/generated/link.native.ts +1 -0
- package/src/generated/link.ts +1 -0
- package/src/generated/pressable.native.ts +5 -0
- package/src/generated/pressable.ts +5 -0
- package/src/generated/refresh-control.native.ts +4 -0
- package/src/generated/refresh-control.ts +1 -0
- package/src/generated/scroll-view.native.ts +4 -0
- package/src/generated/scroll-view.ts +1 -0
- package/src/generated/spaced.ts +2 -0
- package/src/generated/text-input.native.ts +4 -0
- package/src/generated/text-input.ts +1 -0
- package/src/generated/text-size.ts +7 -0
- package/src/generated/text.ts +8 -0
- package/src/generated/view.native.ts +1 -0
- package/src/generated/view.ts +1 -0
- package/src/index.native.ts +24 -0
- package/src/index.ts +1111 -0
- package/src/link.native.tsx +77 -0
- package/src/link.ts +125 -0
- package/src/pressable.native.tsx +538 -0
- package/src/pressable.ts +358 -0
- package/src/runtime-backdrop.native.tsx +22 -0
- package/src/runtime-container.native.tsx +105 -0
- package/src/runtime-grid.native.tsx +143 -0
- package/src/runtime-grid.ts +198 -0
- package/src/runtime-measured.ts +42 -0
- package/src/runtime-spacing.native.tsx +75 -0
- package/src/runtime-spacing.ts +37 -0
- package/src/runtime-text-size.native.tsx +79 -0
- package/src/runtime-transition.native.tsx +183 -0
- package/src/text-input.ts +129 -0
- package/src/view.native.ts +7 -0
- package/src/view.ts +165 -0
- package/src/windowing.ts +415 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { blendColor } from '@hozo/engine'
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
type ElementType,
|
|
5
|
+
type ReactNode,
|
|
6
|
+
useCallback,
|
|
7
|
+
useContext,
|
|
8
|
+
useEffect,
|
|
9
|
+
useLayoutEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useRef,
|
|
12
|
+
useState,
|
|
13
|
+
} from 'react'
|
|
14
|
+
import {
|
|
15
|
+
Animated,
|
|
16
|
+
Easing,
|
|
17
|
+
type GestureResponderEvent,
|
|
18
|
+
type MouseEvent,
|
|
19
|
+
type NativeSyntheticEvent,
|
|
20
|
+
Pressable,
|
|
21
|
+
type PressableProps,
|
|
22
|
+
type PressableStateCallbackType,
|
|
23
|
+
type StyleProp,
|
|
24
|
+
StyleSheet,
|
|
25
|
+
type TargetedEvent,
|
|
26
|
+
Text,
|
|
27
|
+
type TextProps,
|
|
28
|
+
type TextStyle,
|
|
29
|
+
type ViewStyle,
|
|
30
|
+
} from 'react-native'
|
|
31
|
+
|
|
32
|
+
export interface HozoTransition {
|
|
33
|
+
duration: number
|
|
34
|
+
delay?: number
|
|
35
|
+
easing: 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out'
|
|
36
|
+
opacity: boolean
|
|
37
|
+
transform: boolean
|
|
38
|
+
colors: boolean
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface HozoPressableState extends PressableStateCallbackType {
|
|
42
|
+
hovered: boolean
|
|
43
|
+
focused: boolean
|
|
44
|
+
focusVisible: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface HozoPressableProps extends Omit<PressableProps, 'style'> {
|
|
48
|
+
style?: StyleProp<ViewStyle> | ((state: HozoPressableState) => StyleProp<ViewStyle>)
|
|
49
|
+
hozoTransition?: HozoTransition
|
|
50
|
+
/** Enables per-element pointer/keyboard modality inference. */
|
|
51
|
+
hozoFocusVisible?: boolean
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const HOVERED = 1
|
|
55
|
+
const FOCUSED = 2
|
|
56
|
+
const PRESSED = 4
|
|
57
|
+
const FOCUS_VISIBLE = 8
|
|
58
|
+
const AnimatedPressable = Animated.createAnimatedComponent(Pressable)
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Which Pressable to render, and it is not a preference.
|
|
62
|
+
*
|
|
63
|
+
* `Animated.createAnimatedComponent` drops a *function* style. React
|
|
64
|
+
* Native's `AnimatedProps.__getValueWithStaticProps` flattens the static
|
|
65
|
+
* style before handing it on:
|
|
66
|
+
*
|
|
67
|
+
* const flatStaticStyle = flattenStyle(staticStyle);
|
|
68
|
+
* ...
|
|
69
|
+
* } else { props[key] = flatStaticStyle; }
|
|
70
|
+
*
|
|
71
|
+
* and `flattenStyle` of a function is `undefined`, so the element renders
|
|
72
|
+
* with no style at all -- no background, no padding, nothing. A compiled
|
|
73
|
+
* `hover:` class produces exactly that callback, so every Pressable with a
|
|
74
|
+
* state variant and no transition rendered unstyled on both platforms.
|
|
75
|
+
*
|
|
76
|
+
* Measured rather than reasoned: four probes on the census screen, each
|
|
77
|
+
* adding one ingredient, and the one that lost its colour was the callback
|
|
78
|
+
* without a transition (#357). #334 had already fixed the transition half
|
|
79
|
+
* by passing an array, which is why that half draws.
|
|
80
|
+
*
|
|
81
|
+
* So an element with nothing to animate gets the plain Pressable, which
|
|
82
|
+
* takes a callback as its own API and keeps React Native's timing for
|
|
83
|
+
* `pressed` with it.
|
|
84
|
+
*/
|
|
85
|
+
// Annotated because the inferred type of the animated component names an
|
|
86
|
+
// internal React Native type that cannot be referenced from a declaration
|
|
87
|
+
// file. `ElementType` says the only thing the caller needs: it is a
|
|
88
|
+
// component to render.
|
|
89
|
+
export function pressableFor(transition: HozoTransition | undefined): ElementType {
|
|
90
|
+
return transition ? AnimatedPressable : Pressable
|
|
91
|
+
}
|
|
92
|
+
const AnimatedText = Animated.createAnimatedComponent(Text)
|
|
93
|
+
type InteractionContextValue = HozoPressableState & { transition?: HozoTransition }
|
|
94
|
+
const InteractionContext = createContext<InteractionContextValue | null>(null)
|
|
95
|
+
|
|
96
|
+
function easingFor(name: HozoTransition['easing']) {
|
|
97
|
+
switch (name) {
|
|
98
|
+
case 'linear':
|
|
99
|
+
return Easing.linear
|
|
100
|
+
case 'ease-in':
|
|
101
|
+
return Easing.in(Easing.ease)
|
|
102
|
+
case 'ease-out':
|
|
103
|
+
return Easing.out(Easing.ease)
|
|
104
|
+
case 'ease-in-out':
|
|
105
|
+
return Easing.inOut(Easing.ease)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type TransformTarget = { key: string; value: number; degrees: boolean }
|
|
110
|
+
const transformRank = (key: string) =>
|
|
111
|
+
key.startsWith('translate') ? 0 : key.startsWith('rotate') ? 1 : 2
|
|
112
|
+
|
|
113
|
+
function transformTargets(style: StyleProp<ViewStyle>): TransformTarget[] {
|
|
114
|
+
const merged = new Map<string, TransformTarget>()
|
|
115
|
+
const visit = (part: StyleProp<ViewStyle>) => {
|
|
116
|
+
if (!part) return
|
|
117
|
+
if (Array.isArray(part)) return part.forEach(visit)
|
|
118
|
+
const transform = StyleSheet.flatten(part)?.transform
|
|
119
|
+
// React Native also accepts a CSS transform string, which has no
|
|
120
|
+
// entries to read. Nothing here can animate one, and the `as any[]`
|
|
121
|
+
// that used to stand here let it reach `Object.entries` and come back
|
|
122
|
+
// with a key of `'0'`.
|
|
123
|
+
if (typeof transform === 'string') return
|
|
124
|
+
for (const entry of transform || []) {
|
|
125
|
+
const [key, raw] = Object.entries(entry as Record<string, unknown>)[0] || []
|
|
126
|
+
if (!key) continue
|
|
127
|
+
if (key === 'scale' && typeof raw === 'number') {
|
|
128
|
+
merged.set('scaleX', { key: 'scaleX', value: raw, degrees: false })
|
|
129
|
+
merged.set('scaleY', { key: 'scaleY', value: raw, degrees: false })
|
|
130
|
+
} else if (typeof raw === 'number') {
|
|
131
|
+
merged.set(key, { key, value: raw, degrees: false })
|
|
132
|
+
} else if (typeof raw === 'string' && raw.endsWith('deg')) {
|
|
133
|
+
const value = Number(raw.slice(0, -3))
|
|
134
|
+
if (Number.isFinite(value)) merged.set(key, { key, value, degrees: true })
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
visit(style)
|
|
139
|
+
return [...merged.values()].sort((a, b) => transformRank(a.key) - transformRank(b.key))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function identityFor(target: TransformTarget) {
|
|
143
|
+
return target.key.startsWith('scale') ? 1 : 0
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const COLOR_KEYS = ['backgroundColor', 'color'] as const
|
|
147
|
+
type ColorKey = (typeof COLOR_KEYS)[number]
|
|
148
|
+
type ColorRange = { from: string; to: string }
|
|
149
|
+
|
|
150
|
+
function colorTargets(style: StyleProp<ViewStyle>) {
|
|
151
|
+
const flattened = StyleSheet.flatten(style) as Record<string, unknown> | undefined
|
|
152
|
+
const colors = new Map<ColorKey, string>()
|
|
153
|
+
for (const key of COLOR_KEYS) {
|
|
154
|
+
const value = flattened?.[key]
|
|
155
|
+
if (typeof value === 'string') colors.set(key, value)
|
|
156
|
+
}
|
|
157
|
+
return colors
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function HozoPressable({
|
|
161
|
+
style,
|
|
162
|
+
onHoverIn,
|
|
163
|
+
onHoverOut,
|
|
164
|
+
onFocus,
|
|
165
|
+
onBlur,
|
|
166
|
+
onPressIn,
|
|
167
|
+
onPressOut,
|
|
168
|
+
onPointerDown,
|
|
169
|
+
onKeyDown,
|
|
170
|
+
hozoTransition,
|
|
171
|
+
hozoFocusVisible = false,
|
|
172
|
+
...props
|
|
173
|
+
}: HozoPressableProps) {
|
|
174
|
+
const [interaction, setInteraction] = useState(0)
|
|
175
|
+
const interactionRef = useRef(0)
|
|
176
|
+
const initialStyle =
|
|
177
|
+
typeof style === 'function'
|
|
178
|
+
? style({ pressed: false, hovered: false, focused: false, focusVisible: false })
|
|
179
|
+
: style
|
|
180
|
+
const initialOpacity = StyleSheet.flatten(initialStyle)?.opacity
|
|
181
|
+
const opacity = useRef(
|
|
182
|
+
new Animated.Value(typeof initialOpacity === 'number' ? initialOpacity : 1),
|
|
183
|
+
).current
|
|
184
|
+
const colorSpecs = useMemo(() => {
|
|
185
|
+
if (!hozoTransition?.colors || typeof style !== 'function') return [] as ColorKey[]
|
|
186
|
+
const found = new Set<ColorKey>()
|
|
187
|
+
for (const pressed of [false, true]) {
|
|
188
|
+
for (const hovered of [false, true]) {
|
|
189
|
+
for (const focused of [false, true]) {
|
|
190
|
+
for (const focusVisible of [false, true]) {
|
|
191
|
+
for (const key of colorTargets(
|
|
192
|
+
style({ pressed, hovered, focused, focusVisible }),
|
|
193
|
+
).keys())
|
|
194
|
+
found.add(key)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return [...found]
|
|
200
|
+
}, [hozoTransition?.colors, style])
|
|
201
|
+
const initialColors = colorTargets(initialStyle)
|
|
202
|
+
const colorRanges = useRef(new Map<ColorKey, ColorRange>()).current
|
|
203
|
+
for (const key of colorSpecs) {
|
|
204
|
+
if (!colorRanges.has(key)) {
|
|
205
|
+
const initial = initialColors.get(key) ?? (key === 'backgroundColor' ? 'transparent' : '')
|
|
206
|
+
if (initial) colorRanges.set(key, { from: initial, to: initial })
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const colorProgress = useRef(new Animated.Value(0)).current
|
|
210
|
+
const colorFraction = useRef(0)
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
const listener = colorProgress.addListener(({ value }) => {
|
|
213
|
+
colorFraction.current = value
|
|
214
|
+
})
|
|
215
|
+
return () => colorProgress.removeListener(listener)
|
|
216
|
+
}, [colorProgress])
|
|
217
|
+
const animatedColors = Object.fromEntries(
|
|
218
|
+
[...colorRanges].map(([key, range]) => [
|
|
219
|
+
key,
|
|
220
|
+
colorProgress.interpolate({
|
|
221
|
+
inputRange: [0, 1],
|
|
222
|
+
outputRange: [range.from, range.to],
|
|
223
|
+
}),
|
|
224
|
+
]),
|
|
225
|
+
)
|
|
226
|
+
const transformSpecs = useMemo(() => {
|
|
227
|
+
if (!hozoTransition?.transform || typeof style !== 'function') return []
|
|
228
|
+
const states = [false, true].flatMap((pressed) =>
|
|
229
|
+
[false, true].flatMap((hovered) =>
|
|
230
|
+
[false, true].flatMap((focused) =>
|
|
231
|
+
[false, true].map((focusVisible) => ({ pressed, hovered, focused, focusVisible })),
|
|
232
|
+
),
|
|
233
|
+
),
|
|
234
|
+
)
|
|
235
|
+
const merged = new Map<string, TransformTarget>()
|
|
236
|
+
for (const state of states) {
|
|
237
|
+
for (const target of transformTargets(style(state))) merged.set(target.key, target)
|
|
238
|
+
}
|
|
239
|
+
return [...merged.values()].sort((a, b) => transformRank(a.key) - transformRank(b.key))
|
|
240
|
+
}, [hozoTransition?.transform, style])
|
|
241
|
+
const transformValues = useRef(new Map<string, Animated.Value>()).current
|
|
242
|
+
const initialTargets = new Map(
|
|
243
|
+
transformTargets(initialStyle).map((target) => [target.key, target]),
|
|
244
|
+
)
|
|
245
|
+
for (const spec of transformSpecs) {
|
|
246
|
+
if (!transformValues.has(spec.key)) {
|
|
247
|
+
transformValues.set(
|
|
248
|
+
spec.key,
|
|
249
|
+
new Animated.Value(initialTargets.get(spec.key)?.value ?? identityFor(spec)),
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const animatedTransform = transformSpecs.map((spec) => ({
|
|
254
|
+
[spec.key]: spec.degrees
|
|
255
|
+
? transformValues.get(spec.key)!.interpolate({
|
|
256
|
+
inputRange: [-360, 360],
|
|
257
|
+
outputRange: ['-360deg', '360deg'],
|
|
258
|
+
})
|
|
259
|
+
: transformValues.get(spec.key)!,
|
|
260
|
+
}))
|
|
261
|
+
const animateInteraction = useCallback(
|
|
262
|
+
(next: number) => {
|
|
263
|
+
if (!hozoTransition || typeof style !== 'function') return
|
|
264
|
+
const flattened = StyleSheet.flatten(
|
|
265
|
+
style({
|
|
266
|
+
pressed: (next & PRESSED) !== 0,
|
|
267
|
+
hovered: (next & HOVERED) !== 0,
|
|
268
|
+
focused: (next & FOCUSED) !== 0,
|
|
269
|
+
focusVisible: (next & FOCUS_VISIBLE) !== 0,
|
|
270
|
+
}),
|
|
271
|
+
)
|
|
272
|
+
const animations: Animated.CompositeAnimation[] = []
|
|
273
|
+
if (hozoTransition.opacity) {
|
|
274
|
+
const targetOpacity = typeof flattened?.opacity === 'number' ? flattened.opacity : 1
|
|
275
|
+
animations.push(
|
|
276
|
+
Animated.timing(opacity, {
|
|
277
|
+
toValue: targetOpacity,
|
|
278
|
+
duration: hozoTransition.duration,
|
|
279
|
+
delay: hozoTransition.delay ?? 0,
|
|
280
|
+
easing: easingFor(hozoTransition.easing),
|
|
281
|
+
useNativeDriver: true,
|
|
282
|
+
}),
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
if (hozoTransition.transform) {
|
|
286
|
+
const targets = new Map(
|
|
287
|
+
transformTargets(
|
|
288
|
+
style({
|
|
289
|
+
pressed: (next & PRESSED) !== 0,
|
|
290
|
+
hovered: (next & HOVERED) !== 0,
|
|
291
|
+
focused: (next & FOCUSED) !== 0,
|
|
292
|
+
focusVisible: (next & FOCUS_VISIBLE) !== 0,
|
|
293
|
+
}),
|
|
294
|
+
).map((target) => [target.key, target.value]),
|
|
295
|
+
)
|
|
296
|
+
for (const spec of transformSpecs) {
|
|
297
|
+
animations.push(
|
|
298
|
+
Animated.timing(transformValues.get(spec.key)!, {
|
|
299
|
+
toValue: targets.get(spec.key) ?? identityFor(spec),
|
|
300
|
+
duration: hozoTransition.duration,
|
|
301
|
+
delay: hozoTransition.delay ?? 0,
|
|
302
|
+
easing: easingFor(hozoTransition.easing),
|
|
303
|
+
useNativeDriver: true,
|
|
304
|
+
}),
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (hozoTransition.colors) {
|
|
309
|
+
const targets = colorTargets(
|
|
310
|
+
style({
|
|
311
|
+
pressed: (next & PRESSED) !== 0,
|
|
312
|
+
hovered: (next & HOVERED) !== 0,
|
|
313
|
+
focused: (next & FOCUSED) !== 0,
|
|
314
|
+
focusVisible: (next & FOCUS_VISIBLE) !== 0,
|
|
315
|
+
}),
|
|
316
|
+
)
|
|
317
|
+
for (const [key, range] of colorRanges) {
|
|
318
|
+
const current = blendColor(range.from, range.to, colorFraction.current)
|
|
319
|
+
const target = targets.get(key) ?? (key === 'backgroundColor' ? 'transparent' : current)
|
|
320
|
+
colorRanges.set(key, { from: current, to: target })
|
|
321
|
+
}
|
|
322
|
+
colorProgress.setValue(0)
|
|
323
|
+
colorFraction.current = 0
|
|
324
|
+
animations.push(
|
|
325
|
+
Animated.timing(colorProgress, {
|
|
326
|
+
toValue: 1,
|
|
327
|
+
duration: hozoTransition.duration,
|
|
328
|
+
delay: hozoTransition.delay ?? 0,
|
|
329
|
+
easing: easingFor(hozoTransition.easing),
|
|
330
|
+
useNativeDriver: false,
|
|
331
|
+
}),
|
|
332
|
+
)
|
|
333
|
+
}
|
|
334
|
+
Animated.parallel(animations).start()
|
|
335
|
+
},
|
|
336
|
+
[colorProgress, colorRanges, hozoTransition, opacity, style, transformSpecs, transformValues],
|
|
337
|
+
)
|
|
338
|
+
const setFlag = useCallback(
|
|
339
|
+
(flag: number, active: boolean) => {
|
|
340
|
+
const current = interactionRef.current
|
|
341
|
+
const next = active ? current | flag : current & ~flag
|
|
342
|
+
interactionRef.current = next
|
|
343
|
+
animateInteraction(next)
|
|
344
|
+
setInteraction(next)
|
|
345
|
+
},
|
|
346
|
+
[animateInteraction],
|
|
347
|
+
)
|
|
348
|
+
const PressableComponent = pressableFor(hozoTransition)
|
|
349
|
+
const modality = useRef<'keyboard' | 'pointer'>('keyboard')
|
|
350
|
+
|
|
351
|
+
// The style for the state this component is in, resolved here because
|
|
352
|
+
// the animated path cannot hand React Native a callback -- see the
|
|
353
|
+
// `style` prop below.
|
|
354
|
+
const resolvedStyle = useMemo(
|
|
355
|
+
() =>
|
|
356
|
+
typeof style === 'function'
|
|
357
|
+
? style({
|
|
358
|
+
pressed: (interaction & PRESSED) !== 0,
|
|
359
|
+
hovered: (interaction & HOVERED) !== 0,
|
|
360
|
+
focused: (interaction & FOCUSED) !== 0,
|
|
361
|
+
focusVisible: (interaction & FOCUS_VISIBLE) !== 0,
|
|
362
|
+
})
|
|
363
|
+
: style,
|
|
364
|
+
[interaction, style],
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
const context = useMemo(
|
|
368
|
+
() => ({
|
|
369
|
+
pressed: (interaction & PRESSED) !== 0,
|
|
370
|
+
hovered: (interaction & HOVERED) !== 0,
|
|
371
|
+
focused: (interaction & FOCUSED) !== 0,
|
|
372
|
+
focusVisible: (interaction & FOCUS_VISIBLE) !== 0,
|
|
373
|
+
transition: hozoTransition,
|
|
374
|
+
}),
|
|
375
|
+
[hozoTransition, interaction],
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
return (
|
|
379
|
+
<InteractionContext.Provider value={context}>
|
|
380
|
+
<PressableComponent
|
|
381
|
+
{...props}
|
|
382
|
+
onHoverIn={(event: MouseEvent) => {
|
|
383
|
+
setFlag(HOVERED, true)
|
|
384
|
+
onHoverIn?.(event)
|
|
385
|
+
}}
|
|
386
|
+
onHoverOut={(event: MouseEvent) => {
|
|
387
|
+
setFlag(HOVERED, false)
|
|
388
|
+
onHoverOut?.(event)
|
|
389
|
+
}}
|
|
390
|
+
onFocus={(event: NativeSyntheticEvent<TargetedEvent>) => {
|
|
391
|
+
setFlag(FOCUSED, true)
|
|
392
|
+
if (hozoFocusVisible) setFlag(FOCUS_VISIBLE, modality.current === 'keyboard')
|
|
393
|
+
onFocus?.(event)
|
|
394
|
+
}}
|
|
395
|
+
onBlur={(event: NativeSyntheticEvent<TargetedEvent>) => {
|
|
396
|
+
setFlag(FOCUSED, false)
|
|
397
|
+
if (hozoFocusVisible) setFlag(FOCUS_VISIBLE, false)
|
|
398
|
+
onBlur?.(event)
|
|
399
|
+
}}
|
|
400
|
+
onPointerDown={
|
|
401
|
+
hozoFocusVisible
|
|
402
|
+
? (event: Parameters<NonNullable<PressableProps['onPointerDown']>>[0]) => {
|
|
403
|
+
modality.current = 'pointer'
|
|
404
|
+
setFlag(FOCUS_VISIBLE, false)
|
|
405
|
+
onPointerDown?.(event)
|
|
406
|
+
}
|
|
407
|
+
: onPointerDown
|
|
408
|
+
}
|
|
409
|
+
onKeyDown={
|
|
410
|
+
hozoFocusVisible
|
|
411
|
+
? (event: Parameters<NonNullable<PressableProps['onKeyDown']>>[0]) => {
|
|
412
|
+
modality.current = 'keyboard'
|
|
413
|
+
if ((interactionRef.current & FOCUSED) !== 0) setFlag(FOCUS_VISIBLE, true)
|
|
414
|
+
onKeyDown?.(event)
|
|
415
|
+
}
|
|
416
|
+
: onKeyDown
|
|
417
|
+
}
|
|
418
|
+
onPressIn={(event: GestureResponderEvent) => {
|
|
419
|
+
modality.current = 'pointer'
|
|
420
|
+
if (hozoFocusVisible) setFlag(FOCUS_VISIBLE, false)
|
|
421
|
+
setFlag(PRESSED, true)
|
|
422
|
+
onPressIn?.(event)
|
|
423
|
+
}}
|
|
424
|
+
onPressOut={(event: GestureResponderEvent) => {
|
|
425
|
+
setFlag(PRESSED, false)
|
|
426
|
+
onPressOut?.(event)
|
|
427
|
+
}}
|
|
428
|
+
// An array when there is a transition, a callback when there is
|
|
429
|
+
// not, and the difference is not a preference.
|
|
430
|
+
//
|
|
431
|
+
// `AnimatedProps` builds the animated node from `props.style`, and
|
|
432
|
+
// its first act is to skip anything that is not an object:
|
|
433
|
+
//
|
|
434
|
+
// if (key === 'style') {
|
|
435
|
+
// // Ignore `style` if it is not an object (or array).
|
|
436
|
+
// if (typeof value === 'object' && value != null) {
|
|
437
|
+
//
|
|
438
|
+
// A callback is a function, so no node was ever attached. Pressable
|
|
439
|
+
// still called it, so the `Animated.Value`s it returned reached the
|
|
440
|
+
// view as plain objects where colours and numbers belong -- and a
|
|
441
|
+
// `backgroundColor` that is an object is not a colour, so the view
|
|
442
|
+
// dropped it. The compiled `bg-brand` button rendered with no
|
|
443
|
+
// background at all, and its white label with it, for as long as
|
|
444
|
+
// this code has existed. Found by the first screenshot anything
|
|
445
|
+
// ever took of the acceptance screen (#297).
|
|
446
|
+
//
|
|
447
|
+
// The state the callback would have supplied is state this
|
|
448
|
+
// component already keeps: `pressed` is set from `onPressIn` and
|
|
449
|
+
// `onPressOut`, which is what drives Pressable's own flag too.
|
|
450
|
+
style={
|
|
451
|
+
hozoTransition
|
|
452
|
+
? // Cast because `Animated.createAnimatedComponent` types
|
|
453
|
+
// `style` as a resolved ViewStyle, while the whole point of
|
|
454
|
+
// an animated component is that it may hold `Animated.Value`s.
|
|
455
|
+
([
|
|
456
|
+
resolvedStyle,
|
|
457
|
+
{
|
|
458
|
+
...(hozoTransition.opacity ? { opacity } : null),
|
|
459
|
+
...(hozoTransition.transform ? { transform: animatedTransform } : null),
|
|
460
|
+
...(hozoTransition.colors ? animatedColors : null),
|
|
461
|
+
},
|
|
462
|
+
] as unknown as StyleProp<ViewStyle>)
|
|
463
|
+
: ({ pressed }: PressableStateCallbackType) =>
|
|
464
|
+
typeof style === 'function'
|
|
465
|
+
? style({
|
|
466
|
+
pressed: pressed || (interaction & PRESSED) !== 0,
|
|
467
|
+
hovered: (interaction & HOVERED) !== 0,
|
|
468
|
+
focused: (interaction & FOCUSED) !== 0,
|
|
469
|
+
focusVisible: (interaction & FOCUS_VISIBLE) !== 0,
|
|
470
|
+
})
|
|
471
|
+
: style
|
|
472
|
+
}
|
|
473
|
+
/>
|
|
474
|
+
</InteractionContext.Provider>
|
|
475
|
+
)
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
export interface HozoTextProps extends Omit<TextProps, 'style'> {
|
|
479
|
+
children?: ReactNode
|
|
480
|
+
style?: StyleProp<TextStyle> | ((state: HozoPressableState) => StyleProp<TextStyle>)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Text whose interaction state and transition are owned by an enclosing HozoPressable. */
|
|
484
|
+
export function HozoText({ style, ...props }: HozoTextProps) {
|
|
485
|
+
const context = useContext(InteractionContext)
|
|
486
|
+
const state = context ?? { pressed: false, hovered: false, focused: false, focusVisible: false }
|
|
487
|
+
const resolved = typeof style === 'function' ? style(state) : style
|
|
488
|
+
const target = StyleSheet.flatten(resolved)?.color
|
|
489
|
+
const progress = useRef(new Animated.Value(1)).current
|
|
490
|
+
const fraction = useRef(1)
|
|
491
|
+
const previousTarget = useRef(typeof target === 'string' ? target : '')
|
|
492
|
+
const range = useRef<ColorRange>({ from: previousTarget.current, to: previousTarget.current })
|
|
493
|
+
const interaction = `${state.pressed}:${state.hovered}:${state.focused}`
|
|
494
|
+
const previousInteraction = useRef(interaction)
|
|
495
|
+
const pending = previousInteraction.current !== interaction && typeof target === 'string'
|
|
496
|
+
if (pending) {
|
|
497
|
+
range.current = {
|
|
498
|
+
from: blendColor(range.current.from, range.current.to, fraction.current),
|
|
499
|
+
to: target as string,
|
|
500
|
+
}
|
|
501
|
+
previousInteraction.current = interaction
|
|
502
|
+
previousTarget.current = target as string
|
|
503
|
+
}
|
|
504
|
+
useEffect(() => {
|
|
505
|
+
const listener = progress.addListener(({ value }) => {
|
|
506
|
+
fraction.current = value
|
|
507
|
+
})
|
|
508
|
+
return () => progress.removeListener(listener)
|
|
509
|
+
}, [progress])
|
|
510
|
+
useLayoutEffect(() => {
|
|
511
|
+
if (!pending || !context?.transition?.colors) return
|
|
512
|
+
progress.setValue(0)
|
|
513
|
+
fraction.current = 0
|
|
514
|
+
Animated.timing(progress, {
|
|
515
|
+
toValue: 1,
|
|
516
|
+
duration: context.transition.duration,
|
|
517
|
+
delay: context.transition.delay ?? 0,
|
|
518
|
+
easing: easingFor(context.transition.easing),
|
|
519
|
+
useNativeDriver: false,
|
|
520
|
+
}).start()
|
|
521
|
+
// `context?`, not `context`: the context defaults to null, and this
|
|
522
|
+
// component is explicitly written to work without one -- see the
|
|
523
|
+
// `if (!context ...)` below. A dependency array is evaluated on every
|
|
524
|
+
// render, before any of them, so `context.transition` threw for a
|
|
525
|
+
// HozoText used outside a HozoPressable.
|
|
526
|
+
}, [context?.transition, pending, progress])
|
|
527
|
+
const animatedColor =
|
|
528
|
+
context?.transition?.colors && range.current.from
|
|
529
|
+
? progress.interpolate({
|
|
530
|
+
inputRange: [0, 1],
|
|
531
|
+
outputRange: [range.current.from, range.current.to],
|
|
532
|
+
})
|
|
533
|
+
: undefined
|
|
534
|
+
if (!context || typeof style !== 'function') return <Text {...props} style={resolved} />
|
|
535
|
+
return (
|
|
536
|
+
<AnimatedText {...props} style={[resolved, animatedColor ? { color: animatedColor } : null]} />
|
|
537
|
+
)
|
|
538
|
+
}
|