Skip to content

Sidebar

@ailura/alpinejs-sidebar

A sidebar that is open or closed, optionally driven by a media query so it collapses on small screens. The plugin owns the visible state; you write the panel and the trigger.

Terminal window
pnpm add alpinejs @ailura/alpinejs-sidebar

Do this once, before Alpine.start().

import Alpine from "alpinejs";
import sidebarPlugin from "@ailura/alpinejs-sidebar";
Alpine.plugin(sidebarPlugin());
Alpine.start();

That registers a sidebar store, so everything below is reachable at $store.sidebar.

A trigger and a panel, with the overlay and Escape wired.

<div x-data>
<button @click="$store.sidebar.toggle()" :aria-expanded="$store.sidebar.visible">Menu</button>
<div x-show="$store.sidebar.visible" @click="$store.sidebar.hide()">
<aside @click.stop @keydown="$store.sidebar.handleKeydown($event)">
<button @click="$store.sidebar.hide()">Close</button>
<nav>...</nav>
</aside>
</div>
</div>

The method names are show() and hide(), not open() and close().

There are two booleans and they are not the same thing:

  • visible is the current state.
  • isVisible is an alias for it, kept for readability when you pass the whole store around.

A breakpoint makes the sidebar follow a media query. onMismatch decides what happens when the query does not match: hide the sidebar, or leave it alone.

sidebarPlugin({
breakpoint: {
query: "(min-width: 768px)",
onMismatch: "hide",
},
});

matchesBreakpoint tells you whether the query currently matches, so you can render a different trigger for the two cases.

<button x-show="$store.sidebar.matchesBreakpoint" @click="$store.sidebar.toggle()">Menu</button>

Do not close on Escape. Useful when the sidebar holds a form you do not want to lose.

sidebarPlugin({ closeOnEscape: false });

Do not close on overlay click. A sidebar that stays put until the user picks a destination.

sidebarPlugin({ closeOnOverlayClick: false });

Start open. Pass the initial state, useful for a desktop-first layout.

sidebarPlugin({ initial: true });

Go back to the initial state. reset() returns to whatever initial was, which is the one you want on navigation.

$store.sidebar.reset();
Name Type Purpose
$store.sidebar.visible store Whether the sidebar is showing.
$store.sidebar.isVisible store Alias of visible.
$store.sidebar.matchesBreakpoint store Whether the configured media query currently matches.
$store.sidebar.hasOverlay store Whether an overlay should be rendered: visible and closeOnOverlayClick is on.
$store.sidebar.show() / hide() method Change the state.
$store.sidebar.toggle() method Show if hidden, hide if showing.
$store.sidebar.reset() method Return to the initial state.
$store.sidebar.handleKeydown(event) method Escape. The plugin already listens on document, so this is only for a scoped, explicit handler.
$store.sidebar.destroy() method Host-owned teardown: removes the breakpoint listener and the document keydown listener. Nothing calls it for you.
sidebarPlugin({ id: "app-sidebar", storeKey: "nav" });