Skip to content

Commit

Permalink
fix: show Scroll to top link toc.backToTop when `toc.float: false…
Browse files Browse the repository at this point in the history
…` is set (#4126)

upd
  • Loading branch information
dimaMachina authored Jan 30, 2025
1 parent 2188ab3 commit 996b0b5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-hounds-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextra-theme-docs": patch
---

fix: show `Scroll to top` link `toc.backToTop` when `toc.float: false` is set
9 changes: 6 additions & 3 deletions packages/nextra-theme-docs/src/components/toc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Anchor } from 'nextra/components'
import type { FC } from 'react'
import { useEffect, useRef } from 'react'
import scrollIntoView from 'scroll-into-view-if-needed'
import { useActiveAnchor, useThemeConfig } from '../stores'
import { useActiveAnchor, useConfig, useThemeConfig } from '../stores'
import { getGitIssueUrl, gitUrlParse } from '../utils'
import { BackToTop } from './back-to-top'

Expand Down Expand Up @@ -34,7 +34,10 @@ export const TOC: FC<TOCProps> = ({ toc, filePath, pageTitle }) => {
themeConfig.toc.extraContent ||
themeConfig.toc.backToTop

const hasHeadings = toc.length > 0
const { activeType } = useConfig().normalizePagesResult
const anchors = themeConfig.toc.float || activeType === 'page' ? toc : []

const hasHeadings = anchors.length > 0
const activeIndex = toc.findIndex(({ id }) => id === activeSlug)

useEffect(() => {
Expand Down Expand Up @@ -71,7 +74,7 @@ export const TOC: FC<TOCProps> = ({ toc, filePath, pageTitle }) => {
'nextra-mask' // for title/footer shadow
)}
>
{toc.map(({ id, value, depth }) => (
{anchors.map(({ id, value, depth }) => (
<li className="x:my-2 x:scroll-my-6 x:scroll-py-6" key={id}>
<a
href={`#${id}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const ClientWrapper: MDXWrapper = ({
>
{themeContext.toc && (
<TOC
toc={themeConfig.toc.float || activeType === 'page' ? toc : []}
toc={toc}
filePath={metadata.filePath}
pageTitle={metadata.title}
/>
Expand Down

0 comments on commit 996b0b5

Please sign in to comment.