Skip to content

Commit 376b895

Browse files
author
Dimitri POSTOLOV
authoredJul 19, 2024··
[v3] fixes case when setting disabled: true on the scroll to top button prevents scrolling to most top (#2981)
aa
1 parent 6f0817c commit 376b895

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed
 

‎.changeset/brave-hats-flow.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'nextra-theme-docs': patch
3+
---
4+
5+
fixes case when setting `disabled: true` on the scroll to top button prevents scrolling to most top
6+
7+
scroll to most top in toc element too

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import cn from 'clsx'
22
import { ArrowRightIcon } from 'nextra/icons'
3-
import type { ReactElement } from 'react'
3+
import type { ComponentProps, ReactElement } from 'react'
44

5-
function scrollToTop() {
6-
window.scrollTo({ top: 0, behavior: 'smooth' })
5+
const SCROLL_TO_OPTIONS = { top: 0, behavior: 'smooth' } as const
6+
7+
const scrollToTop: ComponentProps<'button'>['onClick'] = event => {
8+
const buttonElement = event.target as HTMLButtonElement
9+
const tocElement = buttonElement.parentElement!
10+
.parentElement as HTMLDivElement
11+
12+
window.scrollTo(SCROLL_TO_OPTIONS)
13+
tocElement.scrollTo(SCROLL_TO_OPTIONS)
14+
15+
// Fixes https://github.com/facebook/react/issues/20770
16+
// Fixes https://github.com/shuding/nextra/issues/2917
17+
buttonElement.disabled = true
718
}
819

920
export function BackToTop({

0 commit comments

Comments
 (0)
Please sign in to comment.