|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { CaretDownIcon, MagnifyingGlassIcon } from '@phosphor-icons/react'; |
| 4 | +import { useSearchContext } from 'fumadocs-ui/provider'; |
| 5 | +import { AnimatePresence, MotionConfig, motion } from 'motion/react'; |
| 6 | +import { usePathname } from 'next/navigation'; |
| 7 | +import { Suspense, useCallback, useEffect, useState } from 'react'; |
| 8 | +import { AsideLink } from '@/components/ui/aside-link'; |
| 9 | +import { Badge } from '@/components/ui/badge'; |
| 10 | +import { cn } from '@/lib/utils'; |
| 11 | +import { contents } from './sidebar-content'; |
| 12 | + |
| 13 | +export default function CustomSidebar() { |
| 14 | + const [currentOpen, setCurrentOpen] = useState<number>(0); |
| 15 | + const pathname = usePathname(); |
| 16 | + const { setOpenSearch } = useSearchContext(); |
| 17 | + |
| 18 | + const getDefaultValue = useCallback(() => { |
| 19 | + const defaultValue = contents.findIndex((item) => |
| 20 | + item.list.some((listItem) => listItem.href === pathname) |
| 21 | + ); |
| 22 | + return defaultValue === -1 ? 0 : defaultValue; |
| 23 | + }, [pathname]); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + setCurrentOpen(getDefaultValue()); |
| 27 | + }, [getDefaultValue]); |
| 28 | + |
| 29 | + const handleSearch = () => { |
| 30 | + setOpenSearch(true); |
| 31 | + }; |
| 32 | + |
| 33 | + return ( |
| 34 | + <div className="fixed top-16 left-0 z-30 h-[calc(100vh-4rem)]"> |
| 35 | + <aside className="flex h-full w-[268px] flex-col overflow-y-auto border-border border-t border-r bg-background lg:w-[286px]"> |
| 36 | + <div className="flex h-full flex-col"> |
| 37 | + <button |
| 38 | + className="flex w-full items-center justify-start gap-3 border-border border-b px-5 py-3 text-muted-foreground transition-colors hover:bg-muted/50 hover:text-foreground" |
| 39 | + onClick={handleSearch} |
| 40 | + type="button" |
| 41 | + > |
| 42 | + <MagnifyingGlassIcon |
| 43 | + className="size-4 flex-shrink-0" |
| 44 | + weight="duotone" |
| 45 | + /> |
| 46 | + <span className="text-sm">Search documentation...</span> |
| 47 | + </button> |
| 48 | + |
| 49 | + <MotionConfig |
| 50 | + transition={{ duration: 0.4, type: 'spring', bounce: 0 }} |
| 51 | + > |
| 52 | + <div className="flex flex-col"> |
| 53 | + {contents.map((item, index) => ( |
| 54 | + <div key={item.title}> |
| 55 | + <button |
| 56 | + className="flex w-full items-center gap-3 border-border border-b px-5 py-2.5 text-left font-medium text-foreground text-sm transition-colors hover:bg-muted/50" |
| 57 | + onClick={() => { |
| 58 | + if (currentOpen === index) { |
| 59 | + setCurrentOpen(-1); |
| 60 | + } else { |
| 61 | + setCurrentOpen(index); |
| 62 | + } |
| 63 | + }} |
| 64 | + type="button" |
| 65 | + > |
| 66 | + <item.Icon |
| 67 | + className="size-5 flex-shrink-0 text-muted-foreground" |
| 68 | + weight="duotone" |
| 69 | + /> |
| 70 | + <span className="flex-1 text-sm">{item.title}</span> |
| 71 | + {item.isNew && <NewBadge />} |
| 72 | + <motion.div |
| 73 | + animate={{ rotate: currentOpen === index ? 180 : 0 }} |
| 74 | + className="flex-shrink-0" |
| 75 | + > |
| 76 | + <CaretDownIcon |
| 77 | + className="h-4 w-4 text-muted-foreground" |
| 78 | + weight="duotone" |
| 79 | + /> |
| 80 | + </motion.div> |
| 81 | + </button> |
| 82 | + <AnimatePresence initial={false}> |
| 83 | + {currentOpen === index && ( |
| 84 | + <motion.div |
| 85 | + animate={{ opacity: 1, height: 'auto' }} |
| 86 | + className="relative overflow-hidden" |
| 87 | + exit={{ opacity: 0, height: 0 }} |
| 88 | + initial={{ opacity: 0, height: 0 }} |
| 89 | + > |
| 90 | + <motion.div className="text-sm"> |
| 91 | + {item.list.map((listItem) => ( |
| 92 | + <div key={listItem.title}> |
| 93 | + <Suspense fallback={<>Loading...</>}> |
| 94 | + {listItem.group ? ( |
| 95 | + <div className="mx-5 my-1 flex flex-row items-center gap-2"> |
| 96 | + <p className="bg-gradient-to-tr from-gray-900 to-stone-900 bg-clip-text text-sm text-transparent dark:from-gray-100 dark:to-stone-200"> |
| 97 | + {listItem.title} |
| 98 | + </p> |
| 99 | + <div className="h-px flex-grow bg-gradient-to-r from-stone-800/90 to-stone-800/60" /> |
| 100 | + </div> |
| 101 | + ) : ( |
| 102 | + <AsideLink |
| 103 | + activeClassName="!bg-muted !text-foreground font-medium" |
| 104 | + className="flex items-center gap-3 px-6 py-2 text-muted-foreground text-sm transition-colors hover:bg-muted/50 hover:text-foreground" |
| 105 | + href={listItem.href || '#'} |
| 106 | + startWith="/docs" |
| 107 | + title={listItem.title} |
| 108 | + > |
| 109 | + <listItem.icon |
| 110 | + className="size-5 flex-shrink-0" |
| 111 | + weight="duotone" |
| 112 | + /> |
| 113 | + <span className="flex-1"> |
| 114 | + {listItem.title} |
| 115 | + </span> |
| 116 | + {listItem.isNew && <NewBadge />} |
| 117 | + </AsideLink> |
| 118 | + )} |
| 119 | + </Suspense> |
| 120 | + </div> |
| 121 | + ))} |
| 122 | + </motion.div> |
| 123 | + </motion.div> |
| 124 | + )} |
| 125 | + </AnimatePresence> |
| 126 | + </div> |
| 127 | + ))} |
| 128 | + </div> |
| 129 | + </MotionConfig> |
| 130 | + </div> |
| 131 | + </aside> |
| 132 | + </div> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +function NewBadge({ isSelected }: { isSelected?: boolean }) { |
| 137 | + return ( |
| 138 | + <div className="flex w-full items-center justify-end"> |
| 139 | + <Badge |
| 140 | + className={cn( |
| 141 | + ' !no-underline !decoration-transparent pointer-events-none border-dashed', |
| 142 | + isSelected && '!border-solid' |
| 143 | + )} |
| 144 | + variant={isSelected ? 'default' : 'outline'} |
| 145 | + > |
| 146 | + New |
| 147 | + </Badge> |
| 148 | + </div> |
| 149 | + ); |
| 150 | +} |
0 commit comments