Skip to content

Commit 210d68a

Browse files
87xiedimaMachina
andauthoredOct 9, 2024··
trigger scrolling when toc item is outside the viewport (#3394)
* trigger scrolling when toc item is outside the viewport * fixes to make it looks even better * fixes --------- Co-authored-by: Dimitri POSTOLOV <dmytropostolov@gmail.com>
1 parent bc6f0b1 commit 210d68a

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed
 

‎.changeset/neat-onions-unite.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra-theme-docs': patch
3+
---
4+
5+
trigger scrolling when the TOC item is outside the viewport

‎docs/pages/docs/docs-theme/theme-configuration.mdx

+5-4
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,11 @@ Options to configure the language dropdown for
637637

638638
<Table>
639639

640-
| | | |
641-
| ------------------- | -------- | ------------------------------------------------------ |
642-
| i18n[number].locale | `string` | Locale from `i18n.locales` field in `next.config` file |
643-
| i18n[number].name | `string` | Locale name in dropdown |
640+
| | | |
641+
| ---------------------- | ---------------- | ------------------------------------------------------ |
642+
| i18n[number].locale | `string` | Locale from `i18n.locales` field in `next.config` file |
643+
| i18n[number].name | `string` | Locale name in dropdown |
644+
| i18n[number].direction | `'ltl' \| 'rtl'` | Locale writing direction. Default: `ltr` |
644645

645646
</Table>
646647

‎packages/nextra-theme-docs/css/styles.css

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ body {
136136

137137
body,
138138
.nextra-nav-container-blur,
139-
.nextra-toc-footer,
140139
.nextra-sidebar-footer {
141140
@apply _bg-[rgb(var(--nextra-bg))];
142141
}

‎packages/nextra-theme-docs/src/components/back-to-top.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function BackToTop({
3535
className={({ disabled }) =>
3636
cn(
3737
'_flex _items-center _gap-1.5',
38+
'_whitespace-nowrap', // Safari
3839
disabled ? '_opacity-0' : '_opacity-100',
3940
className
4041
)

‎packages/nextra-theme-docs/src/components/toc.tsx

+26-13
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,38 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {
4848
behavior: 'smooth',
4949
block: 'center',
5050
inline: 'center',
51-
scrollMode: 'always',
52-
boundary: tocRef.current!.parentElement
51+
scrollMode: 'if-needed',
52+
boundary: tocRef.current
5353
})
5454
}
5555
}, [activeSlug])
5656

5757
return (
5858
<div
5959
className={cn(
60-
'nextra-scrollbar _sticky _top-16 _overflow-y-auto _px-4 _pt-6 _text-sm [hyphens:auto]',
61-
'_max-h-[calc(100vh-var(--nextra-navbar-height)-env(safe-area-inset-bottom))]'
60+
hasHeadings && '_grid _grid-rows-[min-content_1fr_min-content]', // 1fr: toc headings, min-content: title/footer
61+
'_sticky _top-[--nextra-navbar-height] _pt-6 _text-sm',
62+
'_max-h-[calc(100vh-var(--nextra-navbar-height))]'
6263
)}
6364
>
6465
{hasHeadings && (
6566
<>
66-
<p className="_mb-4 _font-semibold _tracking-tight">
67+
<p
68+
className={cn(
69+
'_mx-4', // use margin instead padding to not have shadow on scrollbar
70+
'_font-semibold _tracking-tight',
71+
'_pb-2 _shadow-[0_12px_16px_rgb(var(--nextra-bg))] contrast-more:_shadow-none _z-[1]'
72+
)}
73+
>
6774
{renderComponent(themeConfig.toc.title)}
6875
</p>
69-
<ul ref={tocRef}>
76+
<ul
77+
ref={tocRef}
78+
className={cn(
79+
'_px-4 nextra-scrollbar _overscroll-y-contain _overflow-y-auto _hyphens-auto',
80+
'_py-1.5' // for title/footer shadow
81+
)}
82+
>
7083
{toc.map(({ id, value, depth }) => (
7184
<li className="_my-2 _scroll-my-6 _scroll-py-6" key={id}>
7285
<a
@@ -75,10 +88,10 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {
7588
'nextra-focus',
7689
{
7790
2: '_font-semibold',
78-
3: 'ltr:_ml-4 rtl:_mr-4',
79-
4: 'ltr:_ml-8 rtl:_mr-8',
80-
5: 'ltr:_ml-12 rtl:_mr-12',
81-
6: 'ltr:_ml-16 rtl:_mr-16'
91+
3: '_ms-4',
92+
4: '_ms-8',
93+
5: '_ms-12',
94+
6: '_ms-16'
8295
}[depth],
8396
'_block _transition-colors _subpixel-antialiased',
8497
activeAnchor[id]?.isActive
@@ -98,9 +111,9 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {
98111
{hasMetaInfo && (
99112
<div
100113
className={cn(
101-
hasHeadings && 'nextra-toc-footer _mt-8 _pt-8',
102-
'_sticky _bottom-0 _flex _flex-col _items-start _gap-2 _pb-8',
103-
'_-mx-1 _px-1' // to hide focused toc links
114+
hasHeadings && 'nextra-toc-footer _pt-4',
115+
'_flex _flex-col _items-start _gap-2 _pb-4',
116+
'_mx-4' // for border top width
104117
)}
105118
>
106119
{themeConfig.feedback.content ? (

‎packages/nextra/styles/scrollbar.css

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
.nextra-scrollbar {
22
scrollbar-width: thin; /* Firefox */
3-
scrollbar-color: oklch(55.55% 0 0 / 40%) transparent; /* Firefox */
4-
3+
scrollbar-color: rgba(115, 115, 115, 0.4) transparent; /* Firefox */
54
scrollbar-gutter: stable;
5+
66
&::-webkit-scrollbar {
77
@apply _size-3;
88
}
9+
910
&::-webkit-scrollbar-track {
1011
@apply _bg-transparent;
1112
}
13+
1214
&::-webkit-scrollbar-thumb {
1315
@apply _rounded-[10px];
14-
}
15-
&:hover::-webkit-scrollbar-thumb {
16-
border: 3px solid transparent;
17-
background-color: var(--tw-shadow-color);
18-
background-clip: content-box;
19-
@apply _shadow-neutral-500/20 hover:_shadow-neutral-500/40;
20-
}
2116

22-
@media (max-width: 767px) {
23-
.nextra-container & {
24-
scrollbar-gutter: auto;
17+
&:hover {
18+
border: 3px solid transparent;
19+
background-color: var(--tw-shadow-color);
20+
background-clip: content-box;
21+
@apply _shadow-neutral-500/20 hover:_shadow-neutral-500/40;
2522
}
2623
}
2724
}

0 commit comments

Comments
 (0)
Please sign in to comment.