Skip to content

Commit 39addfd

Browse files
luandroclaude
andauthored
Add translatable "On this page" title to desktop table of contents (#103)
* feat(toc): add translatable 'On this page' title to desktop TOC Add a custom TOC component that displays a translatable "On this page" title above the table of contents on desktop. This improves consistency with the mobile TOC dropdown which already has a title. Changes: - Create custom TOC wrapper component in src/theme/TOC/index.tsx - Add "On this page" title using @docusaurus/Translate for i18n support - Add CSS styling for .toc-heading class with theme support - Include Portuguese translation: "Nesta página" - Include Spanish translation: "En esta página" The component wraps the default Docusaurus TOC and adds a styled heading that adapts to light/dark themes. * fix(toc): restore sticky positioning to TOC wrapper The TOC wrapper div was missing sticky positioning, causing the TOC to scroll away instead of remaining fixed while scrolling the page. Changes: - Add .toc-wrapper class to the wrapper div - Apply position: sticky to .toc-wrapper in CSS - Set appropriate top offset relative to navbar height - Add max-height and overflow-y for scrollable TOC on long pages This fixes the regression introduced in the previous commit where wrapping the TOC component broke the default sticky behavior. * fix(toc): add pagination-nav padding for last item focus Add bottom padding to .pagination-nav to allow the last heading on a page to scroll high enough to trigger the active state in the table of contents. Changes: - Add 50vh padding-bottom to .pagination-nav - Enables last TOC item to become focused/active when scrolled to Without this padding, the page cannot scroll far enough for the last heading to reach the scroll position required to activate its TOC link. * fix(spacing): balance pagination-nav padding and add footer margin Reduce excessive bottom padding on pagination-nav and distribute it more evenly between top and bottom. Add matching margin-bottom to doc footer for visual balance. Changes: - Change pagination-nav from 50vh bottom-only to 2rem top + 20vh bottom - Add 3rem margin-bottom to .theme-doc-footer (matching margin-top) This provides enough space for the last TOC item to activate while maintaining better visual balance and less excessive whitespace. * fix(spacing): remove padding-top from pagination-nav Remove the 2rem top padding from pagination-nav, keeping only the bottom padding needed for last TOC item activation. Changes: - Remove padding-top: 2rem from .pagination-nav - Keep padding-bottom: 20vh for TOC functionality --------- Co-authored-by: Claude <[email protected]>
1 parent 7a63eb4 commit 39addfd

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

i18n/es/code.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"theme.TOC.title": {
3+
"message": "En esta página",
4+
"description": "Title for the table of contents section"
5+
}
6+
}

i18n/pt/code.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"theme.TOC.title": {
3+
"message": "Nesta página",
4+
"description": "Title for the table of contents section"
5+
}
6+
}

src/css/custom.css

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,36 @@ article {
508508
padding-top: 2rem;
509509
}
510510
}
511+
512+
/* TOC wrapper with sticky positioning */
513+
.toc-wrapper {
514+
position: sticky;
515+
top: calc(var(--ifm-navbar-height, 60px) + 1rem);
516+
max-height: calc(100vh - var(--ifm-navbar-height, 60px) - 2rem);
517+
overflow-y: auto;
518+
}
519+
520+
/* TOC heading styling */
521+
.toc-heading {
522+
font-size: 0.875rem;
523+
font-weight: 600;
524+
text-transform: uppercase;
525+
letter-spacing: 0.05em;
526+
color: var(--ifm-color-emphasis-700);
527+
margin: 0 0 0.75rem 0;
528+
padding-left: 0.5rem;
529+
}
530+
531+
[data-theme="dark"] .toc-heading {
532+
color: var(--ifm-color-emphasis-600);
533+
}
534+
535+
/* Add padding to pagination nav to allow last TOC item to scroll into view */
536+
.pagination-nav {
537+
padding-bottom: 20vh;
538+
}
539+
540+
/* Add bottom margin to doc footer matching top margin */
541+
.theme-doc-footer {
542+
margin-bottom: 3rem;
543+
}

src/theme/TOC/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react";
2+
import TOC from "@theme-original/TOC";
3+
import type TOCType from "@theme/TOC";
4+
import type { WrapperProps } from "@docusaurus/types";
5+
import { translate } from "@docusaurus/Translate";
6+
7+
type Props = WrapperProps<typeof TOCType>;
8+
9+
export default function TOCWrapper(props: Props): JSX.Element {
10+
return (
11+
<div className="toc-wrapper">
12+
<h2 className="toc-heading">
13+
{translate({
14+
id: "theme.TOC.title",
15+
message: "On this page",
16+
description: "Title for the table of contents section",
17+
})}
18+
</h2>
19+
<TOC {...props} />
20+
</div>
21+
);
22+
}

0 commit comments

Comments
 (0)