|
| 1 | +import cn from "clsx" |
| 2 | +// eslint-disable-next-line no-restricted-imports -- since we don't need newWindow prop |
| 3 | +import NextLink from "next/link" |
| 4 | +import { ArrowRightIcon } from "nextra/icons" |
| 5 | +import type { Item } from "nextra/normalize-pages" |
| 6 | +import type { ReactElement } from "react" |
| 7 | +import { useThemeConfig } from "nextra-theme-docs" |
| 8 | +import type { DocsThemeConfig } from "nextra-theme-docs" |
| 9 | + |
| 10 | +interface NavLinkProps { |
| 11 | + currentIndex: number |
| 12 | + flatDocsDirectories: Item[] |
| 13 | +} |
| 14 | + |
| 15 | +const classes = { |
| 16 | + link: cn( |
| 17 | + "nextra-focus _text-gray-600 dark:_text-gray-400", |
| 18 | + "hover:_text-gray-800 dark:hover:_text-gray-200", |
| 19 | + "contrast-more:_text-gray-700 contrast-more:dark:_text-gray-100", |
| 20 | + "_flex _max-w-[50%] _items-center _gap-1 _py-4 _text-base _font-medium _transition-colors [word-break:break-word] md:_text-lg", |
| 21 | + ), |
| 22 | + icon: cn("_inline _h-5 _shrink-0"), |
| 23 | +} |
| 24 | + |
| 25 | +export function NavLinks({ |
| 26 | + flatDocsDirectories, |
| 27 | + currentIndex, |
| 28 | +}: NavLinkProps): ReactElement | null { |
| 29 | + const themeConfig = useThemeConfig() |
| 30 | + const nav = themeConfig.navigation |
| 31 | + const navigation: Exclude<DocsThemeConfig["navigation"], boolean> = |
| 32 | + typeof nav === "boolean" ? { prev: nav, next: nav } : nav |
| 33 | + let prev = navigation.prev && flatDocsDirectories[currentIndex - 1] |
| 34 | + let next = navigation.next && flatDocsDirectories[currentIndex + 1] |
| 35 | + |
| 36 | + if (prev && !prev.isUnderCurrentDocsTree) prev = false |
| 37 | + if (next && !next.isUnderCurrentDocsTree) next = false |
| 38 | + |
| 39 | + if (!prev && !next) return null |
| 40 | + |
| 41 | + return ( |
| 42 | + <div |
| 43 | + className={cn( |
| 44 | + "_mb-8 _flex _items-center _border-t _pt-8 dark:_border-neutral-800", |
| 45 | + "contrast-more:_border-neutral-400 dark:contrast-more:_border-neutral-400", |
| 46 | + "print:_hidden", |
| 47 | + )} |
| 48 | + > |
| 49 | + {prev && ( |
| 50 | + <NextLink |
| 51 | + href={prev.route} |
| 52 | + title={prev.title} |
| 53 | + className={cn(classes.link, "ltr:_pr-4 rtl:_pl-4")} |
| 54 | + > |
| 55 | + <ArrowRightIcon className={cn(classes.icon, "ltr:_rotate-180")} /> |
| 56 | + {prev.title} |
| 57 | + </NextLink> |
| 58 | + )} |
| 59 | + {next && ( |
| 60 | + <NextLink |
| 61 | + href={next.route} |
| 62 | + title={next.title} |
| 63 | + className={cn( |
| 64 | + classes.link, |
| 65 | + "ltr:_ml-auto ltr:_pl-4 ltr:_text-right rtl:_mr-auto rtl:_pr-4 rtl:_text-left", |
| 66 | + )} |
| 67 | + > |
| 68 | + {next.title} |
| 69 | + <ArrowRightIcon className={cn(classes.icon, "rtl:_rotate-180")} /> |
| 70 | + </NextLink> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + ) |
| 74 | +} |
0 commit comments