@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,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
|
+
}
|