@hozo/typography 0.3.0 → 0.4.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 +26 -0
- package/README.md +1 -1
- package/dist/code/meta.d.ts +14 -0
- package/dist/code/meta.d.ts.map +1 -0
- package/dist/code/meta.js +62 -0
- package/dist/code/meta.js.map +1 -0
- package/dist/code/model.d.ts +66 -0
- package/dist/code/model.d.ts.map +1 -0
- package/dist/code/model.js +139 -0
- package/dist/code/model.js.map +1 -0
- package/dist/code/reuse.d.ts +22 -0
- package/dist/code/reuse.d.ts.map +1 -0
- package/dist/code/reuse.js +54 -0
- package/dist/code/reuse.js.map +1 -0
- package/dist/code/shiki.d.ts +12 -0
- package/dist/code/shiki.d.ts.map +1 -0
- package/dist/code/shiki.js +28 -0
- package/dist/code/shiki.js.map +1 -0
- package/dist/code-lines.d.ts +34 -0
- package/dist/code-lines.d.ts.map +1 -0
- package/dist/code-lines.js +192 -0
- package/dist/code-lines.js.map +1 -0
- package/dist/code-lines.native.d.ts +54 -0
- package/dist/code-lines.native.d.ts.map +1 -0
- package/dist/code-lines.native.js +114 -0
- package/dist/code-lines.native.js.map +1 -0
- package/dist/index.d.ts +3 -41
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -69
- package/dist/index.js.map +1 -1
- package/dist/index.native.d.ts +3 -82
- package/dist/index.native.d.ts.map +1 -1
- package/dist/index.native.js +3 -140
- package/dist/index.native.js.map +1 -1
- package/dist/text.d.ts +43 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +75 -0
- package/dist/text.js.map +1 -0
- package/dist/text.native.d.ts +84 -0
- package/dist/text.native.d.ts.map +1 -0
- package/dist/text.native.js +144 -0
- package/dist/text.native.js.map +1 -0
- package/package.json +30 -4
- package/src/code/meta.ts +69 -0
- package/src/code/model.ts +189 -0
- package/src/code/reuse.ts +70 -0
- package/src/code/shiki.ts +38 -0
- package/src/code-lines.native.tsx +191 -0
- package/src/code-lines.tsx +310 -0
- package/src/index.native.tsx +4 -245
- package/src/index.tsx +4 -164
- package/src/text.native.tsx +250 -0
- package/src/text.tsx +179 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// biome-ignore-all lint/suspicious/noArrayIndexKey: code lines and tokens use source-position identity
|
|
2
|
+
import { useHozoMessage } from '@hozo/behaviors'
|
|
3
|
+
import {
|
|
4
|
+
initialRange,
|
|
5
|
+
ListMetrics,
|
|
6
|
+
type WindowRange,
|
|
7
|
+
windowRange,
|
|
8
|
+
} from '@hozo/primitives/windowing'
|
|
9
|
+
import {
|
|
10
|
+
type CSSProperties,
|
|
11
|
+
type ReactNode,
|
|
12
|
+
type RefObject,
|
|
13
|
+
useCallback,
|
|
14
|
+
useEffect,
|
|
15
|
+
useLayoutEffect,
|
|
16
|
+
useRef,
|
|
17
|
+
useState,
|
|
18
|
+
} from 'react'
|
|
19
|
+
import { type CodeLine, type CodeMark, type CodeToken, widestCodeLine } from './code/model.ts'
|
|
20
|
+
import { codeKey, useCodeRows, useNormalizedCodeLines } from './code/reuse.ts'
|
|
21
|
+
|
|
22
|
+
export { codeKey, useNormalizedCodeLines }
|
|
23
|
+
|
|
24
|
+
export interface CodeLinesProps {
|
|
25
|
+
lines: readonly CodeLine[] | string
|
|
26
|
+
tabSize?: number
|
|
27
|
+
theme?: string
|
|
28
|
+
wrap?: boolean
|
|
29
|
+
className?: string
|
|
30
|
+
lineClassName?: string
|
|
31
|
+
insClassName?: string
|
|
32
|
+
delClassName?: string
|
|
33
|
+
markClassName?: string
|
|
34
|
+
style?: CSSProperties
|
|
35
|
+
lineStyle?: CSSProperties
|
|
36
|
+
insStyle?: CSSProperties
|
|
37
|
+
delStyle?: CSSProperties
|
|
38
|
+
markStyle?: CSSProperties
|
|
39
|
+
/** @internal A separate, non-selectable gutter supplied by CodeBlock. */
|
|
40
|
+
renderGutter?: (line: CodeLine, index: number) => ReactNode
|
|
41
|
+
/**
|
|
42
|
+
* @internal Everything `renderGutter` draws from besides the line and its
|
|
43
|
+
* index, as `codeKey` gives it. Without it, a gutter's rows are not reused.
|
|
44
|
+
*/
|
|
45
|
+
gutterKey?: string
|
|
46
|
+
/**
|
|
47
|
+
* @internal The element the lines scroll in. Given, only the lines near
|
|
48
|
+
* the visible ones are mounted; CodeBlock decides when (#1380).
|
|
49
|
+
*/
|
|
50
|
+
scroller?: RefObject<HTMLElement | null>
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const hidden: CSSProperties = {
|
|
54
|
+
position: 'absolute',
|
|
55
|
+
width: 1,
|
|
56
|
+
height: 1,
|
|
57
|
+
padding: 0,
|
|
58
|
+
margin: -1,
|
|
59
|
+
overflow: 'hidden',
|
|
60
|
+
clipPath: 'inset(50%)',
|
|
61
|
+
whiteSpace: 'nowrap',
|
|
62
|
+
userSelect: 'none',
|
|
63
|
+
}
|
|
64
|
+
// The whole text of a windowed block, for a screen reader: its own reading
|
|
65
|
+
// moves through the document without scrolling the block, so it would stop
|
|
66
|
+
// at the last mounted line (#1408). One plain row per line costs a fraction
|
|
67
|
+
// of the token rows it stands in for. Line breaks are kept, not collapsed,
|
|
68
|
+
// and select-all and copy stay with the visible rows. Not
|
|
69
|
+
// `content-visibility: auto`, which would be cheaper still: Chrome then
|
|
70
|
+
// leaves the text out of the accessibility tree.
|
|
71
|
+
const readable: CSSProperties = { ...hidden, whiteSpace: 'pre' }
|
|
72
|
+
const tokenCss =
|
|
73
|
+
'.hozo-code-token{color:var(--hozo-code-light,inherit)}@media(prefers-color-scheme:dark){.hozo-code-token{color:var(--hozo-code-dark,var(--hozo-code-light,inherit))}}'
|
|
74
|
+
|
|
75
|
+
function tokenStyle(token: CodeToken, theme?: string): CSSProperties {
|
|
76
|
+
const colors = typeof token.color === 'object' ? token.color : undefined
|
|
77
|
+
return {
|
|
78
|
+
color:
|
|
79
|
+
typeof token.color === 'string'
|
|
80
|
+
? token.color
|
|
81
|
+
: theme
|
|
82
|
+
? (colors?.[theme] ?? colors?.light)
|
|
83
|
+
: undefined,
|
|
84
|
+
...(colors && !theme
|
|
85
|
+
? { '--hozo-code-light': colors.light, '--hozo-code-dark': colors.dark }
|
|
86
|
+
: {}),
|
|
87
|
+
fontStyle: token.italic ? 'italic' : undefined,
|
|
88
|
+
fontWeight: token.bold ? 'bold' : undefined,
|
|
89
|
+
textDecoration: token.underline ? 'underline' : undefined,
|
|
90
|
+
} as CSSProperties
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// What a line is taken to be before one has been measured, and how many go
|
|
94
|
+
// in the first paint: a viewport's worth at ordinary sizes, so the server's
|
|
95
|
+
// HTML shows the top of the block rather than a gap.
|
|
96
|
+
const ESTIMATED_LINE = 20
|
|
97
|
+
const INITIAL_LINES = 40
|
|
98
|
+
// Two viewports either side, as on Native (#1382), and enough new lines per
|
|
99
|
+
// pass that 20px lines fill a viewport in a commit or two.
|
|
100
|
+
const WINDOW_SIZE = 5
|
|
101
|
+
const LINES_PER_BATCH = 50
|
|
102
|
+
// The server has nothing to measure, and `useLayoutEffect` warns there.
|
|
103
|
+
const useBrowserLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Which lines to mount, from where `scroller` is scrolled to.
|
|
107
|
+
*
|
|
108
|
+
* The arithmetic is the Web `FlatList`'s, but the lines stay one `<pre>` in
|
|
109
|
+
* the scroller `CodeBlock` already has rather than becoming rows of a list:
|
|
110
|
+
* what is mounted is still code to a screen reader, and the region still
|
|
111
|
+
* scrolls sideways. Lines are measured after every commit and whenever the
|
|
112
|
+
* scroller resizes, which is when a wrapped line can change height.
|
|
113
|
+
*/
|
|
114
|
+
function useLineWindow(count: number, scroller: RefObject<HTMLElement | null> | undefined) {
|
|
115
|
+
const code = useRef<HTMLElement>(null)
|
|
116
|
+
const metricsRef = useRef<ListMetrics>(undefined as unknown as ListMetrics)
|
|
117
|
+
if (metricsRef.current === undefined) metricsRef.current = new ListMetrics(ESTIMATED_LINE)
|
|
118
|
+
const metrics = metricsRef.current
|
|
119
|
+
metrics.setKeys(Array.from({ length: count }, (_, index) => String(index)))
|
|
120
|
+
const all: WindowRange = { first: 0, last: count - 1 }
|
|
121
|
+
const [range, setRange] = useState(() => (scroller ? initialRange(count, INITIAL_LINES) : all))
|
|
122
|
+
const rangeRef = useRef(range)
|
|
123
|
+
rangeRef.current = range
|
|
124
|
+
|
|
125
|
+
const update = useCallback(() => {
|
|
126
|
+
const root = scroller?.current
|
|
127
|
+
const list = code.current
|
|
128
|
+
if (!root || !list || root.clientHeight === 0) return
|
|
129
|
+
for (const line of list.querySelectorAll<HTMLElement>(':scope > [data-code-line]'))
|
|
130
|
+
metrics.measure(line.dataset.codeLine ?? '', line.getBoundingClientRect().height)
|
|
131
|
+
// Where line 0 starts in the scroller's content; padding above the lines
|
|
132
|
+
// moves it.
|
|
133
|
+
const origin =
|
|
134
|
+
list.getBoundingClientRect().top - root.getBoundingClientRect().top + root.scrollTop
|
|
135
|
+
const next = windowRange({
|
|
136
|
+
count,
|
|
137
|
+
offset: root.scrollTop - origin,
|
|
138
|
+
visibleLength: root.clientHeight,
|
|
139
|
+
windowSize: WINDOW_SIZE,
|
|
140
|
+
maxToRenderPerBatch: LINES_PER_BATCH,
|
|
141
|
+
previous: rangeRef.current,
|
|
142
|
+
metrics,
|
|
143
|
+
})
|
|
144
|
+
if (next.first === rangeRef.current.first && next.last === rangeRef.current.last) return
|
|
145
|
+
rangeRef.current = next
|
|
146
|
+
setRange(next)
|
|
147
|
+
}, [count, scroller, metrics])
|
|
148
|
+
|
|
149
|
+
// A mounted line is a measurable one, and measuring usually moves the
|
|
150
|
+
// window, so this runs after every commit rather than only on scroll.
|
|
151
|
+
useBrowserLayoutEffect(() => {
|
|
152
|
+
update()
|
|
153
|
+
})
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
const root = scroller?.current
|
|
156
|
+
if (!root) return
|
|
157
|
+
root.addEventListener('scroll', update, { passive: true })
|
|
158
|
+
const observer = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(update)
|
|
159
|
+
observer?.observe(root)
|
|
160
|
+
return () => {
|
|
161
|
+
root.removeEventListener('scroll', update)
|
|
162
|
+
observer?.disconnect()
|
|
163
|
+
}
|
|
164
|
+
}, [scroller, update])
|
|
165
|
+
|
|
166
|
+
if (!scroller) return { code, range: all, leading: 0, trailing: 0 }
|
|
167
|
+
// Fewer lines than before can leave the window past the end until the
|
|
168
|
+
// next update; draw what exists.
|
|
169
|
+
const first = Math.max(0, Math.min(range.first, count - 1))
|
|
170
|
+
const last = Math.min(range.last, count - 1)
|
|
171
|
+
return {
|
|
172
|
+
code,
|
|
173
|
+
range: { first, last },
|
|
174
|
+
leading: count ? metrics.offsetAt(first) : 0,
|
|
175
|
+
trailing: count ? Math.max(0, metrics.offsetAt(count) - metrics.offsetAt(last + 1)) : 0,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function CodeLines(props: CodeLinesProps) {
|
|
180
|
+
const message = useHozoMessage()
|
|
181
|
+
const lines = useNormalizedCodeLines(props.lines, props.tabSize)
|
|
182
|
+
const labels: Record<CodeMark, string> = {
|
|
183
|
+
ins: message('hozo.code.added'),
|
|
184
|
+
del: message('hozo.code.removed'),
|
|
185
|
+
mark: message('hozo.code.highlighted'),
|
|
186
|
+
}
|
|
187
|
+
const shown = useLineWindow(lines.length, props.scroller)
|
|
188
|
+
const renderLine = useCodeRows(
|
|
189
|
+
lines,
|
|
190
|
+
props.renderGutter && props.gutterKey === undefined
|
|
191
|
+
? undefined
|
|
192
|
+
: codeKey([
|
|
193
|
+
props.theme,
|
|
194
|
+
props.wrap,
|
|
195
|
+
labels,
|
|
196
|
+
props.gutterKey,
|
|
197
|
+
props.lineClassName,
|
|
198
|
+
props.insClassName,
|
|
199
|
+
props.delClassName,
|
|
200
|
+
props.markClassName,
|
|
201
|
+
props.lineStyle,
|
|
202
|
+
props.insStyle,
|
|
203
|
+
props.delStyle,
|
|
204
|
+
props.markStyle,
|
|
205
|
+
]),
|
|
206
|
+
drawLine,
|
|
207
|
+
)
|
|
208
|
+
const renderReadable = useCodeRows(lines, codeKey(labels), drawReadable)
|
|
209
|
+
function drawReadable(line: (typeof lines)[number], index: number) {
|
|
210
|
+
// A line feed between lines, not block rows: inside the one-pixel box
|
|
211
|
+
// NVDA read block rows as one run, "entry 1entry 2", wrapped at its own
|
|
212
|
+
// line length (#1408). Before the line rather than after it, so a line's
|
|
213
|
+
// row stays the same when lines are appended.
|
|
214
|
+
return (
|
|
215
|
+
<span key={index}>
|
|
216
|
+
{index > 0 ? '\n' : null}
|
|
217
|
+
{line.mark ? `${labels[line.mark]}: ` : null}
|
|
218
|
+
{line.tokens.map((token) => token.text).join('')}
|
|
219
|
+
</span>
|
|
220
|
+
)
|
|
221
|
+
}
|
|
222
|
+
function drawLine(line: (typeof lines)[number], index: number) {
|
|
223
|
+
const Tag = line.mark ?? 'span'
|
|
224
|
+
const className =
|
|
225
|
+
[props.lineClassName, line.mark ? props[`${line.mark}ClassName`] : undefined]
|
|
226
|
+
.filter(Boolean)
|
|
227
|
+
.join(' ') || undefined
|
|
228
|
+
return (
|
|
229
|
+
<span key={index} data-code-line={index} style={{ display: 'flex', minWidth: '100%' }}>
|
|
230
|
+
{props.renderGutter?.(line, index)}
|
|
231
|
+
<Tag
|
|
232
|
+
className={className}
|
|
233
|
+
data-code-mark={line.mark}
|
|
234
|
+
style={{
|
|
235
|
+
display: 'block',
|
|
236
|
+
flex: props.wrap ? '1 1 0' : '1 0 auto',
|
|
237
|
+
minWidth: 0,
|
|
238
|
+
minHeight: '1lh',
|
|
239
|
+
...props.lineStyle,
|
|
240
|
+
...(line.mark ? props[`${line.mark}Style`] : undefined),
|
|
241
|
+
}}
|
|
242
|
+
>
|
|
243
|
+
{line.mark ? <span style={hidden}>{`${labels[line.mark]}: `}</span> : null}
|
|
244
|
+
{line.tokens.map((token, at) => (
|
|
245
|
+
<span
|
|
246
|
+
key={at}
|
|
247
|
+
className={
|
|
248
|
+
typeof token.color === 'object' && !props.theme ? 'hozo-code-token' : undefined
|
|
249
|
+
}
|
|
250
|
+
style={tokenStyle(token, props.theme)}
|
|
251
|
+
>
|
|
252
|
+
{token.text}
|
|
253
|
+
</span>
|
|
254
|
+
))}
|
|
255
|
+
</Tag>
|
|
256
|
+
</span>
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
// An unwrapped block cannot be as wide as a line it has not mounted, so the
|
|
260
|
+
// widest one is drawn once more, at no height and hidden from reading and
|
|
261
|
+
// find-in-page, to hold the scrolling width open, as on Native.
|
|
262
|
+
const widest = props.scroller && !props.wrap ? widestCodeLine(lines) : undefined
|
|
263
|
+
return (
|
|
264
|
+
<>
|
|
265
|
+
<style href="hozo-code-tokens" precedence="hozo-base">
|
|
266
|
+
{tokenCss}
|
|
267
|
+
</style>
|
|
268
|
+
{props.scroller ? (
|
|
269
|
+
<pre dir="ltr" style={readable}>
|
|
270
|
+
<code>{lines.map(renderReadable)}</code>
|
|
271
|
+
</pre>
|
|
272
|
+
) : null}
|
|
273
|
+
<pre
|
|
274
|
+
// Windowed, the rows are only what is on screen; the text above is
|
|
275
|
+
// what is read.
|
|
276
|
+
aria-hidden={props.scroller ? true : undefined}
|
|
277
|
+
className={props.className}
|
|
278
|
+
dir="ltr"
|
|
279
|
+
style={{
|
|
280
|
+
margin: 0,
|
|
281
|
+
fontFamily: 'monospace',
|
|
282
|
+
lineHeight: 1.5,
|
|
283
|
+
...props.style,
|
|
284
|
+
whiteSpace: props.wrap ? 'pre-wrap' : 'pre',
|
|
285
|
+
overflowWrap: props.wrap ? 'anywhere' : undefined,
|
|
286
|
+
}}
|
|
287
|
+
>
|
|
288
|
+
<code ref={shown.code} style={{ font: 'inherit' }}>
|
|
289
|
+
{widest ? (
|
|
290
|
+
<span
|
|
291
|
+
aria-hidden="true"
|
|
292
|
+
style={{ display: 'block', width: 'max-content', height: 0, visibility: 'hidden' }}
|
|
293
|
+
>
|
|
294
|
+
{renderLine(widest.line, widest.index)}
|
|
295
|
+
</span>
|
|
296
|
+
) : null}
|
|
297
|
+
{shown.leading ? (
|
|
298
|
+
<span aria-hidden="true" style={{ display: 'block', height: shown.leading }} />
|
|
299
|
+
) : null}
|
|
300
|
+
{lines
|
|
301
|
+
.slice(shown.range.first, shown.range.last + 1)
|
|
302
|
+
.map((line, at) => renderLine(line, shown.range.first + at))}
|
|
303
|
+
{shown.trailing ? (
|
|
304
|
+
<span aria-hidden="true" style={{ display: 'block', height: shown.trailing }} />
|
|
305
|
+
) : null}
|
|
306
|
+
</code>
|
|
307
|
+
</pre>
|
|
308
|
+
</>
|
|
309
|
+
)
|
|
310
|
+
}
|
package/src/index.native.tsx
CHANGED
|
@@ -1,246 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import { hozoPreflight } from '@hozo/engine/project'
|
|
3
|
-
import { Link, Text } from '@hozo/primitives'
|
|
4
|
-
import React, { type ComponentProps, type ReactNode, useContext } from 'react'
|
|
5
|
-
// The components rather than their names. These files used to render
|
|
6
|
-
// `React.createElement('View')`, and React Native resolves a string tag
|
|
7
|
-
// through its view config registry, where the registered names are
|
|
8
|
-
// `RCTView` and `RCTText` -- never `View` or `Text`. Every one of these
|
|
9
|
-
// would have thrown "View config getter callback for component `View`
|
|
10
|
-
// must be a function" on the first render. Nothing caught it because
|
|
11
|
-
// nothing imported these files: the tests next to them render the Web
|
|
12
|
-
// half through `react-dom/server`.
|
|
13
|
-
import type { AccessibilityRole, StyleProp, TextStyle } from 'react-native'
|
|
14
|
-
import { HozoRuby, HozoRubyText } from './ruby.native.tsx'
|
|
15
|
-
import { BARE_TEXT_SIZE_RATIOS, TEXT_SIZE_RATIOS } from './text-size.ts'
|
|
1
|
+
// Keep data-driven code rendering out of apps that only use semantic text.
|
|
16
2
|
|
|
17
|
-
export {
|
|
18
|
-
export {
|
|
19
|
-
|
|
20
|
-
export interface TypographyNativeProps {
|
|
21
|
-
/**
|
|
22
|
-
* Tailwind classes, read by the compiler and gone by runtime.
|
|
23
|
-
*
|
|
24
|
-
* The Web half of this file has always declared it; this half never
|
|
25
|
-
* did, and nothing noticed because the `react-native` export condition
|
|
26
|
-
* carried no `types`, so a React Native app was type-checked against
|
|
27
|
-
* the Web declarations instead. Uncompiled, the prop is simply ignored
|
|
28
|
-
* here -- which is what the fallback components are for.
|
|
29
|
-
*/
|
|
30
|
-
className?: string
|
|
31
|
-
children?: ReactNode
|
|
32
|
-
/**
|
|
33
|
-
* React Native's own types rather than `any` and `string`.
|
|
34
|
-
*
|
|
35
|
-
* The Web half of this file says `CSSProperties`. This half agreed with
|
|
36
|
-
* anything, so a Web-only style reached a native `Text` in silence --
|
|
37
|
-
* the same erasure `@hozo/tailwind-conformance` exists to catch one
|
|
38
|
-
* layer down.
|
|
39
|
-
*/
|
|
40
|
-
style?: StyleProp<TextStyle>
|
|
41
|
-
testID?: string
|
|
42
|
-
nativeID?: string
|
|
43
|
-
accessibilityLabel?: string
|
|
44
|
-
accessibilityHint?: string
|
|
45
|
-
accessibilityRole?: AccessibilityRole
|
|
46
|
-
accessible?: boolean
|
|
47
|
-
numberOfLines?: number
|
|
48
|
-
ellipsizeMode?: 'head' | 'middle' | 'tail' | 'clip'
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// The public concept keeps one name across platform entry points even
|
|
52
|
-
// though its style field uses React Native's own type here.
|
|
53
|
-
export type TypographyUniversalProps = TypographyNativeProps
|
|
54
|
-
|
|
55
|
-
export type TextProps = TypographyNativeProps
|
|
56
|
-
export type SemanticTextProps = TypographyNativeProps
|
|
57
|
-
|
|
58
|
-
export interface HeadingProps extends TypographyNativeProps {
|
|
59
|
-
level?: 1 | 2 | 3 | 4 | 5 | 6
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Native declaration for the Link value re-exported from primitives. */
|
|
63
|
-
export interface LinkProps extends TypographyNativeProps {
|
|
64
|
-
href: string
|
|
65
|
-
external?: boolean
|
|
66
|
-
replace?: boolean
|
|
67
|
-
prefetch?: boolean
|
|
68
|
-
target?: string
|
|
69
|
-
rel?: string
|
|
70
|
-
download?: boolean | string
|
|
71
|
-
onPress?: (event: { defaultPrevented?: boolean }) => void
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The size the text around this is drawn at.
|
|
76
|
-
*
|
|
77
|
-
* `@hozo/engine`'s, not one of this package's own. The compiler emits
|
|
78
|
-
* `HozoTextSize` for an element whose size it could not read, and that
|
|
79
|
-
* publishes into this context -- so a compiled ancestor and one of the
|
|
80
|
-
* components below, rendered uncompiled, agree about the base. Two
|
|
81
|
-
* contexts would have been two answers.
|
|
82
|
-
*
|
|
83
|
-
* React Native offers nothing to build this on. Its `fontSize` is a
|
|
84
|
-
* number of points with no relative unit, a nested `Text` inherits
|
|
85
|
-
* visually but cannot read what it inherited, and the only ancestor
|
|
86
|
-
* context the platform exposes -- `TextAncestorContext` -- is a boolean
|
|
87
|
-
* saying whether there is a `Text` above at all.
|
|
88
|
-
*
|
|
89
|
-
* The default is React Native's own, and the constants this replaced --
|
|
90
|
-
* 11, 11, 12 -- are exactly the ratios applied to it.
|
|
91
|
-
*/
|
|
92
|
-
const TextSize = HozoTextSizeContext
|
|
93
|
-
const NativeText = Text as unknown as React.ComponentType<TypographyNativeProps>
|
|
94
|
-
|
|
95
|
-
function relative(ratio: number, base: number) {
|
|
96
|
-
return Math.round(base * ratio)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function Paragraph(props: SemanticTextProps) {
|
|
100
|
-
return <NativeText {...props} />
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function Heading({
|
|
104
|
-
level = 1,
|
|
105
|
-
accessibilityRole = 'header',
|
|
106
|
-
style,
|
|
107
|
-
...props
|
|
108
|
-
}: HeadingProps) {
|
|
109
|
-
// Preflight resets headings to inherited size and weight. Without it,
|
|
110
|
-
// reproduce the browser's heading defaults that Native does not have.
|
|
111
|
-
const base = useContext(TextSize)
|
|
112
|
-
const ratio = BARE_TEXT_SIZE_RATIOS.heading[Math.min(6, Math.max(1, level)) - 1] as number
|
|
113
|
-
const defaultStyle = hozoPreflight
|
|
114
|
-
? undefined
|
|
115
|
-
: { fontSize: relative(ratio, base), fontWeight: 'bold' as const }
|
|
116
|
-
return (
|
|
117
|
-
<NativeText accessibilityRole={accessibilityRole} style={[defaultStyle, style]} {...props} />
|
|
118
|
-
)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function Strong({ style, ...props }: TypographyNativeProps) {
|
|
122
|
-
return <NativeText style={[{ fontWeight: 'bold' }, style]} {...props} />
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export function Emphasis({ style, ...props }: TypographyNativeProps) {
|
|
126
|
-
return <NativeText style={[{ fontStyle: 'italic' }, style]} {...props} />
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function Underline({ style, ...props }: TypographyNativeProps) {
|
|
130
|
-
return <NativeText style={[{ textDecorationLine: 'underline' }, style]} {...props} />
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export function Strikethrough({ style, ...props }: TypographyNativeProps) {
|
|
134
|
-
return <NativeText style={[{ textDecorationLine: 'line-through' }, style]} {...props} />
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export const Del = Strikethrough
|
|
138
|
-
|
|
139
|
-
export function Sub({ style, ...props }: TypographyNativeProps) {
|
|
140
|
-
const base = useContext(TextSize)
|
|
141
|
-
const ratios = hozoPreflight ? TEXT_SIZE_RATIOS : BARE_TEXT_SIZE_RATIOS
|
|
142
|
-
return <NativeText style={[{ fontSize: relative(ratios.sub, base) }, style]} {...props} />
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function Sup({ style, ...props }: TypographyNativeProps) {
|
|
146
|
-
const base = useContext(TextSize)
|
|
147
|
-
const ratios = hozoPreflight ? TEXT_SIZE_RATIOS : BARE_TEXT_SIZE_RATIOS
|
|
148
|
-
return <NativeText style={[{ fontSize: relative(ratios.sup, base) }, style]} {...props} />
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function Code({ style, ...props }: TypographyNativeProps) {
|
|
152
|
-
return <NativeText style={[{ fontFamily: 'monospace' }, style]} {...props} />
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// A `Text` keeps spaces and line breaks as written already; what `<pre>`
|
|
156
|
-
// adds on the Web that a `Text` lacks is the monospace face.
|
|
157
|
-
export function Preformatted({ style, ...props }: TypographyNativeProps) {
|
|
158
|
-
return <NativeText style={[{ fontFamily: 'monospace' }, style]} {...props} />
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export function Small({ style, ...props }: TypographyNativeProps) {
|
|
162
|
-
const base = useContext(TextSize)
|
|
163
|
-
const ratios = hozoPreflight ? TEXT_SIZE_RATIOS : BARE_TEXT_SIZE_RATIOS
|
|
164
|
-
return (
|
|
165
|
-
<NativeText
|
|
166
|
-
style={[{ fontSize: relative(ratios.small, base), opacity: 0.8 }, style]}
|
|
167
|
-
{...props}
|
|
168
|
-
/>
|
|
169
|
-
)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function Mark({ style, ...props }: TypographyNativeProps) {
|
|
173
|
-
return <NativeText style={[{ backgroundColor: '#fef08a' }, style]} {...props} />
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/** Recursively replaces normal spaces with Unicode non-breaking spaces (\u00A0) */
|
|
177
|
-
function replaceSpacesWithNbsp(node: ReactNode): ReactNode {
|
|
178
|
-
if (typeof node === 'string') {
|
|
179
|
-
return node.replace(/ /g, '\u00A0')
|
|
180
|
-
}
|
|
181
|
-
if (Array.isArray(node)) {
|
|
182
|
-
return React.Children.map(node, replaceSpacesWithNbsp)
|
|
183
|
-
}
|
|
184
|
-
if (React.isValidElement(node)) {
|
|
185
|
-
const element = node as React.ReactElement<{ children?: ReactNode }>
|
|
186
|
-
if (element.props?.children) {
|
|
187
|
-
return React.cloneElement(element, {
|
|
188
|
-
children: replaceSpacesWithNbsp(element.props.children),
|
|
189
|
-
})
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return node
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export function NoBreak({ children, ...props }: TypographyNativeProps) {
|
|
196
|
-
return <NativeText {...props}>{replaceSpacesWithNbsp(children)}</NativeText>
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* A Native style handed over as if it were a Web one -- a resolver's
|
|
201
|
-
* limit rather than a claim about the value.
|
|
202
|
-
*
|
|
203
|
-
* A package resolves `@hozo/engine`'s types through the Web entry
|
|
204
|
-
* whichever platform it is building for, because `exports` carries a
|
|
205
|
-
* single `types` and this package compiles both halves in one `tsc` run.
|
|
206
|
-
* So these are seen with `style?: CSSProperties` here while the module
|
|
207
|
-
* Metro loads takes `StyleProp<TextStyle>`. `@hozo/semantics` has the
|
|
208
|
-
* same line for the same reason.
|
|
209
|
-
*/
|
|
210
|
-
const asWeb = (style: TypographyNativeProps['style']) =>
|
|
211
|
-
style as unknown as ComponentProps<typeof HozoRuby>['style']
|
|
212
|
-
/**
|
|
213
|
-
* Ruby, sharing the pair the Native backend emits.
|
|
214
|
-
*
|
|
215
|
-
* React Native flattens nested `Text` into one accessibility node, so a
|
|
216
|
-
* screen reader read the base and then the reading: 「漢字かんじ」. The
|
|
217
|
-
* `accessible={false}` that used to be on the annotation could not stop
|
|
218
|
-
* it -- the flattening happens above, and the parent's label is built
|
|
219
|
-
* from the text it contains whatever the children say about themselves.
|
|
220
|
-
*/
|
|
221
|
-
export function Ruby({ children, accessibilityLabel, style, ...props }: TypographyNativeProps) {
|
|
222
|
-
return (
|
|
223
|
-
<HozoRuby accessibilityLabel={accessibilityLabel} style={asWeb(style)} {...props}>
|
|
224
|
-
{children}
|
|
225
|
-
</HozoRuby>
|
|
226
|
-
)
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function RubyText({ style, children, ...props }: TypographyNativeProps) {
|
|
230
|
-
// Half the size of the text it annotates, against the size `Text`
|
|
231
|
-
// published; `fontSize: '0.65em'` was here and React Native ignored it.
|
|
232
|
-
const base = useContext(TextSize)
|
|
233
|
-
return (
|
|
234
|
-
<HozoRubyText
|
|
235
|
-
style={asWeb([{ fontSize: relative(TEXT_SIZE_RATIOS.rubyText, base), opacity: 0.85 }, style])}
|
|
236
|
-
{...props}
|
|
237
|
-
>
|
|
238
|
-
{children}
|
|
239
|
-
</HozoRubyText>
|
|
240
|
-
)
|
|
241
|
-
}
|
|
242
|
-
// Both spellings, the way `TermList` reads both. The member keeps the
|
|
243
|
-
// full name rather than shortening to `Text`: a flat export has to be
|
|
244
|
-
// readable on its own, and a `<Text>` that meant the annotation would
|
|
245
|
-
// not be. The compiler recognises the pair in `hozo_parser`.
|
|
246
|
-
Ruby.RubyText = RubyText
|
|
3
|
+
export type { CodeLine, CodeMark, CodeRange, CodeToken } from './code/model.ts'
|
|
4
|
+
export { CodeLines, type CodeLinesProps } from './code-lines.native.tsx'
|
|
5
|
+
export * from './text.native.tsx'
|