--- layout: layouts/page.njk title: Sidebar description: A composable, themeable and customizable sidebar component. toc: - label: Usage id: usage children: - label: HTML + JavaScript id: usage-html-js children: - label: "Step 1: Include the JavaScript files" id: usage-html-js-1 - label: "Step 2: Add your sidebar HTML" id: usage-html-js-2 - label: HTML structure id: usage-html-js-3 - label: Jinja and Nunjucks id: usage-macro --- {% from "macros/code_block.njk" import code_block %} {% from "sidebar.njk" import sidebar %}

Usage

HTML + JavaScript

Step 1: Include the JavaScript files

You can either include the JavaScript file for all the components, or just the one for this component by adding this to the <head> of your page:

{% set code_script %}{% endset %} {{ code_block(code_script | prettyHtml, "html") }}
Components with JavaScript {% lucide "arrow-right" %} Use the CLI {% lucide "arrow-right" %} sidebar.js {% lucide "arrow-right" %}

Step 2: Add your sidebar HTML

{% set code_html %} {% set icon_square_terminal %}{% lucide "square-terminal" %}{% endset %} {% set icon_bot %}{% lucide "bot" %}{% endset %} {% set icon_settings %}{% lucide "settings" %}{% endset %} {% set menu = [ { type: "group", label: "Getting started", items: [ { label: "Playground", url: "#", icon: icon_square_terminal }, { label: "Models", url: "#", icon: icon_bot }, { label: "Settings", type: "submenu", icon: icon_settings, items: [ { label: "General", url: "#" }, { label: "Team", url: "#" }, { label: "Billing", url: "#" }, { label: "Limits", url: "#" } ] } ]} ] %} {{ sidebar( label="Sidebar navigation", content_attrs={ "class": "scrollbar" }, menu=menu ) }}

Content

{% endset %} {{ code_block(code_html | prettyHtml, "html") }}

To use a sidebar, you will need the sidebar itself and a sibling container for the content of the page (e.g. <main>), with the following structure for the sidebar:

Any elememt on the page can toggle, open or close the sidebar by dispatching the sidebar:toggle, sidebar:open or sidebar:close events.

You can include the JavaScript code provided below, load it as an individual file or use the CLI. Some Alpine.js properties are also required on certain elements (e.g. x-bind, x-data, @click).

HTML structure

<aside class="sidebar" aria-hidden="false">
Wraps around the entire component. It can have the following attributes:
  • aria-hidden="true": controls the default state of the sidebar (hidden or visible).
  • data-side="left": specifies the side of the sidebar (left or right, defaults to left).
<nav>

The navigation element that contains the sidebar's content. It can have the following attributes:

  • id="{BUTTON_ID}": linked to by the aria-labelledby attribute of the listbox.
  • aria-haspopup="menu": indicates that the button opens a menu.
  • aria-controls="{ MENU_ID }": points to the menu's id.
  • aria-expanded="false": tracks the popover's state.
<header> Optional
The header of the sidebar.
<section>
The main navigation list.
<div role="group">
Group of options, can have a aria-labelledby attribute to link to a heading.
<span role="heading">
Group heading, must have an id attribute if you use the aria-labelledby attribute on the group.
<ul>
List of links or buttons.
<li>
Individual item.
<a>
A link. By default, clicking on a link will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present.
<button>
A button. By default, clicking on a button will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present.
<details>
Collapsible section.
<summary>
Summary of the collapsible section.
<ul>
List of links or buttons.
<footer> Optional
The footer of the sidebar.
<main>
A wrapper for the content of the page.
<button type="button" onclick="window.dispatchEvent(new CustomEvent('sidebar:toggle'))">
A button to toggle the sidebar. If you want to use multiple sidebars you will need to add unique ids to the sidebars (i.e. <aside class="sidebar" id="{SIDEBAR_ID}">) and refer to them in the event detail (i.e. window.dispatchEvent(new CustomEvent('sidebar:toggle', { detail: { id: '{SIDEBAR_ID}' } }));).

Jinja and Nunjucks

You can use the sidebar() Nunjucks or Jinja macro for this component.

Use Nunjucks or Jinja macros {% lucide "arrow-right" %} Jinja macro {% lucide "arrow-right" %} Nunjucks macro {% lucide "arrow-right" %}
{% set raw_code %}{% raw %}{% set menu = [ { type: "group", label: "Getting started", items: [ { label: "Playground", url: "#" }, { label: "Models", url: "#" }, { label: "Settings", type: "submenu", items: [ { label: "General", url: "#" }, { label: "Team", url: "#" }, { label: "Billing", url: "#" }, { label: "Limits", url: "#" } ] } ]} ] %} {{ sidebar( label="Sidebar navigation", menu=menu ) }}

Content

{% endraw %}{% endset%} {{ code_block(raw_code, "jinja") }}