-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtypography.tsx
More file actions
230 lines (211 loc) · 7.55 KB
/
typography.tsx
File metadata and controls
230 lines (211 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use client';
import ChevronRightIcon from '@react-spectrum/s2/icons/ChevronRight';
import {getTextWidth} from './textWidth';
import {iconStyle, style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {Link} from '@react-spectrum/s2';
import LinkIcon from '@react-spectrum/s2/icons/Link';
import type {Page} from '@parcel/rsc';
import React, {HTMLAttributes, ReactNode} from 'react';
import {TitleLink} from './Link';
import {useFocusRing} from '@react-aria/focus';
import {useHover} from '@react-aria/interactions';
function childrenToString(children: ReactNode) {
if (typeof children === 'string') {
return children;
}
if (Array.isArray(children)) {
return children.reduce((p, c) => p + childrenToString(c), '');
}
return '';
}
function anchorId(children: ReactNode) {
return childrenToString(children).replace(/\s/g, '-').replace(/[^a-zA-Z0-9-_]/g, '').toLowerCase();
}
function AnchorLink({anchorId, isHovered, level, headingText}) {
let {isFocusVisible, focusProps} = useFocusRing({within: true});
const href = `#${anchorId}`;
return (
<span
{...focusProps}
className={style({
position: 'absolute',
opacity: {
default: 0,
isHovered: 1,
isFocusVisible: 1
},
marginStart: {
default: 8,
level: {
3: 4,
4: 4
}
},
transition: '[opacity 0.2s ease-in-out]'
})({isHovered, isFocusVisible, level})}>
<Link href={href} aria-label={`Link to ${headingText}`}>
<LinkIcon
styles={iconStyle({size: 'S', marginBottom: 4})}
UNSAFE_style={{marginBottom: (level === 3 || level === 4) ? 0 : undefined}} />
</Link>
</span>
);
}
const h1 = style({
font: 'heading-3xl',
// This variable is used to calculate the line height.
// Normally it is set by the fontSize, but the custom clamp prevents this.
'--fs': {
type: 'opacity',
value: {
default: 'pow(1.125, 10)', // heading-2xl
isLongForm: 'pow(1.125, 8)' // heading-xl
}
},
fontSize: {
default: {
// On mobile, adjust heading to fit in the viewport, and clamp between a min and max font size.
default: `clamp(${35 / 16}rem, (100vw - 32px) / var(--width-per-em), ${55 / 16}rem)`,
isLongForm: `clamp(${35 / 16}rem, (100vw - 32px) / var(--width-per-em), ${44 / 16}rem)`
},
lg: {
default: 'heading-3xl',
isLongForm: 'heading-2xl'
}
},
textWrap: 'balance',
marginY: 0,
width: 'full',
maxWidth: '--text-width',
marginX: 'auto'
});
export function H1({children, isLongForm, ...props}) {
return (
<h1 {...props} data-anchor-link id="top" style={{'--width-per-em': getTextWidth(children)} as any} className={h1({isLongForm})}>
{children}
</h1>
);
}
export function H2({children, ...props}) {
let {hoverProps, isHovered} = useHover({});
let id = anchorId(children);
return (
<h2 {...props} data-anchor-link id={id} className={style({font: 'heading-lg', marginTop: 48, marginBottom: 24, maxWidth: '--text-width', marginX: 'auto', textWrap: 'balance', position: 'relative', overflowX: 'clip'})} {...hoverProps}>
{children}
<AnchorLink anchorId={id} isHovered={isHovered} level={2} headingText={children} />
</h2>
);
}
export function H3({children, ...props}) {
let {hoverProps, isHovered} = useHover({});
let id = anchorId(children);
return (
<h3 {...props} data-anchor-link id={id} className={style({font: 'heading', marginTop: 36, marginBottom: 24, maxWidth: '--text-width', marginX: 'auto', textWrap: 'balance', position: 'relative', overflowX: {default: 'clip', ':has([data-step])': 'visible'}})} {...hoverProps}>
{children}
<AnchorLink anchorId={id} isHovered={isHovered} level={3} headingText={children} />
</h3>
);
}
export function H4({children, ...props}) {
let {hoverProps, isHovered} = useHover({});
let id = anchorId(children);
return (
<h4 {...props} data-anchor-link id={id} className={style({font: 'heading-sm', maxWidth: '--text-width', marginX: 'auto', textWrap: 'balance', position: 'relative', overflowX: 'clip'})} {...hoverProps}>
{children}
<AnchorLink anchorId={id} isHovered={isHovered} level={4} headingText={children} />
</h4>
);
}
export function PageDescription(props: HTMLAttributes<HTMLParagraphElement>) {
return (
<p
{...props}
className={style({
font: 'body-xl',
maxWidth: '--text-width',
marginX: 'auto',
marginTop: 8,
marginBottom: 24
})} />
);
}
export function P({isLongForm, ...props}: HTMLAttributes<HTMLParagraphElement> & {isLongForm?: boolean}) {
return (
<p
{...props}
className={style({
font: 'body-lg',
fontFamily: {
default: 'sans',
isLongForm: 'serif'
},
textWrap: 'pretty',
marginY: '[1lh]',
maxWidth: '--text-width',
marginX: 'auto'
})({isLongForm})} />
);
}
export function LI({isLongForm, ...props}: HTMLAttributes<HTMLLIElement> & {isLongForm?: boolean}) {
return (
<li
{...props}
className={style({
font: 'body-lg',
fontFamily: {
default: 'sans',
isLongForm: 'serif'
},
textWrap: 'pretty',
marginY: {
default: 0,
isLongForm: 8
},
maxWidth: '--text-width',
marginX: 'auto'
})({isLongForm})} />
);
}
interface SubpageHeaderProps {
currentPage: Page,
parentPage?: Page,
isLongForm?: boolean
}
export function SubpageHeader({currentPage, parentPage, isLongForm}: SubpageHeaderProps) {
return (
<div className={style({display: 'flex', flexDirection: 'column', gap: 4, maxWidth: '--text-width', marginX: 'auto', marginBottom: 40})}>
<div className={style({display: 'flex', alignItems: 'center', gap: 2})}>
<TitleLink href={parentPage?.url}>{parentPage?.exports?.title ?? parentPage?.tableOfContents?.[0]?.title ?? parentPage?.name}</TitleLink>
<ChevronRightIcon styles={iconStyle({size: 'XS'})} />
</div>
<H1 itemProp="headline" isLongForm={isLongForm}>{currentPage.tableOfContents?.[0].title}</H1>
{currentPage.exports?.author && <Byline author={currentPage.exports.author} authorLink={currentPage.exports.authorLink} date={currentPage.exports.date} />}
{currentPage.exports?.date && !currentPage.exports?.author && <Time date={currentPage.exports.date} />}
</div>
);
}
export function Byline({author, authorLink, date}: {author?: string, authorLink?: string, date: string}) {
let formattedDate = new Date(date).toLocaleDateString('en-US', {year: 'numeric', month: 'long', day: 'numeric'});
return (
<div className={style({font: 'detail', width: 'full', maxWidth: '--text-width', marginX: 'auto', marginTop: 4})}>
{author && (
<>
{'By '}
<span itemProp="author" itemScope itemType="https://schema.org/Person">
{authorLink ? (<>
<meta itemProp="url" content={authorLink} />
<Link href={authorLink} isQuiet rel="author" target="_blank">
<span itemProp="name">{author}</span>
</Link>
</>) : <span itemProp="name">{author}</span>}
</span>
{' · '}
</>
)}
<time itemProp="datePublished" dateTime={date}>{formattedDate}</time>
</div>
);
}
export function Time({date}: {date: string}) {
return <time itemProp="datePublished" dateTime={date} className={style({font: 'detail'})}>{new Date(date).toLocaleDateString('en-US', {year: 'numeric', month: 'long', day: 'numeric'})}</time>;
}