Skip to content

Commit 1a97327

Browse files
87xiedimaMachina
andauthoredSep 27, 2024··
[v3] update the back-to-top option to support i18n (#3258)
* [v3] update the back-to-top option to support i18n * fix eslint sort-keys warning * avoid displaying undefined as text * keep `loading`, `placeholder` and `themeSwitch.useOptions` default theme options only for `en` lang * keep it simpler * lint * prettier --------- Co-authored-by: Dimitri POSTOLOV <dmytropostolov@gmail.com>
1 parent c76ef36 commit 1a97327

File tree

10 files changed

+38
-44
lines changed

10 files changed

+38
-44
lines changed
 

‎.changeset/slimy-vans-rush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra-theme-docs': patch
3+
---
4+
5+
Update the `backToTop` option in the docs theme configuration to support i18n

‎.changeset/wild-windows-grow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra-theme-docs': minor
3+
---
4+
5+
keep `loading`, `placeholder` and `themeSwitch.useOptions` default theme options only for `en` lang

‎docs/next-env.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/basic-features/typescript for more information.
5+
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,11 @@ navigating between headings.
534534
'React.ReactNode | React.FC',
535535
'Title of the TOC sidebar. By default it’s “On This Page”.'
536536
],
537-
['toc.backToTop', 'boolean', 'Add `Scroll to top` link.']
537+
[
538+
'toc.backToTop',
539+
'React.ReactNode | React.FC',
540+
'Text of `Scroll to top` button.'
541+
]
538542
]}
539543
/>
540544

‎examples/swr-site/next-env.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
/// <reference types="next/navigation-types/compat/navigation" />
44

55
// NOTE: This file should not be edited
6-
// see https://nextjs.org/docs/basic-features/typescript for more information.
6+
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.

‎examples/swr-site/theme.config.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ const config: DocsThemeConfig = {
190190
toggleButton: true
191191
},
192192
toc: {
193+
backToTop: function BackToTop() {
194+
const { locale } = useRouter()
195+
return {
196+
en: 'Scroll to top',
197+
es: 'Desplazarse hacia arriba',
198+
ru: 'Перейти наверх'
199+
}[locale!]
200+
},
193201
extraContent: (
194202
<img alt="placeholder cat" src="https://placecats.com/300/200" />
195203
),

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cn from 'clsx'
22
import { Button } from 'nextra/components'
33
import { ArrowRightIcon } from 'nextra/icons'
4-
import type { ComponentProps, ReactElement } from 'react'
4+
import type { ComponentProps, ReactElement, ReactNode } from 'react'
55

66
const SCROLL_TO_OPTIONS = { top: 0, behavior: 'smooth' } as const
77

@@ -18,9 +18,11 @@ const scrollToTop: ComponentProps<'button'>['onClick'] = event => {
1818
}
1919

2020
export function BackToTop({
21+
children,
2122
className,
2223
hidden
2324
}: {
25+
children: ReactNode
2426
className?: string
2527
hidden: boolean
2628
}): ReactElement {
@@ -38,7 +40,7 @@ export function BackToTop({
3840
)
3941
}
4042
>
41-
Scroll to top
43+
{children}
4244
<ArrowRightIcon
4345
height="16"
4446
className="_-rotate-90 _border _rounded-full _border-current"

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ export function TOC({ toc, filePath }: TOCProps): ReactElement {
122122
{renderComponent(themeConfig.toc.extraContent)}
123123

124124
{themeConfig.toc.backToTop && (
125-
<BackToTop className={linkClassName} hidden={activeIndex < 2} />
125+
<BackToTop className={linkClassName} hidden={activeIndex < 2}>
126+
{renderComponent(themeConfig.toc.backToTop)}
127+
</BackToTop>
126128
)}
127129
</div>
128130
)}

‎packages/nextra-theme-docs/src/constants.tsx

+5-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint sort-keys: error */
2-
import { useRouter } from 'next/router'
2+
import { useRouter } from 'nextra/hooks'
33
import { DiscordIcon, GitHubIcon } from 'nextra/icons'
44
import { isValidElement } from 'react'
55
import type { z } from 'zod'
@@ -20,20 +20,6 @@ export const DEFAULT_LOCALE = 'en-US'
2020
export type DocsThemeConfig = z.infer<typeof themeSchema>
2121
export type PartialDocsThemeConfig = z.infer<typeof publicThemeSchema>
2222

23-
const LOADING_LOCALES: Record<string, string> = {
24-
'en-US': 'Loading',
25-
fr: 'Сhargement',
26-
ru: 'Загрузка',
27-
'zh-CN': '正在加载'
28-
}
29-
30-
const PLACEHOLDER_LOCALES: Record<string, string> = {
31-
'en-US': 'Search documentation',
32-
fr: 'Rechercher documents',
33-
ru: 'Поиск документации',
34-
'zh-CN': '搜索文档'
35-
}
36-
3723
export const DEFAULT_THEME: DocsThemeConfig = {
3824
backgroundColor: {
3925
dark: '17,17,17',
@@ -164,37 +150,19 @@ export const DEFAULT_THEME: DocsThemeConfig = {
164150
</span>
165151
),
166152
error: 'Failed to load search index.',
167-
loading: function useLoading() {
168-
const { locale, defaultLocale = DEFAULT_LOCALE } = useRouter()
169-
const text =
170-
(locale && LOADING_LOCALES[locale]) || LOADING_LOCALES[defaultLocale]
171-
return <>{text}</>
172-
},
173-
placeholder: function usePlaceholder() {
174-
const { locale, defaultLocale = DEFAULT_LOCALE } = useRouter()
175-
const text =
176-
(locale && PLACEHOLDER_LOCALES[locale]) ||
177-
PLACEHOLDER_LOCALES[defaultLocale]
178-
return `${text}…`
179-
}
153+
loading: 'Loading…',
154+
placeholder: 'Search documentation…'
180155
},
181156
sidebar: {
182157
defaultMenuCollapseLevel: 2,
183158
toggleButton: true
184159
},
185160
themeSwitch: {
186161
component: ThemeSwitch,
187-
useOptions() {
188-
const { locale } = useRouter()
189-
190-
if (locale === 'zh-CN') {
191-
return { dark: '深色主题', light: '浅色主题', system: '系统默认' }
192-
}
193-
return { dark: 'Dark', light: 'Light', system: 'System' }
194-
}
162+
useOptions: { dark: 'Dark', light: 'Light', system: 'System' }
195163
},
196164
toc: {
197-
backToTop: true,
165+
backToTop: 'Scroll to top',
198166
component: TOC,
199167
float: true,
200168
title: 'On This Page'

‎packages/nextra-theme-docs/src/schemas.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const themeSchema = /* @__PURE__ */ (() =>
126126
)
127127
}),
128128
toc: z.strictObject({
129-
backToTop: z.boolean(),
129+
backToTop: z.custom<ReactNode | FC>(...reactNode),
130130
component: z.custom<ReactNode | FC<TOCProps>>(...reactNode),
131131
extraContent: z.custom<ReactNode | FC>(...reactNode),
132132
float: z.boolean(),

0 commit comments

Comments
 (0)
Please sign in to comment.