Blocks

Navbar 30

Navbars
Logo LeftMenu CenterMenu RightMega MenuMultiple ImagesImage
Navbar 30

Code

relume-navbar30.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;
};

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

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

type CategoryProduct = {
  title: string;
  description: string;
  image: ImageProps;
  url: string;
};

type MegaMenuProps = {
  categories?: CategoryGroup[];
  categoryProducts?: CategoryProduct[];
};

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

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

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

export const Navbar30 = (props: Navbar30Props) => {
  const { logo, links, buttons } = {
    ...Navbar30Defaults,
    ...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 gap-6 pt-6 md:gap-12 md:py-8 lg:flex-row lg:gap-20">
          <div className="grid w-full auto-cols-fr grid-cols-1 items-start justify-start gap-6 md:max-w-xl md:grid-cols-3 md:gap-12">
            {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.subLinks.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 auto-cols-fr grid-cols-1 items-start gap-6 pb-6 sm:grid-cols-2 md:pb-0">
            {megaMenu?.categoryProducts?.map((product, index) => (
              <div key={index} className="flex flex-col items-stretch">
                <a href={product.url} className="relative">
                  <div className="mb-3 w-full overflow-hidden lg:mb-4">
                    <img
                      src={product.image.src}
                      alt={product.image.alt}
                      className="aspect-[3/2] size-full object-cover"
                    />
                  </div>
                  <p className="font-semibold">{product.title}</p>
                </a>
              </div>
            ))}
          </div>
        </div>
      </motion.div>
    </div>
  );
};

export const Navbar30Defaults: 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",
            subLinks: [
              { title: "Category One", url: "#" },
              { title: "Category Two", url: "#" },
              { title: "Category Three", url: "#" },
              { title: "Category Four", url: "#" },
              { title: "Category Five", url: "#" },
            ],
          },
          {
            title: "Product categories",
            subLinks: [
              { title: "Category Six", url: "#" },
              { title: "Category Seven", url: "#" },
              { title: "Category Eight", url: "#" },
              { title: "Category Nine", url: "#" },
              { title: "Category Ten", url: "#" },
            ],
          },
          {
            title: "Product categories",
            subLinks: [
              { title: "Category Eleven", url: "#" },
              { title: "Category Twelve", url: "#" },
              { title: "Category Thirteen", url: "#" },
              { title: "Category Fourteen", url: "#" },
              { title: "Category Fifteen", url: "#" },
            ],
          },
        ],
        categoryProducts: [
          {
            title: "Product category",
            description:
              "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique.",
            url: "#",
            image: {
              src: "https://d22po4pjz3o32e.cloudfront.net/placeholder-image.svg",
              alt: "Relume placeholder image",
            },
          },
          {
            title: "Product category",
            description:
              "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique.",
            url: "#",
            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 @radix-ui/react-tabs class-variance-authority@0.7.1 clsx@^2.1.1 embla-carousel-react@^8.5.2 framer-motion motion@^12.15.0 relume-icons@1.3.0 tailwind-merge@^2.2.2