|
| 1 | +'use client' |
| 2 | + |
| 3 | +import React, { useEffect } from 'react' |
| 4 | + |
| 5 | +import { Container } from '@/components/container' |
| 6 | +import { ThemeSwitcher } from '@/components/theme-switcher' |
| 7 | +import { IconBrandGithub, IconBrandJustd } from '@irsyadadl/paranoid' |
| 8 | +import { LayoutGroup, motion } from 'framer-motion' |
| 9 | +import { Button, Link, ListBox, ListBoxItem, ListBoxItemProps } from 'react-aria-components' |
| 10 | +import { tv } from 'tailwind-variants' |
| 11 | +import { buttonStyles } from './ui/button' |
| 12 | +import { useMediaQuery } from './ui/primitive' |
| 13 | +import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetBody } from './ui/sheet' |
| 14 | +import { Outlet, useLocation } from '@remix-run/react' |
| 15 | + |
| 16 | +const navigations = [ |
| 17 | + { |
| 18 | + name: 'Home', |
| 19 | + url: '/' |
| 20 | + }, |
| 21 | + { |
| 22 | + name: 'About', |
| 23 | + url: '/about' |
| 24 | + }, |
| 25 | + { |
| 26 | + name: 'Contact', |
| 27 | + url: '/contact' |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'Login', |
| 31 | + url: '/login' |
| 32 | + }, |
| 33 | + { |
| 34 | + name: 'Components', |
| 35 | + url: 'https://justd.co/components' |
| 36 | + }, |
| 37 | + { |
| 38 | + name: 'Colors', |
| 39 | + url: 'https://justd.co/colors' |
| 40 | + }, |
| 41 | + { |
| 42 | + name: 'Icons', |
| 43 | + url: 'https://paranoid.irsyad.co' |
| 44 | + } |
| 45 | +] |
| 46 | + |
| 47 | +export function Nav() { |
| 48 | + const isMobile = useMediaQuery('(max-width: 640px)') |
| 49 | + return ( |
| 50 | + <main> |
| 51 | + <nav className="sm:py-1 py-2.5 border-b bg-background"> |
| 52 | + <Container> |
| 53 | + <div className="flex items-center justify-between"> |
| 54 | + <div className="flex gap-x-8 items-center"> |
| 55 | + <Link href="/" className=""> |
| 56 | + <IconBrandJustd className="size-5" /> |
| 57 | + </Link> |
| 58 | + {!isMobile && ( |
| 59 | + <span className="sm:inline hidden"> |
| 60 | + <NavContent /> |
| 61 | + </span> |
| 62 | + )} |
| 63 | + </div> |
| 64 | + <div className="flex items-center gap-2 justify-end"> |
| 65 | + <ThemeSwitcher /> |
| 66 | + <Link |
| 67 | + className={buttonStyles({ appearance: 'outline', size: 'square-petite' })} |
| 68 | + href="https://github.com/irsyadadl/next-starter-kit" |
| 69 | + > |
| 70 | + <IconBrandGithub /> |
| 71 | + </Link> |
| 72 | + <Link className={buttonStyles({ appearance: 'outline', size: 'square-petite' })} href="https://justd.co"> |
| 73 | + <IconBrandJustd /> |
| 74 | + </Link> |
| 75 | + {isMobile && <NavResponsive />} |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </Container> |
| 79 | + </nav> |
| 80 | + <Outlet /> |
| 81 | + </main> |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +const navStyles = tv({ |
| 86 | + base: 'text-sm relative py-0 sm:py-4 inline-flex focus:outline-none focus-visible:text-fg font-medium', |
| 87 | + variants: { |
| 88 | + isCurrent: { |
| 89 | + true: 'text-fg', |
| 90 | + false: 'text-muted-fg' |
| 91 | + } |
| 92 | + } |
| 93 | +}) |
| 94 | + |
| 95 | +function NavResponsive() { |
| 96 | + const [isOpen, setOpen] = React.useState(false) |
| 97 | + const pathname = useLocation() |
| 98 | + useEffect(() => { |
| 99 | + setOpen(false) |
| 100 | + }, [pathname.pathname]) |
| 101 | + return ( |
| 102 | + <Sheet onOpenChange={setOpen} isOpen={isOpen}> |
| 103 | + <Button size="small" appearance="outline"> |
| 104 | + Menu |
| 105 | + </Button> |
| 106 | + <SheetContent> |
| 107 | + <SheetHeader className="text-left p-4 border-b"> |
| 108 | + <SheetTitle className="text-sm flex items-center gap-2"> |
| 109 | + <IconBrandJustd /> |
| 110 | + Starter Kit |
| 111 | + </SheetTitle> |
| 112 | + </SheetHeader> |
| 113 | + <SheetBody className="-mx-2 pt-4"> |
| 114 | + <NavContent /> |
| 115 | + </SheetBody> |
| 116 | + </SheetContent> |
| 117 | + </Sheet> |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +function NavContent() { |
| 122 | + const isMobile = useMediaQuery('(max-width: 640px)') |
| 123 | + const id = React.useId() |
| 124 | + return ( |
| 125 | + <LayoutGroup id={id}> |
| 126 | + <ListBox |
| 127 | + orientation={isMobile ? 'vertical' : 'horizontal'} |
| 128 | + layout={isMobile ? 'stack' : 'grid'} |
| 129 | + className="flex relative sm:flex-row flex-col sm:items-center gap-3 sm:gap-6" |
| 130 | + items={navigations} |
| 131 | + > |
| 132 | + {(item) => ( |
| 133 | + <NavLink |
| 134 | + target={['Components', 'Colors', 'Icons'].includes(item.name) ? '_blank' : undefined} |
| 135 | + href={item.url} |
| 136 | + id={item.url} |
| 137 | + > |
| 138 | + {item.name} |
| 139 | + </NavLink> |
| 140 | + )} |
| 141 | + </ListBox> |
| 142 | + </LayoutGroup> |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +interface LinkProps extends ListBoxItemProps { |
| 147 | + isCurrent?: boolean |
| 148 | + className?: string |
| 149 | + children: React.ReactNode |
| 150 | +} |
| 151 | + |
| 152 | +function NavLink({ children, className, ...props }: LinkProps) { |
| 153 | + const isCurrent = useLocation().pathname === props.href |
| 154 | + return ( |
| 155 | + <ListBoxItem className={navStyles({ isCurrent, className })} {...props}> |
| 156 | + {children} |
| 157 | + {isCurrent && <CurrentIndicator />} |
| 158 | + </ListBoxItem> |
| 159 | + ) |
| 160 | +} |
| 161 | + |
| 162 | +function CurrentIndicator() { |
| 163 | + return ( |
| 164 | + <motion.span |
| 165 | + className="h-full inset-y-0 sm:inset-auto sm:h-0.5 w-0.5 sm:w-full rounded-full bg-fg -left-4 sm:bottom-[-5px] sm:inset-x block absolute" |
| 166 | + layoutId="current" |
| 167 | + /> |
| 168 | + ) |
| 169 | +} |
0 commit comments