|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Sidebar as SidebarIcon } from 'lucide-react'; |
| 4 | +import { type ComponentProps, useMemo } from 'react'; |
| 5 | +import { cn } from '../../../lib/cn'; |
| 6 | +import { buttonVariants } from '../../ui/button'; |
| 7 | +import { useSidebar } from 'fumadocs-ui/contexts/sidebar'; |
| 8 | +import { useNav } from 'fumadocs-ui/contexts/layout'; |
| 9 | +import { SidebarCollapseTrigger } from '../../sidebar'; |
| 10 | +import { SearchToggle } from '../../search-toggle'; |
| 11 | +import type { Option } from '../../root-toggle'; |
| 12 | +import { usePathname } from 'fumadocs-core/framework'; |
| 13 | +import { isTabActive } from '../../../lib/is-active'; |
| 14 | +import Link from 'fumadocs-core/link'; |
| 15 | + |
| 16 | +export function Navbar(props: ComponentProps<'header'>) { |
| 17 | + const { isTransparent } = useNav(); |
| 18 | + |
| 19 | + return ( |
| 20 | + <header |
| 21 | + id="nd-subnav" |
| 22 | + {...props} |
| 23 | + className={cn( |
| 24 | + 'fixed top-(--fd-banner-height) left-0 right-(--removed-body-scroll-bar-size,0) z-30 flex items-center ps-4 pe-2.5 border-b transition-colors backdrop-blur-sm', |
| 25 | + !isTransparent && 'bg-fd-background/80', |
| 26 | + props.className, |
| 27 | + )} |
| 28 | + > |
| 29 | + {props.children} |
| 30 | + </header> |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +export function LayoutBody(props: ComponentProps<'main'>) { |
| 35 | + const { collapsed } = useSidebar(); |
| 36 | + |
| 37 | + return ( |
| 38 | + <main |
| 39 | + id="nd-docs-layout" |
| 40 | + {...props} |
| 41 | + className={cn( |
| 42 | + 'flex flex-1 flex-col pt-(--fd-nav-height) transition-[padding] fd-default-layout', |
| 43 | + !collapsed && 'mx-(--fd-layout-offset)', |
| 44 | + props.className, |
| 45 | + )} |
| 46 | + style={{ |
| 47 | + ...props.style, |
| 48 | + paddingInlineStart: collapsed |
| 49 | + ? 'min(calc(100vw - var(--fd-page-width)), var(--fd-sidebar-width))' |
| 50 | + : 'var(--fd-sidebar-width)', |
| 51 | + }} |
| 52 | + > |
| 53 | + {props.children} |
| 54 | + </main> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +export function CollapsibleControl() { |
| 59 | + const { collapsed } = useSidebar(); |
| 60 | + |
| 61 | + return ( |
| 62 | + <div |
| 63 | + className={cn( |
| 64 | + 'fixed flex shadow-lg transition-opacity rounded-xl p-0.5 border bg-fd-muted text-fd-muted-foreground z-10 max-md:hidden xl:start-4 max-xl:end-4', |
| 65 | + !collapsed && 'pointer-events-none opacity-0', |
| 66 | + )} |
| 67 | + style={{ |
| 68 | + top: 'calc(var(--fd-banner-height) + var(--fd-tocnav-height) + var(--spacing) * 4)', |
| 69 | + }} |
| 70 | + > |
| 71 | + <SidebarCollapseTrigger |
| 72 | + className={cn( |
| 73 | + buttonVariants({ |
| 74 | + color: 'ghost', |
| 75 | + size: 'icon-sm', |
| 76 | + className: 'rounded-lg', |
| 77 | + }), |
| 78 | + )} |
| 79 | + > |
| 80 | + <SidebarIcon /> |
| 81 | + </SidebarCollapseTrigger> |
| 82 | + <SearchToggle className="rounded-lg" hideIfDisabled /> |
| 83 | + </div> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +export function LayoutTabs({ |
| 88 | + options, |
| 89 | + ...props |
| 90 | +}: ComponentProps<'div'> & { |
| 91 | + options: Option[]; |
| 92 | +}) { |
| 93 | + const pathname = usePathname(); |
| 94 | + const selected = useMemo(() => { |
| 95 | + return options.findLast((option) => isTabActive(option, pathname)); |
| 96 | + }, [options, pathname]); |
| 97 | + |
| 98 | + return ( |
| 99 | + <div |
| 100 | + {...props} |
| 101 | + className={cn( |
| 102 | + 'flex flex-row items-end gap-6 overflow-auto', |
| 103 | + props.className, |
| 104 | + )} |
| 105 | + > |
| 106 | + {options.map((option) => ( |
| 107 | + <LayoutTab |
| 108 | + key={option.url} |
| 109 | + selected={selected === option} |
| 110 | + option={option} |
| 111 | + /> |
| 112 | + ))} |
| 113 | + </div> |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +function LayoutTab({ |
| 118 | + option: { title, url, unlisted, props }, |
| 119 | + selected = false, |
| 120 | +}: { |
| 121 | + option: Option; |
| 122 | + selected?: boolean; |
| 123 | +}) { |
| 124 | + return ( |
| 125 | + <Link |
| 126 | + href={url} |
| 127 | + {...props} |
| 128 | + className={cn( |
| 129 | + 'inline-flex border-b-2 border-transparent transition-colors items-center pb-1.5 font-medium gap-2 text-fd-muted-foreground text-sm text-nowrap hover:text-fd-accent-foreground', |
| 130 | + unlisted && !selected && 'hidden', |
| 131 | + selected && 'border-fd-primary text-fd-primary', |
| 132 | + props?.className, |
| 133 | + )} |
| 134 | + > |
| 135 | + {title} |
| 136 | + </Link> |
| 137 | + ); |
| 138 | +} |
0 commit comments