@hozo/engine 0.4.0 → 0.5.1

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 (76) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/dist/authored-edges.d.ts +24 -0
  3. package/dist/authored-edges.d.ts.map +1 -0
  4. package/dist/authored-edges.js +73 -0
  5. package/dist/authored-edges.js.map +1 -0
  6. package/dist/class-name.native.d.ts +6 -0
  7. package/dist/class-name.native.d.ts.map +1 -1
  8. package/dist/class-name.native.js.map +1 -1
  9. package/dist/class-resolver.d.ts +39 -4
  10. package/dist/class-resolver.d.ts.map +1 -1
  11. package/dist/class-resolver.js +357 -67
  12. package/dist/class-resolver.js.map +1 -1
  13. package/dist/filter-slots.d.ts +11 -0
  14. package/dist/filter-slots.d.ts.map +1 -0
  15. package/dist/filter-slots.js +57 -0
  16. package/dist/filter-slots.js.map +1 -0
  17. package/dist/generated/authored-edges.d.ts +2 -0
  18. package/dist/generated/authored-edges.d.ts.map +1 -0
  19. package/dist/generated/authored-edges.js +3 -0
  20. package/dist/generated/authored-edges.js.map +1 -0
  21. package/dist/generated/class-resolver.d.ts +1 -1
  22. package/dist/generated/class-resolver.d.ts.map +1 -1
  23. package/dist/generated/class-resolver.js +1 -1
  24. package/dist/generated/class-resolver.js.map +1 -1
  25. package/dist/generated/filter-slots.d.ts +2 -0
  26. package/dist/generated/filter-slots.d.ts.map +1 -0
  27. package/dist/generated/filter-slots.js +3 -0
  28. package/dist/generated/filter-slots.js.map +1 -0
  29. package/dist/generated/gradient-slots.d.ts +2 -0
  30. package/dist/generated/gradient-slots.d.ts.map +1 -0
  31. package/dist/generated/gradient-slots.js +3 -0
  32. package/dist/generated/gradient-slots.js.map +1 -0
  33. package/dist/generated/shadow-slots.d.ts +2 -0
  34. package/dist/generated/shadow-slots.d.ts.map +1 -0
  35. package/dist/generated/shadow-slots.js +3 -0
  36. package/dist/generated/shadow-slots.js.map +1 -0
  37. package/dist/gradient-slots.d.ts +19 -0
  38. package/dist/gradient-slots.d.ts.map +1 -0
  39. package/dist/gradient-slots.js +61 -0
  40. package/dist/gradient-slots.js.map +1 -0
  41. package/dist/inline-edges.d.ts +8 -0
  42. package/dist/inline-edges.d.ts.map +1 -0
  43. package/dist/inline-edges.js +13 -0
  44. package/dist/inline-edges.js.map +1 -0
  45. package/dist/shadow-slots.d.ts +26 -0
  46. package/dist/shadow-slots.d.ts.map +1 -0
  47. package/dist/shadow-slots.js +59 -0
  48. package/dist/shadow-slots.js.map +1 -0
  49. package/dist/spread-styles.d.ts +3 -1
  50. package/dist/spread-styles.d.ts.map +1 -1
  51. package/dist/spread-styles.js +6 -5
  52. package/dist/spread-styles.js.map +1 -1
  53. package/dist/text-inheritance.d.ts +0 -5
  54. package/dist/text-inheritance.d.ts.map +1 -1
  55. package/dist/text-inheritance.js +1 -7
  56. package/dist/text-inheritance.js.map +1 -1
  57. package/dist/transform-slots.d.ts +1 -1
  58. package/dist/transform-slots.d.ts.map +1 -1
  59. package/dist/transform-slots.js +12 -5
  60. package/dist/transform-slots.js.map +1 -1
  61. package/package.json +17 -1
  62. package/src/authored-edges.ts +94 -0
  63. package/src/class-name.native.ts +10 -0
  64. package/src/class-resolver.ts +424 -76
  65. package/src/filter-slots.ts +67 -0
  66. package/src/generated/authored-edges.ts +2 -0
  67. package/src/generated/class-resolver.ts +1 -0
  68. package/src/generated/filter-slots.ts +2 -0
  69. package/src/generated/gradient-slots.ts +2 -0
  70. package/src/generated/shadow-slots.ts +2 -0
  71. package/src/gradient-slots.ts +73 -0
  72. package/src/inline-edges.ts +14 -0
  73. package/src/shadow-slots.ts +78 -0
  74. package/src/spread-styles.ts +10 -3
  75. package/src/text-inheritance.ts +1 -16
  76. package/src/transform-slots.ts +11 -3
@@ -0,0 +1,67 @@
1
+ // Slot numbers and function strings come from the compiler, not a second
2
+ // Tailwind ordering table. Only split filters and candidate maps import this.
3
+ type Style = Record<string, unknown>
4
+ type FilterStyle = Style | false | null | undefined | readonly FilterStyle[]
5
+ export interface FilterSlots {
6
+ mode: 'slots' | 'none' | 'authored'
7
+ slots?: readonly (readonly [number, string])[]
8
+ authored?: string
9
+ }
10
+ export type FilterSpec = readonly [Style, FilterSlots]
11
+
12
+ export function hozoFilterStyles(
13
+ styles: readonly FilterStyle[],
14
+ specs: readonly FilterSpec[] | ReadonlyMap<Style, FilterSlots>,
15
+ candidates?: ReadonlyMap<Style, FilterSlots> | null,
16
+ ): Style[] {
17
+ const metadata: ReadonlyMap<Style, FilterSlots> = Array.isArray(specs)
18
+ ? new Map(specs)
19
+ : (specs as ReadonlyMap<Style, FilterSlots>)
20
+ const result: Style[] = []
21
+ const slots = new Map<number, string>()
22
+ const state = { mode: 'slots' as FilterSlots['mode'], authored: '' }
23
+ let touched = false
24
+ let opaque = false
25
+ function visit(style: FilterStyle): void {
26
+ if (!style) return
27
+ if (Array.isArray(style)) {
28
+ for (const part of style) visit(part)
29
+ return
30
+ }
31
+ const object = style as Style
32
+ // Preserve identity: transform metadata may refer to the very same entry.
33
+ // The final filter wins over these intermediate whole-chain strings.
34
+ result.push(object)
35
+ const spec = metadata.get(object) ?? candidates?.get(object)
36
+ if (!spec) {
37
+ if ('filter' in object) {
38
+ // A mixed chain with an unassessed candidate (e.g. drop-shadow)
39
+ // must retain whole-property replacement, not compose partially.
40
+ opaque = true
41
+ slots.clear()
42
+ touched = false
43
+ }
44
+ return
45
+ }
46
+ touched = true
47
+ for (const [key, value] of spec.slots ?? []) slots.set(key, value)
48
+ state.mode = spec.mode
49
+ if (spec.authored !== undefined) state.authored = spec.authored
50
+ }
51
+ for (const style of styles) visit(style)
52
+ if (touched && !(opaque && candidates !== undefined)) {
53
+ result.push({
54
+ filter:
55
+ state.mode === 'none'
56
+ ? 'none'
57
+ : state.mode === 'authored'
58
+ ? state.authored
59
+ : [...slots]
60
+ .sort(([a], [b]) => a - b)
61
+ .map(([, value]) => value)
62
+ .filter(Boolean)
63
+ .join(' '),
64
+ })
65
+ }
66
+ return result
67
+ }
@@ -0,0 +1,2 @@
1
+ // Native-only: on the Web an inline style beats a class by the cascade.
2
+ export { hozoAuthoredEdges, hozoEdgeSpreadStyles } from '../authored-edges.ts'
@@ -3,6 +3,7 @@ export {
3
3
  type ClassResolver,
4
4
  type ClassResolverPlan,
5
5
  createClassResolver,
6
+ hozoMixedTextMetricStyles,
6
7
  hozoTextMetricStyles,
7
8
  type StyleObject,
8
9
  } from '../class-resolver.ts'
@@ -0,0 +1,2 @@
1
+ // Ordinary static filters need no runtime import.
2
+ export { hozoFilterStyles } from '../filter-slots.ts'
@@ -0,0 +1,2 @@
1
+ // Only boundaries with independently active gradient registers import this.
2
+ export { hozoGradientStyles } from '../gradient-slots.ts'
@@ -0,0 +1,2 @@
1
+ // Only independently composed Native shadows need this generated leaf.
2
+ export { hozoShadowStyles } from '../shadow-slots.ts'
@@ -0,0 +1,73 @@
1
+ // Values/defaults are compiler data. This joins selected gradient registers;
2
+ // it neither parses CSS nor chooses classes, conditions or precedence.
3
+ type Style = Record<string, unknown>
4
+ type Image = { kind: string; prelude: string } | string | null
5
+ export interface GradientSlots {
6
+ image?: Image
7
+ colors?: readonly (readonly [number, string | null])[]
8
+ positions?: readonly (readonly [number, string])[]
9
+ }
10
+ export interface GradientDefaults {
11
+ color: string
12
+ positions: readonly string[]
13
+ }
14
+ export type GradientSpec = readonly [Style, GradientSlots]
15
+ export type GradientStyle = Style | number | false | null | undefined | readonly GradientStyle[]
16
+
17
+ export function hozoGradientStyles(
18
+ styles: readonly GradientStyle[],
19
+ specs: readonly GradientSpec[] | ReadonlyMap<Style, GradientSlots>,
20
+ defaults: GradientDefaults,
21
+ candidates?: ReadonlyMap<Style, GradientSlots>,
22
+ ): (Style | number)[] {
23
+ const metadata: ReadonlyMap<Style, GradientSlots> = Array.isArray(specs)
24
+ ? new Map(specs)
25
+ : (specs as ReadonlyMap<Style, GradientSlots>)
26
+ const result: (Style | number)[] = []
27
+ const state: { image?: Image } = {}
28
+ const colors = new Map<number, string | null>()
29
+ const positions = new Map<number, string>()
30
+ let opaque = false
31
+ function visit(style: GradientStyle): void {
32
+ if (!style) return
33
+ if (Array.isArray(style)) {
34
+ for (const part of style) visit(part)
35
+ return
36
+ }
37
+ if (typeof style === 'number') {
38
+ // A registered RN style ID cannot be inspected without RN. Preserve
39
+ // the previous whole-property path rather than overriding unseen data.
40
+ opaque = true
41
+ result.push(style)
42
+ return
43
+ }
44
+ const object = style as Style
45
+ result.push(object)
46
+ const slots = metadata.get(object) ?? candidates?.get(object)
47
+ if (!slots) {
48
+ if ('backgroundImage' in object) {
49
+ state.image = undefined
50
+ colors.clear()
51
+ positions.clear()
52
+ }
53
+ return
54
+ }
55
+ if (slots.image !== undefined) state.image = slots.image
56
+ for (const [key, value] of slots.colors ?? []) colors.set(key, value)
57
+ for (const [key, value] of slots.positions ?? []) positions.set(key, value)
58
+ }
59
+ for (const style of styles) visit(style)
60
+ const image = state.image
61
+ if (opaque || image === undefined) return result
62
+ let value: string
63
+ if (image === null) value = 'none'
64
+ else if (typeof image === 'string') value = image
65
+ else {
66
+ const stop = (index: number) =>
67
+ `${colors.get(index) ?? defaults.color} ${positions.get(index) ?? defaults.positions[index]}`
68
+ const stops = [stop(0), ...(colors.get(1) ? [stop(1)] : []), stop(2)]
69
+ value = `${image.kind}(${[...(image.prelude ? [image.prelude] : []), ...stops].join(', ')})`
70
+ }
71
+ result.push({ backgroundImage: value })
72
+ return result
73
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Each inline family as `[start, end, left, right]`, in React Native's keys.
3
+ * Its own module so the class resolver, which reads it, does not also load
4
+ * the authored-edge and spread composers.
5
+ */
6
+ export const INLINE_EDGES = [
7
+ ['paddingStart', 'paddingEnd', 'paddingLeft', 'paddingRight'],
8
+ ['marginStart', 'marginEnd', 'marginLeft', 'marginRight'],
9
+ ['start', 'end', 'left', 'right'],
10
+ ['borderStartWidth', 'borderEndWidth', 'borderLeftWidth', 'borderRightWidth'],
11
+ ['borderStartColor', 'borderEndColor', 'borderLeftColor', 'borderRightColor'],
12
+ ] as const
13
+
14
+ export type InlineFamily = (typeof INLINE_EDGES)[number]
@@ -0,0 +1,78 @@
1
+ type Style = Record<string, unknown>
2
+ type Shadow =
3
+ | string
4
+ | { value: string; parts: readonly (string | readonly [string, string])[] }
5
+ | null
6
+ export interface ShadowSlots {
7
+ outer?: Shadow
8
+ outerColor?: string | null
9
+ insetShadow?: Shadow
10
+ insetColor?: string | null
11
+ ringWidth?: number
12
+ ringColor?: string | null
13
+ insetRingWidth?: number
14
+ insetRingColor?: string | null
15
+ offsetWidth?: number
16
+ offsetColor?: string | null
17
+ ringInset?: boolean
18
+ }
19
+ type Spec = readonly [Style, ShadowSlots]
20
+ type Input = Style | number | false | null | undefined | readonly Input[]
21
+
22
+ function paint(shadow: Exclude<Shadow, null>, color: string | null | undefined) {
23
+ if (typeof shadow === 'string') return shadow
24
+ return color == null
25
+ ? shadow.value
26
+ : shadow.parts
27
+ .map((part) => (typeof part === 'string' ? part : part[0] + color + part[1]))
28
+ .join(', ')
29
+ }
30
+
31
+ export function hozoShadowStyles(
32
+ styles: readonly Input[],
33
+ specs: readonly Spec[] | ReadonlyMap<Style, ShadowSlots>,
34
+ defaults: { ring: string; offset: string },
35
+ candidates?: ReadonlyMap<Style, ShadowSlots>,
36
+ ): (Style | number)[] {
37
+ const metadata: ReadonlyMap<Style, ShadowSlots> = Array.isArray(specs)
38
+ ? new Map(specs)
39
+ : (specs as ReadonlyMap<Style, ShadowSlots>)
40
+ const result: (Style | number)[] = []
41
+ let state: ShadowSlots = {}
42
+ let opaque = false
43
+ function visit(style: Input): void {
44
+ if (!style) return
45
+ if (Array.isArray(style)) {
46
+ for (const part of style) visit(part)
47
+ return
48
+ }
49
+ if (typeof style === 'number') {
50
+ // Cannot inspect a registered style ID without importing RN.
51
+ opaque = true
52
+ result.push(style)
53
+ return
54
+ }
55
+ const object = style as Style
56
+ result.push(object)
57
+ const slots = metadata.get(object) ?? candidates?.get(object)
58
+ if (slots) Object.assign(state, slots)
59
+ else if ('boxShadow' in object) state = {}
60
+ }
61
+ for (const style of styles) visit(style)
62
+ if (opaque) return result
63
+
64
+ const layers: string[] = []
65
+ if (state.insetShadow != null) layers.push(paint(state.insetShadow, state.insetColor))
66
+ if (state.insetRingWidth !== undefined)
67
+ layers.push(`inset 0 0 0 ${state.insetRingWidth}px ${state.insetRingColor ?? defaults.ring}`)
68
+ const inset = state.ringInset ? 'inset ' : ''
69
+ if (state.offsetWidth !== undefined)
70
+ layers.push(`${inset}0 0 0 ${state.offsetWidth}px ${state.offsetColor ?? defaults.offset}`)
71
+ if (state.ringWidth !== undefined)
72
+ layers.push(
73
+ `${inset}0 0 0 ${state.ringWidth + (state.offsetWidth ?? 0)}px ${state.ringColor ?? defaults.ring}`,
74
+ )
75
+ if (state.outer != null) layers.push(paint(state.outer, state.outerColor))
76
+ if (layers.length || state.outer === null) result.push({ boxShadow: layers.join(', ') || 'none' })
77
+ return result
78
+ }
@@ -19,20 +19,26 @@ function resolve(value: StyleValue, state: unknown): StyleValue {
19
19
  return typeof value === 'function' ? (value as StyleFunction)(state) : value
20
20
  }
21
21
 
22
+ /** How a class style and the caller's are put together: in that order. */
23
+ export type SpreadCombine = (generated: StyleValue, author: StyleValue) => StyleValue
24
+
25
+ const inOrder: SpreadCombine = (generated, author) => [generated, author]
26
+
22
27
  /** Class styles first, the caller's after; a pressable's style function stays one. */
23
28
  function compose(
24
29
  compiled: StyleValue,
25
30
  own: StyleValue,
26
31
  normalize?: (value: StyleValue) => StyleValue,
32
+ combine: SpreadCombine = inOrder,
27
33
  ): StyleValue {
28
34
  if (typeof compiled === 'function' || typeof own === 'function') {
29
35
  return (state: unknown) => {
30
36
  const generated = resolve(compiled, state)
31
37
  const author = resolve(own, state)
32
- return [generated, normalize ? normalize(author) : author]
38
+ return combine(generated, normalize ? normalize(author) : author)
33
39
  }
34
40
  }
35
- return [compiled, normalize ? normalize(own) : own]
41
+ return combine(compiled, normalize ? normalize(own) : own)
36
42
  }
37
43
 
38
44
  /**
@@ -57,6 +63,7 @@ export function composeSpreadStyles(
57
63
  writtenLast: readonly string[],
58
64
  sources: readonly unknown[],
59
65
  normalize?: (value: StyleValue) => StyleValue,
66
+ combine?: SpreadCombine,
60
67
  ): Record<string, unknown> {
61
68
  let props: Record<string, unknown> = {}
62
69
  for (const source of sources) {
@@ -67,7 +74,7 @@ export function composeSpreadStyles(
67
74
  for (const [name, compiled] of Object.entries(styles)) {
68
75
  props[name] =
69
76
  !writtenLast.includes(name) && Object.hasOwn(props, name)
70
- ? compose(compiled, props[name], normalize)
77
+ ? compose(compiled, props[name], normalize, combine)
71
78
  : compiled
72
79
  }
73
80
  return props
@@ -20,12 +20,8 @@ export type HozoInheritedTextMetrics = Readonly<{
20
20
  textAlign?: string
21
21
  textTransform?: string
22
22
  }>
23
- // Opaque Native-generated paint transport. A null colour masks outer motion
24
- // while retaining the animated host boundary for an explicit local override.
25
- export type HozoInheritedTextMotion = Readonly<{ color: unknown }>
26
23
  export type HozoInheritedTextPaint = Readonly<{
27
24
  color?: string | number
28
- motion?: HozoInheritedTextMotion
29
25
  }>
30
26
  export type HozoInheritedTextFrame = Readonly<{
31
27
  metrics: HozoInheritedTextMetrics
@@ -33,7 +29,6 @@ export type HozoInheritedTextFrame = Readonly<{
33
29
  }>
34
30
  export type HozoInheritedTextOwn = HozoInheritedTextMetrics & Pick<HozoInheritedTextPaint, 'color'>
35
31
  const EMPTY = Object.freeze({})
36
- const MASKED_MOTION: HozoInheritedTextMotion = Object.freeze({ color: null })
37
32
  export const HozoInheritedMetricsContext = createContext<HozoInheritedTextMetrics>(EMPTY)
38
33
  export const HozoInheritedPaintContext = createContext<HozoInheritedTextPaint>(EMPTY)
39
34
 
@@ -47,8 +42,6 @@ export function useHozoInheritedTextFrame(own: HozoInheritedTextOwn): HozoInheri
47
42
  const textAlign = own.textAlign ?? inherited.textAlign
48
43
  const textTransform = own.textTransform ?? inherited.textTransform
49
44
  const color = own.color ?? inheritedPaint.color
50
- const motion =
51
- own.color == null ? inheritedPaint.motion : inheritedPaint.motion ? MASKED_MOTION : undefined
52
45
  // Local overrides can keep the effective record stable even when an outer
53
46
  // owner changes. Never mutate a record shared with an earlier commit.
54
47
  const metrics = useMemo<HozoInheritedTextMetrics>(() => {
@@ -62,14 +55,6 @@ export function useHozoInheritedTextFrame(own: HozoInheritedTextOwn): HozoInheri
62
55
  }).filter(([, value]) => value != null)
63
56
  return entries.length ? Object.freeze(Object.fromEntries(entries)) : EMPTY
64
57
  }, [fontSize, fontWeight, lineHeight, letterSpacing, textAlign, textTransform])
65
- const paint = useMemo(
66
- () =>
67
- motion
68
- ? Object.freeze({ ...(color == null ? {} : { color }), motion })
69
- : color == null
70
- ? EMPTY
71
- : Object.freeze({ color }),
72
- [color, motion],
73
- )
58
+ const paint = useMemo(() => (color == null ? EMPTY : Object.freeze({ color })), [color])
74
59
  return { metrics, paint }
75
60
  }
@@ -23,6 +23,8 @@ export type TransformStyle = Style | false | null | undefined | readonly Transfo
23
23
  export function hozoTransformStyles(
24
24
  styles: readonly TransformStyle[],
25
25
  specs: readonly TransformSpec[] | ReadonlyMap<Style, TransformSlots>,
26
+ candidates?: ReadonlyMap<Style, TransformSlots>,
27
+ preserveIdentities = false,
26
28
  ): Style[] {
27
29
  // Candidate maps can contain thousands of entries. They pre-index once;
28
30
  // resolving one class string must never walk the entire project map.
@@ -57,7 +59,7 @@ export function hozoTransformStyles(
57
59
  return
58
60
  }
59
61
  const object = style as Style
60
- const slots = metadata.get(object)
62
+ const slots = metadata.get(object) ?? candidates?.get(object)
61
63
  if (!slots) {
62
64
  result.push(object)
63
65
  // An authored RN transform is a whole-array override, not CSS slots.
@@ -73,8 +75,14 @@ export function hozoTransformStyles(
73
75
  }
74
76
  return
75
77
  }
76
- const { transform: _transform, ...rest } = object
77
- if (Object.keys(rest).length) result.push(rest)
78
+ if (preserveIdentities) {
79
+ // A deferred family (e.g. gradient stops) may also index this object.
80
+ // The final transform still wins without copying away that identity.
81
+ result.push(object)
82
+ } else {
83
+ const { transform: _transform, ...rest } = object
84
+ if (Object.keys(rest).length) result.push(rest)
85
+ }
78
86
  touched = true
79
87
  if (slots.rotate) rotate = slots.rotate
80
88
  if (slots.scale3d !== undefined) scale3d = slots.scale3d