Skip to content

Commit fa044df

Browse files
committed
Lots more ToC tweaks notably including hiding it on small screens
Also reformatting bits of related code for readability and tweaking positioning & when each part is considered 'active'.
1 parent f0cc730 commit fa044df

File tree

6 files changed

+71
-41
lines changed

6 files changed

+71
-41
lines changed

src/components/layout/documentation/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export async function DocumentationLayout({ title, children, links }: Component<
4141
{children}
4242
</StyledDocumentationLayoutContentWrapper>
4343
<StyledDocumentationLayoutNavigationWrapper>
44-
{links?.length ? <NavigationSidebarLinks title="On this page" links={links} /> : null}
44+
{links?.length
45+
? <NavigationSidebarLinks title="On this page" links={links} />
46+
: null
47+
}
4548
</StyledDocumentationLayoutNavigationWrapper>
4649
</StyledDocumentationLayoutWrapper>
4750
</StyledDocumentationGlobalWrapper>

src/components/modules/navigation-sidebar-links/navigation-sidebar-links.styles.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export const StyledNavigationSidebarLinksWrapper = styled.div`
1414
position: sticky;
1515
top: 10px;
1616
align-self: self-start;
17+
18+
@media (max-width: ${({ theme }) => theme.screens['xl']}) {
19+
display: none;
20+
}
1721
`;
1822

1923
export const StyledNavigationSidebarLinksTitle = styled(Heading)``;

src/components/modules/table-content/hooks/use-active-toc.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,53 @@ const getHeadings = () =>
77

88
function useActiveToc() {
99
useEffect(() => {
10-
const setCurrent: IntersectionObserverCallback = entries => {
10+
const isBigScreen = matchMedia(`(min-width: ${screens.xl})`);
11+
12+
const updateActiveHeading = (
13+
container: HTMLElement,
14+
headingElem: HTMLElement
15+
) => {
16+
// Remove active class from all toc items
17+
container.querySelectorAll('a')
18+
.forEach(e => e.classList.remove('active'));
19+
20+
// Add active to the correct toc
21+
headingElem.classList.add('active');
22+
23+
if (isBigScreen.matches) {
24+
const elementTop = headingElem.offsetTop;
25+
const containerHeight = container.clientHeight;
26+
const scrollOffset = elementTop - (containerHeight / 2);
27+
28+
container.scroll({
29+
top: scrollOffset,
30+
behavior: 'smooth'
31+
});
32+
}
33+
}
34+
35+
const updateFromIntersection: IntersectionObserverCallback = entries => {
1136
const scrollContainer = document
1237
.querySelector('#table-of-content-headings')
1338
?.parentElement;
1439
if (!scrollContainer) return;
1540

1641
for (const entry of entries) {
1742
const { id } = entry.target as HTMLElement;
18-
const isMobile = matchMedia(`(max-width: ${screens.lg})`);
19-
2043
const tocHeadingEl = scrollContainer.querySelector<HTMLElement>(`a[href="#${id}"]`);
21-
if (!tocHeadingEl) return;
22-
23-
if (entry.isIntersecting) {
24-
// Remove active class from all toc items
25-
scrollContainer.querySelectorAll('a')
26-
.forEach(e => e.classList.remove('active'));
27-
28-
// Add active to the correct toc
29-
tocHeadingEl.classList.add('active');
30-
31-
if (!isMobile.matches) {
32-
const elementTop = tocHeadingEl.offsetTop;
33-
const containerHeight = scrollContainer.clientHeight;
34-
const scrollOffset = elementTop - (containerHeight / 2);
35-
36-
scrollContainer.scroll({
37-
top: scrollOffset,
38-
behavior: 'smooth'
39-
});
40-
}
44+
45+
if (tocHeadingEl && entry.isIntersecting) {
46+
updateActiveHeading(scrollContainer, tocHeadingEl);
4147
}
4248
}
4349
};
4450

4551
const observerOptions: IntersectionObserverInit = {
46-
rootMargin: '0px 0px -66%',
52+
rootMargin: '0px 0px -50%',
4753
threshold: 1,
4854
};
4955

50-
const headingObserver = new IntersectionObserver(setCurrent, observerOptions);
56+
const headingObserver = new IntersectionObserver(updateFromIntersection, observerOptions);
5157

5258
getHeadings().forEach(heading => headingObserver.observe(heading));
5359

src/components/modules/table-content/index.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,25 @@ export const TableContent = ({ isCollapsible, links }: TableContentProps) => {
7272
/>
7373
));
7474

75-
if (!isCollapsible) return <StyledTableContentWrapper data-match-scroll>{content}</StyledTableContentWrapper>;
75+
if (!isCollapsible) {
76+
return <StyledTableContentWrapper data-match-scroll>{content}</StyledTableContentWrapper>;
77+
}
7678

77-
const defaultOpenItem = links.find(item => {
78-
return item.subItems?.some(subItem => getPathWithoutHash(subItem.href) === currentPath);
79-
});
79+
const defaultOpenItem = links.find(item =>
80+
item.subItems?.some(subItem =>
81+
getPathWithoutHash(subItem.href) === currentPath
82+
)
83+
);
8084

8185
return (
8286
<StyledTableContentWrapper>
83-
<Accordion.Root asChild type="single" defaultValue={defaultOpenItem?.text || links[0].text} collapsible>
84-
<>{content}</>
87+
<Accordion.Root
88+
asChild
89+
type="single"
90+
defaultValue={defaultOpenItem?.text || links[0].text}
91+
collapsible
92+
>
93+
<>{ content }</>
8594
</Accordion.Root>
8695
</StyledTableContentWrapper>
8796
);

src/components/sections/content-with-table/content-with-table.styles.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,25 @@ export const StyledContentWithTableContentWrapper = styled.div`
5151
padding: 64px 0 32px 0;
5252
max-width: initial;
5353
gap: 45px;
54-
grid-template-columns: 1fr ${({ theme }) => theme.screens.content} 1fr;
54+
grid-template-columns: ${({ theme }) => theme.screens.content};
55+
}
56+
57+
@media (min-width: ${({ theme }) => theme.screens.xl}) {
58+
grid-template-columns: 1fr ${({ theme }) => theme.screens.content} 1fr;
5559
}
5660
`;
5761

5862
export const StyledContentWithTableTableWrapper = styled.aside`
59-
@media (min-width: ${({ theme }) => theme.screens.lg}) {
60-
position: sticky;
61-
top: 10px;
62-
}
63+
display: none;
6364
64-
@media (min-width: ${({ theme }) => theme.screens['2xl']}) {
65+
@media (min-width: ${({ theme }) => theme.screens.xl}) {
6566
display: flex;
6667
justify-content: center;
6768
align-self: flex-start;
6869
70+
position: sticky;
71+
top: 16px;
72+
6973
& > div {
7074
max-width: 296px;
7175
min-width: 296px;

src/components/sections/content-with-table/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ export const ContentWithTable = ({
3131
<Container>
3232
<StyledContentWithTableContentWrapper>
3333
<StyledContentWithTableTableWrapper>
34-
{Array.isArray(links) && links?.length === 1 && links[0]?.subItems?.length === 0 ? null : (
35-
<TableContent isCollapsible={false} links={links} />
36-
)}
34+
{
35+
Array.isArray(links) &&
36+
links?.length === 1 &&
37+
links[0]?.subItems?.length === 0
38+
? null
39+
: <TableContent isCollapsible={false} links={links} />
40+
}
3741
</StyledContentWithTableTableWrapper>
3842

3943
<StyledContentRichText>

0 commit comments

Comments
 (0)