@hozo/engine 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 (67) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +34 -0
  3. package/dist/class-resolver.d.ts +28 -5
  4. package/dist/class-resolver.d.ts.map +1 -1
  5. package/dist/class-resolver.js +59 -22
  6. package/dist/class-resolver.js.map +1 -1
  7. package/dist/color-transition.d.ts.map +1 -1
  8. package/dist/color-transition.js +6 -0
  9. package/dist/color-transition.js.map +1 -1
  10. package/dist/generated/animation.d.ts +1 -0
  11. package/dist/generated/animation.d.ts.map +1 -1
  12. package/dist/generated/animation.js +1 -0
  13. package/dist/generated/animation.js.map +1 -1
  14. package/dist/generated/transform-slots.d.ts +2 -0
  15. package/dist/generated/transform-slots.d.ts.map +1 -0
  16. package/dist/generated/transform-slots.js +4 -0
  17. package/dist/generated/transform-slots.js.map +1 -0
  18. package/dist/index.d.ts +1 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.native.d.ts +1 -0
  23. package/dist/index.native.d.ts.map +1 -1
  24. package/dist/index.native.js +1 -0
  25. package/dist/index.native.js.map +1 -1
  26. package/dist/interactive.d.ts +2 -0
  27. package/dist/interactive.d.ts.map +1 -1
  28. package/dist/interactive.js +4 -0
  29. package/dist/interactive.js.map +1 -1
  30. package/dist/keyframes.native.d.ts +27 -0
  31. package/dist/keyframes.native.d.ts.map +1 -0
  32. package/dist/keyframes.native.js +167 -0
  33. package/dist/keyframes.native.js.map +1 -0
  34. package/dist/project.d.ts +8 -0
  35. package/dist/project.d.ts.map +1 -1
  36. package/dist/project.js +9 -0
  37. package/dist/project.js.map +1 -1
  38. package/dist/project.native.d.ts +1 -1
  39. package/dist/project.native.d.ts.map +1 -1
  40. package/dist/project.native.js +1 -1
  41. package/dist/project.native.js.map +1 -1
  42. package/dist/transform-slots.d.ts +22 -0
  43. package/dist/transform-slots.d.ts.map +1 -0
  44. package/dist/transform-slots.js +108 -0
  45. package/dist/transform-slots.js.map +1 -0
  46. package/dist/view-base-css.d.ts +2 -0
  47. package/dist/view-base-css.d.ts.map +1 -0
  48. package/dist/view-base-css.js +20 -0
  49. package/dist/view-base-css.js.map +1 -0
  50. package/dist/view-base.d.ts +6 -0
  51. package/dist/view-base.d.ts.map +1 -0
  52. package/dist/view-base.js +15 -0
  53. package/dist/view-base.js.map +1 -0
  54. package/package.json +6 -1
  55. package/src/class-resolver.ts +93 -26
  56. package/src/color-transition.ts +6 -0
  57. package/src/generated/animation.ts +1 -0
  58. package/src/generated/transform-slots.ts +3 -0
  59. package/src/index.native.ts +5 -0
  60. package/src/index.ts +1 -1
  61. package/src/interactive.ts +4 -0
  62. package/src/keyframes.native.ts +210 -0
  63. package/src/project.native.ts +1 -1
  64. package/src/project.ts +11 -0
  65. package/src/transform-slots.ts +119 -0
  66. package/src/view-base-css.ts +19 -0
  67. package/src/view-base.ts +20 -0
@@ -1,5 +1,11 @@
1
1
  function rgba(color: string): [number, number, number, number] | null {
2
2
  if (color === 'transparent') return [0, 0, 0, 0]
3
+ // An interrupted blend must be able to read its own serialized result.
4
+ const blended = /^rgba\((\d+), (\d+), (\d+), ([\d.e+-]+)\)$/.exec(color)
5
+ if (blended) {
6
+ const channels = blended.slice(1).map(Number) as [number, number, number, number]
7
+ return channels.every(Number.isFinite) ? channels : null
8
+ }
3
9
  const match = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(color)
4
10
  if (!match) return null
5
11
  let hex = match[1]!
@@ -1,2 +1,3 @@
1
1
  // Native-only. See `./environment.ts`.
2
2
  export { type HozoAnimation, useHozoAnimation } from '../hooks.native.ts'
3
+ export { type HozoKeyframe, type HozoKeyframesSpec, useHozoKeyframes } from '../keyframes.native.ts'
@@ -0,0 +1,3 @@
1
+ // Only reset-bearing generated code imports the slot composer. Ordinary
2
+ // dynamic resolution must not pull this module into a Native bundle.
3
+ export { hozoTransformStyles } from '../transform-slots.ts'
@@ -35,4 +35,9 @@ export {
35
35
  } from './hooks.native.ts'
36
36
  export { hozoImageSource } from './image-source.native.ts'
37
37
  export * from './index.ts'
38
+ export {
39
+ type HozoKeyframe,
40
+ type HozoKeyframesSpec,
41
+ useHozoKeyframes,
42
+ } from './keyframes.native.ts'
38
43
  export { type HozoSafeAreaInsets, useHozoSafeArea } from './safe-area.native.ts'
package/src/index.ts CHANGED
@@ -42,9 +42,9 @@ export {
42
42
  useResponderDomProps,
43
43
  } from './responder.ts'
44
44
  export { hozoScrollable } from './scrollable.ts'
45
-
46
45
  export {
47
46
  HOZO_DEFAULT_FONT_SIZE,
48
47
  HozoTextSizeContext,
49
48
  useHozoTextSize,
50
49
  } from './text-size.ts'
50
+ export { hozoViewBase, hozoViewClassName } from './view-base.ts'
@@ -49,6 +49,7 @@ export function hozoInteractive(onPress?: PressHandler, disabled?: unknown) {
49
49
  // a boolean here would have made every control permanently
50
50
  // disabled-looking. `undefined` is the only value React omits.
51
51
  'data-hozo-disabled': '',
52
+ 'data-hozo-pressable': undefined,
52
53
  tabIndex: -1,
53
54
  onClick: undefined,
54
55
  onKeyDown: undefined,
@@ -60,6 +61,9 @@ export function hozoInteractive(onPress?: PressHandler, disabled?: unknown) {
60
61
  // is noise a screen reader has to read past.
61
62
  'aria-disabled': undefined,
62
63
  'data-hozo-disabled': undefined,
64
+ // The pointer cursor's hook (see the view base CSS): present only
65
+ // while clicking does something.
66
+ 'data-hozo-pressable': onPress ? '' : undefined,
63
67
  tabIndex: 0,
64
68
  onClick: onPress,
65
69
  onKeyDown: hozoActivateKeyDown,
@@ -0,0 +1,210 @@
1
+ // A project's own `@keyframes`, on React Native (decision 007, slice 3).
2
+ //
3
+ // `useHozoAnimation` runs Tailwind's four loops, whose frames are known in
4
+ // advance and written out by hand. This runs frames the compiler read from
5
+ // the project: a StyleX `stylex.keyframes(...)`, or a Tailwind theme's
6
+ // `--animate-*` and its `@keyframes`. The compiler hands over the frames as
7
+ // React Native style objects and the timing as CSS said it; this turns
8
+ // them into one progress value, interpolated per property.
9
+ //
10
+ // What it can move is what `HozoAnimated` can: opacity, the colours, and
11
+ // transforms. The compiler leaves out anything else and says so, because a
12
+ // width that jumped from frame to frame would be a different animation
13
+ // from the one on the Web.
14
+
15
+ import { useEffect, useMemo, useRef, useState } from 'react'
16
+ import { Animated, Easing, type EasingFunction } from 'react-native'
17
+
18
+ export interface HozoKeyframe {
19
+ /** Where the frame sits, 0 to 1: `from`, `50%`, `to`. */
20
+ at: number
21
+ /** The frame as a React Native style. */
22
+ style: {
23
+ opacity?: number
24
+ backgroundColor?: string
25
+ borderColor?: string
26
+ color?: string
27
+ transform?: Record<string, number | string>[]
28
+ }
29
+ }
30
+
31
+ export interface HozoKeyframesSpec {
32
+ frames: HozoKeyframe[]
33
+ /** `animation-duration`, in milliseconds. */
34
+ duration: number
35
+ /** `animation-delay`, in milliseconds. */
36
+ delay?: number
37
+ /** `animation-timing-function`: a keyword, or a cubic-bezier's four numbers. */
38
+ easing?:
39
+ | 'linear'
40
+ | 'ease'
41
+ | 'ease-in'
42
+ | 'ease-out'
43
+ | 'ease-in-out'
44
+ | [number, number, number, number]
45
+ /** `animation-iteration-count`; `-1` is `infinite`. */
46
+ iterations?: number
47
+ direction?: 'normal' | 'reverse' | 'alternate' | 'alternate-reverse'
48
+ fillMode?: 'none' | 'forwards' | 'backwards' | 'both'
49
+ }
50
+
51
+ const COLOR_KEYS = ['backgroundColor', 'borderColor', 'color'] as const
52
+
53
+ /** What a transform key is when no frame sets it. */
54
+ function identity(key: string): number | string {
55
+ if (key.startsWith('scale')) return 1
56
+ if (key.startsWith('rotate') || key.startsWith('skew')) return '0deg'
57
+ return 0
58
+ }
59
+
60
+ function easingFor(easing: HozoKeyframesSpec['easing']): EasingFunction {
61
+ if (Array.isArray(easing)) return Easing.bezier(...easing)
62
+ switch (easing) {
63
+ case 'linear':
64
+ return Easing.linear
65
+ case 'ease-in':
66
+ return Easing.bezier(0.42, 0, 1, 1)
67
+ case 'ease-out':
68
+ return Easing.bezier(0, 0, 0.58, 1)
69
+ case 'ease-in-out':
70
+ return Easing.bezier(0.42, 0, 0.58, 1)
71
+ default:
72
+ // CSS's initial value, and what `animation` means with none written.
73
+ return Easing.bezier(0.25, 0.1, 0.25, 1)
74
+ }
75
+ }
76
+
77
+ /**
78
+ * One property's track: the frames that set it, in order, with the ends
79
+ * filled in where no frame does. CSS fills them from the element's own
80
+ * value; this fills them with the identity, which is the element's own
81
+ * value unless it has an opacity or a transform of its own.
82
+ */
83
+ function track<T>(points: { at: number; value: T }[], fill: T | undefined) {
84
+ const sorted = [...points].sort((a, b) => a.at - b.at)
85
+ if (fill !== undefined) {
86
+ if (sorted[0]?.at !== 0) sorted.unshift({ at: 0, value: fill })
87
+ if (sorted[sorted.length - 1]?.at !== 1) sorted.push({ at: 1, value: fill })
88
+ }
89
+ // `interpolate` needs a strictly increasing input range. Two frames at the
90
+ // same point are the later one winning, as in CSS.
91
+ const deduped: typeof sorted = []
92
+ for (const point of sorted) {
93
+ if (deduped.length > 0 && deduped[deduped.length - 1]?.at === point.at) deduped.pop()
94
+ deduped.push(point)
95
+ }
96
+ return deduped
97
+ }
98
+
99
+ type Phase = 'waiting' | 'running' | 'done'
100
+
101
+ export function useHozoKeyframes(spec: HozoKeyframesSpec) {
102
+ const progress = useRef(new Animated.Value(0)).current
103
+ const [phase, setPhase] = useState<Phase>(spec.delay ? 'waiting' : 'running')
104
+ const signature = JSON.stringify(spec)
105
+
106
+ // biome-ignore lint/correctness/useExhaustiveDependencies: `signature` is the spec, compared by value
107
+ useEffect(() => {
108
+ const iterations = spec.iterations ?? 1
109
+ const direction = spec.direction ?? 'normal'
110
+ const reversed = direction === 'reverse' || direction === 'alternate-reverse'
111
+ const alternates = direction === 'alternate' || direction === 'alternate-reverse'
112
+ const colours = spec.frames.some((frame) =>
113
+ COLOR_KEYS.some((key) => frame.style[key] !== undefined),
114
+ )
115
+ const leg = (toValue: number) =>
116
+ Animated.timing(progress, {
117
+ toValue,
118
+ duration: spec.duration,
119
+ easing: easingFor(spec.easing),
120
+ // React Native cannot interpolate a colour on the native driver.
121
+ useNativeDriver: !colours,
122
+ })
123
+
124
+ let animation: Animated.CompositeAnimation
125
+ if (alternates) {
126
+ const forth = leg(reversed ? 0 : 1)
127
+ const back = leg(reversed ? 1 : 0)
128
+ animation =
129
+ iterations < 0
130
+ ? Animated.loop(Animated.sequence([forth, back]))
131
+ : Animated.sequence(
132
+ Array.from({ length: iterations }, (_, i) => leg((i % 2 === 0) !== reversed ? 1 : 0)),
133
+ )
134
+ } else {
135
+ animation = Animated.loop(leg(reversed ? 0 : 1), {
136
+ iterations: iterations < 0 ? -1 : iterations,
137
+ })
138
+ }
139
+
140
+ progress.setValue(reversed ? 1 : 0)
141
+ let timer: ReturnType<typeof setTimeout> | undefined
142
+ const start = () => {
143
+ setPhase('running')
144
+ animation.start(({ finished }) => {
145
+ if (finished) setPhase('done')
146
+ })
147
+ }
148
+ if (spec.delay) {
149
+ setPhase('waiting')
150
+ timer = setTimeout(start, spec.delay)
151
+ } else {
152
+ start()
153
+ }
154
+ return () => {
155
+ clearTimeout(timer)
156
+ animation.stop()
157
+ }
158
+ }, [progress, signature])
159
+
160
+ // biome-ignore lint/correctness/useExhaustiveDependencies: as above
161
+ const animated = useMemo(() => {
162
+ const style: Record<string, unknown> = {}
163
+ const range = (points: { at: number; value: number | string }[]) => ({
164
+ inputRange: points.map((point) => point.at),
165
+ // Numbers or unit strings, never mixed: the compiler writes one track in
166
+ // one unit. The cast is to the overload; the runtime takes either.
167
+ outputRange: points.map((point) => point.value) as unknown as string[],
168
+ })
169
+
170
+ const opacity = spec.frames.flatMap((frame) =>
171
+ frame.style.opacity === undefined ? [] : [{ at: frame.at, value: frame.style.opacity }],
172
+ )
173
+ if (opacity.length > 0) {
174
+ style.opacity = progress.interpolate(range(track(opacity, 1)))
175
+ }
176
+
177
+ for (const key of COLOR_KEYS) {
178
+ const points = spec.frames.flatMap((frame) =>
179
+ frame.style[key] === undefined ? [] : [{ at: frame.at, value: frame.style[key] as string }],
180
+ )
181
+ // No identity for a colour: the ends hold the nearest frame.
182
+ if (points.length > 0) style[key] = progress.interpolate(range(track(points, undefined)))
183
+ }
184
+
185
+ const keys: string[] = []
186
+ for (const frame of spec.frames) {
187
+ for (const entry of frame.style.transform ?? []) {
188
+ for (const key of Object.keys(entry)) if (!keys.includes(key)) keys.push(key)
189
+ }
190
+ }
191
+ if (keys.length > 0) {
192
+ style.transform = keys.map((key) => {
193
+ const points = spec.frames.flatMap((frame) => {
194
+ const entry = frame.style.transform?.find((candidate) => key in candidate)
195
+ return entry ? [{ at: frame.at, value: entry[key] as number | string }] : []
196
+ })
197
+ return { [key]: progress.interpolate(range(track(points, identity(key)))) }
198
+ })
199
+ }
200
+ return style
201
+ }, [progress, signature])
202
+
203
+ // Outside the animation's run, CSS shows the element's own style unless
204
+ // the fill mode says otherwise: `backwards` holds the first frame through
205
+ // the delay, `forwards` the last after the end.
206
+ const fill = spec.fillMode ?? 'none'
207
+ if (phase === 'waiting' && fill !== 'backwards' && fill !== 'both') return null
208
+ if (phase === 'done' && fill !== 'forwards' && fill !== 'both') return null
209
+ return animated
210
+ }
@@ -1 +1 @@
1
- export { hozoPreflight } from './project.ts'
1
+ export { hozoClasses, hozoPreflight } from './project.ts'
package/src/project.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { createClassResolver } from './class-resolver.ts'
2
+
1
3
  /**
2
4
  * Project-wide Native defaults when no bundler integration supplies them.
3
5
  *
@@ -7,3 +9,12 @@
7
9
  * reset is the common case and was the fallback before this entry existed.
8
10
  */
9
11
  export const hozoPreflight = true
12
+
13
+ /**
14
+ * The project's class resolver, for code the compiler does not lower --
15
+ * `@hozo/patterns`' Native components resolve their class props through it
16
+ * (#886). Redirected to the generated candidate module like `hozoPreflight`;
17
+ * unconfigured, there is no candidate map, so it resolves nothing, which is
18
+ * what those props did before.
19
+ */
20
+ export const hozoClasses = createClassResolver({})
@@ -0,0 +1,119 @@
1
+ // CSS has four independently cascading transform properties. Metadata lives
2
+ // beside, never inside, RN StyleSheet entries; no private style key reaches RN.
3
+ export type TransformEntry = Record<string, number | string | readonly number[]>
4
+ type Mode = 'slots' | 'authored' | 'none'
5
+ export interface TransformSlots {
6
+ translate?: Mode
7
+ translateSlots?: TransformEntry[]
8
+ translateAuthored?: TransformEntry[]
9
+ rotate?: TransformEntry[]
10
+ scale?: Mode
11
+ scaleSlots?: TransformEntry[]
12
+ scale3d?: boolean
13
+ scaleAuthored?: TransformEntry[]
14
+ functions?: Mode
15
+ functionsSlots?: TransformEntry[]
16
+ functionsAuthored?: TransformEntry[]
17
+ }
18
+ type Style = Record<string, unknown>
19
+ export type TransformSpec = readonly [Style, TransformSlots]
20
+ export type TransformStyle = Style | false | null | undefined | readonly TransformStyle[]
21
+
22
+ /** Only generated reset-bearing boundaries need this; plain styles bypass it. */
23
+ export function hozoTransformStyles(
24
+ styles: readonly TransformStyle[],
25
+ specs: readonly TransformSpec[] | ReadonlyMap<Style, TransformSlots>,
26
+ ): Style[] {
27
+ // Candidate maps can contain thousands of entries. They pre-index once;
28
+ // resolving one class string must never walk the entire project map.
29
+ const metadata: ReadonlyMap<Style, TransformSlots> = Array.isArray(specs)
30
+ ? new Map(specs)
31
+ : (specs as ReadonlyMap<Style, TransformSlots>)
32
+ const result: Style[] = []
33
+ const families = {
34
+ translate: {
35
+ slots: new Map<string, TransformEntry>(),
36
+ authored: [] as TransformEntry[],
37
+ mode: 'slots' as Mode,
38
+ },
39
+ scale: {
40
+ slots: new Map<string, TransformEntry>(),
41
+ authored: [] as TransformEntry[],
42
+ mode: 'slots' as Mode,
43
+ },
44
+ functions: {
45
+ slots: new Map<string, TransformEntry>(),
46
+ authored: [] as TransformEntry[],
47
+ mode: 'slots' as Mode,
48
+ },
49
+ }
50
+ let rotate: TransformEntry[] = []
51
+ let scale3d = false
52
+ let touched = false
53
+ function visit(style: TransformStyle): void {
54
+ if (!style) return
55
+ if (Array.isArray(style)) {
56
+ for (const part of style) visit(part)
57
+ return
58
+ }
59
+ const object = style as Style
60
+ const slots = metadata.get(object)
61
+ if (!slots) {
62
+ result.push(object)
63
+ // An authored RN transform is a whole-array override, not CSS slots.
64
+ if ('transform' in object) {
65
+ for (const family of Object.values(families)) {
66
+ family.slots.clear()
67
+ family.authored = []
68
+ family.mode = 'none'
69
+ }
70
+ rotate = []
71
+ scale3d = false
72
+ touched = false
73
+ }
74
+ return
75
+ }
76
+ const { transform: _transform, ...rest } = object
77
+ if (Object.keys(rest).length) result.push(rest)
78
+ touched = true
79
+ if (slots.rotate) rotate = slots.rotate
80
+ if (slots.scale3d !== undefined) scale3d = slots.scale3d
81
+ for (const name of ['translate', 'scale', 'functions'] as const) {
82
+ const family = families[name]
83
+ for (const entry of slots[`${name}Slots`] ?? []) {
84
+ // A uniform 2D scale writes both registers. Keep Z's matrix separate.
85
+ if (name === 'scale' && 'scale' in entry) {
86
+ family.slots.set('scaleX', { scaleX: entry.scale })
87
+ family.slots.set('scaleY', { scaleY: entry.scale })
88
+ } else {
89
+ const key = Object.keys(entry)[0]
90
+ if (key) family.slots.set(key, entry)
91
+ }
92
+ }
93
+ const authored = slots[`${name}Authored`]
94
+ if (authored) family.authored = authored
95
+ if (slots[name]) family.mode = slots[name]
96
+ }
97
+ }
98
+ for (const style of styles) visit(style)
99
+ if (touched) {
100
+ const output = (name: keyof typeof families, keys: string[]): TransformEntry[] => {
101
+ const family = families[name]
102
+ if (family.mode === 'none') return []
103
+ if (family.mode === 'authored') return family.authored
104
+ return keys.flatMap((key) => {
105
+ const entry = family.slots.get(key)
106
+ return entry ? [entry] : []
107
+ })
108
+ }
109
+ result.push({
110
+ transform: [
111
+ ...output('translate', ['translateX', 'translateY', 'matrix']),
112
+ ...rotate,
113
+ ...output('scale', scale3d ? ['scaleX', 'scaleY', 'matrix'] : ['scaleX', 'scaleY']),
114
+ ...output('functions', ['rotateX', 'rotateY', 'rotateZ', 'skewX', 'skewY']),
115
+ ],
116
+ })
117
+ }
118
+ return result
119
+ }
@@ -0,0 +1,19 @@
1
+ // Generated by scripts/view-base.mjs from the Web backend's shared CSS.
2
+ export const HOZO_VIEW_BASE_CSS = `@layer base {
3
+ :where(.hozo-view) {
4
+ display: flex;
5
+ flex-direction: column;
6
+ flex-shrink: 0;
7
+ position: relative;
8
+ min-width: 0;
9
+ box-sizing: border-box;
10
+ margin: 0;
11
+ padding: 0;
12
+ border: 0 solid;
13
+ }
14
+
15
+ :where([data-hozo-pressable]:not(:disabled, [data-hozo-disabled], [aria-disabled="true"])) {
16
+ cursor: pointer;
17
+ }
18
+ }
19
+ `
@@ -0,0 +1,20 @@
1
+ import { createElement } from 'react'
2
+ import { HOZO_VIEW_BASE_CSS } from './view-base-css.ts'
3
+
4
+ // A React 19 stylesheet resource, not an effect or a DOM mutation. React
5
+ // hoists and deduplicates it in SSR and on the client, even if every box asks
6
+ // for it. Defaults belong in CSS, not inline styles that defeat utilities.
7
+ const resource = createElement(
8
+ 'style',
9
+ { href: 'hozo-view-base', precedence: 'hozo-base' },
10
+ HOZO_VIEW_BASE_CSS,
11
+ )
12
+
13
+ export function hozoViewBase() {
14
+ return resource
15
+ }
16
+
17
+ export function hozoViewClassName(className?: string) {
18
+ if (!className) return 'hozo-view'
19
+ return className.split(/\s+/).includes('hozo-view') ? className : `hozo-view ${className}`
20
+ }