@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.
Files changed (218) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/dist/flat-list.native.d.ts +17 -0
  4. package/dist/flat-list.native.d.ts.map +1 -0
  5. package/dist/flat-list.native.js +89 -0
  6. package/dist/flat-list.native.js.map +1 -0
  7. package/dist/foundation.d.ts +104 -0
  8. package/dist/foundation.d.ts.map +1 -0
  9. package/dist/foundation.js +115 -0
  10. package/dist/foundation.js.map +1 -0
  11. package/dist/foundation.native.d.ts +59 -0
  12. package/dist/foundation.native.d.ts.map +1 -0
  13. package/dist/foundation.native.js +40 -0
  14. package/dist/foundation.native.js.map +1 -0
  15. package/dist/generated/animated.d.ts +2 -0
  16. package/dist/generated/animated.d.ts.map +1 -0
  17. package/dist/generated/animated.js +3 -0
  18. package/dist/generated/animated.js.map +1 -0
  19. package/dist/generated/backdrop-filter.d.ts +2 -0
  20. package/dist/generated/backdrop-filter.d.ts.map +1 -0
  21. package/dist/generated/backdrop-filter.js +3 -0
  22. package/dist/generated/backdrop-filter.js.map +1 -0
  23. package/dist/generated/container.d.ts +2 -0
  24. package/dist/generated/container.d.ts.map +1 -0
  25. package/dist/generated/container.js +3 -0
  26. package/dist/generated/container.js.map +1 -0
  27. package/dist/generated/flat-list.d.ts +2 -0
  28. package/dist/generated/flat-list.d.ts.map +1 -0
  29. package/dist/generated/flat-list.js +2 -0
  30. package/dist/generated/flat-list.js.map +1 -0
  31. package/dist/generated/flat-list.native.d.ts +3 -0
  32. package/dist/generated/flat-list.native.d.ts.map +1 -0
  33. package/dist/generated/flat-list.native.js +2 -0
  34. package/dist/generated/flat-list.native.js.map +1 -0
  35. package/dist/generated/grid.d.ts +3 -0
  36. package/dist/generated/grid.d.ts.map +1 -0
  37. package/dist/generated/grid.js +4 -0
  38. package/dist/generated/grid.js.map +1 -0
  39. package/dist/generated/link.d.ts +2 -0
  40. package/dist/generated/link.d.ts.map +1 -0
  41. package/dist/generated/link.js +2 -0
  42. package/dist/generated/link.js.map +1 -0
  43. package/dist/generated/link.native.d.ts +2 -0
  44. package/dist/generated/link.native.d.ts.map +1 -0
  45. package/dist/generated/link.native.js +2 -0
  46. package/dist/generated/link.native.js.map +1 -0
  47. package/dist/generated/pressable.d.ts +2 -0
  48. package/dist/generated/pressable.d.ts.map +1 -0
  49. package/dist/generated/pressable.js +2 -0
  50. package/dist/generated/pressable.js.map +1 -0
  51. package/dist/generated/pressable.native.d.ts +2 -0
  52. package/dist/generated/pressable.native.d.ts.map +1 -0
  53. package/dist/generated/pressable.native.js +2 -0
  54. package/dist/generated/pressable.native.js.map +1 -0
  55. package/dist/generated/refresh-control.d.ts +2 -0
  56. package/dist/generated/refresh-control.d.ts.map +1 -0
  57. package/dist/generated/refresh-control.js +2 -0
  58. package/dist/generated/refresh-control.js.map +1 -0
  59. package/dist/generated/refresh-control.native.d.ts +2 -0
  60. package/dist/generated/refresh-control.native.d.ts.map +1 -0
  61. package/dist/generated/refresh-control.native.js +2 -0
  62. package/dist/generated/refresh-control.native.js.map +1 -0
  63. package/dist/generated/scroll-view.d.ts +2 -0
  64. package/dist/generated/scroll-view.d.ts.map +1 -0
  65. package/dist/generated/scroll-view.js +2 -0
  66. package/dist/generated/scroll-view.js.map +1 -0
  67. package/dist/generated/scroll-view.native.d.ts +2 -0
  68. package/dist/generated/scroll-view.native.d.ts.map +1 -0
  69. package/dist/generated/scroll-view.native.js +2 -0
  70. package/dist/generated/scroll-view.native.js.map +1 -0
  71. package/dist/generated/spaced.d.ts +2 -0
  72. package/dist/generated/spaced.d.ts.map +1 -0
  73. package/dist/generated/spaced.js +3 -0
  74. package/dist/generated/spaced.js.map +1 -0
  75. package/dist/generated/text-input.d.ts +2 -0
  76. package/dist/generated/text-input.d.ts.map +1 -0
  77. package/dist/generated/text-input.js +2 -0
  78. package/dist/generated/text-input.js.map +1 -0
  79. package/dist/generated/text-input.native.d.ts +2 -0
  80. package/dist/generated/text-input.native.d.ts.map +1 -0
  81. package/dist/generated/text-input.native.js +2 -0
  82. package/dist/generated/text-input.native.js.map +1 -0
  83. package/dist/generated/text-size.d.ts +2 -0
  84. package/dist/generated/text-size.d.ts.map +1 -0
  85. package/dist/generated/text-size.js +3 -0
  86. package/dist/generated/text-size.js.map +1 -0
  87. package/dist/generated/text.d.ts +2 -0
  88. package/dist/generated/text.d.ts.map +1 -0
  89. package/dist/generated/text.js +5 -0
  90. package/dist/generated/text.js.map +1 -0
  91. package/dist/generated/view.d.ts +2 -0
  92. package/dist/generated/view.d.ts.map +1 -0
  93. package/dist/generated/view.js +2 -0
  94. package/dist/generated/view.js.map +1 -0
  95. package/dist/generated/view.native.d.ts +2 -0
  96. package/dist/generated/view.native.d.ts.map +1 -0
  97. package/dist/generated/view.native.js +2 -0
  98. package/dist/generated/view.native.js.map +1 -0
  99. package/dist/index.d.ts +188 -0
  100. package/dist/index.d.ts.map +1 -0
  101. package/dist/index.js +718 -0
  102. package/dist/index.js.map +1 -0
  103. package/dist/index.native.d.ts +22 -0
  104. package/dist/index.native.d.ts.map +1 -0
  105. package/dist/index.native.js +8 -0
  106. package/dist/index.native.js.map +1 -0
  107. package/dist/link.d.ts +320 -0
  108. package/dist/link.d.ts.map +1 -0
  109. package/dist/link.js +69 -0
  110. package/dist/link.js.map +1 -0
  111. package/dist/link.native.d.ts +33 -0
  112. package/dist/link.native.d.ts.map +1 -0
  113. package/dist/link.native.js +44 -0
  114. package/dist/link.native.js.map +1 -0
  115. package/dist/pressable.d.ts +65 -0
  116. package/dist/pressable.d.ts.map +1 -0
  117. package/dist/pressable.js +202 -0
  118. package/dist/pressable.js.map +1 -0
  119. package/dist/pressable.native.d.ts +55 -0
  120. package/dist/pressable.native.d.ts.map +1 -0
  121. package/dist/pressable.native.js +405 -0
  122. package/dist/pressable.native.js.map +1 -0
  123. package/dist/runtime-backdrop.native.d.ts +8 -0
  124. package/dist/runtime-backdrop.native.d.ts.map +1 -0
  125. package/dist/runtime-backdrop.native.js +12 -0
  126. package/dist/runtime-backdrop.native.js.map +1 -0
  127. package/dist/runtime-container.native.d.ts +41 -0
  128. package/dist/runtime-container.native.d.ts.map +1 -0
  129. package/dist/runtime-container.native.js +61 -0
  130. package/dist/runtime-container.native.js.map +1 -0
  131. package/dist/runtime-grid.d.ts +37 -0
  132. package/dist/runtime-grid.d.ts.map +1 -0
  133. package/dist/runtime-grid.js +139 -0
  134. package/dist/runtime-grid.js.map +1 -0
  135. package/dist/runtime-grid.native.d.ts +26 -0
  136. package/dist/runtime-grid.native.d.ts.map +1 -0
  137. package/dist/runtime-grid.native.js +76 -0
  138. package/dist/runtime-grid.native.js.map +1 -0
  139. package/dist/runtime-measured.d.ts +20 -0
  140. package/dist/runtime-measured.d.ts.map +1 -0
  141. package/dist/runtime-measured.js +42 -0
  142. package/dist/runtime-measured.js.map +1 -0
  143. package/dist/runtime-spacing.d.ts +9 -0
  144. package/dist/runtime-spacing.d.ts.map +1 -0
  145. package/dist/runtime-spacing.js +35 -0
  146. package/dist/runtime-spacing.js.map +1 -0
  147. package/dist/runtime-spacing.native.d.ts +12 -0
  148. package/dist/runtime-spacing.native.d.ts.map +1 -0
  149. package/dist/runtime-spacing.native.js +64 -0
  150. package/dist/runtime-spacing.native.js.map +1 -0
  151. package/dist/runtime-text-size.native.d.ts +29 -0
  152. package/dist/runtime-text-size.native.d.ts.map +1 -0
  153. package/dist/runtime-text-size.native.js +51 -0
  154. package/dist/runtime-text-size.native.js.map +1 -0
  155. package/dist/runtime-transition.native.d.ts +15 -0
  156. package/dist/runtime-transition.native.d.ts.map +1 -0
  157. package/dist/runtime-transition.native.js +156 -0
  158. package/dist/runtime-transition.native.js.map +1 -0
  159. package/dist/text-input.d.ts +40 -0
  160. package/dist/text-input.d.ts.map +1 -0
  161. package/dist/text-input.js +54 -0
  162. package/dist/text-input.js.map +1 -0
  163. package/dist/view.d.ts +42 -0
  164. package/dist/view.d.ts.map +1 -0
  165. package/dist/view.js +84 -0
  166. package/dist/view.js.map +1 -0
  167. package/dist/view.native.d.ts +8 -0
  168. package/dist/view.native.d.ts.map +1 -0
  169. package/dist/view.native.js +5 -0
  170. package/dist/view.native.js.map +1 -0
  171. package/dist/windowing.d.ts +149 -0
  172. package/dist/windowing.d.ts.map +1 -0
  173. package/dist/windowing.js +336 -0
  174. package/dist/windowing.js.map +1 -0
  175. package/package.json +160 -0
  176. package/src/flat-list.native.tsx +158 -0
  177. package/src/foundation.native.tsx +179 -0
  178. package/src/foundation.tsx +323 -0
  179. package/src/generated/animated.ts +6 -0
  180. package/src/generated/backdrop-filter.ts +5 -0
  181. package/src/generated/container.ts +7 -0
  182. package/src/generated/flat-list.native.ts +6 -0
  183. package/src/generated/flat-list.ts +7 -0
  184. package/src/generated/grid.ts +4 -0
  185. package/src/generated/link.native.ts +1 -0
  186. package/src/generated/link.ts +1 -0
  187. package/src/generated/pressable.native.ts +5 -0
  188. package/src/generated/pressable.ts +5 -0
  189. package/src/generated/refresh-control.native.ts +4 -0
  190. package/src/generated/refresh-control.ts +1 -0
  191. package/src/generated/scroll-view.native.ts +4 -0
  192. package/src/generated/scroll-view.ts +1 -0
  193. package/src/generated/spaced.ts +2 -0
  194. package/src/generated/text-input.native.ts +4 -0
  195. package/src/generated/text-input.ts +1 -0
  196. package/src/generated/text-size.ts +7 -0
  197. package/src/generated/text.ts +8 -0
  198. package/src/generated/view.native.ts +1 -0
  199. package/src/generated/view.ts +1 -0
  200. package/src/index.native.ts +24 -0
  201. package/src/index.ts +1111 -0
  202. package/src/link.native.tsx +77 -0
  203. package/src/link.ts +125 -0
  204. package/src/pressable.native.tsx +538 -0
  205. package/src/pressable.ts +358 -0
  206. package/src/runtime-backdrop.native.tsx +22 -0
  207. package/src/runtime-container.native.tsx +105 -0
  208. package/src/runtime-grid.native.tsx +143 -0
  209. package/src/runtime-grid.ts +198 -0
  210. package/src/runtime-measured.ts +42 -0
  211. package/src/runtime-spacing.native.tsx +75 -0
  212. package/src/runtime-spacing.ts +37 -0
  213. package/src/runtime-text-size.native.tsx +79 -0
  214. package/src/runtime-transition.native.tsx +183 -0
  215. package/src/text-input.ts +129 -0
  216. package/src/view.native.ts +7 -0
  217. package/src/view.ts +165 -0
  218. package/src/windowing.ts +415 -0
@@ -0,0 +1,77 @@
1
+ import { hozoTextChildren } from '@hozo/behaviors'
2
+ import {
3
+ activateHozoNavigation,
4
+ prefetchHozoNavigation,
5
+ useHozoNavigation,
6
+ } from '@hozo/engine/navigation'
7
+ import { type ReactNode, useRef } from 'react'
8
+ import { Linking, Pressable, type PressableProps } from 'react-native'
9
+
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
+ }
18
+
19
+ /**
20
+ * Native semantic link with the same destination-bearing API as `<a>`.
21
+ *
22
+ * `accessibilityRole` is a default rather than a constant, so an author
23
+ * building a deliberately custom widget can override it. Hozo's own
24
+ * destination-bearing primitives do not: a component that follows an
25
+ * `href` is announced as a link even when `Button` supplies its visual
26
+ * treatment.
27
+ *
28
+ * A string child is wrapped, because `Pressable` is a View and React
29
+ * Native throws on a bare string inside one. The compiler already wraps
30
+ * text children -- `<Link href>Docs</Link>` lowers to
31
+ * `<HozoLink><Text>Docs</Text></HozoLink>` -- so this is for the
32
+ * uncompiled path, where `@hozo/core`'s `Button` and
33
+ * `@hozo/typography`'s `Link` hand their children straight through.
34
+ * `Link` carried its own copy of exactly this and is why it existed
35
+ * separately at all; it delegates here now.
36
+ *
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`.
40
+ */
41
+ export function HozoLink({
42
+ href,
43
+ external,
44
+ replace,
45
+ prefetch,
46
+ onPress,
47
+ onPressIn,
48
+ disabled,
49
+ children,
50
+ accessibilityRole = 'link',
51
+ ...props
52
+ }: HozoLinkProps) {
53
+ const navigation = useHozoNavigation()
54
+ const prefetchedHref = useRef<string | null>(null)
55
+ return (
56
+ <Pressable
57
+ {...props}
58
+ disabled={disabled}
59
+ accessibilityRole={accessibilityRole}
60
+ onPressIn={(event) => {
61
+ onPressIn?.(event)
62
+ if (prefetch && !disabled && prefetchedHref.current !== href) {
63
+ prefetchedHref.current = href
64
+ prefetchHozoNavigation(navigation, { href, external, replace })
65
+ }
66
+ }}
67
+ onPress={(event) => {
68
+ onPress?.(event)
69
+ if (!event.defaultPrevented) {
70
+ void activateHozoNavigation(navigation, { href, external, replace }, Linking.openURL)
71
+ }
72
+ }}
73
+ >
74
+ {typeof children === 'function' ? children : hozoTextChildren(children as ReactNode)}
75
+ </Pressable>
76
+ )
77
+ }
package/src/link.ts ADDED
@@ -0,0 +1,125 @@
1
+ // The Web half of the link, which the browser already is.
2
+ //
3
+ // `<a href>` navigates, is in the tab order, and announces itself as a
4
+ // link without being asked, so the Web backend lowers straight to the
5
+ // element and nothing here is on the compiled path.
6
+ //
7
+ // It exists for the reason `disclosure.tsx` and `ruby.tsx` do: a package
8
+ // resolves this one's types through the Web entry whichever platform it
9
+ // builds for, so a component the Native backend emits has to be nameable
10
+ // here or `@hozo/core` cannot import it.
11
+ //
12
+ // It is also the one anchor Hozo renders when it is the one rendering.
13
+ // `@hozo/core`'s `Button` and `@hozo/typography`'s `Link` had a hand-written
14
+ // `<a>` each, and both carried their own copy of the rule below -- the
15
+ // one that turns `external` into `target="_blank"` and a `rel` that
16
+ // stops the opened page reaching back through `window.opener`. A rule
17
+ // with two implementations is a rule that will be changed in one of
18
+ // them; these are the uncompiled fallbacks, so a divergence would show
19
+ // up only in the projects Hozo could not lower, which are the ones least
20
+ // likely to be looked at.
21
+
22
+ // `createElement` rather than JSX, and the file is `.ts`: this package
23
+ // runs its tests as `node --test src/*.test.ts`, straight through Node's
24
+ // type stripping, which has no JSX transform. A `.tsx` reached from the
25
+ // Web entry fails the whole file with ERR_UNKNOWN_FILE_EXTENSION -- Node
26
+ // does not recognise the extension at all. The native halves here are
27
+ // `.tsx` and are never imported by a test that runs this way.
28
+
29
+ import { externalLinkAttributes } from '@hozo/engine'
30
+ import type { AnchorHTMLAttributes, CSSProperties, MouseEventHandler, ReactNode, Ref } from 'react'
31
+ import { createElement } from 'react'
32
+
33
+ export interface HozoLinkProps
34
+ extends Omit<
35
+ AnchorHTMLAttributes<HTMLAnchorElement>,
36
+ 'children' | 'className' | 'download' | 'href' | 'onClick' | 'rel' | 'role' | 'style' | 'target'
37
+ > {
38
+ href: string
39
+ children?: ReactNode
40
+ onPress?: MouseEventHandler<HTMLAnchorElement>
41
+ className?: string
42
+ style?: CSSProperties
43
+ testID?: string
44
+ nativeID?: string
45
+ accessibilityLabel?: string
46
+ accessibilityHint?: string
47
+ accessibilityRole?: string
48
+ disabled?: boolean
49
+ /** Leaves the app: `_blank` and a `rel` that severs `window.opener`. */
50
+ external?: boolean
51
+ /** Ask an installed router to replace its current history entry. */
52
+ replace?: boolean
53
+ /** Warm an application-owned route when the user shows intent to follow it. */
54
+ prefetch?: boolean
55
+ target?: '_blank' | '_self' | '_parent' | '_top' | string
56
+ rel?: string
57
+ download?: boolean | string
58
+ 'aria-hidden'?: boolean
59
+ ref?: Ref<HTMLAnchorElement>
60
+ }
61
+
62
+ export function HozoLink({
63
+ href,
64
+ children,
65
+ onPress,
66
+ className,
67
+ style,
68
+ testID,
69
+ nativeID,
70
+ accessibilityLabel,
71
+ accessibilityHint,
72
+ accessibilityRole,
73
+ disabled,
74
+ external,
75
+ replace,
76
+ prefetch,
77
+ target,
78
+ rel,
79
+ download,
80
+ 'aria-hidden': ariaHidden,
81
+ ...anchorProps
82
+ }: HozoLinkProps) {
83
+ const destination = externalLinkAttributes(external, target, rel)
84
+ return createElement(
85
+ 'a',
86
+ {
87
+ ...anchorProps,
88
+ href,
89
+ target: destination.target,
90
+ rel: destination.rel,
91
+ download,
92
+ className,
93
+ style,
94
+ 'data-testid': testID,
95
+ id: nativeID,
96
+ // Only when asked for: an `<a href>` is already a link, and saying
97
+ // so again is noise in the accessibility tree. Kept as an escape
98
+ // hatch for deliberately custom widgets, not used by Hozo's
99
+ // destination-bearing primitives.
100
+ role: accessibilityRole,
101
+ 'aria-label': accessibilityLabel,
102
+ 'aria-description': accessibilityHint,
103
+ 'aria-hidden': ariaHidden,
104
+ // There is no `disabled` on an anchor. The pair below is what a
105
+ // disabled link is: announced as unavailable, and not followed.
106
+ 'aria-disabled': disabled ? true : undefined,
107
+ // `disabled:` compiles to `[data-hozo-disabled]` rather than to
108
+ // `:disabled`, which matches form controls only. Without the
109
+ // attribute the utility silently stops matching on this element
110
+ // while it matches everywhere else.
111
+ 'data-hozo-disabled': disabled ? '' : undefined,
112
+ // The document-level navigation adapter sees compiled anchors too,
113
+ // so router-only intent travels as inert data rather than an invalid
114
+ // DOM `replace` attribute.
115
+ 'data-hozo-navigation-replace': replace ? '' : undefined,
116
+ 'data-hozo-navigation-prefetch': prefetch ? '' : undefined,
117
+ onClick: disabled
118
+ ? (event: React.MouseEvent<HTMLAnchorElement>) => {
119
+ event.preventDefault()
120
+ }
121
+ : onPress,
122
+ },
123
+ children,
124
+ )
125
+ }