Sidebar

A composable, themeable and customizable sidebar component.

Usage

HTML + Javascript

This component requires Javascript.

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:

  • A <div class="sidebar"> which wraps around the entire component and holds it state (e.g. open/close). It accepts an optional data-side set to left, or right to specify the side of the sidebar (defaults to left).
  • A <nav> that contains the actual sidebar with the following children:
    • A <header> and <footer> for the header and footer of the sidebar (fixed position).
    • A <section> for the main navigation list. To add links, you must wrap them in a <div role="group"> first. These groups can contain <h3"> for group headings and lists (i.e. <ul>) of links or buttons. You can also use <details> to wrap around collapsbile sections.

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).

Jinja and Nunjucks

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

{% 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
) }}
<main>
  <h1>Content</h1>
</main>