--- 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 %}
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:
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:
<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).<nav> that contains the actual sidebar with the following children:
<header> and <footer> for the header and footer of the sidebar (fixed position).<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).
<aside class="sidebar" aria-hidden="false">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<section><div role="group">aria-labelledby attribute to link to a heading.<span role="heading">id attribute if you use the aria-labelledby attribute on the group.<ul><li><a>data-keep-mobile-sidebar-open attribute is present.<button>data-keep-mobile-sidebar-open attribute is present.<details><summary><ul><footer> Optional<main><button type="button" onclick="window.dispatchEvent(new CustomEvent('sidebar:toggle'))"><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}' } }));).You can use the sidebar() Nunjucks or Jinja macro for this component.