Dropdown Menu in Navbar
March 30, 2025 · AI Conversations
{{ __('Users') }}
{{ __('Youtube Categories') }}
{{ __('Portfolios') }}
{{ __('Project Quotes Categories') }}
add dropdown m,enu in navbar
—
### ✅ Example: Add a Dropdown Menu
<!-- Dropdown Parent -->
<div class="relative" x-data="{ open: false }">
<!-- Dropdown Trigger -->
<button @click="open = !open" class="inline-flex items-center px-4 py-2 text-sm font-medium leading-5 text-gray-700 hover:text-gray-900 focus:outline-none">
{{ __('Categories') }}
<svg class="ml-1 h-4 w-4" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414L10 13.414l-4.707-4.707a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</button>
<!-- Dropdown Menu -->
<div x-show="open" @click.away="open = false" class="absolute z-50 mt-2 w-48 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5">
<div class="py-1">
<x-nav-link :href="route('admin.search-categories.index')" :active="request()->routeIs('admin.search-categories')">
{{ __('Youtube Categories') }}
</x-nav-link>
<x-nav-link :href="route('admin.projectquote-categories.index')" :active="request()->routeIs('admin.projectquote-categories')">
{{ __('Project Quotes Categories') }}
</x-nav-link>
</div>
</div>
</div>
—
### 💡 Then your nav bar would look like:
<x-nav-link :href="route('admin.dashboard')" :active="request()->routeIs('admin.dashboard')">
{{ __('Dashboard') }}
</x-nav-link>
<x-nav-link :href="route('admin.users.index')" :active="request()->routeIs('admin.users')">
{{ __('Users') }}
</x-nav-link>
<x-nav-link :href="route('admin.portfolios.index')" :active="request()->routeIs('admin.portfolios')">
{{ __('Portfolios') }}
</x-nav-link>
<!-- Categories Dropdown Here -->
@include('components.admin-navbar-categories-dropdown') <!-- Optional: break it into a partial -->
—
Let me know if you're using something different (like Bootstrap, Vue components, or a JS framework), and I’ll tailor the solution accordingly!
@php
$classes = ($active ?? false)
? 'inline-flex items-center px-1 pt-1 border-b-2 border-indigo-400 text-sm font-medium leading-5 text-gray-900 focus:outline-none focus:border-indigo-700 transition duration-150 ease-in-out'
: 'inline-flex items-center px-1 pt-1 border-b-2 border-transparent text-sm font-medium leading-5 text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700 focus:border-gray-300 transition duration-150 ease-in-out';
@endphp
merge(['class' => $classes]) }}>
{{ $slot }}
create same component for use css menu dropdowns
—
### ✅ Step 1: Create New Blade Component for Dropdown Links
Let's call it: `resources/views/components/nav-dropdown-link.blade.php`
@props(['href', 'active'])
@php
$classes = ($active ?? false)
? 'block w-full text-left px-4 py-2 text-sm text-gray-900 bg-gray-100'
: 'block w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900';
@endphp
<a href="{{ $href }}" {{ $attributes->merge(['class' => $classes]) }}>
{{ $slot }}
</a>
—
### 🧩 Step 2: Use it inside your Dropdown Menu
Update your dropdown menu like this:
<!-- Dropdown Categories -->
<div class="relative" x-data="{ open: false }">
<button @click="open = !open" class="inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 hover:text-gray-900 focus:outline-none">
{{ __('Categories') }}
<svg class="ml-1 h-4 w-4" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414L10 13.414l-4.707-4.707a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</button>
<div x-show="open" @click.away="open = false" class="absolute z-50 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5">
<div class="py-1">
<x-nav-dropdown-link :href="route('admin.search-categories.index')" :active="request()->routeIs('admin.search-categories')">
{{ __('Youtube Categories') }}
</x-nav-dropdown-link>
<x-nav-dropdown-link :href="route('admin.projectquote-categories.index')" :active="request()->routeIs('admin.projectquote-categories')">
{{ __('Project Quotes Categories') }}
</x-nav-dropdown-link>
</div>
</div>
</div>
—
### 🎯 Result
– Your dropdown menu now uses the same logic (`active` detection) and styling principle.
– It’s fully reusable using “ just like “.
Want to extend it with icons or dividers? Just say the word!
Dashboard
Team
Projects
Calendar
View notifications
Open user menu
Your Profile
Settings
Sign out
Dashboard
Team
Projects
Calendar
dropdown menu permanantly opened
—
### ✅ Update This Section (Profile dropdown)
**Original:**
<div class="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white py-1 ring-1 shadow-lg ring-black/5 focus:outline-hidden" role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button" tabindex="-1">
**✅ Change to (always visible):**
<div class="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white py-1 ring-1 shadow-lg ring-black/5" role="menu" aria-orientation="vertical">
And also **remove** the `aria-expanded`, `aria-haspopup`, and `tabindex` attributes from the button if you're not using them anymore.
—
### ✅ Optional: Remove the toggle button if dropdown is always visible
<!-- Remove or keep as a placeholder -->
<!-- <button type="button" ...>...</button> -->
—
### ✅ Final (Always-Open Dropdown Example):
<div class="relative ml-3">
<div class="relative flex rounded-full bg-gray-800 text-sm">
<img class="size-8 rounded-full" src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80" alt="">
</div>
<div class="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white py-1 shadow-lg ring-1 ring-black/5" role="menu">
<a href="#" class="block px-4 py-2 text-sm text-gray-700">Your Profile</a>
<a href="#" class="block px-4 py-2 text-sm text-gray-700">Settings</a>
<a href="#" class="block px-4 py-2 text-sm text-gray-700">Sign out</a>
</div>
</div>
—
Let me know if you want to:
– Keep dropdown open **only on desktop**
– Turn it into a **hover dropdown**
– Or **remove all JS behavior completely** for pure CSS control
Happy to tailor it!
But in your original code, the dropdown lacks the functionality to toggle its visibility properly. We can fix that by adding **Alpine.js** for simple interactivity, or using **Tailwind’s peer/focus-within** trick if you want pure CSS.
—
### ✅ Here's how to toggle the dropdown using **Alpine.js**
Make sure Alpine.js is included in your layout:
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
Then update your profile dropdown section like this:
<!-- Wrapper with Alpine toggle -->
<div class="relative ml-3" x-data="{ open: false }">
<!-- Button to toggle dropdown -->
<button @click="open = !open" type="button" class="relative flex rounded-full bg-gray-800 text-sm focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800" id="user-menu-button">
<span class="sr-only">Open user menu</span>
<img class="size-8 rounded-full" src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80" alt="">
</button>
<!-- Dropdown menu -->
<div x-show="open" @click.away="open = false" x-transition
class="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white py-1 shadow-lg ring-1 ring-black/5"
role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button">
<a href="#" class="block px-4 py-2 text-sm text-gray-700" role="menuitem">Your Profile</a>
<a href="#" class="block px-4 py-2 text-sm text-gray-700" role="menuitem">Settings</a>
<a href="#" class="block px-4 py-2 text-sm text-gray-700" role="menuitem">Sign out</a>
</div>
</div>
—
### ✅ Bonus: Smooth transitions and closing on outside click
– `x-show="open"` handles toggling.
– `@click.away="open = false"` closes the dropdown if clicked outside.
– `x-transition` adds a nice fade-in/fade-out effect.
—
Would you like me to apply the same dropdown logic to your mobile menu too? Or turn the **“Categories”** into a dropdown like this?
import Alpine from 'alpinejs';
window.Alpine = Alpine;
Alpine.start();
—
### ✅ Update Your Profile Dropdown Section
Replace your existing dropdown code with this updated Alpine-enabled version:
<!-- Profile dropdown using Alpine.js -->
<div class="relative ml-3" x-data="{ open: false }">
<!-- Toggle Button -->
<button @click="open = !open" type="button"
class="relative flex rounded-full bg-gray-800 text-sm focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800"
id="user-menu-button" aria-expanded="false" aria-haspopup="true">
<span class="sr-only">Open user menu</span>
<img class="size-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt="">
</button>
<!-- Dropdown Menu -->
<div x-show="open"
@click.away="open = false"
x-transition
class="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white py-1 shadow-lg ring-1 ring-black/5"
role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button">
<a href="#" class="block px-4 py-2 text-sm text-gray-700" role="menuitem">Your Profile</a>
<a href="#" class="block px-4 py-2 text-sm text-gray-700" role="menuitem">Settings</a>
<a href="#" class="block px-4 py-2 text-sm text-gray-700" role="menuitem">Sign out</a>
</div>
</div>
—
### 🔍 Explanation:
– `x-data="{ open: false }"` defines the dropdown state.
– `@click="open = !open"` toggles the dropdown open/close.
– `@click.away="open = false"` closes the dropdown when clicking outside.
– `x-show="open"` controls visibility.
– `x-transition` makes it look smooth and polished.
—
Let me know if you also want dropdowns for other menu items like “Categories”, or if you want the dropdown to close when pressing `Escape`.