Skip to content

Commit 0ecf094

Browse files
committed
add title font size resizing behavior to s2 docs
1 parent c9aa612 commit 0ecf094

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

packages/dev/s2-docs/src/Layout.tsx

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {PickerItem, Provider} from '@react-spectrum/s2';
1515
import {PropTable} from './PropTable';
1616
import {StateTable} from './StateTable';
1717
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
18+
import {TitleResizer} from './TitleResizer';
1819
import {TypeLink} from './types';
1920
import {VisualExample} from './VisualExample';
2021

@@ -96,28 +97,8 @@ export function Layout(props: PageProps & {children: ReactElement<any>}) {
9697
lg: 'none'
9798
}
9899
})}>
99-
<div
100-
className={style({
101-
isolation: 'isolate',
102-
display: 'flex',
103-
flexDirection: 'column',
104-
alignItems: 'center',
105-
maxWidth: {
106-
default: 'full',
107-
lg: 1280
108-
},
109-
marginX: 'auto',
110-
marginY: 0,
111-
padding: {
112-
default: 0,
113-
lg: 12
114-
},
115-
paddingBottom: 0,
116-
gap: {
117-
default: 0,
118-
lg: 12
119-
}
120-
})}>
100+
<div className={style({isolation: 'isolate', width: 'full'})}>
101+
<TitleResizer />
121102
<Header pages={pages} currentPage={currentPage} />
122103
<MobileHeader
123104
toc={<MobileToc key="toc" toc={currentPage.tableOfContents ?? []} />}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use client';
2+
3+
import {useCallback, useRef} from 'react';
4+
import {useLayoutEffect, useResizeObserver} from '@react-aria/utils';
5+
6+
export function TitleResizer() {
7+
let titleRef = useRef<HTMLHeadingElement>(null);
8+
9+
// Size the title to fit the available space.
10+
let updateTitleFontSize = useCallback(() => {
11+
if (titleRef.current) {
12+
let fontSize = parseInt(window.getComputedStyle(titleRef.current).fontSize, 10);
13+
14+
// Constrain font size to 58px, or 10% of the window width, whichever is smaller.
15+
let maxFontSize = Math.min(58, Math.round(window.innerWidth * 0.1));
16+
if (fontSize > maxFontSize) {
17+
fontSize = maxFontSize;
18+
titleRef.current.style.fontSize = maxFontSize + 'px';
19+
}
20+
21+
// If the font size is less than the maximum font size,
22+
// increase the font size until it overflows.
23+
while (fontSize < maxFontSize && titleRef.current.scrollWidth <= titleRef.current.clientWidth) {
24+
fontSize++;
25+
titleRef.current.style.fontSize = fontSize + 'px';
26+
}
27+
28+
// Reduce the font size until it doesn't overflow.
29+
while (fontSize > 10 && titleRef.current.scrollWidth > titleRef.current.clientWidth + 1) {
30+
fontSize--;
31+
titleRef.current.style.fontSize = fontSize + 'px';
32+
}
33+
}
34+
}, []);
35+
36+
useLayoutEffect(() => {
37+
titleRef.current = document.querySelector('h1');
38+
updateTitleFontSize();
39+
}, [updateTitleFontSize]);
40+
41+
useResizeObserver({
42+
ref: titleRef,
43+
onResize: updateTitleFontSize
44+
});
45+
46+
return null;
47+
}

0 commit comments

Comments
 (0)