@hozo/primitives 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.
- package/CHANGELOG.md +42 -0
- package/README.md +24 -2
- package/dist/flat-list.native.d.ts +8 -8
- package/dist/flat-list.native.d.ts.map +1 -1
- package/dist/flat-list.native.js +55 -25
- package/dist/flat-list.native.js.map +1 -1
- package/dist/foundation.d.ts +7 -4
- package/dist/foundation.d.ts.map +1 -1
- package/dist/foundation.js +33 -16
- package/dist/foundation.js.map +1 -1
- package/dist/foundation.native.d.ts +16 -2
- package/dist/foundation.native.d.ts.map +1 -1
- package/dist/foundation.native.js.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -9
- package/dist/index.js.map +1 -1
- package/dist/index.native.d.ts +1 -0
- package/dist/index.native.d.ts.map +1 -1
- package/dist/index.native.js +1 -0
- package/dist/index.native.js.map +1 -1
- package/dist/link.native.d.ts +16 -7
- package/dist/link.native.d.ts.map +1 -1
- package/dist/link.native.js +9 -5
- package/dist/link.native.js.map +1 -1
- package/dist/presence-context.d.ts +15 -0
- package/dist/presence-context.d.ts.map +1 -0
- package/dist/presence-context.js +11 -0
- package/dist/presence-context.js.map +1 -0
- package/dist/presence.d.ts +12 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +45 -0
- package/dist/presence.js.map +1 -0
- package/dist/presence.native.d.ts +8 -0
- package/dist/presence.native.d.ts.map +1 -0
- package/dist/presence.native.js +66 -0
- package/dist/presence.native.js.map +1 -0
- package/dist/pressable.d.ts +9 -2
- package/dist/pressable.d.ts.map +1 -1
- package/dist/pressable.js +9 -6
- package/dist/pressable.js.map +1 -1
- package/dist/pressable.native.d.ts.map +1 -1
- package/dist/pressable.native.js +181 -195
- package/dist/pressable.native.js.map +1 -1
- package/dist/runtime-container.native.d.ts +11 -9
- package/dist/runtime-container.native.d.ts.map +1 -1
- package/dist/runtime-container.native.js +8 -6
- package/dist/runtime-container.native.js.map +1 -1
- package/dist/runtime-transition.native.d.ts +16 -1
- package/dist/runtime-transition.native.d.ts.map +1 -1
- package/dist/runtime-transition.native.js +34 -7
- package/dist/runtime-transition.native.js.map +1 -1
- package/dist/text-input.d.ts +319 -3
- package/dist/text-input.d.ts.map +1 -1
- package/dist/text-input.js +4 -3
- package/dist/text-input.js.map +1 -1
- package/dist/view.d.ts +11 -2
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +7 -6
- package/dist/view.js.map +1 -1
- package/package.json +4 -3
- package/src/flat-list.native.tsx +69 -47
- package/src/foundation.native.tsx +16 -1
- package/src/foundation.tsx +66 -25
- package/src/index.native.ts +2 -0
- package/src/index.ts +209 -219
- package/src/link.native.tsx +29 -18
- package/src/presence-context.ts +27 -0
- package/src/presence.native.tsx +81 -0
- package/src/presence.tsx +55 -0
- package/src/pressable.native.tsx +251 -243
- package/src/pressable.ts +92 -79
- package/src/runtime-container.native.tsx +30 -15
- package/src/runtime-transition.native.tsx +68 -7
- package/src/text-input.ts +74 -72
- package/src/view.ts +76 -61
package/src/index.ts
CHANGED
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
} from '@hozo/engine'
|
|
7
7
|
import {
|
|
8
8
|
createElement,
|
|
9
|
-
forwardRef,
|
|
10
9
|
type HTMLAttributes,
|
|
11
10
|
isValidElement,
|
|
12
11
|
type ReactElement,
|
|
@@ -31,6 +30,7 @@ import {
|
|
|
31
30
|
} from './windowing.ts'
|
|
32
31
|
|
|
33
32
|
export * from './foundation.tsx'
|
|
33
|
+
export { Presence, type PresenceProps } from './presence.tsx'
|
|
34
34
|
|
|
35
35
|
export interface HozoScrollEvent {
|
|
36
36
|
nativeEvent: {
|
|
@@ -183,223 +183,216 @@ function useMeasurements(
|
|
|
183
183
|
}, [rootRef, contentRef])
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
export interface HozoScrollViewProps extends SharedScrollableProps {
|
|
186
|
+
export interface HozoScrollViewProps extends SharedScrollableProps {
|
|
187
|
+
/** Declared here, because React 19 takes `ref` as an ordinary prop. */
|
|
188
|
+
ref?: Ref<HTMLDivElement>
|
|
189
|
+
}
|
|
187
190
|
|
|
188
|
-
export
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
191
|
+
export function HozoScrollView({
|
|
192
|
+
ref: forwardedRef,
|
|
193
|
+
children,
|
|
194
|
+
className,
|
|
195
|
+
style,
|
|
196
|
+
contentContainerStyle,
|
|
197
|
+
horizontal,
|
|
198
|
+
refreshing,
|
|
199
|
+
onRefresh,
|
|
200
|
+
refreshControl,
|
|
201
|
+
showsVerticalScrollIndicator = true,
|
|
202
|
+
showsHorizontalScrollIndicator = true,
|
|
203
|
+
onScroll,
|
|
204
|
+
scrollEventThrottle = 0,
|
|
205
|
+
onScrollBeginDrag,
|
|
206
|
+
onScrollEndDrag,
|
|
207
|
+
onMomentumScrollEnd,
|
|
208
|
+
onContentSizeChange,
|
|
209
|
+
onLayout,
|
|
210
|
+
testID,
|
|
211
|
+
nativeID,
|
|
212
|
+
pointerEvents,
|
|
213
|
+
accessibilityLabel,
|
|
214
|
+
accessibilityHint,
|
|
215
|
+
accessibilityRole,
|
|
216
|
+
accessibilityState,
|
|
217
|
+
accessibilityValue,
|
|
218
|
+
accessibilityLiveRegion,
|
|
219
|
+
keyboardShouldPersistTaps: _keyboardShouldPersistTaps,
|
|
220
|
+
stickyHeaderIndices: _stickyHeaderIndices,
|
|
221
|
+
contentInsetAdjustmentBehavior: _contentInsetAdjustmentBehavior,
|
|
222
|
+
automaticallyAdjustKeyboardInsets: _automaticallyAdjustKeyboardInsets,
|
|
223
|
+
bounces: _bounces,
|
|
224
|
+
pagingEnabled: _pagingEnabled,
|
|
225
|
+
directionalLockEnabled: _directionalLockEnabled,
|
|
226
|
+
nestedScrollEnabled: _nestedScrollEnabled,
|
|
227
|
+
alwaysBounceVertical: _alwaysBounceVertical,
|
|
228
|
+
overScrollMode: _overScrollMode,
|
|
229
|
+
snapToOffsets: _snapToOffsets,
|
|
230
|
+
snapToEnd: _snapToEnd,
|
|
231
|
+
onPointerDown,
|
|
232
|
+
onPointerDownCapture,
|
|
233
|
+
onPointerMove,
|
|
234
|
+
onPointerMoveCapture,
|
|
235
|
+
onPointerUp,
|
|
236
|
+
onPointerCancel,
|
|
237
|
+
onLostPointerCapture,
|
|
238
|
+
onStartShouldSetResponder,
|
|
239
|
+
onStartShouldSetResponderCapture,
|
|
240
|
+
onMoveShouldSetResponder,
|
|
241
|
+
onMoveShouldSetResponderCapture,
|
|
242
|
+
onResponderGrant,
|
|
243
|
+
onResponderStart,
|
|
244
|
+
onResponderMove,
|
|
245
|
+
onResponderEnd,
|
|
246
|
+
onResponderRelease,
|
|
247
|
+
onResponderReject,
|
|
248
|
+
onResponderTerminate,
|
|
249
|
+
onResponderTerminationRequest,
|
|
250
|
+
...domProps
|
|
251
|
+
}: HozoScrollViewProps): ReactElement {
|
|
252
|
+
const rootRef = useRef<HTMLDivElement>(null)
|
|
253
|
+
const contentRef = useRef<HTMLDivElement>(null)
|
|
254
|
+
const lastScroll = useRef(0)
|
|
255
|
+
const setRoot = useCallback(
|
|
256
|
+
(element: HTMLDivElement | null) => {
|
|
257
|
+
rootRef.current = element
|
|
258
|
+
assignRef(forwardedRef, element)
|
|
249
259
|
},
|
|
250
|
-
forwardedRef,
|
|
251
|
-
)
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
)
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const activeRefresh = onRefresh ?? nestedRefresh?.onRefresh
|
|
286
|
-
const hideScrollbar = horizontal
|
|
287
|
-
? !showsHorizontalScrollIndicator
|
|
288
|
-
: !showsVerticalScrollIndicator
|
|
289
|
-
const viewport = horizontal
|
|
290
|
-
? { overflowX: 'auto' as const, overflowY: 'hidden' as const }
|
|
291
|
-
: { overflowX: 'hidden' as const, overflowY: 'auto' as const }
|
|
292
|
-
const emitScroll = () => (rootRef.current ? nativeScrollEvent(rootRef.current) : undefined)
|
|
293
|
-
const emitTo = (callback: ((event: HozoScrollEvent) => void) | undefined) => {
|
|
294
|
-
const event = emitScroll()
|
|
295
|
-
if (event) callback?.(event)
|
|
296
|
-
}
|
|
260
|
+
[forwardedRef],
|
|
261
|
+
)
|
|
262
|
+
useMeasurements(rootRef, contentRef, onLayout, onContentSizeChange)
|
|
263
|
+
const responderProps = {
|
|
264
|
+
onStartShouldSetResponder,
|
|
265
|
+
onStartShouldSetResponderCapture,
|
|
266
|
+
onMoveShouldSetResponder,
|
|
267
|
+
onMoveShouldSetResponderCapture,
|
|
268
|
+
onResponderGrant,
|
|
269
|
+
onResponderStart,
|
|
270
|
+
onResponderMove,
|
|
271
|
+
onResponderEnd,
|
|
272
|
+
onResponderRelease,
|
|
273
|
+
onResponderReject,
|
|
274
|
+
onResponderTerminate,
|
|
275
|
+
onResponderTerminationRequest,
|
|
276
|
+
}
|
|
277
|
+
const hasResponderContract = Object.values(responderProps).some(Boolean)
|
|
278
|
+
const responder = useResponderDomProps(
|
|
279
|
+
rootRef,
|
|
280
|
+
responderProps,
|
|
281
|
+
hasResponderContract && accessibilityState?.disabled !== true,
|
|
282
|
+
)
|
|
283
|
+
const nestedRefresh = refreshProps(refreshControl)
|
|
284
|
+
const activeRefreshing = refreshing ?? nestedRefresh?.refreshing ?? false
|
|
285
|
+
const activeRefresh = onRefresh ?? nestedRefresh?.onRefresh
|
|
286
|
+
const hideScrollbar = horizontal ? !showsHorizontalScrollIndicator : !showsVerticalScrollIndicator
|
|
287
|
+
const viewport = horizontal
|
|
288
|
+
? { overflowX: 'auto' as const, overflowY: 'hidden' as const }
|
|
289
|
+
: { overflowX: 'hidden' as const, overflowY: 'auto' as const }
|
|
290
|
+
const emitScroll = () => (rootRef.current ? nativeScrollEvent(rootRef.current) : undefined)
|
|
291
|
+
const emitTo = (callback: ((event: HozoScrollEvent) => void) | undefined) => {
|
|
292
|
+
const event = emitScroll()
|
|
293
|
+
if (event) callback?.(event)
|
|
294
|
+
}
|
|
297
295
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
},
|
|
387
|
-
onScrollEnd: onMomentumScrollEnd ? () => emitTo(onMomentumScrollEnd) : undefined,
|
|
296
|
+
return createElement(
|
|
297
|
+
'div',
|
|
298
|
+
{
|
|
299
|
+
...domProps,
|
|
300
|
+
ref: setRoot,
|
|
301
|
+
className,
|
|
302
|
+
id: nativeID,
|
|
303
|
+
'data-testid': testID,
|
|
304
|
+
role: accessibilityRole ?? domProps.role,
|
|
305
|
+
'data-hozo-pointer-events': pointerEvents,
|
|
306
|
+
'data-hozo-disabled': accessibilityState?.disabled ? '' : undefined,
|
|
307
|
+
'data-hozo-horizontal': horizontal ? '' : undefined,
|
|
308
|
+
'data-hozo-hide-scrollbar': hideScrollbar ? '' : undefined,
|
|
309
|
+
'aria-label': accessibilityLabel,
|
|
310
|
+
'aria-description': accessibilityHint,
|
|
311
|
+
'aria-disabled': accessibilityState?.disabled,
|
|
312
|
+
'aria-selected': accessibilityState?.selected,
|
|
313
|
+
'aria-checked': accessibilityState?.checked,
|
|
314
|
+
'aria-busy': accessibilityState?.busy || activeRefreshing || undefined,
|
|
315
|
+
'aria-expanded': accessibilityState?.expanded,
|
|
316
|
+
'aria-valuemin': accessibilityValue?.min,
|
|
317
|
+
'aria-valuemax': accessibilityValue?.max,
|
|
318
|
+
'aria-valuenow': accessibilityValue?.now,
|
|
319
|
+
'aria-valuetext': accessibilityValue?.text,
|
|
320
|
+
'aria-live': accessibilityLiveRegion === 'none' ? undefined : accessibilityLiveRegion,
|
|
321
|
+
style: { ...viewport, ...hozoDomStyle(style) },
|
|
322
|
+
onPointerDown:
|
|
323
|
+
onScrollBeginDrag || responder.onPointerDown || onPointerDown
|
|
324
|
+
? (event) => {
|
|
325
|
+
emitTo(onScrollBeginDrag)
|
|
326
|
+
responder.onPointerDown?.(event)
|
|
327
|
+
onPointerDown?.(event)
|
|
328
|
+
}
|
|
329
|
+
: undefined,
|
|
330
|
+
onPointerDownCapture:
|
|
331
|
+
responder.onPointerDownCapture || onPointerDownCapture
|
|
332
|
+
? (event) => {
|
|
333
|
+
responder.onPointerDownCapture?.(event)
|
|
334
|
+
onPointerDownCapture?.(event)
|
|
335
|
+
}
|
|
336
|
+
: undefined,
|
|
337
|
+
onPointerMove:
|
|
338
|
+
responder.onPointerMove || onPointerMove
|
|
339
|
+
? (event) => {
|
|
340
|
+
responder.onPointerMove?.(event)
|
|
341
|
+
onPointerMove?.(event)
|
|
342
|
+
}
|
|
343
|
+
: undefined,
|
|
344
|
+
onPointerMoveCapture:
|
|
345
|
+
responder.onPointerMoveCapture || onPointerMoveCapture
|
|
346
|
+
? (event) => {
|
|
347
|
+
responder.onPointerMoveCapture?.(event)
|
|
348
|
+
onPointerMoveCapture?.(event)
|
|
349
|
+
}
|
|
350
|
+
: undefined,
|
|
351
|
+
onPointerUp:
|
|
352
|
+
onScrollEndDrag || responder.onPointerUp || onPointerUp
|
|
353
|
+
? (event) => {
|
|
354
|
+
emitTo(onScrollEndDrag)
|
|
355
|
+
responder.onPointerUp?.(event)
|
|
356
|
+
onPointerUp?.(event)
|
|
357
|
+
}
|
|
358
|
+
: undefined,
|
|
359
|
+
onPointerCancel:
|
|
360
|
+
responder.onPointerCancel || onPointerCancel
|
|
361
|
+
? (event) => {
|
|
362
|
+
responder.onPointerCancel?.(event)
|
|
363
|
+
onPointerCancel?.(event)
|
|
364
|
+
}
|
|
365
|
+
: undefined,
|
|
366
|
+
onLostPointerCapture:
|
|
367
|
+
responder.onLostPointerCapture || onLostPointerCapture
|
|
368
|
+
? (event) => {
|
|
369
|
+
responder.onLostPointerCapture?.(event)
|
|
370
|
+
onLostPointerCapture?.(event)
|
|
371
|
+
}
|
|
372
|
+
: undefined,
|
|
373
|
+
onScroll: () => {
|
|
374
|
+
const now = Date.now()
|
|
375
|
+
const event = emitScroll()
|
|
376
|
+
if (!event) return
|
|
377
|
+
if (
|
|
378
|
+
onScroll &&
|
|
379
|
+
(scrollEventThrottle <= 0 || now - lastScroll.current >= scrollEventThrottle)
|
|
380
|
+
) {
|
|
381
|
+
lastScroll.current = now
|
|
382
|
+
onScroll(event)
|
|
383
|
+
}
|
|
388
384
|
},
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
)
|
|
401
|
-
},
|
|
402
|
-
)
|
|
385
|
+
onScrollEnd: onMomentumScrollEnd ? () => emitTo(onMomentumScrollEnd) : undefined,
|
|
386
|
+
},
|
|
387
|
+
activeRefresh
|
|
388
|
+
? createElement(HozoRefreshControl, {
|
|
389
|
+
refreshing: activeRefreshing,
|
|
390
|
+
onRefresh: activeRefresh,
|
|
391
|
+
})
|
|
392
|
+
: null,
|
|
393
|
+
createElement('div', { ref: contentRef, style: hozoDomStyle(contentContainerStyle) }, children),
|
|
394
|
+
)
|
|
395
|
+
}
|
|
403
396
|
|
|
404
397
|
export interface HozoFlatListRenderInfo<T> {
|
|
405
398
|
item: T
|
|
@@ -489,8 +482,9 @@ function invertedTransform(horizontal: boolean | undefined) {
|
|
|
489
482
|
return horizontal ? [{ scaleX: -1 }] : [{ scaleY: -1 }]
|
|
490
483
|
}
|
|
491
484
|
|
|
492
|
-
function
|
|
485
|
+
export function HozoFlatList<T>(props: HozoFlatListProps<T> & { ref?: Ref<HozoFlatListHandle> }) {
|
|
493
486
|
const {
|
|
487
|
+
ref: forwardedRef,
|
|
494
488
|
data = [],
|
|
495
489
|
renderItem,
|
|
496
490
|
keyExtractor,
|
|
@@ -1101,10 +1095,6 @@ function HozoFlatListInner<T>(props: HozoFlatListProps<T>, forwardedRef: Ref<Hoz
|
|
|
1101
1095
|
)
|
|
1102
1096
|
}
|
|
1103
1097
|
|
|
1104
|
-
export const HozoFlatList = forwardRef(HozoFlatListInner) as <T>(
|
|
1105
|
-
props: HozoFlatListProps<T> & { ref?: Ref<HozoFlatListHandle> },
|
|
1106
|
-
) => ReactElement
|
|
1107
|
-
|
|
1108
1098
|
export { HozoFlatList as FlatList, HozoScrollView as ScrollView }
|
|
1109
1099
|
export type FlatListRenderInfo<T> = HozoFlatListRenderInfo<T>
|
|
1110
1100
|
export type FlatListProps<T> = HozoFlatListProps<T>
|
package/src/link.native.tsx
CHANGED
|
@@ -4,17 +4,23 @@ import {
|
|
|
4
4
|
prefetchHozoNavigation,
|
|
5
5
|
useHozoNavigation,
|
|
6
6
|
} from '@hozo/engine/navigation'
|
|
7
|
-
import { type ReactNode, useRef } from 'react'
|
|
7
|
+
import { type ComponentPropsWithRef, type ElementType, type ReactNode, useRef } from 'react'
|
|
8
8
|
import { Linking, Pressable, type PressableProps } from 'react-native'
|
|
9
|
+
import type { HozoPressable } from './pressable.native.tsx'
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
type LinkHost = typeof Pressable | typeof HozoPressable
|
|
12
|
+
|
|
13
|
+
export type HozoLinkProps<Host extends LinkHost = typeof Pressable> =
|
|
14
|
+
ComponentPropsWithRef<Host> & {
|
|
15
|
+
/** Compiler-only host selection; ordinary links retain RN's cheap path. */
|
|
16
|
+
hozoLinkComponent?: Host
|
|
17
|
+
href: string
|
|
18
|
+
external?: boolean
|
|
19
|
+
replace?: boolean
|
|
20
|
+
prefetch?: boolean
|
|
21
|
+
onPress?: PressableProps['onPress']
|
|
22
|
+
children?: PressableProps['children']
|
|
23
|
+
}
|
|
18
24
|
|
|
19
25
|
/**
|
|
20
26
|
* Native semantic link with the same destination-bearing API as `<a>`.
|
|
@@ -34,11 +40,15 @@ export interface HozoLinkProps extends Omit<PressableProps, 'onPress'> {
|
|
|
34
40
|
* `Link` carried its own copy of exactly this and is why it existed
|
|
35
41
|
* separately at all; it delegates here now.
|
|
36
42
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
43
|
+
* Normalization wraps text runs, not whole arrays: embedded controls remain
|
|
44
|
+
* controls. Function children are still evaluated by React Native itself.
|
|
45
|
+
*
|
|
46
|
+
* State variants select HozoPressable without duplicating its event machine
|
|
47
|
+
* or navigation. The type-only import keeps that machinery out of ordinary
|
|
48
|
+
* links' Metro graph, where unused static imports would still cost modules.
|
|
40
49
|
*/
|
|
41
|
-
export function HozoLink({
|
|
50
|
+
export function HozoLink<Host extends LinkHost = typeof Pressable>({
|
|
51
|
+
hozoLinkComponent,
|
|
42
52
|
href,
|
|
43
53
|
external,
|
|
44
54
|
replace,
|
|
@@ -49,22 +59,23 @@ export function HozoLink({
|
|
|
49
59
|
children,
|
|
50
60
|
accessibilityRole = 'link',
|
|
51
61
|
...props
|
|
52
|
-
}: HozoLinkProps) {
|
|
62
|
+
}: HozoLinkProps<Host>) {
|
|
63
|
+
const Component: ElementType = hozoLinkComponent ?? Pressable
|
|
53
64
|
const navigation = useHozoNavigation()
|
|
54
65
|
const prefetchedHref = useRef<string | null>(null)
|
|
55
66
|
return (
|
|
56
|
-
<
|
|
67
|
+
<Component
|
|
57
68
|
{...props}
|
|
58
69
|
disabled={disabled}
|
|
59
70
|
accessibilityRole={accessibilityRole}
|
|
60
|
-
onPressIn={(event) => {
|
|
71
|
+
onPressIn={(event: Parameters<NonNullable<PressableProps['onPressIn']>>[0]) => {
|
|
61
72
|
onPressIn?.(event)
|
|
62
73
|
if (prefetch && !disabled && prefetchedHref.current !== href) {
|
|
63
74
|
prefetchedHref.current = href
|
|
64
75
|
prefetchHozoNavigation(navigation, { href, external, replace })
|
|
65
76
|
}
|
|
66
77
|
}}
|
|
67
|
-
onPress={(event) => {
|
|
78
|
+
onPress={(event: Parameters<NonNullable<PressableProps['onPress']>>[0]) => {
|
|
68
79
|
onPress?.(event)
|
|
69
80
|
if (!event.defaultPrevented) {
|
|
70
81
|
void activateHozoNavigation(navigation, { href, external, replace }, Linking.openURL)
|
|
@@ -72,6 +83,6 @@ export function HozoLink({
|
|
|
72
83
|
}}
|
|
73
84
|
>
|
|
74
85
|
{typeof children === 'function' ? children : hozoTextChildren(children as ReactNode)}
|
|
75
|
-
</
|
|
86
|
+
</Component>
|
|
76
87
|
)
|
|
77
88
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// What `Presence` tells the elements that animate out inside it, on
|
|
2
|
+
// Native (decision 007, amendment 1).
|
|
3
|
+
//
|
|
4
|
+
// On Web the same thing travels as an attribute, `data-state`, and CSS
|
|
5
|
+
// does the rest. React Native has no attributes to select on, so the
|
|
6
|
+
// state is a context instead, and `HozoAnimated` -- the component the
|
|
7
|
+
// compiler puts in place of an element with a `data-[state=closed]:`
|
|
8
|
+
// class -- reads it.
|
|
9
|
+
|
|
10
|
+
import { createContext } from 'react'
|
|
11
|
+
|
|
12
|
+
export type PresenceState = 'open' | 'closed'
|
|
13
|
+
|
|
14
|
+
export interface PresenceContextValue {
|
|
15
|
+
state: PresenceState
|
|
16
|
+
/**
|
|
17
|
+
* Called by an element that will animate out, once, on mount. `Presence`
|
|
18
|
+
* waits for every registered element before it removes its child, and
|
|
19
|
+
* removes it at once when none registered. The returned function
|
|
20
|
+
* unregisters.
|
|
21
|
+
*/
|
|
22
|
+
register(id: symbol, durationMs: number): () => void
|
|
23
|
+
/** Called by a registered element when its exit animation has ended. */
|
|
24
|
+
done(id: symbol): void
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const PresenceContext = createContext<PresenceContextValue | null>(null)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Keeps a child mounted while it animates out (decision 007, slice 2).
|
|
2
|
+
//
|
|
3
|
+
// React removes an element the moment it stops being rendered, so nothing
|
|
4
|
+
// can animate after that. `Presence` holds on to its child after `show`
|
|
5
|
+
// turns false, tells it it is leaving, and lets it go when every element
|
|
6
|
+
// inside that animates out has finished.
|
|
7
|
+
//
|
|
8
|
+
// The Web half is `./presence.tsx`. Both are written against the same
|
|
9
|
+
// class: `data-[state=closed]:`.
|
|
10
|
+
|
|
11
|
+
import { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
12
|
+
import { PresenceContext, type PresenceContextValue } from './presence-context.ts'
|
|
13
|
+
|
|
14
|
+
export interface PresenceProps {
|
|
15
|
+
/** Whether the child is shown. Turning it false starts the exit. */
|
|
16
|
+
show: boolean
|
|
17
|
+
children?: ReactNode
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* How long past the longest registered transition to wait for one that
|
|
22
|
+
* never reports. An animation interrupted by an unmount above, or one the
|
|
23
|
+
* runtime dropped, would otherwise keep the child on screen for good.
|
|
24
|
+
*/
|
|
25
|
+
const GRACE_MS = 100
|
|
26
|
+
|
|
27
|
+
export function Presence({ show, children }: PresenceProps) {
|
|
28
|
+
const [mounted, setMounted] = useState(show)
|
|
29
|
+
// Set during render rather than in an effect, so a child that is shown
|
|
30
|
+
// again appears in the same commit instead of one frame late.
|
|
31
|
+
if (show && !mounted) setMounted(true)
|
|
32
|
+
|
|
33
|
+
const exiting = useRef(new Map<symbol, number>())
|
|
34
|
+
const waiting = useRef(new Set<symbol>())
|
|
35
|
+
|
|
36
|
+
const finish = useCallback(() => {
|
|
37
|
+
waiting.current.clear()
|
|
38
|
+
setMounted(false)
|
|
39
|
+
}, [])
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
// Shown again before the exit ended: whatever was leaving is staying.
|
|
43
|
+
if (show) {
|
|
44
|
+
waiting.current.clear()
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
if (!mounted) return
|
|
48
|
+
if (exiting.current.size === 0) {
|
|
49
|
+
finish()
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
waiting.current = new Set(exiting.current.keys())
|
|
53
|
+
const longest = Math.max(...exiting.current.values())
|
|
54
|
+
const timer = setTimeout(finish, longest + GRACE_MS)
|
|
55
|
+
return () => clearTimeout(timer)
|
|
56
|
+
}, [show, mounted, finish])
|
|
57
|
+
|
|
58
|
+
const register = useCallback((id: symbol, durationMs: number) => {
|
|
59
|
+
exiting.current.set(id, durationMs)
|
|
60
|
+
return () => {
|
|
61
|
+
exiting.current.delete(id)
|
|
62
|
+
waiting.current.delete(id)
|
|
63
|
+
}
|
|
64
|
+
}, [])
|
|
65
|
+
|
|
66
|
+
const done = useCallback(
|
|
67
|
+
(id: symbol) => {
|
|
68
|
+
if (!waiting.current.delete(id)) return
|
|
69
|
+
if (waiting.current.size === 0) finish()
|
|
70
|
+
},
|
|
71
|
+
[finish],
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
const value = useMemo<PresenceContextValue>(
|
|
75
|
+
() => ({ state: show ? 'open' : 'closed', register, done }),
|
|
76
|
+
[show, register, done],
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if (!mounted) return null
|
|
80
|
+
return <PresenceContext.Provider value={value}>{children}</PresenceContext.Provider>
|
|
81
|
+
}
|