@hozo/primitives 0.2.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.
@@ -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
- export interface HozoLinkProps extends Omit<PressableProps, 'onPress'> {
11
- href: string
12
- external?: boolean
13
- replace?: boolean
14
- prefetch?: boolean
15
- onPress?: PressableProps['onPress']
16
- children?: PressableProps['children']
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
- * Only a lone string, which is the case that occurs: a label. A mixed
38
- * array is left alone rather than guessed at, since wrapping the lot
39
- * would put whatever else is in there inside a `Text`.
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
- <Pressable
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
- </Pressable>
86
+ </Component>
76
87
  )
77
88
  }