@hozo/engine 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.
Files changed (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -0
  3. package/dist/activate.d.ts +14 -0
  4. package/dist/activate.d.ts.map +1 -0
  5. package/dist/activate.js +38 -0
  6. package/dist/activate.js.map +1 -0
  7. package/dist/ambient.d.ts +79 -0
  8. package/dist/ambient.d.ts.map +1 -0
  9. package/dist/ambient.js +117 -0
  10. package/dist/ambient.js.map +1 -0
  11. package/dist/backdrop.d.ts +36 -0
  12. package/dist/backdrop.d.ts.map +1 -0
  13. package/dist/backdrop.js +41 -0
  14. package/dist/backdrop.js.map +1 -0
  15. package/dist/class-name.native.d.ts +20 -0
  16. package/dist/class-name.native.d.ts.map +1 -0
  17. package/dist/class-name.native.js +34 -0
  18. package/dist/class-name.native.js.map +1 -0
  19. package/dist/class-resolver.d.ts +26 -0
  20. package/dist/class-resolver.d.ts.map +1 -0
  21. package/dist/class-resolver.js +56 -0
  22. package/dist/class-resolver.js.map +1 -0
  23. package/dist/color-transition.d.ts +3 -0
  24. package/dist/color-transition.d.ts.map +1 -0
  25. package/dist/color-transition.js +31 -0
  26. package/dist/color-transition.js.map +1 -0
  27. package/dist/dom-style.d.ts +10 -0
  28. package/dist/dom-style.d.ts.map +1 -0
  29. package/dist/dom-style.js +145 -0
  30. package/dist/dom-style.js.map +1 -0
  31. package/dist/dom-style.native.d.ts +7 -0
  32. package/dist/dom-style.native.d.ts.map +1 -0
  33. package/dist/dom-style.native.js +9 -0
  34. package/dist/dom-style.native.js.map +1 -0
  35. package/dist/external-link.d.ts +6 -0
  36. package/dist/external-link.d.ts.map +1 -0
  37. package/dist/external-link.js +9 -0
  38. package/dist/external-link.js.map +1 -0
  39. package/dist/generated/animation.d.ts +2 -0
  40. package/dist/generated/animation.d.ts.map +1 -0
  41. package/dist/generated/animation.js +3 -0
  42. package/dist/generated/animation.js.map +1 -0
  43. package/dist/generated/class-resolver.d.ts +2 -0
  44. package/dist/generated/class-resolver.d.ts.map +1 -0
  45. package/dist/generated/class-resolver.js +3 -0
  46. package/dist/generated/class-resolver.js.map +1 -0
  47. package/dist/generated/dom-style.d.ts +2 -0
  48. package/dist/generated/dom-style.d.ts.map +1 -0
  49. package/dist/generated/dom-style.js +2 -0
  50. package/dist/generated/dom-style.js.map +1 -0
  51. package/dist/generated/dom-style.native.d.ts +2 -0
  52. package/dist/generated/dom-style.native.d.ts.map +1 -0
  53. package/dist/generated/dom-style.native.js +2 -0
  54. package/dist/generated/dom-style.native.js.map +1 -0
  55. package/dist/generated/environment.d.ts +2 -0
  56. package/dist/generated/environment.d.ts.map +1 -0
  57. package/dist/generated/environment.js +5 -0
  58. package/dist/generated/environment.js.map +1 -0
  59. package/dist/generated/image-source.d.ts +2 -0
  60. package/dist/generated/image-source.d.ts.map +1 -0
  61. package/dist/generated/image-source.js +3 -0
  62. package/dist/generated/image-source.js.map +1 -0
  63. package/dist/generated/interactive.d.ts +2 -0
  64. package/dist/generated/interactive.d.ts.map +1 -0
  65. package/dist/generated/interactive.js +2 -0
  66. package/dist/generated/interactive.js.map +1 -0
  67. package/dist/generated/safe-area.d.ts +2 -0
  68. package/dist/generated/safe-area.d.ts.map +1 -0
  69. package/dist/generated/safe-area.js +4 -0
  70. package/dist/generated/safe-area.js.map +1 -0
  71. package/dist/generated/scrollable.d.ts +2 -0
  72. package/dist/generated/scrollable.d.ts.map +1 -0
  73. package/dist/generated/scrollable.js +2 -0
  74. package/dist/generated/scrollable.js.map +1 -0
  75. package/dist/hooks.native.d.ts +92 -0
  76. package/dist/hooks.native.d.ts.map +1 -0
  77. package/dist/hooks.native.js +284 -0
  78. package/dist/hooks.native.js.map +1 -0
  79. package/dist/image-source.native.d.ts +5 -0
  80. package/dist/image-source.native.d.ts.map +1 -0
  81. package/dist/image-source.native.js +5 -0
  82. package/dist/image-source.native.js.map +1 -0
  83. package/dist/index.d.ts +11 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +30 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/index.native.d.ts +8 -0
  88. package/dist/index.native.d.ts.map +1 -0
  89. package/dist/index.native.js +28 -0
  90. package/dist/index.native.js.map +1 -0
  91. package/dist/interactive.d.ts +31 -0
  92. package/dist/interactive.d.ts.map +1 -0
  93. package/dist/interactive.js +64 -0
  94. package/dist/interactive.js.map +1 -0
  95. package/dist/navigation-context.d.ts +11 -0
  96. package/dist/navigation-context.d.ts.map +1 -0
  97. package/dist/navigation-context.js +12 -0
  98. package/dist/navigation-context.js.map +1 -0
  99. package/dist/navigation-entry.d.ts +3 -0
  100. package/dist/navigation-entry.d.ts.map +1 -0
  101. package/dist/navigation-entry.js +3 -0
  102. package/dist/navigation-entry.js.map +1 -0
  103. package/dist/navigation-entry.native.d.ts +2 -0
  104. package/dist/navigation-entry.native.d.ts.map +1 -0
  105. package/dist/navigation-entry.native.js +6 -0
  106. package/dist/navigation-entry.native.js.map +1 -0
  107. package/dist/navigation.d.ts +34 -0
  108. package/dist/navigation.d.ts.map +1 -0
  109. package/dist/navigation.js +27 -0
  110. package/dist/navigation.js.map +1 -0
  111. package/dist/project.d.ts +10 -0
  112. package/dist/project.d.ts.map +1 -0
  113. package/dist/project.js +10 -0
  114. package/dist/project.js.map +1 -0
  115. package/dist/project.native.d.ts +2 -0
  116. package/dist/project.native.d.ts.map +1 -0
  117. package/dist/project.native.js +2 -0
  118. package/dist/project.native.js.map +1 -0
  119. package/dist/responder.d.ts +100 -0
  120. package/dist/responder.d.ts.map +1 -0
  121. package/dist/responder.js +256 -0
  122. package/dist/responder.js.map +1 -0
  123. package/dist/safe-area.native.d.ts +18 -0
  124. package/dist/safe-area.native.d.ts.map +1 -0
  125. package/dist/safe-area.native.js +72 -0
  126. package/dist/safe-area.native.js.map +1 -0
  127. package/dist/scrollable.d.ts +16 -0
  128. package/dist/scrollable.d.ts.map +1 -0
  129. package/dist/scrollable.js +114 -0
  130. package/dist/scrollable.js.map +1 -0
  131. package/dist/text-size.d.ts +12 -0
  132. package/dist/text-size.d.ts.map +1 -0
  133. package/dist/text-size.js +22 -0
  134. package/dist/text-size.js.map +1 -0
  135. package/package.json +131 -0
  136. package/src/activate.ts +45 -0
  137. package/src/ambient.ts +199 -0
  138. package/src/backdrop.ts +66 -0
  139. package/src/class-name.native.ts +53 -0
  140. package/src/class-resolver.ts +74 -0
  141. package/src/color-transition.ts +27 -0
  142. package/src/dom-style.native.ts +13 -0
  143. package/src/dom-style.ts +173 -0
  144. package/src/external-link.ts +12 -0
  145. package/src/generated/animation.ts +2 -0
  146. package/src/generated/class-resolver.ts +2 -0
  147. package/src/generated/dom-style.native.ts +1 -0
  148. package/src/generated/dom-style.ts +1 -0
  149. package/src/generated/environment.ts +10 -0
  150. package/src/generated/image-source.ts +2 -0
  151. package/src/generated/interactive.ts +1 -0
  152. package/src/generated/safe-area.ts +3 -0
  153. package/src/generated/scrollable.ts +1 -0
  154. package/src/hooks.native.ts +408 -0
  155. package/src/image-source.native.ts +4 -0
  156. package/src/index.native.ts +38 -0
  157. package/src/index.ts +50 -0
  158. package/src/interactive.ts +68 -0
  159. package/src/navigation-context.tsx +20 -0
  160. package/src/navigation-entry.native.ts +5 -0
  161. package/src/navigation-entry.ts +12 -0
  162. package/src/navigation.ts +55 -0
  163. package/src/project.native.ts +1 -0
  164. package/src/project.ts +9 -0
  165. package/src/responder.ts +372 -0
  166. package/src/safe-area.native.ts +87 -0
  167. package/src/scrollable.ts +119 -0
  168. package/src/text-size.ts +25 -0
@@ -0,0 +1,74 @@
1
+ /** A React Native style object, as the compiler emits it. */
2
+ export type StyleObject = Record<string, unknown>
3
+
4
+ /**
5
+ * Resolves whitespace-separated class names to React Native style objects.
6
+ *
7
+ * Returns an array, which is a valid `style` value on its own and also
8
+ * nests inside one -- RN flattens arbitrarily nested style arrays, so
9
+ * callers never have to spread it.
10
+ */
11
+ export type ClassResolver = (value: unknown) => StyleObject[]
12
+
13
+ /**
14
+ * Builds a resolver over one project's candidate map.
15
+ *
16
+ * Called by the generated candidate module rather than by application
17
+ * code: `styles` and `unsupported` are build output, and binding them here
18
+ * keeps the generated file to data plus one call.
19
+ *
20
+ * `unsupported` maps a class the compiler *recognized but can't express*
21
+ * as a plain style object -- a conditional utility (`hover:`, `md:`), or
22
+ * one that's Web-only -- to the reason. Those are reported when they're
23
+ * actually used rather than at build time, because a class appearing in
24
+ * the project's candidate scan doesn't mean any dynamic expression ever
25
+ * produces it.
26
+ */
27
+ export function createClassResolver(
28
+ styles: Record<string, StyleObject>,
29
+ unsupported: Record<string, string> = {},
30
+ ): ClassResolver {
31
+ // Class strings repeat constantly across renders and list items, and the
32
+ // result is immutable, so each distinct string is resolved once.
33
+ const cache = new Map<string, StyleObject[]>()
34
+ // Warn once per class, not once per render -- a warning inside a list's
35
+ // render path would otherwise repeat for every row, every frame.
36
+ const warned = new Set<string>()
37
+
38
+ return function hozoClasses(value: unknown): StyleObject[] {
39
+ // Falsy is the normal case, not an error: `cond && 'p-4'` yields
40
+ // `false` whenever the condition is off.
41
+ if (typeof value !== 'string' || value === '') {
42
+ return []
43
+ }
44
+
45
+ const cached = cache.get(value)
46
+ if (cached) {
47
+ return cached
48
+ }
49
+
50
+ const resolved: StyleObject[] = []
51
+ for (const name of value.split(/\s+/)) {
52
+ if (name === '') {
53
+ continue
54
+ }
55
+ const style = styles[name]
56
+ if (style) {
57
+ resolved.push(style)
58
+ continue
59
+ }
60
+ // An unknown class isn't necessarily a Hozo problem -- it may be an
61
+ // app's own non-Tailwind class, or a testID-ish marker -- so only the
62
+ // ones the compiler recognized and refused are reported.
63
+ const reason = unsupported[name]
64
+ if (reason && !warned.has(name)) {
65
+ warned.add(name)
66
+ // eslint-disable-next-line no-console
67
+ console.warn(`[hozo] ${reason}`)
68
+ }
69
+ }
70
+
71
+ cache.set(value, resolved)
72
+ return resolved
73
+ }
74
+ }
@@ -0,0 +1,27 @@
1
+ function rgba(color: string): [number, number, number, number] | null {
2
+ if (color === 'transparent') return [0, 0, 0, 0]
3
+ const match = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(color)
4
+ if (!match) return null
5
+ let hex = match[1]!
6
+ if (hex.length === 3) hex = [...hex].map((digit) => digit + digit).join('')
7
+ const alpha = hex.length === 8 ? parseInt(hex.slice(6, 8), 16) / 255 : 1
8
+ return [
9
+ parseInt(hex.slice(0, 2), 16),
10
+ parseInt(hex.slice(2, 4), 16),
11
+ parseInt(hex.slice(4, 6), 16),
12
+ alpha,
13
+ ]
14
+ }
15
+
16
+ /** The visible colour at `progress`, used when an interrupted transition restarts. */
17
+ export function blendColor(from: string, to: string, progress: number) {
18
+ const a = rgba(from)
19
+ const b = rgba(to)
20
+ if (!a || !b) return progress < 0.5 ? from : to
21
+ // Indexed with literal types rather than mapped: `rgba` returns a
22
+ // 4-tuple, and a tuple read at a known index is a `number` where the
23
+ // same read on the `number[]` that `.map` produces is `number |
24
+ // undefined`.
25
+ const at = (index: 0 | 1 | 2 | 3) => a[index] + (b[index] - a[index]) * progress
26
+ return `rgba(${Math.round(at(0))}, ${Math.round(at(1))}, ${Math.round(at(2))}, ${at(3)})`
27
+ }
@@ -0,0 +1,13 @@
1
+ import type { HozoDomStyle } from './dom-style.ts'
2
+
3
+ export type { HozoDomStyle } from './dom-style.ts'
4
+
5
+ /** React Native already owns StyleProp flattening and value semantics. */
6
+ export function hozoDomStyle(input: HozoDomStyle): HozoDomStyle {
7
+ return input
8
+ }
9
+
10
+ /** React Native props need no DOM normalization. */
11
+ export function hozoDomProps<T>(props: T): T {
12
+ return props
13
+ }
@@ -0,0 +1,173 @@
1
+ import type { CSSProperties } from 'react'
2
+
3
+ export type HozoDomStyle =
4
+ | CSSProperties
5
+ | Readonly<Record<string, unknown>>
6
+ | readonly HozoDomStyle[]
7
+ | false
8
+ | null
9
+ | undefined
10
+
11
+ const warned = new Set<string>()
12
+
13
+ function warnOnce(key: string, message: string) {
14
+ if (warned.has(key)) return
15
+ warned.add(key)
16
+ console.warn(`[hozo] ${message}`)
17
+ }
18
+
19
+ function flattenInto(value: unknown, target: Record<string, unknown>) {
20
+ if (value == null || value === false) return
21
+ if (Array.isArray(value)) {
22
+ for (const item of value) flattenInto(item, target)
23
+ return
24
+ }
25
+ if (typeof value !== 'object') {
26
+ warnOnce(
27
+ `style:${typeof value}`,
28
+ `A React Native style ${JSON.stringify(value)} could not be resolved for the DOM. Pass style objects rather than registered numeric IDs or primitive values.`,
29
+ )
30
+ return
31
+ }
32
+ Object.assign(target, value)
33
+ }
34
+
35
+ function length(value: unknown): string | undefined {
36
+ if (typeof value === 'number' && Number.isFinite(value)) return `${value}px`
37
+ if (typeof value === 'string') return value
38
+ return undefined
39
+ }
40
+
41
+ function transformValue(name: string, value: unknown): string | undefined {
42
+ if (
43
+ name === 'matrix' &&
44
+ Array.isArray(value) &&
45
+ value.every((part) => typeof part === 'number')
46
+ ) {
47
+ return `matrix(${value.join(', ')})`
48
+ }
49
+ if (name === 'translate' && Array.isArray(value) && value.length === 2) {
50
+ const x = length(value[0])
51
+ const y = length(value[1])
52
+ return x && y ? `translate(${x}, ${y})` : undefined
53
+ }
54
+ if (name === 'translateX' || name === 'translateY' || name === 'perspective') {
55
+ const resolved = length(value)
56
+ return resolved ? `${name}(${resolved})` : undefined
57
+ }
58
+ if (
59
+ name === 'rotate' ||
60
+ name === 'rotateX' ||
61
+ name === 'rotateY' ||
62
+ name === 'rotateZ' ||
63
+ name === 'skewX' ||
64
+ name === 'skewY'
65
+ ) {
66
+ return typeof value === 'string' ? `${name}(${value})` : undefined
67
+ }
68
+ if (name === 'scale' || name === 'scaleX' || name === 'scaleY') {
69
+ return typeof value === 'number' && Number.isFinite(value) ? `${name}(${value})` : undefined
70
+ }
71
+ return undefined
72
+ }
73
+
74
+ function normalizeTransform(value: unknown): string | undefined {
75
+ if (typeof value === 'string') return value
76
+ if (!Array.isArray(value)) return undefined
77
+ const functions: string[] = []
78
+ for (const entry of value) {
79
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
80
+ warnOnce(
81
+ 'transform:entry',
82
+ 'A React Native transform entry is not a one-property object and was omitted on Web.',
83
+ )
84
+ continue
85
+ }
86
+ const entries = Object.entries(entry)
87
+ if (entries.length !== 1) {
88
+ warnOnce(
89
+ 'transform:shape',
90
+ 'A React Native transform entry must contain exactly one transform and was omitted on Web.',
91
+ )
92
+ continue
93
+ }
94
+ const [name, input] = entries[0] as [string, unknown]
95
+ const resolved = transformValue(name, input)
96
+ if (resolved) functions.push(resolved)
97
+ else
98
+ warnOnce(
99
+ `transform:${name}`,
100
+ `React Native transform \`${name}\` has no compatible DOM conversion for this value and was omitted.`,
101
+ )
102
+ }
103
+ return functions.length > 0 ? functions.join(' ') : undefined
104
+ }
105
+
106
+ const unsupported = new Map<string, string>([
107
+ ['borderCurve', 'CSS has no equivalent for React Native borderCurve'],
108
+ ['elevation', 'CSS has no equivalent for Android elevation without choosing an invented shadow'],
109
+ ['includeFontPadding', 'CSS cannot reproduce Android font padding'],
110
+ ['overlayColor', 'CSS images have no equivalent for React Native overlayColor'],
111
+ ['shadowColor', 'legacy React Native shadow props need an explicit CSS boxShadow'],
112
+ ['shadowOffset', 'legacy React Native shadow props need an explicit CSS boxShadow'],
113
+ ['shadowOpacity', 'legacy React Native shadow props need an explicit CSS boxShadow'],
114
+ ['shadowRadius', 'legacy React Native shadow props need an explicit CSS boxShadow'],
115
+ ['textAlignVertical', 'CSS vertical-align does not reproduce React Native textAlignVertical'],
116
+ ['tintColor', 'CSS has no general color-preserving equivalent for React Native tintColor'],
117
+ ])
118
+
119
+ /**
120
+ * Converts React Native's StyleProp shape to the single object React DOM requires.
121
+ * Arrays are recursively flattened, falsy entries are ignored, and later objects win.
122
+ */
123
+ export function hozoDomStyle(input: HozoDomStyle): CSSProperties | undefined {
124
+ const flat: Record<string, unknown> = {}
125
+ flattenInto(input, flat)
126
+
127
+ if ('transform' in flat) {
128
+ const transform = normalizeTransform(flat.transform)
129
+ if (transform) flat.transform = transform
130
+ else delete flat.transform
131
+ }
132
+ if (Array.isArray(flat.transformOrigin)) {
133
+ const origin = flat.transformOrigin.map(length)
134
+ if (origin.every(Boolean)) flat.transformOrigin = origin.join(' ')
135
+ else {
136
+ warnOnce(
137
+ 'transformOrigin',
138
+ 'A React Native transformOrigin array contained a value CSS cannot express and was omitted on Web.',
139
+ )
140
+ delete flat.transformOrigin
141
+ }
142
+ }
143
+ if (Array.isArray(flat.fontVariant)) flat.fontVariant = flat.fontVariant.join(' ')
144
+ if (typeof flat.resizeMode === 'string') {
145
+ const objectFit = { contain: 'contain', cover: 'cover', stretch: 'fill', center: 'none' }[
146
+ flat.resizeMode
147
+ ]
148
+ if (objectFit) flat.objectFit = objectFit
149
+ else
150
+ warnOnce(
151
+ `resizeMode:${flat.resizeMode}`,
152
+ `React Native resizeMode \`${flat.resizeMode}\` has no equivalent CSS object-fit value and was omitted.`,
153
+ )
154
+ delete flat.resizeMode
155
+ }
156
+ for (const [name, reason] of unsupported) {
157
+ if (!(name in flat)) continue
158
+ warnOnce(`property:${name}`, `${reason}; inline style property \`${name}\` was omitted on Web.`)
159
+ delete flat[name]
160
+ }
161
+
162
+ return Object.keys(flat).length > 0 ? (flat as CSSProperties) : undefined
163
+ }
164
+
165
+ /** Normalizes a possible style field without cloning ordinary prop spreads. */
166
+ export function hozoDomProps<T>(props: T): T {
167
+ if (!props || typeof props !== 'object' || !Object.hasOwn(props, 'style')) return props
168
+ // Clone first so an accessor is read exactly once, as it was by the
169
+ // original JSX spread. Reading props.style separately would run it twice.
170
+ const copy = { ...props } as T & { style?: HozoDomStyle }
171
+ copy.style = hozoDomStyle(copy.style)
172
+ return copy
173
+ }
@@ -0,0 +1,12 @@
1
+ /** Resolve safe browser attributes for a destination that opens a new context. */
2
+ export function externalLinkAttributes(
3
+ external: boolean | undefined,
4
+ target: string | undefined,
5
+ rel: string | undefined,
6
+ ): { target: string | undefined; rel: string | undefined } {
7
+ const finalTarget = external ? '_blank' : target
8
+ return {
9
+ target: finalTarget,
10
+ rel: external || target === '_blank' ? (rel ?? 'noreferrer noopener') : rel,
11
+ }
12
+ }
@@ -0,0 +1,2 @@
1
+ // Native-only. See `./environment.ts`.
2
+ export { type HozoAnimation, useHozoAnimation } from '../hooks.native.ts'
@@ -0,0 +1,2 @@
1
+ // Imported by the Metro candidate module rather than by a component.
2
+ export { type ClassResolver, createClassResolver, type StyleObject } from '../class-resolver.ts'
@@ -0,0 +1 @@
1
+ export { type HozoDomStyle, hozoDomProps, hozoDomStyle } from '../dom-style.native.ts'
@@ -0,0 +1 @@
1
+ export { type HozoDomStyle, hozoDomProps, hozoDomStyle } from '../dom-style.ts'
@@ -0,0 +1,10 @@
1
+ // Native-only: on the Web these conditions are CSS media queries and need
2
+ // no script. Importing this installs the shared platform subscriptions,
3
+ // which is why `hooks.native.js` is the package's one declared side effect.
4
+ export {
5
+ useHozoBreakpoint,
6
+ useHozoDark,
7
+ useHozoEnvironment,
8
+ useHozoViewport,
9
+ useHozoWidthAtLeast,
10
+ } from '../hooks.native.ts'
@@ -0,0 +1,2 @@
1
+ // Native-only: the Web backend passes `src` straight to `<img>`.
2
+ export { hozoImageSource } from '../image-source.native.ts'
@@ -0,0 +1 @@
1
+ export { hozoInteractive } from '../interactive.ts'
@@ -0,0 +1,3 @@
1
+ // Native-only. A module of its own because it is one: nothing else in the
2
+ // environment hooks needs the safe-area provider.
3
+ export { type HozoSafeAreaInsets, useHozoSafeArea } from '../safe-area.native.ts'
@@ -0,0 +1 @@
1
+ export { hozoScrollable } from '../scrollable.ts'