Skip to content

Commit

Permalink
feat: Add mobile navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
hampfh committed Feb 15, 2024
1 parent 7dbecf5 commit 92c7f56
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 12 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
},
"dependencies": {
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-navigation-menu": "^1.1.4",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slot": "^1.0.2",
"@t3-oss/env-nextjs": "^0.9.2",
"@tanstack/react-query": "^5.20.1",
Expand Down
113 changes: 101 additions & 12 deletions src/components/shared/NavigationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import Link from "next/link"
import * as React from "react"

import { Page } from "@/components/shared/Page"
import { useScreenSize } from "@/components/shared/hooks/useScreenSize"
import {
NavigationMenu as BaseNavigationMenu,
NavigationMenuContent,
Expand All @@ -12,9 +14,13 @@ import {
NavigationMenuTrigger,
navigationMenuTriggerStyle
} from "@/components/ui/navigation-menu"
import { Separator } from "@/components/ui/separator"
import { Sheet, SheetContent, SheetTrigger } from "@/components/ui/sheet"
import { cn } from "@/lib/utils"
import { HamburgerMenuIcon } from "@radix-ui/react-icons"
import { DateTime } from "luxon"
import Image from "next/image"
import { useEffect, useState } from "react"

const companyLinks: { title: string; href: string; description: string }[] = [
{
Expand All @@ -39,13 +45,98 @@ const companyLinks: { title: string; href: string; description: string }[] = [
}
]

const studentLinks: { title: string; href: string; description: string }[] = [
{
title: "Exhibitors",
href: "/student/exhibitors",
description: `Get an in depth look at the companies attending the fair`
},
{
title: "Events",
href: "/student/events",
description: "See the events leading up to the fair"
},
{
title: "Recruitment",
href: "/student/recruitment",
description:
"Join Armada {DateTime.now().year}. See which roles are available"
}
]

export function NavigationMenu(props: React.HTMLAttributes<HTMLDivElement>) {
const [sheetOpen, setSheetOpen] = useState<boolean>()
const { className, ...rest } = props

const { width } = useScreenSize()

useEffect(() => {
// Always close sheet if its open when expanding the screen size
if (width > 768 && sheetOpen) {
setSheetOpen(false)
}
}, [width, sheetOpen])

useEffect(() => {
if (sheetOpen) {
// This gives back the control to the underlying radix component
// Without this the exit button and click on overlay won't work
setSheetOpen(undefined)
}
}, [sheetOpen])

return (
<div
className={cn("flex w-screen items-center gap-x-10 px-5 py-4", className)}
className={cn(
"flex w-screen items-center justify-end gap-x-10 px-5 py-4 md:justify-start",
className
)}
{...rest}>
<BaseNavigationMenu>
{/** Sheet is used for mobile navigation */}
<Sheet open={sheetOpen}>
<SheetTrigger className="md:hidden" onClick={() => setSheetOpen(true)}>
<HamburgerMenuIcon width={30} height={30} />
</SheetTrigger>
<SheetContent className="md:hidden">
<Link href="/" onClick={() => setSheetOpen(false)}>
<p className="font-bebas-neue text-xl text-melon-700">Home</p>
</Link>
<Separator className="my-4" />
<Page.Header tier="secondary" className="text-2xl">
Student
</Page.Header>
{studentLinks.map(component => (
<div key={component.href} className="mt-2">
<Link
onClick={() => setSheetOpen(false)}
className="font-bebas-neue text-xl text-melon-700"
href={component.href}>
{component.title}
</Link>
</div>
))}
<Separator className="my-4" />
<Page.Header tier="secondary" className="text-2xl">
Exhibitor
</Page.Header>
{companyLinks.map(component => (
<div key={component.href} className="mt-2">
<Link
onClick={() => setSheetOpen(false)}
className="font-bebas-neue text-xl text-melon-700"
href={component.href}>
{component.title}
</Link>
</div>
))}
<Separator className="my-4" />
<Link href="/" onClick={() => setSheetOpen(false)}>
<p className="font-bebas-neue text-xl text-melon-700">About us</p>
</Link>
</SheetContent>
</Sheet>
{/** BaseNavigationMenu is used for desktop navigation */}
<BaseNavigationMenu className="hidden md:block">
<NavigationMenuList>
<NavigationMenuItem className="dark:hover:text-melon-700">
<Link href="/" legacyBehavior passHref>
Expand Down Expand Up @@ -87,16 +178,14 @@ export function NavigationMenu(props: React.HTMLAttributes<HTMLDivElement>) {
</a>
</NavigationMenuLink>
</li>
<ListItem href="/student/exhibitors" title="Exhibitors">
Get an in depth look at the companies attending the fair
</ListItem>
<ListItem href="/student/events" title="Events">
See the events leading up to the fair
</ListItem>
<ListItem href="/student/recruitment" title="Recruitment">
Join Armada {DateTime.now().year}. See which roles are
available
</ListItem>
{studentLinks.map(component => (
<ListItem
key={component.href}
href={component.href}
title={component.title}>
{component.description}
</ListItem>
))}
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
Expand Down
26 changes: 26 additions & 0 deletions src/components/shared/hooks/useScreenSize.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState } from "react"

export function useScreenSize() {
const [screenSize, setScreenSize] = useState({
width: window.innerWidth,
height: window.innerHeight
})

useEffect(() => {
function handleResize() {
setScreenSize({
width: window.innerWidth,
height: window.innerHeight
})
}

window.addEventListener("resize", handleResize)

// Clean up the event listener when the component unmounts
return () => {
window.removeEventListener("resize", handleResize)
}
}, [])

return screenSize
}
31 changes: 31 additions & 0 deletions src/components/ui/separator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client"

import * as React from "react"
import * as SeparatorPrimitive from "@radix-ui/react-separator"

import { cn } from "@/lib/utils"

const Separator = React.forwardRef<
React.ElementRef<typeof SeparatorPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof SeparatorPrimitive.Root>
>(
(
{ className, orientation = "horizontal", decorative = true, ...props },
ref
) => (
<SeparatorPrimitive.Root
ref={ref}
decorative={decorative}
orientation={orientation}
className={cn(
"shrink-0 bg-stone-200 dark:bg-stone-800",
orientation === "horizontal" ? "h-[1px] w-full" : "h-full w-[1px]",
className
)}
{...props}
/>
)
)
Separator.displayName = SeparatorPrimitive.Root.displayName

export { Separator }
142 changes: 142 additions & 0 deletions src/components/ui/sheet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"use client"

import * as SheetPrimitive from "@radix-ui/react-dialog"
import { cva, type VariantProps } from "class-variance-authority"
import { X } from "lucide-react"
import * as React from "react"

import { cn } from "@/lib/utils"

const Sheet = SheetPrimitive.Root

const SheetTrigger = SheetPrimitive.Trigger

const SheetClose = SheetPrimitive.Close

const SheetPortal = SheetPrimitive.Portal

const SheetOverlay = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<SheetPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
SheetOverlay.displayName = SheetPrimitive.Overlay.displayName

const sheetVariants = cva(
"fixed z-50 gap-4 bg-white p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500 dark:bg-stone-950",
{
variants: {
side: {
top: "inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top",
bottom:
"inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",
left: "inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm",
right:
"inset-y-0 right-0 h-full w-3/4 border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm"
}
},
defaultVariants: {
side: "right"
}
}
)

interface SheetContentProps
extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
VariantProps<typeof sheetVariants> {}

const SheetContent = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Content>,
SheetContentProps
>(({ side = "right", className, children, ...props }, ref) => (
<SheetPortal>
<SheetOverlay />
<SheetPrimitive.Content
ref={ref}
className={cn(sheetVariants({ side }), className)}
{...props}>
{children}
<SheetPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-white transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-stone-950 focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-stone-100 dark:ring-offset-stone-950 dark:focus:ring-stone-300 dark:data-[state=open]:bg-stone-800">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</SheetPrimitive.Close>
</SheetPrimitive.Content>
</SheetPortal>
))
SheetContent.displayName = SheetPrimitive.Content.displayName

const SheetHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className
)}
{...props}
/>
)
SheetHeader.displayName = "SheetHeader"

const SheetFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
SheetFooter.displayName = "SheetFooter"

const SheetTitle = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Title>
>(({ className, ...props }, ref) => (
<SheetPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold text-stone-950 dark:text-stone-50",
className
)}
{...props}
/>
))
SheetTitle.displayName = SheetPrimitive.Title.displayName

const SheetDescription = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Description>
>(({ className, ...props }, ref) => (
<SheetPrimitive.Description
ref={ref}
className={cn("text-sm text-stone-500 dark:text-stone-400", className)}
{...props}
/>
))
SheetDescription.displayName = SheetPrimitive.Description.displayName

export {
Sheet,
SheetClose,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetOverlay,
SheetPortal,
SheetTitle,
SheetTrigger
}

0 comments on commit 92c7f56

Please sign in to comment.