Skip to content

Commit

Permalink
[v3] fix normalize pages issues, back to top issues (#2654)
Browse files Browse the repository at this point in the history
* aa

* fix

* fix lint

* fix build error

* fix back to top

* prettier

* back to top true by default

* remove some stuff

* a

* prettier

* optimize imports

* aa

* try

* comment for now
  • Loading branch information
Dimitri POSTOLOV authored Jan 27, 2024
1 parent e0d18e0 commit 45471df
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 278 deletions.
11 changes: 11 additions & 0 deletions .changeset/hungry-suits-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'nextra-theme-docs': patch
---

fix “Scroll to top” is not supposed to be interactable when it is hidden.

`display:children` doesn't collapse breadcrumbs

hide external links from pagination

remove xmlns attribute from icons
3 changes: 0 additions & 3 deletions docs/theme.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ const config: DocsThemeConfig = {
</p>
</div>
)
},
toc: {
backToTop: true
}
}

Expand Down
3 changes: 1 addition & 2 deletions examples/swr-site/pages/es/examples/_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export default {
basic: 'Uso Básico',
auth: 'Autenticación',
'infinite-loading': 'Carga Infinita',
'error-handling': 'Manejo De Errores',
ssr: 'Next.js SSR'
'error-handling': 'Manejo De Errores'
}
1 change: 0 additions & 1 deletion examples/swr-site/theme.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ const config: DocsThemeConfig = {
toggleButton: true
},
toc: {
backToTop: true,
extraContent: (
// eslint-disable-next-line @next/next/no-img-element -- ignore since url is external and dynamic
<img alt="placeholder cat" src="https://placekitten.com/g/300/200" />
Expand Down
28 changes: 10 additions & 18 deletions packages/nextra-theme-docs/src/components/back-to-top.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import cn from 'clsx'
import { ArrowRightIcon } from 'nextra/icons'
import type { ReactElement } from 'react'
import { useEffect, useRef } from 'react'

function scrollToTop() {
window.scrollTo({ top: 0, behavior: 'smooth' })
}

export function BackToTop({ className }: { className?: string }): ReactElement {
const ref = useRef<HTMLButtonElement>(null)
useEffect(() => {
function toggleVisible() {
const { scrollTop } = document.documentElement
ref.current?.classList.toggle('_opacity-0', scrollTop < 300)
}

window.addEventListener('scroll', toggleVisible)
return () => {
window.removeEventListener('scroll', toggleVisible)
}
}, [])

export function BackToTop({
className,
hidden
}: {
className?: string
hidden: boolean
}): ReactElement {
return (
<button
ref={ref}
aria-hidden="true"
onClick={scrollToTop}
disabled={hidden}
className={cn(
'_flex _items-center _gap-1.5 _transition _opacity-0',
'_flex _items-center _gap-1.5 _transition _opacity-100 disabled:_opacity-0',
className
)}
>
Scroll to top
<ArrowRightIcon className="_-rotate-90 _w-3.5 _h-3.5 _border _rounded-full _border-current" />
<ArrowRightIcon className="_-rotate-90 _size-4 _border _rounded-full _border-current" />
</button>
)
}
14 changes: 7 additions & 7 deletions packages/nextra-theme-docs/src/components/toc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const linkClassName = cn(

export function TOC({ toc, filePath }: TOCProps): ReactElement {
const activeAnchor = useActiveAnchor()
const tocRef = useRef<HTMLDivElement>(null)
const tocRef = useRef<HTMLUListElement>(null)
const themeConfig = useThemeConfig()

const hasHeadings = toc.length > 0
Expand All @@ -34,12 +34,11 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {
const activeSlug = Object.entries(activeAnchor).find(
([, { isActive }]) => isActive
)?.[0]
const activeIndex = toc.findIndex(({ id }) => id === activeSlug)

useEffect(() => {
if (!activeSlug) return
const anchor = tocRef.current?.querySelector(
`li > a[href="#${activeSlug}"]`
)
const anchor = tocRef.current?.querySelector(`a[href="#${activeSlug}"]`)

if (anchor) {
scrollIntoView(anchor, {
Expand All @@ -54,7 +53,6 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {

return (
<div
ref={tocRef}
className={cn(
'nextra-scrollbar _sticky _top-16 _overflow-y-auto _pr-4 _pt-6 _text-sm [hyphens:auto]',
'_max-h-[calc(100vh-var(--nextra-navbar-height)-env(safe-area-inset-bottom))] ltr:_-mr-4 rtl:_-ml-4'
Expand All @@ -65,7 +63,7 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {
<p className="_mb-4 _font-semibold _tracking-tight">
{renderComponent(themeConfig.toc.title)}
</p>
<ul>
<ul ref={tocRef}>
{toc.map(({ id, value, depth }) => (
<li className="_my-2 _scroll-my-6 _scroll-py-6" key={id}>
<a
Expand Down Expand Up @@ -120,7 +118,9 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {

{renderComponent(themeConfig.toc.extraContent)}

{themeConfig.toc.backToTop && <BackToTop className={linkClassName} />}
{themeConfig.toc.backToTop && (
<BackToTop className={linkClassName} hidden={activeIndex < 2} />
)}
</div>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra-theme-docs/src/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const DEFAULT_THEME: DocsThemeConfig = {
}
},
toc: {
backToTop: false,
backToTop: true,
component: TOC,
float: true,
title: 'On This Page'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,6 @@ exports[`normalize-page > zh-CN getting-started 1`] = `
},
{
"name": "ssr",
"route": "#",
"title": "Next.js SSR",
"type": "doc",
},
Expand Down Expand Up @@ -2110,7 +2109,6 @@ exports[`normalize-page > zh-CN getting-started 1`] = `
},
{
"name": "ssr",
"route": "#",
"title": "Next.js SSR",
"type": "doc",
},
Expand Down Expand Up @@ -2306,7 +2304,6 @@ exports[`normalize-page > zh-CN getting-started 1`] = `
{
"isUnderCurrentDocsTree": true,
"name": "ssr",
"route": "#",
"title": "Next.js SSR",
"type": "doc",
},
Expand Down Expand Up @@ -2539,7 +2536,6 @@ exports[`normalize-page > zh-CN home 1`] = `
},
{
"name": "ssr",
"route": "#",
"title": "Next.js SSR",
"type": "doc",
},
Expand Down Expand Up @@ -2736,7 +2732,6 @@ exports[`normalize-page > zh-CN home 1`] = `
},
{
"name": "ssr",
"route": "#",
"title": "Next.js SSR",
"type": "doc",
},
Expand Down Expand Up @@ -2932,7 +2927,6 @@ exports[`normalize-page > zh-CN home 1`] = `
{
"isUnderCurrentDocsTree": true,
"name": "ssr",
"route": "#",
"title": "Next.js SSR",
"type": "doc",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,6 @@ exports[`Page Process > should match i18n site page maps 1`] = `
"basic": "Uso Básico",
"error-handling": "Manejo De Errores",
"infinite-loading": "Carga Infinita",
"ssr": "Next.js SSR",
},
},
{
Expand Down

This file was deleted.

Empty file.

This file was deleted.

Empty file.
Loading

0 comments on commit 45471df

Please sign in to comment.