@hozo/semantics 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.
@@ -0,0 +1,314 @@
1
+ import { hozoTextChildren } from '@hozo/behaviors'
2
+ import { hozoPreflight } from '@hozo/engine/project'
3
+ import React, { type ComponentProps, type ReactNode } from 'react'
4
+ // The components rather than their names. These files used to render
5
+ // `React.createElement('View')`, and React Native resolves a string tag
6
+ // through its view config registry, where the registered names are
7
+ // `RCTView` and `RCTText` -- never `View` or `Text`. Every one of these
8
+ // would have thrown "View config getter callback for component `View`
9
+ // must be a function" on the first render. Nothing caught it because
10
+ // nothing imported these files: the tests next to them render the Web
11
+ // half through `react-dom/server`.
12
+ import {
13
+ type AccessibilityRole,
14
+ type Role,
15
+ type StyleProp,
16
+ Text,
17
+ type TextStyle,
18
+ View,
19
+ type ViewStyle,
20
+ } from 'react-native'
21
+ import { HozoDetails, HozoSummary } from './disclosure.native.tsx'
22
+
23
+ export interface SemanticsNativeProps {
24
+ /**
25
+ * Tailwind classes, read by the compiler and gone by runtime.
26
+ *
27
+ * The Web half of this file has always declared it; this half never
28
+ * did, and nothing noticed because the `react-native` export condition
29
+ * carried no `types`, so a React Native app was type-checked against
30
+ * the Web declarations instead. Uncompiled, the prop is simply ignored
31
+ * here -- which is what the fallback components are for.
32
+ */
33
+ className?: string
34
+ children?: ReactNode
35
+ style?: StyleProp<ViewStyle>
36
+ testID?: string
37
+ nativeID?: string
38
+ /**
39
+ * React Native's own unions rather than `string`.
40
+ *
41
+ * `any` and a bare `string` are the same mistake at different sizes: a
42
+ * prop that agrees with anything checks nothing. The Web half of this
43
+ * file has always said `CSSProperties`; this half said `any`, so a Web-only
44
+ * style or a role React Native has never heard of passed in silence.
45
+ *
46
+ * `Role` is the wider of the two and carries the landmark roles these
47
+ * components exist for. `AccessibilityRole` is the older prop and does
48
+ * not -- which is a difference this file had a bug in.
49
+ */
50
+ role?: Role
51
+ accessibilityRole?: AccessibilityRole
52
+ accessibilityLabel?: string
53
+ accessibilityHint?: string
54
+ accessible?: boolean
55
+ }
56
+
57
+ // The public concept keeps one name across platform entry points even
58
+ // though its style and role fields use the platform's own types here.
59
+ export type SemanticsUniversalProps = SemanticsNativeProps
60
+
61
+ /** The same, for the ones that render text rather than a box. */
62
+ export interface SemanticsTextNativeProps extends Omit<SemanticsNativeProps, 'style'> {
63
+ style?: StyleProp<TextStyle>
64
+ }
65
+
66
+ export interface TimeNativeProps extends SemanticsTextNativeProps {
67
+ dateTime?: string
68
+ datetime?: string
69
+ }
70
+
71
+ export function Main({ role = 'main', children, ...props }: SemanticsNativeProps) {
72
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
73
+ }
74
+
75
+ export function Header({ role = 'banner', children, ...props }: SemanticsNativeProps) {
76
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
77
+ }
78
+
79
+ export function Footer({ role = 'contentinfo', children, ...props }: SemanticsNativeProps) {
80
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
81
+ }
82
+
83
+ export function Aside({ role = 'complementary', children, ...props }: SemanticsNativeProps) {
84
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
85
+ }
86
+
87
+ /**
88
+ * The one landmark React Native has no word for.
89
+ *
90
+ * Its `Role` union carries every other role in this file -- banner,
91
+ * complementary, contentinfo, navigation, main, figure, group, list,
92
+ * separator -- and for search it has `searchbox`, which is the field
93
+ * rather than the region around it. `accessibilityRole: 'search'` means
94
+ * the field too.
95
+ *
96
+ * So this is a plain box here, and says so, rather than claiming a
97
+ * landmark by naming a widget. The Web half is a real `<search>`.
98
+ */
99
+ export function Search({ children, ...props }: SemanticsNativeProps) {
100
+ return React.createElement(View, props, hozoTextChildren(children))
101
+ }
102
+
103
+ export function Section({ children, ...props }: SemanticsNativeProps) {
104
+ return React.createElement(View, props, hozoTextChildren(children))
105
+ }
106
+
107
+ export function Article({ role = 'article', children, ...props }: SemanticsNativeProps) {
108
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
109
+ }
110
+
111
+ export function Nav({ role = 'navigation', children, ...props }: SemanticsNativeProps) {
112
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
113
+ }
114
+
115
+ export interface ListProps extends SemanticsNativeProps {
116
+ ordered?: boolean
117
+ }
118
+
119
+ export function List({ ordered: _ordered, role = 'list', children, ...props }: ListProps) {
120
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
121
+ }
122
+
123
+ export function ListItem({ role = 'listitem', children, ...props }: SemanticsNativeProps) {
124
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
125
+ }
126
+
127
+ export function Figure({ role = 'figure', children, ...props }: SemanticsNativeProps) {
128
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
129
+ }
130
+
131
+ export function Figcaption(props: SemanticsTextNativeProps) {
132
+ return React.createElement(Text, props)
133
+ }
134
+
135
+ export function Time(props: TimeNativeProps) {
136
+ return React.createElement(Text, props)
137
+ }
138
+
139
+ export function Address({ children, ...props }: SemanticsNativeProps) {
140
+ return React.createElement(View, props, hozoTextChildren(children))
141
+ }
142
+
143
+ export function Fieldset({ role = 'group', children, ...props }: SemanticsNativeProps) {
144
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
145
+ }
146
+
147
+ export function Legend({ style, ...props }: SemanticsTextNativeProps) {
148
+ return React.createElement(Text, { style: [{ fontWeight: 'bold' }, style], ...props })
149
+ }
150
+
151
+ export interface DetailsNativeProps extends SemanticsNativeProps {
152
+ open?: boolean
153
+ defaultOpen?: boolean
154
+ onToggle?: (open: boolean) => void
155
+ }
156
+
157
+ /**
158
+ * The disclosure the Native backend emits, under the names this package
159
+ * publishes.
160
+ *
161
+ * One implementation rather than two. The package-owned pair below is
162
+ * what a compiled `<Details>` becomes; the old copy beside it had the same
163
+ * bug the compiled path had, which
164
+ * is how a second implementation goes wrong: it rendered every child
165
+ * whether it was open or not, so the body never actually hid.
166
+ */
167
+ /**
168
+ * The one place a Native style has to be handed over as if it were a Web
169
+ * one, and it is a resolver's limit rather than a claim about the value.
170
+ *
171
+ * A package resolves the component's Web types while checking both platform
172
+ * entries
173
+ * whichever platform it is building for, because `exports` carries a
174
+ * single `types` and this package compiles both halves in one `tsc` run.
175
+ * so the cast below is the boundary between CSS and Native style shapes.
176
+ *
177
+ * The alternative was a second disclosure in this file, which is what
178
+ * used to be here: it rendered every child whether it was open or not, so
179
+ * the body never hid. A cast at a known boundary beats a copy that drifts.
180
+ */
181
+ const asWeb = (style: DetailsNativeProps['style']) =>
182
+ style as unknown as ComponentProps<typeof HozoDetails>['style']
183
+
184
+ export function Details({
185
+ open,
186
+ defaultOpen,
187
+ onToggle,
188
+ children,
189
+ style,
190
+ ...props
191
+ }: DetailsNativeProps) {
192
+ return (
193
+ <HozoDetails
194
+ open={open}
195
+ defaultOpen={defaultOpen}
196
+ onToggle={onToggle}
197
+ style={asWeb(style)}
198
+ {...props}
199
+ >
200
+ {children}
201
+ </HozoDetails>
202
+ )
203
+ }
204
+
205
+ export function Summary({ children, style, ...props }: SemanticsNativeProps) {
206
+ return (
207
+ <HozoSummary style={asWeb(style)} {...props}>
208
+ {children}
209
+ </HozoSummary>
210
+ )
211
+ }
212
+ export function Term({ style, ...props }: SemanticsTextNativeProps) {
213
+ return React.createElement(Text, { style: [{ fontWeight: 'bold' }, style], ...props })
214
+ }
215
+
216
+ export function Description({ children, ...props }: SemanticsNativeProps) {
217
+ return React.createElement(View, props, hozoTextChildren(children))
218
+ }
219
+
220
+ export function TermList({ role = 'list', children, ...props }: SemanticsNativeProps) {
221
+ return React.createElement(View, { role, ...props }, hozoTextChildren(children))
222
+ }
223
+
224
+ TermList.Term = Term
225
+ TermList.Description = Description
226
+
227
+ export interface SeparatorProps extends SemanticsNativeProps {
228
+ orientation?: 'horizontal' | 'vertical'
229
+ decorative?: boolean
230
+ }
231
+
232
+ export function Separator({
233
+ orientation = 'horizontal',
234
+ decorative = false,
235
+ role = decorative ? 'none' : 'separator',
236
+ style,
237
+ children,
238
+ ...props
239
+ }: SeparatorProps) {
240
+ const thickness = hozoPreflight ? 1 : 2
241
+ const defaultStyle =
242
+ orientation === 'vertical'
243
+ ? { width: thickness, alignSelf: 'stretch' as const }
244
+ : { height: thickness, alignSelf: 'stretch' as const }
245
+ // Only `role` carries "separator": React Native's `accessibilityRole`
246
+ // has no such value, so the line that used to set it here was a string
247
+ // the platform would ignore. `none` it does have, and that is the half
248
+ // worth keeping -- a decorative rule should be silent on both props.
249
+ return React.createElement(
250
+ View,
251
+ {
252
+ role,
253
+ accessibilityRole: decorative ? 'none' : undefined,
254
+ style: [defaultStyle, style],
255
+ ...props,
256
+ },
257
+ hozoTextChildren(children),
258
+ )
259
+ }
260
+
261
+ export interface ProgressProps extends SemanticsNativeProps {
262
+ value?: number
263
+ max?: number
264
+ accessibilityValue?: {
265
+ min?: number
266
+ max?: number
267
+ now?: number
268
+ text?: string
269
+ }
270
+ }
271
+
272
+ export function Progress({
273
+ value,
274
+ max,
275
+ role = 'progressbar',
276
+ accessibilityValue,
277
+ style,
278
+ children,
279
+ ...props
280
+ }: ProgressProps) {
281
+ return React.createElement(
282
+ View,
283
+ {
284
+ role,
285
+ accessibilityRole: 'progressbar',
286
+ accessibilityValue: accessibilityValue ?? {
287
+ min: 0,
288
+ max: max ?? 100,
289
+ now: value,
290
+ },
291
+ // The box the browser gives `<progress>` and React Native gives
292
+ // nothing. Measured in headless Chrome under this project’s own
293
+ // preflight: 160 x 16. Without it a `View` is zero pixels in both
294
+ // directions -- invisible on screen, and absent from the
295
+ // accessibility tree as well, since Android does not report a
296
+ // zero-area view (#309).
297
+ //
298
+ // The author’s style comes second, so any of it wins.
299
+ style: [{ width: 160, height: 16 }, style],
300
+ ...props,
301
+ },
302
+ hozoTextChildren(children),
303
+ )
304
+ }
305
+
306
+ /**
307
+ * The names `@hozo/core` re-exports on the Web.
308
+ *
309
+ * The interfaces here are spelled `…NativeProps` because they are not the
310
+ * DOM ones, but a caller importing `DetailsProps` from `@hozo/core` should
311
+ * get a type on both platforms rather than one.
312
+ */
313
+ export type DetailsProps = DetailsNativeProps
314
+ export type TimeProps = TimeNativeProps
package/src/index.tsx ADDED
@@ -0,0 +1,250 @@
1
+ import type { CSSProperties, ReactNode } from 'react'
2
+
3
+ export interface SemanticsUniversalProps {
4
+ className?: string
5
+ children?: ReactNode
6
+ style?: CSSProperties
7
+ testID?: string
8
+ nativeID?: string
9
+ role?: string
10
+ accessibilityLabel?: string
11
+ accessibilityHint?: string
12
+ 'aria-hidden'?: boolean
13
+ 'aria-label'?: string
14
+ }
15
+
16
+ export interface TimeProps extends SemanticsUniversalProps {
17
+ dateTime?: string
18
+ datetime?: string
19
+ }
20
+
21
+ function domProps(props: Omit<SemanticsUniversalProps, 'style' | 'className' | 'children'>) {
22
+ return {
23
+ 'data-testid': props.testID,
24
+ id: props.nativeID,
25
+ role: props.role,
26
+ 'aria-label': props['aria-label'] ?? props.accessibilityLabel,
27
+ 'aria-description': props.accessibilityHint,
28
+ 'aria-hidden': props['aria-hidden'],
29
+ }
30
+ }
31
+
32
+ export function Main({ className, children, style, ...props }: SemanticsUniversalProps) {
33
+ return (
34
+ <main className={className} style={style} {...domProps(props)}>
35
+ {children}
36
+ </main>
37
+ )
38
+ }
39
+
40
+ export function Header({ className, children, style, ...props }: SemanticsUniversalProps) {
41
+ return (
42
+ <header className={className} style={style} {...domProps(props)}>
43
+ {children}
44
+ </header>
45
+ )
46
+ }
47
+
48
+ export function Footer({ className, children, style, ...props }: SemanticsUniversalProps) {
49
+ return (
50
+ <footer className={className} style={style} {...domProps(props)}>
51
+ {children}
52
+ </footer>
53
+ )
54
+ }
55
+
56
+ export function Aside({ className, children, style, ...props }: SemanticsUniversalProps) {
57
+ return (
58
+ <aside className={className} style={style} {...domProps(props)}>
59
+ {children}
60
+ </aside>
61
+ )
62
+ }
63
+
64
+ export function Search({ className, children, style, ...props }: SemanticsUniversalProps) {
65
+ return (
66
+ <search className={className} style={style} {...domProps(props)}>
67
+ {children}
68
+ </search>
69
+ )
70
+ }
71
+
72
+ export function Section({ className, children, style, ...props }: SemanticsUniversalProps) {
73
+ return (
74
+ <section className={className} style={style} {...domProps(props)}>
75
+ {children}
76
+ </section>
77
+ )
78
+ }
79
+
80
+ export function Article({ className, children, style, ...props }: SemanticsUniversalProps) {
81
+ return (
82
+ <article className={className} style={style} {...domProps(props)}>
83
+ {children}
84
+ </article>
85
+ )
86
+ }
87
+
88
+ export function Nav({ className, children, style, ...props }: SemanticsUniversalProps) {
89
+ return (
90
+ <nav className={className} style={style} {...domProps(props)}>
91
+ {children}
92
+ </nav>
93
+ )
94
+ }
95
+
96
+ export interface ListProps extends SemanticsUniversalProps {
97
+ ordered?: boolean
98
+ }
99
+
100
+ export function List({ ordered = false, className, children, style, ...props }: ListProps) {
101
+ const Tag = ordered ? 'ol' : 'ul'
102
+ return (
103
+ <Tag className={className} style={style} {...domProps(props)}>
104
+ {children}
105
+ </Tag>
106
+ )
107
+ }
108
+
109
+ export function ListItem({ className, children, style, ...props }: SemanticsUniversalProps) {
110
+ return (
111
+ <li className={className} style={style} {...domProps(props)}>
112
+ {children}
113
+ </li>
114
+ )
115
+ }
116
+
117
+ export function Figure({ className, children, style, ...props }: SemanticsUniversalProps) {
118
+ return (
119
+ <figure className={className} style={style} {...domProps(props)}>
120
+ {children}
121
+ </figure>
122
+ )
123
+ }
124
+
125
+ export function Figcaption({ className, children, style, ...props }: SemanticsUniversalProps) {
126
+ return (
127
+ <figcaption className={className} style={style} {...domProps(props)}>
128
+ {children}
129
+ </figcaption>
130
+ )
131
+ }
132
+
133
+ export function Time({ className, children, style, dateTime, datetime, ...props }: TimeProps) {
134
+ return (
135
+ <time className={className} style={style} dateTime={dateTime ?? datetime} {...domProps(props)}>
136
+ {children}
137
+ </time>
138
+ )
139
+ }
140
+
141
+ export function Address({ className, children, style, ...props }: SemanticsUniversalProps) {
142
+ return (
143
+ <address className={className} style={style} {...domProps(props)}>
144
+ {children}
145
+ </address>
146
+ )
147
+ }
148
+
149
+ export function Fieldset({ className, children, style, ...props }: SemanticsUniversalProps) {
150
+ return (
151
+ <fieldset className={className} style={style} {...domProps(props)}>
152
+ {children}
153
+ </fieldset>
154
+ )
155
+ }
156
+
157
+ export function Legend({ className, children, style, ...props }: SemanticsUniversalProps) {
158
+ return (
159
+ <legend className={className} style={style} {...domProps(props)}>
160
+ {children}
161
+ </legend>
162
+ )
163
+ }
164
+
165
+ export interface DetailsProps extends SemanticsUniversalProps {
166
+ open?: boolean
167
+ }
168
+
169
+ export function Details({ className, children, style, open, ...props }: DetailsProps) {
170
+ return (
171
+ <details className={className} style={style} open={open} {...domProps(props)}>
172
+ {children}
173
+ </details>
174
+ )
175
+ }
176
+
177
+ export function Summary({ className, children, style, ...props }: SemanticsUniversalProps) {
178
+ return (
179
+ <summary className={className} style={style} {...domProps(props)}>
180
+ {children}
181
+ </summary>
182
+ )
183
+ }
184
+
185
+ export function Term({ className, children, style, ...props }: SemanticsUniversalProps) {
186
+ return (
187
+ <dt className={className} style={style} {...domProps(props)}>
188
+ {children}
189
+ </dt>
190
+ )
191
+ }
192
+
193
+ export function Description({ className, children, style, ...props }: SemanticsUniversalProps) {
194
+ return (
195
+ <dd className={className} style={style} {...domProps(props)}>
196
+ {children}
197
+ </dd>
198
+ )
199
+ }
200
+
201
+ export function TermList({ className, children, style, ...props }: SemanticsUniversalProps) {
202
+ return (
203
+ <dl className={className} style={style} {...domProps(props)}>
204
+ {children}
205
+ </dl>
206
+ )
207
+ }
208
+
209
+ TermList.Term = Term
210
+ TermList.Description = Description
211
+
212
+ export interface SeparatorProps extends SemanticsUniversalProps {
213
+ orientation?: 'horizontal' | 'vertical'
214
+ decorative?: boolean
215
+ }
216
+
217
+ export function Separator({
218
+ className,
219
+ style,
220
+ orientation = 'horizontal',
221
+ decorative = false,
222
+ role,
223
+ ...props
224
+ }: SeparatorProps) {
225
+ const dom = domProps(props)
226
+ return (
227
+ <hr
228
+ className={className}
229
+ style={style}
230
+ aria-orientation={orientation === 'vertical' ? 'vertical' : undefined}
231
+ {...dom}
232
+ role={role ?? (decorative ? 'none' : 'separator')}
233
+ aria-hidden={decorative ? true : dom['aria-hidden']}
234
+ />
235
+ )
236
+ }
237
+
238
+ export interface ProgressProps extends SemanticsUniversalProps {
239
+ value?: number
240
+ max?: number
241
+ children?: ReactNode
242
+ }
243
+
244
+ export function Progress({ value, max, className, children, style, ...props }: ProgressProps) {
245
+ return (
246
+ <progress value={value} max={max} className={className} style={style} {...domProps(props)}>
247
+ {children ?? (value != null ? `${value}%` : undefined)}
248
+ </progress>
249
+ )
250
+ }