Certainly! Below is a responsive Navbar component built using shadcn/ui components. It supports both dark and light themes and adjusts gracefully on different screen sizes.
Prerequisites
Before we begin, ensure you have the following set up in your project:
Next.js or React application.
shadcn/ui installed and configured.
Tailwind CSS configured with dark mode support.
next-themes package for theme management.
Navbar Component (Navbar.tsx or Navbar.jsx)
tsx Copy code
// Import statements
import React from 'react';
import Link from 'next/link';
import { useTheme } from 'next-themes';
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui';
import {
SunIcon,
MoonIcon,
MenuIcon,
XIcon,
} from '@heroicons/react/outline';
const Navbar = () => {
const [mobileMenuOpen, setMobileMenuOpen] = React.useState(false);
const { theme, setTheme } = useTheme();
return (
<nav className="bg-white dark:bg-gray-900 border-b border-gray-200 dark:border-gray-800">
<div className="container mx-auto px-4 sm:px-6 lg:px-8">
<div className="flex items-center justify-between h-16">
{/* Logo Section */}
<div className="flex-shrink-0">
<Link href="/">
<a className="text-2xl font-bold text-gray-900 dark:text-white">
YourBrand
</a>
</Link>
</div>
{/* Desktop Menu */}
<div className="hidden md:flex space-x-8">
<Link href="/">
<a className="text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">
Home
</a>
</Link>
<Link href="/about">
<a className="text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">
About
</a>
</Link>
<Link href="/services">
<a className="text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">
Services
</a>
</Link>
<Link href="/contact">
<a className="text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white">
Contact
</a>
</Link>
</div>
{/* Right-side icons */}
<div className="flex items-center">
{/* Theme Toggle */}
<Button
variant="ghost"
size="icon"
onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
aria-label="Toggle Dark Mode"
>
{theme === 'light' ? (
<MoonIcon className="w-5 h-5 text-gray-800" />
) : (
<SunIcon className="w-5 h-5 text-yellow-500" />
)}
</Button>
{/* Mobile Menu Button */}
<div className="md:hidden">
<Button
variant="ghost"
size="icon"
onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
aria-label="Toggle Mobile Menu"
>
{mobileMenuOpen ? (
<XIcon className="w-6 h-6 text-gray-800 dark:text-white" />
) : (
<MenuIcon className="w-6 h-6 text-gray-800 dark:text-white" />
)}
</Button>
</div>
</div>
</div>
</div>
{/* Mobile Menu */}
{mobileMenuOpen && (
<div className="md:hidden">
<div className="px-2 pt-2 pb-3 space-y-1 sm:px-3">
<Link href="/">
<a className="block text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white px-3 py-2 rounded-md text-base font-medium">
Home
</a>
</Link>
<Link href="/about">
<a className="block text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white px-3 py-2 rounded-md text-base font-medium">
About
</a>
</Link>
<Link href="/services">
<a className="block text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white px-3 py-2 rounded-md text-base font-medium">
Services
</a>
</Link>
<Link href="/contact">
<a className="block text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white px-3 py-2 rounded-md text-base font-medium">
Contact
</a>
</Link>
</div>
</div>
)}
</nav>
);
};
export default Navbar;
Explanation
Imports
React and Next.js Components
React for component building.
Link from next/link for client-side navigation.
Theme Management
useTheme from next-themes for handling theme toggling.
shadcn/ui Components
Button for interactive elements.
DropdownMenu components (imported but not used in this snippet; included for extensibility).
Icons
Icons from @heroicons/react/outline for visual representation.
State Management
mobileMenuOpen: Controls the visibility of the mobile menu.
theme and setTheme: Used to get and set the current theme.
Structure
Navbar Container : A <nav> element with responsive and theme-based classes.
Logo Section : Displays your brand name or logo, linking back to the homepage.
Desktop Menu : Visible on medium and larger screens (md:flex), hidden on smaller screens.
Mobile Menu Button : Toggles the mobile menu on smaller screens.
Theme Toggle Button : Switches between light and dark modes.
Mobile Menu : Visible only when mobileMenuOpen is true and on smaller screens.
Styling with Tailwind CSS
Responsive Classes : Uses md:hidden and md:flex to control visibility based on screen size.
Theme-Based Classes : Utilizes dark: prefix to apply styles in dark mode.
Utility Classes : For padding (px-4), margins (space-x-8), typography, and more.
Setting Up Dark/Light Theme with Tailwind CSS
Ensure your tailwind.config.js is configured to support dark mode:
js Copy code
// tailwind.config.js
module.exports = {
darkMode: 'class', // Enables class-based dark mode
// other configurations...
};
Using next-themes for Theme Management
Install next-themes:
bash Copy code
npm install next-themes
Wrap your application with the ThemeProvider:
tsx Copy code
// _app.tsx or _app.jsx
import { ThemeProvider } from 'next-themes';
import '@/styles/globals.css';
function MyApp({ Component, pageProps }) {
return (
<ThemeProvider attribute="class">
<Component {...pageProps} />
</ThemeProvider>
);
}
export default MyApp;
Additional Notes
Customization : Replace 'YourBrand' with your actual brand name or logo.
Navigation Links : Update the href attributes to match your application's routes.
Accessibility : The aria-label attributes improve accessibility for screen readers.
Extensibility : You can import and use additional components from shadcn/ui as needed.
Dependencies
Ensure you have the following packages installed:
bash Copy code
npm install next-themes @heroicons/react
Tailwind CSS Styles
Make sure to include Tailwind CSS in your project and import the base styles:
css Copy code
/* globals.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
Responsive Design Implementation
Mobile Screens (< md breakpoint)
The desktop menu is hidden (hidden md:flex).
The mobile menu button is visible.
When the mobile menu button is clicked, the menu items appear in a vertical stack.
Desktop Screens (≥ md breakpoint)
The desktop menu is visible, displaying menu items horizontally.
The mobile menu button is hidden (md:hidden).
Testing the Navbar
Start your development server:
Navigate to the page where you've included the Navbar component.
Test the responsiveness by resizing your browser window.
Toggle between dark and light themes using the theme toggle button.
Click on the mobile menu button to test the mobile navigation.
Conclusion
You've now created a beautiful, responsive navbar using shadcn/ui that supports dark and light themes. This component is fully customizable and serves as a solid foundation for your application's navigation.
Feel free to ask if you need further assistance or modifications!