Drawer sidebar | jaspr_daisyui_components
jaspr_daisyui_components
pub.dev ↗

Drawer sidebar

Drawer is a grid layout that can show/hide a sidebar on the left or right side of the page.

Class name Type Description
drawer Component The wrapper for sidebar and content
drawer-toggle Part The hidden checkbox that controls the state of drawer
drawer-content Part Content part
drawer-side Part Sidebar part
drawer-overlay Part Label that covers the page when drawer is open
drawer-button Part Toggle
drawer-end Placement puts drawer to the other side
drawer-open Modifier Forces the drawer to be open
is-drawer-open: Variant Applies styles when the drawer is open
is-drawer-close: Variant Applies styles when the drawer is closed

Structure#

Drawer is a grid layout that can show/hide a sidebar on the left or right side of the page, based on the screen size or based on the value of a drawer-toggle checkbox.
Drawer must be the parent element of the content and sidebar.

.drawer // The root container
  ├── .drawer-toggle // A hidden checkbox to toggle the visibility of the sidebar
  ├── .drawer-content // All your page content goes here
  │    ╰── // navbar, content, footer
  │
  ╰── .drawer-side // Sidebar wrapper
       ├── .drawer-overlay // A dark overlay that covers the whole page when the drawer is open
       ╰── // Sidebar content (menu or anything)

Functionality#

Drawer sidebar is hidden by default. You can make it visible on larger screens using lg:drawer-open class (or using other responsive prefixes: sm, md, lg, xl)

You can check/uncheck the checkbox using JavaScript or by clicking the label tag which is assigned to the hidden checkbox

Opening a drawer adds a scrollbar-gutter to the page to avoid layout shift on operating systems that have a fixed scrollbar. On recent Chromium based browsers vertical scrollbar presence is detected automatically. On Safari and on mobile devices the scrollbar is displayed as overlay so there will not be gutter. On Firefox you need to detect the presence of vertical scrollbar and set the scrollbar-gutter: stable or scrollbar-gutter: unset on :root element yourself. If you don't want to use this feature, you can exclude rootscrollgutter.

Drawer sidebar

<div class="drawer">
  <input id="my-drawer-1" type="checkbox" class="drawer-toggle" />
  <div class="drawer-content">
    <!-- Page content here -->
    <label for="my-drawer-1" class="btn drawer-button">Open drawer</label>
  </div>
  <div class="drawer-side">
    <label for="my-drawer-1" aria-label="close sidebar" class="drawer-overlay"></label>
    <ul class="menu bg-base-200 min-h-full w-80 p-4">
      <!-- Sidebar content here -->
      <li><a>Sidebar Item 1</a></li>
      <li><a>Sidebar Item 2</a></li>
    </ul>
  </div>
</div>
Drawer(
  [
    DrawerToggle(const [], id: 'my-drawer-1'),
    DrawerContent(
      [
        DrawerButton([Component.text('Open drawer')], classes: 'btn', for_: 'my-drawer-1'),
      ],
    ),
    DrawerSide(
      [
        DrawerOverlay(const [], for_: 'my-drawer-1', attributes: {'aria-label': 'close sidebar'}),
        Menu(
          [
            li([a([Component.text('Sidebar Item 1')], href: '#')]),
            li([a([Component.text('Sidebar Item 2')], href: '#')]),
          ],
          classes: 'bg-base-200 min-h-full w-80 p-4',
        ),
      ],
    ),
  ],
);

Responsive: Sidebar is always visible on large screen, can be toggled on small screen

Sidebar is always visible on large screen, can be toggled on small screen because of lg:drawer-open class
<div class="drawer lg:drawer-open">
  <input id="my-drawer-3" type="checkbox" class="drawer-toggle" />
  <div class="drawer-content flex flex-col items-center justify-center">
    <!-- Page content here -->
    <label for="my-drawer-3" class="btn drawer-button lg:hidden">Open drawer</label>
  </div>
  <div class="drawer-side">
    <label for="my-drawer-3" aria-label="close sidebar" class="drawer-overlay"></label>
    <ul class="menu bg-base-200 min-h-full w-80 p-4">
      <!-- Sidebar content here -->
      <li><a>Sidebar Item 1</a></li>
      <li><a>Sidebar Item 2</a></li>
    </ul>
  </div>
</div>
Drawer(
  [
    DrawerToggle(const [], id: 'my-drawer-3'),
    DrawerContent(
      [
        DrawerButton([Component.text('Open drawer')], classes: 'btn lg:hidden', for_: 'my-drawer-3'),
      ],
      classes: 'flex flex-col items-center justify-center',
    ),
    DrawerSide(
      [
        DrawerOverlay(const [], for_: 'my-drawer-3', attributes: {'aria-label': 'close sidebar'}),
        Menu(
          [
            li([a([Component.text('Sidebar Item 1')], href: '#')]),
            li([a([Component.text('Sidebar Item 2')], href: '#')]),
          ],
          classes: 'bg-base-200 min-h-full w-80 p-4',
        ),
      ],
    ),
  ],
  classes: 'lg:drawer-open',
);

Responsive collapsible Icon-only drawer sidebar. Using is-drawer-close and is-drawer-open

In this example instead of completely hiding the drawer sidebar, we only show the icons when the drawer is closed. To add styles based on the state of the drawer, we use the is-drawer-open and is-drawer-close variants. For example is-drawer-close:hidden or is-drawer-open:w-80. Also we have tooltips when the drawer is closed and a switch button to open/close the drawer and rotates the button icon based on the state of the drawer.
Page Content
<div class="drawer lg:drawer-open">
  <input id="my-drawer-4" type="checkbox" class="drawer-toggle inline" />
  <div class="drawer-content">
    <!-- Navbar -->
    <nav class="navbar w-full bg-base-300">
      <label for="my-drawer-4" aria-label="open sidebar" class="btn btn-square btn-ghost drawer-button">
        <!-- Sidebar toggle icon -->
        <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="my-1.5 inline-block size-4">
          <path d="M4 4m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z"></path>
          <path d="M9 4v16"></path>
          <path d="M14 10l2 2l-2 2"></path>
        </svg>
      </label>
      <div class="px-4">Navbar Title</div>
    </nav>
    <!-- Page content here -->
    <div class="p-4">Page Content</div>
  </div>
  <div class="drawer-side is-drawer-close:overflow-visible">
    <label for="my-drawer-4" aria-label="close sidebar" class="drawer-overlay"></label>
    <div class="flex min-h-full flex-col items-start bg-base-200 is-drawer-close:w-14 is-drawer-open:w-64">
      <!-- Sidebar content here -->
      <ul class="menu w-full grow">
        <!-- List item -->
        <li>
          <button class="is-drawer-close:tooltip is-drawer-close:tooltip-right" data-tip="Homepage">
            <!-- Home icon -->
            <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="my-1.5 inline-block size-4">
              <path d="M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8"></path>
              <path d="M3 10a2 2 0 0 1 .709-1.528l7-5.999a2 2 0 0 1 2.582 0l7 5.999A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
            </svg>
            <span class="is-drawer-close:hidden">Homepage</span>
          </button>
        </li>
        <!-- List item -->
        <li>
          <button class="is-drawer-close:tooltip is-drawer-close:tooltip-right" data-tip="Settings">
            <!-- Settings icon -->
            <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="my-1.5 inline-block size-4">
              <path d="M20 7h-9"></path>
              <path d="M14 17H5"></path>
              <circle cx="17" cy="17" r="3"></circle>
              <circle cx="7" cy="7" r="3"></circle>
            </svg>
            <span class="is-drawer-close:hidden">Settings</span>
          </button>
        </li>
      </ul>
    </div>
  </div>
</div>
Drawer(
  [
    DrawerToggle(const [], classes: 'inline', id: 'my-drawer-4'),
    DrawerContent(
      [
        nav(
          [
            DrawerButton(
              [
                RawText(r'''<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="my-1.5 inline-block size-4"><path d="M4 4m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z"></path><path d="M9 4v16"></path><path d="M14 10l2 2l-2 2"></path></svg>'''),
              ],
              classes: 'btn btn-square btn-ghost',
              for_: 'my-drawer-4',
              attributes: {'aria-label': 'open sidebar'},
            ),
            div([Component.text('Navbar Title')], classes: 'px-4'),
          ],
          classes: 'navbar w-full bg-base-300',
        ),
        div([Component.text('Page Content')], classes: 'p-4'),
      ],
    ),
    DrawerSide(
      [
        DrawerOverlay(const [], for_: 'my-drawer-4', attributes: {'aria-label': 'close sidebar'}),
        div(
          [
            Menu(
              [
                li(
                  [
                    button(
                      [
                        RawText(r'''<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="my-1.5 inline-block size-4"><path d="M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8"></path><path d="M3 10a2 2 0 0 1 .709-1.528l7-5.999a2 2 0 0 1 2.582 0l7 5.999A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path></svg>'''),
                        span([Component.text('Homepage')], classes: 'is-drawer-close:hidden'),
                      ],
                      classes: 'is-drawer-close:tooltip is-drawer-close:tooltip-right',
                      attributes: {'data-tip': 'Homepage'},
                    ),
                  ],
                ),
                li(
                  [
                    button(
                      [
                        RawText(r'''<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="my-1.5 inline-block size-4"><path d="M20 7h-9"></path><path d="M14 17H5"></path><circle cx="17" cy="17" r="3"></circle><circle cx="7" cy="7" r="3"></circle></svg>'''),
                        span([Component.text('Settings')], classes: 'is-drawer-close:hidden'),
                      ],
                      classes: 'is-drawer-close:tooltip is-drawer-close:tooltip-right',
                      attributes: {'data-tip': 'Settings'},
                    ),
                  ],
                ),
              ],
              classes: 'w-full grow',
            ),
          ],
          classes: 'flex min-h-full flex-col items-start bg-base-200 is-drawer-close:w-14 is-drawer-open:w-64',
        ),
      ],
      classes: 'is-drawer-close:overflow-visible',
    ),
  ],
  classes: 'lg:drawer-open',
);

Drawer sidebar that opens from right side of page

<div class="drawer drawer-end">
  <input id="my-drawer-5" type="checkbox" class="drawer-toggle" />
  <div class="drawer-content">
    <!-- Page content here -->
    <label for="my-drawer-5" class="drawer-button btn btn-primary">Open drawer</label>
  </div>
  <div class="drawer-side">
    <label for="my-drawer-5" aria-label="close sidebar" class="drawer-overlay"></label>
    <ul class="menu bg-base-200 min-h-full w-80 p-4">
      <!-- Sidebar content here -->
      <li><a>Sidebar Item 1</a></li>
      <li><a>Sidebar Item 2</a></li>
    </ul>
  </div>
</div>
Drawer(
  [
    DrawerToggle(const [], id: 'my-drawer-5'),
    DrawerContent(
      [
        DrawerButton(
          [Component.text('Open drawer')],
          classes: 'btn btn-primary',
          for_: 'my-drawer-5',
        ),
      ],
    ),
    DrawerSide(
      [
        DrawerOverlay(const [], for_: 'my-drawer-5', attributes: {'aria-label': 'close sidebar'}),
        Menu(
          [
            li([a([Component.text('Sidebar Item 1')], href: '#')]),
            li([a([Component.text('Sidebar Item 2')], href: '#')]),
          ],
          classes: 'bg-base-200 min-h-full w-80 p-4',
        ),
      ],
    ),
  ],
  placement: DrawerPlacement.end,
);