Blocks

Navbar 25

Navbars
Logo LeftMenu CenterMenu RightMega MenuImageNavbar
Navbar 25

Code

relume-navbar25.tsx
"use client";

import { useState } from "react";
import { motion } from "motion/react";
import { useMediaQuery } from "@/hooks/use-media-query";
import { Button, type ButtonProps } from "@/components/ui/button";
import { KeyboardArrowDown } from "relume-icons";

type ImageProps = {
  url?: string;
  src: string;
  alt?: string;
  button?: ButtonProps;
};

type SubMenuLink = {
  title: string;
  url: string;
};

type CategoryGroup = {
  title: string;
  links: SubMenuLink[];
};

type MegaMenuProps = {
  title: string;
  description: string;
  button: ButtonProps;
  subLinks?: SubMenuLink[];
  categories?: CategoryGroup[];
  image?: ImageProps;
};

type LinkProps = {
  title: string;
  url: string;
  megaMenu?: MegaMenuProps;
};

type Props = {
  logo: ImageProps;
  links: LinkProps[];
  buttons: ButtonProps[];
};

export type Navbar25Props = React.ComponentPropsWithoutRef<"section"> & Partial<Props>;

export const Navbar25 = (props: Navbar25Props) => {
  const { logo, links, buttons } = {
    ...Navbar25Defaults,
    ...props,
  };

  const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
  const isMobile = useMediaQuery("(max-width: 991px)");

  return (
    <section className="relative z-[999] flex w-full items-center justify-between border-b border-scheme-border bg-scheme-background lg:min-h-18 lg:px-[5%]">
      <div className="size-full lg:flex lg:items-center lg:justify-between">
        <div className="lg:flex">
          <div className="flex min-h-16 items-center justify-between px-[5%] md:min-h-18 lg:min-h-full lg:px-0">
            <a href={logo.url}>
              <img src={logo.src} alt={logo.alt} />
            </a>
            <button
              className="-mr-2 flex size-12 flex-col items-center justify-center lg:hidden"
              onClick={() => setIsMobileMenuOpen((prev) => !prev)}
            >
              <motion.span
                className="my-[3px] h-0.5 w-6 bg-neutral-darkest"
                animate={isMobileMenuOpen ? ["open", "rotatePhase"] : "closed"}
                variants={topLineVariants}
              />
              <motion.span
                className="my-[3px] h-0.5 w-6 bg-neutral-darkest"
                animate={isMobileMenuOpen ? "open" : "closed"}
                variants={middleLineVariants}
              />
              <motion.span
                className="my-[3px] h-0.5 w-6 bg-neutral-darkest"
                animate={isMobileMenuOpen ? ["open", "rotatePhase"] : "closed"}
                variants={bottomLineVariants}
              />
            </button>
          </div>
          <motion.div
            variants={{
              open: {
                height: "var(--height-open, 100dvh)",
              },
              close: {
                height: "var(--height-closed, 0)",
              },
            }}
            initial="close"
            exit="close"
            animate={isMobileMenuOpen ? "open" : "close"}
            transition={{ duration: 0.4 }}
            className="overflow-auto px-[5%] lg:ml-6 lg:flex lg:items-center lg:px-0 lg:[--height-closed:auto] lg:[--height-open:auto]"
          >
            {links.map((link, index) =>
              link.megaMenu ? (
                <SubMenu
                  key={index}
                  megaMenu={link.megaMenu}
                  title={link.title}
                  isMobile={isMobile}
                />
              ) : (
                <a
                  key={index}
                  href={link.url}
                  className="text-md block py-3 first:pt-7 lg:px-4 lg:py-6 lg:text-base first:lg:pt-6"
                >
                  {link.title}
                </a>
              ),
            )}
            <div className="mt-6 flex w-full flex-col gap-y-4 pb-24 lg:hidden lg:pb-0">
              {buttons.map((button, index) => (
                <Button key={index} className="w-full" {...button}>
                  {button.title}
                </Button>
              ))}
            </div>
          </motion.div>
        </div>
        <div className="hidden lg:flex lg:gap-4">
          {buttons.map((button, index) => (
            <Button key={index} {...button}>
              {button.title}
            </Button>
          ))}
        </div>
      </div>
    </section>
  );
};

const SubMenu = ({
  title,
  isMobile,
  megaMenu,
}: {
  title: string;
  isMobile: boolean;
  megaMenu: MegaMenuProps;
}) => {
  const [isDropdownOpen, setIsDropdownOpen] = useState(false);
  return (
    <div
      onMouseEnter={() => !isMobile && setIsDropdownOpen(true)}
      onMouseLeave={() => !isMobile && setIsDropdownOpen(false)}
    >
      <button
        className="text-md flex w-full items-center justify-between gap-x-2 py-3 text-center lg:w-auto lg:flex-none lg:justify-start lg:px-4 lg:py-6 lg:text-base"
        onClick={() => setIsDropdownOpen((prev) => !prev)}
      >
        <span>{title}</span>
        <motion.span
          variants={{
            rotated: { rotate: 180 },
            initial: { rotate: 0 },
          }}
          animate={isDropdownOpen ? "rotated" : "initial"}
          transition={{ duration: 0.3 }}
        >
          <KeyboardArrowDown className="text-scheme-text" />
        </motion.span>
      </button>
      <motion.div
        variants={{
          open: {
            visibility: "visible",
            opacity: 1,
            height: "var(--height-open, auto)",
          },
          close: {
            visibility: "hidden",
            opacity: "0",
            height: "var(--height-close, 0)",
          },
        }}
        initial="close"
        exit="close"
        animate={isDropdownOpen ? "open" : "close"}
        transition={{ duration: 0.3 }}
        className="top-full bottom-auto left-0 w-full max-w-full min-w-full overflow-hidden bg-scheme-background lg:absolute lg:w-[100vw] lg:border-b lg:border-scheme-border lg:px-[5%] lg:[--height-close:auto]"
      >
        <div className="flex w-full flex-col flex-nowrap justify-between lg:flex-row lg:gap-12">
          <div className="grid w-full auto-cols-fr grid-cols-1 items-start justify-start gap-6 py-6 md:grid-cols-2 md:gap-12 md:py-8 lg:max-w-xs">
            {megaMenu?.categories?.map((category, index) => (
              <div key={index} className="text-left">
                <p className="mb-4 font-semibold">{category.title}</p>
                <div className="flex flex-col items-start">
                  {category.links.map((link, linkIndex) => (
                    <a key={linkIndex} href={link.url} className="block py-2 text-sm">
                      {link.title}
                    </a>
                  ))}
                </div>
              </div>
            ))}
          </div>
          <div className="relative grid w-full max-w-xl auto-cols-fr grid-cols-1 items-start justify-items-stretch gap-6 p-6 sm:grid-cols-[1fr_0.75fr] sm:px-8 md:gap-8 lg:py-8 lg:pl-8">
            <div className="absolute top-0 bottom-0 left-0 z-0 w-full bg-neutral-lightest lg:w-screen" />
            <div className="relative max-w-sm">
              <h4 className="mb-3 text-2xl font-bold md:mb-4 md:text-3xl md:leading-[1.3] lg:text-4xl">
                {megaMenu.title}
              </h4>
              <p>{megaMenu.description}</p>
              <div className="mt-6 flex flex-wrap items-center gap-4 md:mt-8">
                <Button {...megaMenu.button}>{megaMenu.button.title}</Button>
              </div>
            </div>
            <div className="relative h-full w-full overflow-hidden md:w-auto">
              <img
                src={megaMenu.image?.src}
                alt={megaMenu.image?.alt}
                className="aspect-square size-full rounded-image object-cover"
              />
            </div>
          </div>
        </div>
      </motion.div>
    </div>
  );
};

export const Navbar25Defaults: Props = {
  logo: {
    url: "#",
    src: "https://d22po4pjz3o32e.cloudfront.net/logo-image.svg",
    alt: "Logo image",
  },
  links: [
    { title: "Link One", url: "#" },
    { title: "Link Two", url: "#" },
    { title: "Link Three", url: "#" },
    {
      title: "Link Four",
      url: "#",
      megaMenu: {
        categories: [
          {
            title: "Product categories",
            links: [
              { title: "Category One", url: "#" },
              { title: "Category Two", url: "#" },
              { title: "Category Three", url: "#" },
              { title: "Category Four", url: "#" },
              { title: "Category Five", url: "#" },
            ],
          },
          {
            title: "Product categories",
            links: [
              { title: "Category Six", url: "#" },
              { title: "Category Seven", url: "#" },
              { title: "Category Eight", url: "#" },
              { title: "Category Nine", url: "#" },
              { title: "Category Ten", url: "#" },
            ],
          },
        ],
        subLinks: [
          { title: "Link One", url: "#" },
          { title: "Link Two", url: "#" },
          { title: "Link Three", url: "#" },
          { title: "Link Four", url: "#" },
          { title: "Link Five", url: "#" },
          { title: "Link Six", url: "#" },
          { title: "Link Seven", url: "#" },
          { title: "Link Eight", url: "#" },
        ],
        title: "Products",
        description:
          "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique.",
        button: { title: "View all", variant: "secondary", size: "sm" },
        image: {
          src: "https://d22po4pjz3o32e.cloudfront.net/placeholder-image.svg",
          alt: "Relume placeholder image",
        },
      },
    },
  ],
  buttons: [
    {
      title: "Button",
      variant: "secondary",
      size: "sm",
    },
    {
      title: "Button",
      size: "sm",
    },
  ],
};

const topLineVariants = {
  open: {
    translateY: 8,
    transition: { delay: 0.1 },
  },
  rotatePhase: {
    rotate: -45,
    transition: { delay: 0.2 },
  },
  closed: {
    translateY: 0,
    rotate: 0,
    transition: { duration: 0.2 },
  },
};

const middleLineVariants = {
  open: {
    width: 0,
    transition: { duration: 0.1 },
  },
  closed: {
    width: "1.5rem",
    transition: { delay: 0.3, duration: 0.2 },
  },
};

const bottomLineVariants = {
  open: {
    translateY: -8,
    transition: { delay: 0.1 },
  },
  rotatePhase: {
    rotate: 45,
    transition: { delay: 0.2 },
  },
  closed: {
    translateY: 0,
    rotate: 0,
    transition: { duration: 0.2 },
  },
};

npm i @radix-ui/react-accordion @radix-ui/react-dialog @radix-ui/react-slot class-variance-authority@0.7.1 clsx@^2.1.1 embla-carousel-react@^8.5.2 motion@^12.15.0 relume-icons@1.3.0 tailwind-merge@^2.2.2