slym758/filament-collapsible-sidebar
Add a desktop-only collapsible sidebar to Filament 4 with a simple toggle button. Collapses to icon-only mode, persists state via localStorage, supports dark mode, and includes smooth animations. Compatible with PHP 8.2+.
composer require slym758/filament-collapsible-sidebar
PanelProvider:
use Slym758\CollapsibleSidebar\CollapsibleSidebarPlugin;
public function panel(Panel $panel): Panel {
return $panel->plugins([
CollapsibleSidebarPlugin::make(),
]);
}
php artisan view:clear
State Persistence:
localStorage under the key filament-collapsible-sidebar-state.Responsive Handling:
max-width: 767px).Icon-Only View:
NavigationItem uses Icon components (e.g., <x-filament-icons::heroicon-o-home />).Custom Toggle Button:
Modify the toggle button’s appearance by overriding the plugin’s Blade view (resources/views/vendor/filament-collapsible-sidebar/toggle.blade.php).
Example:
<button class="custom-toggle">
{{ $toggleIcon }}
</button>
Conditional Loading:
Disable the plugin for specific panels or users by checking conditions in panel():
->plugins([
auth()->user()->can('toggle-sidebar') ? CollapsibleSidebarPlugin::make() : null,
])
Animation Tweaks:
Adjust CSS transitions in the plugin’s assets (e.g., public/vendor/filament-collapsible-sidebar/css/collapsible-sidebar.css):
.sidebar-transition {
transition: all 0.3s ease-in-out; /* Customize duration/easing */
}
Dark Mode: The plugin supports dark mode out-of-the-box. For custom themes, ensure your Filament theme includes dark-mode CSS variables.
LocalStorage Conflicts:
localStorage state.CollapsibleSidebarPlugin::make()->storageKey('panel-name-sidebar-state');
Mobile Detection:
@media (max-width: 767px)).Icon Visibility:
NavigationItems include an Icon component.Caching:
php artisan filament:cache-reset
State Not Persisting:
Check the browser’s DevTools (Application > LocalStorage) for the filament-collapsible-sidebar-state key. If missing, verify:
panel().Console tab).Toggle Button Missing: Ensure the plugin is loaded in the correct panel. Test in an incognito window to rule out cached views.
Custom Toggle Logic: Extend the plugin’s JavaScript to add custom toggle behavior:
document.addEventListener('filament-collapsible-sidebar-initialized', (e) => {
const toggleButton = e.detail.toggleButton;
toggleButton.addEventListener('click', (e) => {
// Custom logic (e.g., disable toggle for admins)
if (userIsAdmin()) return;
});
});
Server-Side State: For critical apps, sync the sidebar state with your database:
// In your PanelProvider
->plugins([
CollapsibleSidebarPlugin::make()->useServerSideState(fn () => User::find(auth()->id())->sidebar_collapsed),
]);
Animation Events: Listen for animation start/end via JavaScript:
document.addEventListener('filament-collapsible-sidebar-animation-start', (e) => {
console.log('Sidebar animating:', e.detail.direction);
});
Default Collapsed State: Set the initial state via configuration:
CollapsibleSidebarPlugin::make()->defaultCollapsed(true);
Exclude Pages: Disable the plugin for specific pages by checking the current URL in the toggle button’s logic (requires JS extension).
Accessibility:
The toggle button includes ARIA attributes (aria-expanded). For full compliance, ensure your theme’s icons are accessible (e.g., include aria-label).
How can I help you explore Laravel packages today?