Skip to content

Commit 97f5beb

Browse files
committed
add title font size resizing behavior to s2 docs
1 parent 81c7fb3 commit 97f5beb

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {PickerItem, Provider} from '@react-spectrum/s2';
1414
import {PropTable} from './PropTable';
1515
import {StateTable} from './StateTable';
1616
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
17+
import {TitleResizer} from './TitleResizer';
1718
import {TypeLink} from './types';
1819
import {VisualExample} from './VisualExample';
1920

@@ -111,6 +112,7 @@ export function Layout(props: PageProps & {children: ReactElement<any>}) {
111112
}
112113
})}>
113114
<div className={style({isolation: 'isolate', width: 'full'})}>
115+
<TitleResizer />
114116
<Header pages={pages} currentPage={currentPage} />
115117
<MobileHeader
116118
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)