@hozo/typography 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 +162 -0
- package/dist/fonts-expo.d.ts +27 -0
- package/dist/fonts-expo.d.ts.map +1 -0
- package/dist/fonts-expo.js +63 -0
- package/dist/fonts-expo.js.map +1 -0
- package/dist/fonts-native.d.ts +28 -0
- package/dist/fonts-native.d.ts.map +1 -0
- package/dist/fonts-native.js +47 -0
- package/dist/fonts-native.js.map +1 -0
- package/dist/fonts.d.ts +62 -0
- package/dist/fonts.d.ts.map +1 -0
- package/dist/fonts.js +172 -0
- package/dist/fonts.js.map +1 -0
- package/dist/generated/ruby.d.ts +2 -0
- package/dist/generated/ruby.d.ts.map +1 -0
- package/dist/generated/ruby.js +2 -0
- package/dist/generated/ruby.js.map +1 -0
- package/dist/generated/ruby.native.d.ts +2 -0
- package/dist/generated/ruby.native.d.ts.map +1 -0
- package/dist/generated/ruby.native.js +2 -0
- package/dist/generated/ruby.native.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.d.ts +82 -0
- package/dist/index.native.d.ts.map +1 -0
- package/dist/index.native.js +136 -0
- package/dist/index.native.js.map +1 -0
- package/dist/ruby.native.d.ts +43 -0
- package/dist/ruby.native.d.ts.map +1 -0
- package/dist/ruby.native.js +50 -0
- package/dist/ruby.native.js.map +1 -0
- package/dist/text-size.d.ts +35 -0
- package/dist/text-size.d.ts.map +1 -0
- package/dist/text-size.js +40 -0
- package/dist/text-size.js.map +1 -0
- package/package.json +93 -0
- package/src/fonts-expo.ts +90 -0
- package/src/fonts-native.ts +85 -0
- package/src/fonts.ts +240 -0
- package/src/generated/ruby.native.ts +6 -0
- package/src/generated/ruby.ts +1 -0
- package/src/index.native.tsx +240 -0
- package/src/index.tsx +152 -0
- package/src/ruby.native.tsx +91 -0
- package/src/text-size.ts +41 -0
package/src/index.tsx
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Link, type LinkProps, Text, type TextProps } from '@hozo/primitives'
|
|
2
|
+
import type { CSSProperties, ReactNode } from 'react'
|
|
3
|
+
|
|
4
|
+
export { Link, type LinkProps, Text, type TextProps }
|
|
5
|
+
|
|
6
|
+
export interface TypographyUniversalProps {
|
|
7
|
+
className?: string
|
|
8
|
+
children?: ReactNode
|
|
9
|
+
style?: CSSProperties
|
|
10
|
+
testID?: string
|
|
11
|
+
nativeID?: string
|
|
12
|
+
accessibilityLabel?: string
|
|
13
|
+
accessibilityHint?: string
|
|
14
|
+
'aria-hidden'?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type SemanticTextProps = TypographyUniversalProps
|
|
18
|
+
|
|
19
|
+
export interface HeadingProps extends TypographyUniversalProps {
|
|
20
|
+
level?: 1 | 2 | 3 | 4 | 5 | 6
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function domProps(props: Omit<TypographyUniversalProps, 'style' | 'className' | 'children'>) {
|
|
24
|
+
return {
|
|
25
|
+
'data-testid': props.testID,
|
|
26
|
+
id: props.nativeID,
|
|
27
|
+
'aria-label': props.accessibilityLabel,
|
|
28
|
+
'aria-description': props.accessibilityHint,
|
|
29
|
+
'aria-hidden': props['aria-hidden'],
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function Paragraph({ className, children, style, ...props }: SemanticTextProps) {
|
|
34
|
+
return (
|
|
35
|
+
<p className={className} style={style} {...domProps(props)}>
|
|
36
|
+
{children}
|
|
37
|
+
</p>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function Heading({ level = 1, className, children, style, ...props }: HeadingProps) {
|
|
42
|
+
const Tag = `h${Math.min(6, Math.max(1, level))}` as 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
|
|
43
|
+
return (
|
|
44
|
+
<Tag className={className} style={style} {...domProps(props)}>
|
|
45
|
+
{children}
|
|
46
|
+
</Tag>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function Strong({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
51
|
+
return (
|
|
52
|
+
<strong className={className} style={style} {...domProps(props)}>
|
|
53
|
+
{children}
|
|
54
|
+
</strong>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function Emphasis({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
59
|
+
return (
|
|
60
|
+
<em className={className} style={style} {...domProps(props)}>
|
|
61
|
+
{children}
|
|
62
|
+
</em>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function Underline({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
67
|
+
return (
|
|
68
|
+
<u className={className} style={style} {...domProps(props)}>
|
|
69
|
+
{children}
|
|
70
|
+
</u>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function Strikethrough({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
75
|
+
return (
|
|
76
|
+
<s className={className} style={style} {...domProps(props)}>
|
|
77
|
+
{children}
|
|
78
|
+
</s>
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const Del = Strikethrough
|
|
83
|
+
|
|
84
|
+
export function Sub({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
85
|
+
return (
|
|
86
|
+
<sub className={className} style={style} {...domProps(props)}>
|
|
87
|
+
{children}
|
|
88
|
+
</sub>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function Sup({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
93
|
+
return (
|
|
94
|
+
<sup className={className} style={style} {...domProps(props)}>
|
|
95
|
+
{children}
|
|
96
|
+
</sup>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function Code({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
101
|
+
return (
|
|
102
|
+
<code className={className} style={style} {...domProps(props)}>
|
|
103
|
+
{children}
|
|
104
|
+
</code>
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function Small({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
109
|
+
return (
|
|
110
|
+
<small className={className} style={style} {...domProps(props)}>
|
|
111
|
+
{children}
|
|
112
|
+
</small>
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function Mark({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
117
|
+
return (
|
|
118
|
+
<mark className={className} style={style} {...domProps(props)}>
|
|
119
|
+
{children}
|
|
120
|
+
</mark>
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function NoBreak({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
125
|
+
return (
|
|
126
|
+
<span className={className} style={{ whiteSpace: 'nowrap', ...style }} {...domProps(props)}>
|
|
127
|
+
{children}
|
|
128
|
+
</span>
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function Ruby({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
133
|
+
return (
|
|
134
|
+
<ruby className={className} style={style} {...domProps(props)}>
|
|
135
|
+
{children}
|
|
136
|
+
</ruby>
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function RubyText({ className, children, style, ...props }: TypographyUniversalProps) {
|
|
141
|
+
return (
|
|
142
|
+
<rt className={className} style={style} {...domProps(props)}>
|
|
143
|
+
{children}
|
|
144
|
+
</rt>
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Both spellings, the way `TermList` reads both. The member keeps the
|
|
149
|
+
// full name rather than shortening to `Text`: a flat export has to be
|
|
150
|
+
// readable on its own, and a `<Text>` that meant the annotation would
|
|
151
|
+
// not be. The compiler recognises the pair in `hozo_parser`.
|
|
152
|
+
Ruby.RubyText = RubyText
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Ruby that is read once.
|
|
2
|
+
//
|
|
3
|
+
// `<ruby>漢字<rt>かんじ</rt></ruby>` on the Web is one element a browser
|
|
4
|
+
// knows how to announce. React Native has no ruby layout, so both halves
|
|
5
|
+
// are `Text` -- and nested `Text` is flattened into a single
|
|
6
|
+
// accessibility node, so a screen reader reads the base and then the
|
|
7
|
+
// reading: 「漢字かんじ」. The word twice, the second time spelled out.
|
|
8
|
+
//
|
|
9
|
+
// `accessible={false}` on the annotation does not help, because the
|
|
10
|
+
// flattening happens above it: the parent's label is built from the text
|
|
11
|
+
// it contains, whatever the children say about themselves. The lever that
|
|
12
|
+
// does work is the parent's own `accessibilityLabel`, which replaces the
|
|
13
|
+
// children rather than being assembled from them.
|
|
14
|
+
//
|
|
15
|
+
// The compiler writes that label itself when the base text is static,
|
|
16
|
+
// which is most ruby. This pair is for when it is not -- the same
|
|
17
|
+
// arrangement `HozoRelativeText` has, and the same reason: a value only
|
|
18
|
+
// React Native can see.
|
|
19
|
+
|
|
20
|
+
import { Children, isValidElement, type ReactNode } from 'react'
|
|
21
|
+
import { type StyleProp, Text, type TextStyle } from 'react-native'
|
|
22
|
+
|
|
23
|
+
export interface HozoRubyProps {
|
|
24
|
+
/**
|
|
25
|
+
* Tailwind classes, the same prop the Web half takes.
|
|
26
|
+
*
|
|
27
|
+
* On a tag the compiler lowers it is gone by runtime, replaced by a
|
|
28
|
+
* `StyleSheet` entry. Anywhere else it is carried and ignored here --
|
|
29
|
+
* this side has no CSS engine to resolve a class list against -- and
|
|
30
|
+
* the type still has to accept it, because an app is type-checked
|
|
31
|
+
* against the source the compiler reads rather than its output.
|
|
32
|
+
*/
|
|
33
|
+
className?: string
|
|
34
|
+
children?: ReactNode
|
|
35
|
+
/** The author's own, which wins: they know what the word is. */
|
|
36
|
+
accessibilityLabel?: string
|
|
37
|
+
style?: StyleProp<TextStyle>
|
|
38
|
+
testID?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface HozoRubyTextProps {
|
|
42
|
+
/**
|
|
43
|
+
* Tailwind classes, the same prop the Web half takes.
|
|
44
|
+
*
|
|
45
|
+
* On a tag the compiler lowers it is gone by runtime, replaced by a
|
|
46
|
+
* `StyleSheet` entry. Anywhere else it is carried and ignored here --
|
|
47
|
+
* this side has no CSS engine to resolve a class list against -- and
|
|
48
|
+
* the type still has to accept it, because an app is type-checked
|
|
49
|
+
* against the source the compiler reads rather than its output.
|
|
50
|
+
*/
|
|
51
|
+
className?: string
|
|
52
|
+
children?: ReactNode
|
|
53
|
+
style?: StyleProp<TextStyle>
|
|
54
|
+
testID?: string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The reading, and the half that must not be announced.
|
|
59
|
+
*
|
|
60
|
+
* Declared before `HozoRuby` because that one compares against it by
|
|
61
|
+
* identity to decide what the word is.
|
|
62
|
+
*/
|
|
63
|
+
export function HozoRubyText({ children, ...props }: HozoRubyTextProps) {
|
|
64
|
+
return (
|
|
65
|
+
<Text accessible={false} {...props}>
|
|
66
|
+
{children}
|
|
67
|
+
</Text>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Every string under a node, with the readings left out. */
|
|
72
|
+
function baseText(children: ReactNode): string {
|
|
73
|
+
return Children.toArray(children)
|
|
74
|
+
.map((child) => {
|
|
75
|
+
if (typeof child === 'string' || typeof child === 'number') return String(child)
|
|
76
|
+
if (!isValidElement(child)) return ''
|
|
77
|
+
if (child.type === HozoRubyText) return ''
|
|
78
|
+
const nested = (child.props as { children?: ReactNode }).children
|
|
79
|
+
return nested === undefined ? '' : baseText(nested)
|
|
80
|
+
})
|
|
81
|
+
.join('')
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function HozoRuby({ children, accessibilityLabel, ...props }: HozoRubyProps) {
|
|
85
|
+
const label = accessibilityLabel ?? baseText(children)
|
|
86
|
+
return (
|
|
87
|
+
<Text accessibilityLabel={label === '' ? undefined : label} {...props}>
|
|
88
|
+
{children}
|
|
89
|
+
</Text>
|
|
90
|
+
)
|
|
91
|
+
}
|
package/src/text-size.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The ratio table, kept in a file with no React Native in it.
|
|
2
|
+
//
|
|
3
|
+
// `ratios.test.ts` checks these against the compiler's copy, and a Node
|
|
4
|
+
// test cannot import `index.native.tsx`: React Native ships Flow-typed
|
|
5
|
+
// JavaScript that Node will not parse. The table is data either way.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* How much smaller than the text around it each of these draws.
|
|
9
|
+
*
|
|
10
|
+
* The same table the compiler applies in `crates/hozo_native/src/text.rs`,
|
|
11
|
+
* and `ratios.test.ts` reads that file to check the two still agree. Two
|
|
12
|
+
* copies of a number is how a compiled build and an uncompiled one come
|
|
13
|
+
* to render the same source at different sizes -- which is what the
|
|
14
|
+
* constants these replace were already doing.
|
|
15
|
+
*
|
|
16
|
+
* These are the numbers a project shipping Tailwind's preflight renders,
|
|
17
|
+
* which is what `preflight: 'auto'` gives anything using Tailwind. The
|
|
18
|
+
* bare user agent is a second set below. `@hozo/metro` carries the resolved
|
|
19
|
+
* choice through `@hozo/engine/project`, so the compiler and fallback
|
|
20
|
+
* components select the same table (#336).
|
|
21
|
+
*
|
|
22
|
+
* `small` was 0.85 here and in the compiler, and 0.85 is neither number.
|
|
23
|
+
* Measured in headless Chrome at bases 16, 20 and 32: 0.80 with the
|
|
24
|
+
* reset, 0.8333 without it.
|
|
25
|
+
*/
|
|
26
|
+
export const TEXT_SIZE_RATIOS = {
|
|
27
|
+
sub: 0.75,
|
|
28
|
+
sup: 0.75,
|
|
29
|
+
small: 0.8,
|
|
30
|
+
rubyText: 0.5,
|
|
31
|
+
heading: [2, 1.5, 1.17, 1, 0.83, 0.67],
|
|
32
|
+
} as const
|
|
33
|
+
|
|
34
|
+
/** Browser defaults when the project deliberately ships no reset. */
|
|
35
|
+
export const BARE_TEXT_SIZE_RATIOS = {
|
|
36
|
+
sub: 1 / 1.2,
|
|
37
|
+
sup: 1 / 1.2,
|
|
38
|
+
small: 1 / 1.2,
|
|
39
|
+
rubyText: 0.5,
|
|
40
|
+
heading: TEXT_SIZE_RATIOS.heading,
|
|
41
|
+
} as const
|