@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,198 @@
|
|
|
1
|
+
export type GridTrack =
|
|
2
|
+
| { kind: 'fr'; value: number }
|
|
3
|
+
| { kind: 'points'; value: number }
|
|
4
|
+
| { kind: 'minmax'; min: number; value: number }
|
|
5
|
+
|
|
6
|
+
export interface GridCell {
|
|
7
|
+
child: number | null
|
|
8
|
+
tracks: readonly GridTrack[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface GridItemPlacement {
|
|
12
|
+
span: number
|
|
13
|
+
/** Zero-based explicit column; absent means normal row auto-placement. */
|
|
14
|
+
columnStart?: number
|
|
15
|
+
rowSpan?: number
|
|
16
|
+
/** Zero-based explicit row; absent means normal row auto-placement. */
|
|
17
|
+
rowStart?: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PlacedGridItem {
|
|
21
|
+
child: number
|
|
22
|
+
column: number
|
|
23
|
+
columnSpan: number
|
|
24
|
+
row: number
|
|
25
|
+
rowSpan: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function gridLayout(
|
|
29
|
+
items: readonly GridItemPlacement[],
|
|
30
|
+
columnCount: number,
|
|
31
|
+
): PlacedGridItem[] {
|
|
32
|
+
if (columnCount <= 0) return []
|
|
33
|
+
const occupied: boolean[][] = []
|
|
34
|
+
const placed: PlacedGridItem[] = []
|
|
35
|
+
let cursorRow = 0
|
|
36
|
+
let cursorColumn = 0
|
|
37
|
+
const free = (row: number, column: number, width: number, height: number) => {
|
|
38
|
+
for (let y = row; y < row + height; y += 1) {
|
|
39
|
+
for (let x = column; x < column + width; x += 1) {
|
|
40
|
+
if (occupied[y]?.[x]) return false
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return true
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
items.forEach((item, child) => {
|
|
47
|
+
const columnSpan = Math.min(columnCount, Math.max(1, Math.trunc(item.span)))
|
|
48
|
+
const rowSpan = Math.max(1, Math.trunc(item.rowSpan ?? 1))
|
|
49
|
+
let row = Math.max(0, Math.trunc(item.rowStart ?? cursorRow))
|
|
50
|
+
let column =
|
|
51
|
+
item.columnStart === undefined
|
|
52
|
+
? item.rowStart === undefined
|
|
53
|
+
? cursorColumn
|
|
54
|
+
: 0
|
|
55
|
+
: Math.min(columnCount - columnSpan, Math.max(0, Math.trunc(item.columnStart)))
|
|
56
|
+
|
|
57
|
+
while (column + columnSpan > columnCount || !free(row, column, columnSpan, rowSpan)) {
|
|
58
|
+
if (item.columnStart !== undefined) {
|
|
59
|
+
row += 1
|
|
60
|
+
} else {
|
|
61
|
+
column += 1
|
|
62
|
+
if (column + columnSpan > columnCount) {
|
|
63
|
+
row += 1
|
|
64
|
+
column = 0
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
for (let y = row; y < row + rowSpan; y += 1) {
|
|
69
|
+
const cells = (occupied[y] ??= [])
|
|
70
|
+
for (let x = column; x < column + columnSpan; x += 1) cells[x] = true
|
|
71
|
+
}
|
|
72
|
+
placed.push({ child, column, columnSpan, row, rowSpan })
|
|
73
|
+
cursorRow = row
|
|
74
|
+
cursorColumn = column + columnSpan
|
|
75
|
+
if (cursorColumn >= columnCount) {
|
|
76
|
+
cursorRow += 1
|
|
77
|
+
cursorColumn = 0
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
return placed
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Pure row auto-placement. Explicit coordinates/dense can replace this step later. */
|
|
84
|
+
export function gridRows(
|
|
85
|
+
items: readonly GridItemPlacement[],
|
|
86
|
+
tracks: readonly GridTrack[],
|
|
87
|
+
): GridCell[][] {
|
|
88
|
+
if (tracks.length === 0 || items.length === 0) return []
|
|
89
|
+
const layout = gridLayout(items, tracks.length)
|
|
90
|
+
const rowCount = Math.max(0, ...layout.map((item) => item.row + item.rowSpan))
|
|
91
|
+
const rows: GridCell[][] = Array.from({ length: rowCount }, () => [])
|
|
92
|
+
for (let row = 0; row < rowCount; row += 1) {
|
|
93
|
+
// Read once: `rows` was built with exactly `rowCount` entries, so this
|
|
94
|
+
// index is in range by construction.
|
|
95
|
+
const cells = rows[row]!
|
|
96
|
+
let column = 0
|
|
97
|
+
const starts = layout.filter((item) => item.row === row).sort((a, b) => a.column - b.column)
|
|
98
|
+
for (const item of starts) {
|
|
99
|
+
while (column < item.column) {
|
|
100
|
+
cells.push({ child: null, tracks: tracks.slice(column, column + 1) })
|
|
101
|
+
column += 1
|
|
102
|
+
}
|
|
103
|
+
cells.push({ child: item.child, tracks: tracks.slice(column, column + item.columnSpan) })
|
|
104
|
+
column += item.columnSpan
|
|
105
|
+
}
|
|
106
|
+
while (column < tracks.length) {
|
|
107
|
+
cells.push({ child: null, tracks: tracks.slice(column, column + 1) })
|
|
108
|
+
column += 1
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return rows
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function gridCellStyle(
|
|
115
|
+
tracks: readonly GridTrack[],
|
|
116
|
+
internalGap = 0,
|
|
117
|
+
): Record<string, number> {
|
|
118
|
+
const fr = tracks.reduce(
|
|
119
|
+
(sum, track) => sum + (track.kind === 'fr' || track.kind === 'minmax' ? track.value : 0),
|
|
120
|
+
0,
|
|
121
|
+
)
|
|
122
|
+
const points = tracks.reduce(
|
|
123
|
+
(sum, track) =>
|
|
124
|
+
sum + (track.kind === 'points' ? track.value : track.kind === 'minmax' ? track.min : 0),
|
|
125
|
+
Math.max(0, tracks.length - 1) * internalGap,
|
|
126
|
+
)
|
|
127
|
+
return { flexBasis: points, flexGrow: fr, flexShrink: fr > 0 ? 1 : 0 }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function gridTrackSizes(tracks: readonly GridTrack[], width: number, gap: number): number[] {
|
|
131
|
+
const fixed = tracks.reduce(
|
|
132
|
+
(sum, track) =>
|
|
133
|
+
sum + (track.kind === 'points' ? track.value : track.kind === 'minmax' ? track.min : 0),
|
|
134
|
+
0,
|
|
135
|
+
)
|
|
136
|
+
const fr = tracks.reduce(
|
|
137
|
+
(sum, track) => sum + (track.kind === 'fr' || track.kind === 'minmax' ? track.value : 0),
|
|
138
|
+
0,
|
|
139
|
+
)
|
|
140
|
+
const free = Math.max(0, width - fixed - Math.max(0, tracks.length - 1) * gap)
|
|
141
|
+
return tracks.map((track) => {
|
|
142
|
+
if (track.kind === 'points') return track.value
|
|
143
|
+
const share = fr === 0 ? 0 : (free * track.value) / fr
|
|
144
|
+
return track.kind === 'minmax' ? track.min + share : share
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function gridRowSizes(
|
|
149
|
+
layout: readonly PlacedGridItem[],
|
|
150
|
+
measuredHeights: readonly number[],
|
|
151
|
+
rowGap: number,
|
|
152
|
+
explicitTracks: readonly GridTrack[] = [],
|
|
153
|
+
): number[] {
|
|
154
|
+
const count = Math.max(explicitTracks.length, 0, ...layout.map((item) => item.row + item.rowSpan))
|
|
155
|
+
const rows = Array.from({ length: count }, (_, row) =>
|
|
156
|
+
explicitTracks[row]?.kind === 'points'
|
|
157
|
+
? explicitTracks[row].value
|
|
158
|
+
: explicitTracks[row]?.kind === 'minmax'
|
|
159
|
+
? explicitTracks[row].min
|
|
160
|
+
: 0,
|
|
161
|
+
)
|
|
162
|
+
for (const item of layout.filter((item) => item.rowSpan === 1)) {
|
|
163
|
+
const track = explicitTracks[item.row]
|
|
164
|
+
if (track?.kind === 'points') continue
|
|
165
|
+
const factor = track?.kind === 'fr' || track?.kind === 'minmax' ? track.value : 1
|
|
166
|
+
const intrinsic = Math.max(
|
|
167
|
+
0,
|
|
168
|
+
(measuredHeights[item.child] ?? 0) - (track?.kind === 'minmax' ? track.min : 0),
|
|
169
|
+
)
|
|
170
|
+
const unit = intrinsic / factor
|
|
171
|
+
if (track?.kind === 'fr' || track?.kind === 'minmax') {
|
|
172
|
+
for (let row = 0; row < explicitTracks.length; row += 1) {
|
|
173
|
+
const candidate = explicitTracks[row]!
|
|
174
|
+
if (candidate.kind === 'fr') rows[row] = Math.max(rows[row]!, unit * candidate.value)
|
|
175
|
+
if (candidate.kind === 'minmax') {
|
|
176
|
+
rows[row] = Math.max(rows[row]!, candidate.min + unit * candidate.value)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
rows[item.row] = Math.max(rows[item.row] ?? 0, measuredHeights[item.child] ?? 0)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
for (const item of [...layout].sort((a, b) => a.rowSpan - b.rowSpan)) {
|
|
184
|
+
if (item.rowSpan === 1) continue
|
|
185
|
+
const current =
|
|
186
|
+
rows.slice(item.row, item.row + item.rowSpan).reduce((a, b) => a + b, 0) +
|
|
187
|
+
(item.rowSpan - 1) * rowGap
|
|
188
|
+
const deficit = Math.max(0, (measuredHeights[item.child] ?? 0) - current)
|
|
189
|
+
const flexible = Array.from({ length: item.rowSpan }, (_, offset) => item.row + offset).filter(
|
|
190
|
+
(row) => explicitTracks[row]?.kind !== 'points',
|
|
191
|
+
)
|
|
192
|
+
const targets = flexible.length > 0 ? flexible : []
|
|
193
|
+
for (const row of targets) {
|
|
194
|
+
rows[row] = (rows[row] ?? 0) + deficit / targets.length
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return rows
|
|
198
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// When a measurement has moved, for a layout that feeds its own input.
|
|
2
|
+
//
|
|
3
|
+
// `HozoGrid` and `HozoContainer` both read a box back from `onLayout` and
|
|
4
|
+
// store it, and both then render something whose size depends on what they
|
|
5
|
+
// stored. That loop is fine while the number comes back the same. It does
|
|
6
|
+
// not always: `onLayout` reports floats, and 372.99998 and 373.00002 are
|
|
7
|
+
// different numbers to `===` and the same width to everyone else.
|
|
8
|
+
//
|
|
9
|
+
// The grid found this the hard way. On an emulator its screen never
|
|
10
|
+
// stopped drawing -- `uiautomator dump` could not find an idle window
|
|
11
|
+
// across six attempts and the frame counter climbed by about twenty-four a
|
|
12
|
+
// second with nobody touching it -- and a screenshot showed the screen
|
|
13
|
+
// half-built, stopped inside the grid. The guard existed on the heights it
|
|
14
|
+
// measured and not on the width, two adjacent lines apart.
|
|
15
|
+
//
|
|
16
|
+
// So the rule lives here rather than as a constant copied into each. Both
|
|
17
|
+
// components had their own idea of what "unchanged" meant, and one of them
|
|
18
|
+
// was wrong; a second copy of the fix would be the same bet again.
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* How far a measurement has to move before it counts as having moved.
|
|
22
|
+
*
|
|
23
|
+
* Half a device pixel: below anything a track size or a container query can
|
|
24
|
+
* express, and above the noise a Yoga pass produces.
|
|
25
|
+
*/
|
|
26
|
+
export const SETTLED = 0.5
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The value to keep, given what is stored and what was just measured.
|
|
30
|
+
*
|
|
31
|
+
* Returns `current` when the two are the same size, so React bails out of
|
|
32
|
+
* the render rather than starting the next lap. A tolerance rather than a
|
|
33
|
+
* lock: a rotation moves a width by hundreds of pixels and the layout still
|
|
34
|
+
* has to follow it.
|
|
35
|
+
*
|
|
36
|
+
* `undefined` and `NaN` both count as moved, which is what an unmeasured
|
|
37
|
+
* box should do -- it has no size yet, so any size is news.
|
|
38
|
+
*/
|
|
39
|
+
export function keepIfSettled(current: number | undefined, measured: number): number {
|
|
40
|
+
if (current === undefined) return measured
|
|
41
|
+
return Math.abs(current - measured) < SETTLED ? current : measured
|
|
42
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// The React glue for `./spacing.ts`. Thin on purpose -- everything worth
|
|
2
|
+
// testing is there, free of `react` imports; this file is the part that can
|
|
3
|
+
// only be verified by running an app, same division as `./hooks.native.ts`.
|
|
4
|
+
//
|
|
5
|
+
// Generated components render this; nothing here is meant to be written by
|
|
6
|
+
// hand.
|
|
7
|
+
|
|
8
|
+
import { Children, cloneElement, isValidElement, type ReactElement, type ReactNode } from 'react'
|
|
9
|
+
|
|
10
|
+
import { spacingTargets } from './runtime-spacing.ts'
|
|
11
|
+
|
|
12
|
+
interface Props {
|
|
13
|
+
/**
|
|
14
|
+
* The style each spaced child receives -- a `StyleSheet` entry the
|
|
15
|
+
* compiler generated from the parent's `space-*`/`divide-*` utilities.
|
|
16
|
+
*/
|
|
17
|
+
style: unknown
|
|
18
|
+
children?: ReactNode
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Applies a parent's `space-*`/`divide-*` to its children.
|
|
23
|
+
*
|
|
24
|
+
* Renders no host view: it returns its children rather than an element, so
|
|
25
|
+
* Yoga sees exactly the same tree it would without this in it, and the
|
|
26
|
+
* parent's flex layout is unchanged.
|
|
27
|
+
*
|
|
28
|
+
* The style is merged *behind* each child's own, matching the `:where()`
|
|
29
|
+
* wrapper the Web rule uses to hold specificity at zero -- a child's own
|
|
30
|
+
* `mt-8` beats the parent's `space-y-4` on both backends.
|
|
31
|
+
*
|
|
32
|
+
* Compile-time resolution isn't possible in general and that is why this
|
|
33
|
+
* exists: `{items.map(...)}` is one child as far as the compiler can see
|
|
34
|
+
* and any number of them at runtime, so the count that decides which child
|
|
35
|
+
* is last is only known here.
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* The parent spacing behind a child's own style, in whatever shape the
|
|
39
|
+
* child's style has.
|
|
40
|
+
*
|
|
41
|
+
* A Pressable's style can be a *callback*, and the compiler emits one for
|
|
42
|
+
* any element with a state variant -- `hover:`, `focus-visible:`,
|
|
43
|
+
* `active:`. Putting a function in an array does not compose it with
|
|
44
|
+
* anything: React Native flattens the array, finds a function where a
|
|
45
|
+
* style object belongs, and skips it. The child then renders with the
|
|
46
|
+
* spacing and nothing else -- no background, no padding, no radius.
|
|
47
|
+
*
|
|
48
|
+
* That is what made the acceptance screen's Continue button invisible
|
|
49
|
+
* (#357), and it was not the button. Six probes on a screen whose
|
|
50
|
+
* container uses `gap-*` all drew, including one copied prop for prop;
|
|
51
|
+
* the difference was the `space-y-4` on the container.
|
|
52
|
+
*
|
|
53
|
+
* So a callback stays a callback, with the spacing behind whatever it
|
|
54
|
+
* returns for the state it is called with.
|
|
55
|
+
*/
|
|
56
|
+
function spaced(parent: unknown, own: unknown): unknown {
|
|
57
|
+
if (typeof own !== 'function') return [parent, own]
|
|
58
|
+
const resolve = own as (state: unknown) => unknown
|
|
59
|
+
return (state: unknown) => [parent, resolve(state)]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function HozoSpaced({ style, children }: Props): ReactNode {
|
|
63
|
+
// `toArray` flattens nested arrays and drops null/undefined/booleans, so
|
|
64
|
+
// a conditional child that renders nothing doesn't take the last slot and
|
|
65
|
+
// silently swallow the spacing -- the same way an unrendered element
|
|
66
|
+
// wouldn't be `:last-child` on Web.
|
|
67
|
+
const list = Children.toArray(children)
|
|
68
|
+
const targets = new Set(spacingTargets(list, isValidElement))
|
|
69
|
+
|
|
70
|
+
return list.map((child, index) => {
|
|
71
|
+
if (!targets.has(index)) return child
|
|
72
|
+
const element = child as ReactElement<{ style?: unknown }>
|
|
73
|
+
return cloneElement(element, { style: spaced(style, element.props.style) })
|
|
74
|
+
})
|
|
75
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Which children `space-*` and `divide-*` reach.
|
|
2
|
+
//
|
|
3
|
+
// On Web these compile to `:where(.cls > :not(:last-child))` and the browser
|
|
4
|
+
// answers the question at match time. React Native has no selector engine,
|
|
5
|
+
// so the answer has to be computed -- and computed the same way, or the two
|
|
6
|
+
// backends lay out differently from the same source.
|
|
7
|
+
//
|
|
8
|
+
// The subtlety worth having a testable function for is what counts as a
|
|
9
|
+
// child. `:not(:last-child)` is an *element* selector: a text node at the
|
|
10
|
+
// end of the parent is not `:last-child`, so the last element still gets no
|
|
11
|
+
// margin. `React.Children.toArray` keeps strings in the array, so taking
|
|
12
|
+
// "everything but the final entry" would disagree with Web exactly when a
|
|
13
|
+
// component ends with a stray line of text -- which is easy to write and
|
|
14
|
+
// hard to notice.
|
|
15
|
+
//
|
|
16
|
+
// Kept free of `react` imports so it can be tested without one; the glue
|
|
17
|
+
// lives in `./spacing.native.tsx`.
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The indices of `children` that should receive the spacing style: every
|
|
21
|
+
* element except the last one.
|
|
22
|
+
*
|
|
23
|
+
* `isElement` is passed in rather than imported so this stays testable --
|
|
24
|
+
* it's `React.isValidElement` at the call site.
|
|
25
|
+
*/
|
|
26
|
+
export function spacingTargets(
|
|
27
|
+
children: readonly unknown[],
|
|
28
|
+
isElement: (child: unknown) => boolean,
|
|
29
|
+
): number[] {
|
|
30
|
+
const elements: number[] = []
|
|
31
|
+
for (const [index, child] of children.entries()) {
|
|
32
|
+
if (isElement(child)) elements.push(index)
|
|
33
|
+
}
|
|
34
|
+
// Nothing to space if there's one element or none: on Web the sole
|
|
35
|
+
// element *is* `:last-child` and matches nothing.
|
|
36
|
+
return elements.slice(0, -1)
|
|
37
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The size a relative one scales against, where the compiler could not
|
|
2
|
+
// resolve it.
|
|
3
|
+
//
|
|
4
|
+
// React Native offers nothing for this. Its `fontSize` is a number of
|
|
5
|
+
// points with no `em`, a nested `Text` inherits visually but cannot read
|
|
6
|
+
// what it inherited, and the only ancestor context the platform exposes --
|
|
7
|
+
// `TextAncestorContext` -- is a boolean saying whether there is a `Text`
|
|
8
|
+
// above at all.
|
|
9
|
+
//
|
|
10
|
+
// So Hozo resolves it, and prefers to do so at build time: a `<Small>`
|
|
11
|
+
// under a `text-xl` compiles to a plain `<Text>` with a number in a
|
|
12
|
+
// stylesheet, and neither of these components appears. They are for the
|
|
13
|
+
// case the compiler cannot see -- an author's `style={{ fontSize: 20 }}`
|
|
14
|
+
// or a `{...spread}`, which is a size that exists only once React Native
|
|
15
|
+
// has evaluated it.
|
|
16
|
+
//
|
|
17
|
+
// The same pattern `HozoGrid` and `HozoSpaced` already follow: static
|
|
18
|
+
// where the answer is knowable, a component where it is not.
|
|
19
|
+
|
|
20
|
+
import { HozoTextSizeContext } from '@hozo/engine'
|
|
21
|
+
import { type ReactNode, useContext } from 'react'
|
|
22
|
+
import { type StyleProp, StyleSheet, Text, type TextProps, type TextStyle } from 'react-native'
|
|
23
|
+
|
|
24
|
+
export interface HozoTextSizeProps extends Omit<TextProps, 'style'> {
|
|
25
|
+
children?: ReactNode
|
|
26
|
+
style?: StyleProp<TextStyle>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A `Text` that says what size it resolved to.
|
|
31
|
+
*
|
|
32
|
+
* Emitted for an element whose style the compiler could not read and
|
|
33
|
+
* which has something below it that scales against the size. `flatten`
|
|
34
|
+
* is what React Native itself does with a style array, so this reads the
|
|
35
|
+
* value that applied rather than one of the values that contributed.
|
|
36
|
+
*/
|
|
37
|
+
export function HozoTextSize({ children, style, ...props }: HozoTextSizeProps) {
|
|
38
|
+
const inherited = useContext(HozoTextSizeContext)
|
|
39
|
+
const size = StyleSheet.flatten(style)?.fontSize
|
|
40
|
+
const resolved = typeof size === 'number' ? size : inherited
|
|
41
|
+
return (
|
|
42
|
+
<HozoTextSizeContext.Provider value={resolved}>
|
|
43
|
+
<Text style={style} {...props}>
|
|
44
|
+
{children}
|
|
45
|
+
</Text>
|
|
46
|
+
</HozoTextSizeContext.Provider>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface HozoRelativeTextProps extends HozoTextSizeProps {
|
|
51
|
+
/** The multiplier, from the compiler's own table. */
|
|
52
|
+
hozoRelative: number
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A `Text` sized as a fraction of the text around it.
|
|
57
|
+
*
|
|
58
|
+
* Publishes what it resolved to as well as reading it, so `<Small>`
|
|
59
|
+
* inside a `<Small>` compounds the way `small small` does in a browser.
|
|
60
|
+
* An explicit size in `style` still wins: it is later in the array, which
|
|
61
|
+
* is how React Native resolves the two.
|
|
62
|
+
*/
|
|
63
|
+
export function HozoRelativeText({
|
|
64
|
+
children,
|
|
65
|
+
hozoRelative,
|
|
66
|
+
style,
|
|
67
|
+
...props
|
|
68
|
+
}: HozoRelativeTextProps) {
|
|
69
|
+
const base = useContext(HozoTextSizeContext)
|
|
70
|
+
const resolved = Math.round(base * hozoRelative)
|
|
71
|
+
const flattened = StyleSheet.flatten(style)?.fontSize
|
|
72
|
+
return (
|
|
73
|
+
<HozoTextSizeContext.Provider value={typeof flattened === 'number' ? flattened : resolved}>
|
|
74
|
+
<Text style={[{ fontSize: resolved }, style]} {...props}>
|
|
75
|
+
{children}
|
|
76
|
+
</Text>
|
|
77
|
+
</HozoTextSizeContext.Provider>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// A transition driven by the style changing, rather than by an
|
|
2
|
+
// interaction.
|
|
3
|
+
//
|
|
4
|
+
// `./pressable.native.tsx` already animates, and it is built the other way
|
|
5
|
+
// round: it knows the four interaction booleans, calls the author's `style`
|
|
6
|
+
// function for each combination, and animates between the results. That
|
|
7
|
+
// shape is right there and wrong here, because the conditions this has to
|
|
8
|
+
// serve are ambient -- `dark:`, `md:`, `motion-reduce:` -- and the
|
|
9
|
+
// compiler expresses those as a guarded array, `[base, __hozoDark &&
|
|
10
|
+
// dark]`, which is already resolved by the time it arrives.
|
|
11
|
+
//
|
|
12
|
+
// So this one asks a simpler question: what did the style just become, and
|
|
13
|
+
// what was it before? Everything that differs and can be interpolated is
|
|
14
|
+
// animated to its new value; everything else is passed straight through.
|
|
15
|
+
// That covers the interaction case too, in principle, which is why this is
|
|
16
|
+
// not named after the conditions that prompted it.
|
|
17
|
+
//
|
|
18
|
+
// Colours run on the JavaScript driver and opacity and transforms do not.
|
|
19
|
+
// React Native cannot interpolate a colour natively, so mixing the two in
|
|
20
|
+
// one `Animated.timing` would drag the fast ones onto the slow driver.
|
|
21
|
+
// They are separate animations for that reason and not for tidiness.
|
|
22
|
+
|
|
23
|
+
import { type ReactNode, useEffect, useMemo, useRef } from 'react'
|
|
24
|
+
import { Animated, Easing, type StyleProp, StyleSheet, type ViewStyle } from 'react-native'
|
|
25
|
+
|
|
26
|
+
export interface HozoTransitionSpec {
|
|
27
|
+
duration: number
|
|
28
|
+
delay?: number
|
|
29
|
+
easing: 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The colour properties worth animating, in the order they are read. */
|
|
33
|
+
const COLOR_KEYS = ['backgroundColor', 'borderColor', 'color'] as const
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The transform entries, flattened to `{ key: value }`.
|
|
37
|
+
*
|
|
38
|
+
* React Native's `transform` is an array of one-key objects, which is a
|
|
39
|
+
* shape nothing can be diffed against directly -- two arrays with the same
|
|
40
|
+
* entries in a different order are the same transform and not equal.
|
|
41
|
+
*/
|
|
42
|
+
function transformMap(style: ViewStyle | undefined): Map<string, number> {
|
|
43
|
+
const out = new Map<string, number>()
|
|
44
|
+
const transform = style?.transform
|
|
45
|
+
if (!Array.isArray(transform)) return out
|
|
46
|
+
for (const entry of transform) {
|
|
47
|
+
for (const [key, value] of Object.entries(entry as Record<string, unknown>)) {
|
|
48
|
+
if (typeof value === 'number') out.set(key, value)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return out
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** What a transform key means when nothing sets it. */
|
|
55
|
+
function identity(key: string): number {
|
|
56
|
+
return key.startsWith('scale') ? 1 : 0
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface HozoAnimatedProps {
|
|
60
|
+
style?: StyleProp<ViewStyle>
|
|
61
|
+
hozoTransition?: HozoTransitionSpec
|
|
62
|
+
children?: ReactNode
|
|
63
|
+
[key: string]: unknown
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function HozoAnimated({ style, hozoTransition, children, ...props }: HozoAnimatedProps) {
|
|
67
|
+
const flat = useMemo(() => StyleSheet.flatten(style) ?? {}, [style])
|
|
68
|
+
|
|
69
|
+
// One progress value per animation rather than one per property. The
|
|
70
|
+
// properties change together -- they are the same rule flipping -- so
|
|
71
|
+
// animating a single 0 → 1 and interpolating each property from its old
|
|
72
|
+
// value to its new one is both cheaper and impossible to get out of
|
|
73
|
+
// step with itself.
|
|
74
|
+
const progress = useRef(new Animated.Value(1)).current
|
|
75
|
+
const from = useRef<ViewStyle>(flat)
|
|
76
|
+
const to = useRef<ViewStyle>(flat)
|
|
77
|
+
|
|
78
|
+
// What changed, computed during render so the effect below has nothing
|
|
79
|
+
// to work out. `JSON.stringify` on the two style objects is enough: they
|
|
80
|
+
// are flat, their values are primitives, and the compiler emits the keys
|
|
81
|
+
// in a stable order.
|
|
82
|
+
const signature = JSON.stringify(flat)
|
|
83
|
+
const previous = useRef(signature)
|
|
84
|
+
if (previous.current !== signature) {
|
|
85
|
+
// The *visible* style becomes the new starting point, not the
|
|
86
|
+
// previous target. Interrupting a half-finished fade and restarting
|
|
87
|
+
// from where it was going would make it jump backwards first.
|
|
88
|
+
from.current = to.current
|
|
89
|
+
to.current = flat
|
|
90
|
+
previous.current = signature
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// `signature` is read above rather than inside, and it is what says the
|
|
94
|
+
// style moved -- `hozoTransition` does not change when only the values
|
|
95
|
+
// do. Without it a style change starts no transition at all, which is
|
|
96
|
+
// the whole feature. `biome check --unsafe` offers to remove it.
|
|
97
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: it is what says the style changed
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (!hozoTransition) return
|
|
100
|
+
progress.setValue(0)
|
|
101
|
+
const animation = Animated.timing(progress, {
|
|
102
|
+
toValue: 1,
|
|
103
|
+
duration: hozoTransition.duration,
|
|
104
|
+
delay: hozoTransition.delay ?? 0,
|
|
105
|
+
easing: easingFor(hozoTransition.easing),
|
|
106
|
+
// Colours are in here too, and React Native cannot interpolate one
|
|
107
|
+
// on the native driver. One animation on the JavaScript driver is
|
|
108
|
+
// the honest choice: two drivers running the same rule would drift
|
|
109
|
+
// against each other on a busy frame, and a background that arrives
|
|
110
|
+
// before the text that sits on it is worse than both arriving late.
|
|
111
|
+
useNativeDriver: false,
|
|
112
|
+
})
|
|
113
|
+
animation.start()
|
|
114
|
+
return () => animation.stop()
|
|
115
|
+
}, [progress, signature, hozoTransition])
|
|
116
|
+
|
|
117
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: as above -- `signature` is what changed
|
|
118
|
+
const animated = useMemo(() => {
|
|
119
|
+
if (!hozoTransition) return flat
|
|
120
|
+
const overrides: Record<string, unknown> = {}
|
|
121
|
+
|
|
122
|
+
if (typeof from.current.opacity === 'number' || typeof flat.opacity === 'number') {
|
|
123
|
+
const start = typeof from.current.opacity === 'number' ? from.current.opacity : 1
|
|
124
|
+
const end = typeof flat.opacity === 'number' ? flat.opacity : 1
|
|
125
|
+
if (start !== end) {
|
|
126
|
+
overrides.opacity = progress.interpolate({ inputRange: [0, 1], outputRange: [start, end] })
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const key of COLOR_KEYS) {
|
|
131
|
+
// Read through a record: `color` lives on `TextStyle` and the rest
|
|
132
|
+
// on `ViewStyle`, and a component that may be either has to look at
|
|
133
|
+
// both. The runtime check below is what makes it safe.
|
|
134
|
+
const start = (from.current as Record<string, unknown>)[key]
|
|
135
|
+
const end = (flat as Record<string, unknown>)[key]
|
|
136
|
+
if (typeof start !== 'string' || typeof end !== 'string' || start === end) continue
|
|
137
|
+
overrides[key] = progress.interpolate({ inputRange: [0, 1], outputRange: [start, end] })
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const before = transformMap(from.current)
|
|
141
|
+
const after = transformMap(flat)
|
|
142
|
+
const keys = [...new Set([...before.keys(), ...after.keys()])]
|
|
143
|
+
const moved = keys.filter(
|
|
144
|
+
(key) => (before.get(key) ?? identity(key)) !== (after.get(key) ?? identity(key)),
|
|
145
|
+
)
|
|
146
|
+
if (moved.length > 0) {
|
|
147
|
+
// Every key, not only the moved ones: `transform` is replaced
|
|
148
|
+
// wholesale, so leaving one out would reset it to identity halfway
|
|
149
|
+
// through the animation of its neighbour.
|
|
150
|
+
overrides.transform = keys.map((key) => ({
|
|
151
|
+
[key]: progress.interpolate({
|
|
152
|
+
inputRange: [0, 1],
|
|
153
|
+
outputRange: [before.get(key) ?? identity(key), after.get(key) ?? identity(key)],
|
|
154
|
+
}),
|
|
155
|
+
}))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return [flat, overrides]
|
|
159
|
+
}, [flat, hozoTransition, progress, signature])
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<Animated.View style={animated as StyleProp<ViewStyle>} {...props}>
|
|
163
|
+
{children}
|
|
164
|
+
</Animated.View>
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function easingFor(name: HozoTransitionSpec['easing']) {
|
|
169
|
+
// Built here rather than in a module-level table, for the reason
|
|
170
|
+
// `hooks.native.ts` does the same: a table calls `Easing.bezier` on
|
|
171
|
+
// import, for every project that loads the package and never
|
|
172
|
+
// transitions. The import itself is free; the call is not.
|
|
173
|
+
switch (name) {
|
|
174
|
+
case 'linear':
|
|
175
|
+
return Easing.linear
|
|
176
|
+
case 'ease-in':
|
|
177
|
+
return Easing.bezier(0.4, 0, 1, 1)
|
|
178
|
+
case 'ease-out':
|
|
179
|
+
return Easing.bezier(0, 0, 0.2, 1)
|
|
180
|
+
default:
|
|
181
|
+
return Easing.bezier(0.4, 0, 0.2, 1)
|
|
182
|
+
}
|
|
183
|
+
}
|