Skip to content

Centered navigation bar in React

Logo on the left, links in the middle, call to action on the right, collapsible menu on mobile.

Here is an excerpt of this component's code in React. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in React

CenteredNavbar.tsx
import { useMobileMenu } from "./useMobileMenu";

export type NavLink = { label: string; href: string; current?: boolean };

type CenteredNavbarProps = {
  brand: string;
  links: NavLink[];
  cta: { label: string; href: string };
};

export function CenteredNavbar({ brand, links, cta }: CenteredNavbarProps) {
  const menu = useMobileMenu();
useMobileMenu.ts
import { useEffect, useState, type KeyboardEvent } from "react";

/** Open state of a mobile navigation panel, closed with Escape and when the viewport grows. */
export function useMobileMenu(desktopQuery = "(min-width: 768px)") {
  const [open, setOpen] = useState(false);

  useEffect(() => {
    const media = window.matchMedia(desktopQuery);
    const close = () => media.matches && setOpen(false);
    media.addEventListener("change", close);
    return () => media.removeEventListener("change", close);
  }, [desktopQuery]);

Free component: copy it freely, including for commercial projects.

Navigation bars components in React
  • A navigation that sits at the bottom of the screen, under the thumb, on phones, and becomes a horizontal bar at the top on desktop.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • The house name in the middle, links split on each side, restaurant or designer-shop style.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A strip of practical details (phone, opening hours) above the main navigation.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component