Skip to content

Instantly share code, notes, and snippets.

@afsakar
Last active April 12, 2025 13:53
Show Gist options
  • Save afsakar/92d1ac808034afed2e25dbba469e300e to your computer and use it in GitHub Desktop.
Save afsakar/92d1ac808034afed2e25dbba469e300e 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>
@afsakar
Copy link
Author

afsakar commented Mar 24, 2025

And don't forget to run npm run build command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment