Skip to content

Instantly share code, notes, and snippets.

@vermaysha
Forked from afsakar/AdminPanelProvider.php
Created March 27, 2025 00:58
Show Gist options
  • Save vermaysha/3adc118637e6424d86c1b14950533e75 to your computer and use it in GitHub Desktop.
Save vermaysha/3adc118637e6424d86c1b14950533e75 to your computer and use it in GitHub Desktop.
Filter for navigation items in FilamentPHP
<?php
namespace App\Providers\Filament;
use Filament\Panel;
use Filament\View\PanelsRenderHook;
use Illuminate\Support\Facades\Blade;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->renderHook(PanelsRenderHook::SIDEBAR_NAV_START, fn () => view('filament.components.navigation-filter'));
}
<div class=""
@if (filament()->isSidebarCollapsibleOnDesktop())
x-bind:class="$store.sidebar.isOpen ? 'block' : 'hidden'"
@endif
>
<x-filament::input.wrapper class="relative">
<span class="absolute left-1 top-1/2 transform -translate-y-1/2 text-gray-600 dark:text-gray-400 dark:border-gray-600 text-xs px-2 flex items-center justify-center gap-1 py-1 rounded">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="h-4 w-4">
<path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
</svg>
</span>
<x-filament::input
type="text"
placeholder="Menüde ara..."
x-data="sidebarSearch()"
x-ref="search"
x-on:input.debounce.300ms="filterItems($event.target.value)"
x-on:keydown.escape="clearSearch"
x-on:keydown.meta.j.prevent.document="$refs.search.focus()"
inline-suffix=""
class="!pr-14 !pl-10"
/>
<kbd class="absolute right-2 top-1/2 transform -translate-y-1/2 bg-gray-100 border border-gray-300 text-gray-600 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-50 text-xs px-2 flex items-center justify-center gap-1 py-1 rounded">
<svg class="h-3 w-3" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 6v12a3 3 0 1 0 3-3H6a3 3 0 1 0 3 3V6a3 3 0 1 0-3 3h12a3 3 0 1 0-3-3"/></svg>
<kd>J</kd>
</kbd>
</x-filament::input.wrapper>
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('sidebarSearch', () => ({
init() {
this.$refs.search.value = ''
},
filterItems(searchTerm) {
const groups = document.querySelectorAll('.fi-sidebar-nav-groups .fi-sidebar-group')
searchTerm = searchTerm.toLowerCase()
groups.forEach(group => {
const groupButton = group.querySelector('.fi-sidebar-group-button')
const groupText = groupButton?.textContent.toLowerCase() || ''
const items = group.querySelectorAll('.fi-sidebar-item')
let hasVisibleItems = false
const groupMatches = groupText.includes(searchTerm)
items.forEach(item => {
const itemText = item.textContent.toLowerCase()
const isVisible = itemText.includes(searchTerm) || groupMatches
item.style.display = isVisible ? '' : 'none'
if (isVisible) hasVisibleItems = true
})
group.style.display = (hasVisibleItems || groupMatches) ? '' : 'none'
})
},
clearSearch() {
this.$refs.search.value = ''
this.filterItems('')
}
}))
})
</script>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment