Dropdown | jaspr_daisyui_components
jaspr_daisyui_components
pub.dev ↗

Dropdown

Dropdown can open a menu or any other element when the button is clicked.

Class name Type Description
dropdown Component Dropdown container
dropdown-content Part Content part
dropdown-start Placement Align horizontally to start of button
dropdown-center Placement Align horizontally to center of button
dropdown-end Placement Align horizontally to end of button
dropdown-top Placement Open from top
dropdown-bottom Placement Open from bottom
dropdown-left Placement Open from left
dropdown-right Placement Open from right
dropdown-hover Modifier Opens on hover too
dropdown-open Modifier Force open
dropdown-close Modifier Force close

There are 3 methods to use a dropdowns#

  1. details and summary elements
  2. popover API and anchor positioning new
  3. CSS focus

Method 1. details and summary#

details and summary are native HTML elements that can be used to create dropdowns.
Dropdown using details and summary opens and closes the content when the summary is clicked. You can also open/close it using JS by adding/removing the open attribute

  Container for the button + content
              │
              │    button to toggle the visibility of the content
              │                        │
<details>   ──╯                        │
  <summary>open or close</summary>   ──╯
  Content
</details>

Method 2. popover API and anchor positioning new#

Popover API is the new HTML standard for creating dropdowns. It opens the dropdown on a top layer, which means there's no need to z-index management and there's no overflow issue.
Anchor positioning is a new CSS standard for positioning elements relative to an anchor.

<button
  style="anchor-name:--anchor-1"  ────────────────╮
  popovertarget="popover-1"   ───────╮            │
>Button</button>                Unique ID         │

                                     │            │
<div popover                         │      Positions this dropdown relative to this button
  id="popover-1"   ──────────────────╯            │
  style="position-anchor:--anchor-1"  ────────────╯
>Content</div>

popovertarget is the unique ID of the popover content. anchor-name/position-anchor is the unique name of the anchor.

Method 3. CSS focus#

The content gets displayed when the button is focused.

                                                    Focusable button
                                                            │
<div>                                                       │
  <div tabindex="0" role="button">Click to open</div>  ─────╯
  <div tabindex="-1">Content</div>  ───────╮
</div>                                     │

                           Content shown when button is focused

Why div? Safari has a CSS bug since 2008 that prevents button elements from being focused, so we use div tabindex="0" as a workaround. This approach is accessible with the addition of role="button" and functions consistently across all browsers.

This dropdown closes when focus is lost. Avoid placing elements in it that remove focus from dropdown content. For example a <dialog> element inside the dropdown.

To close the dropdown after click, you can use something like onclick="document.activeElement.blur()". Example

Positions#

Modifiers#

Force open

<div class="dropdown dropdown-open">
  <div tabindex="0" role="button" class="btn m-1">Button</div>
  <ul tabindex="-1" class="dropdown-content menu bg-base-100 rounded-box z-1 w-52 p-2 shadow-sm">
    <li><a>Item 1</a></li>
    <li><a>Item 2</a></li>
  </ul>
</div>
Dropdown(
  [
    div(
      [Component.text('Button')],
      classes: 'btn m-1',
      attributes: {'tabindex': '0', 'role': 'button'},
    ),
    DropdownContent(
      [
        li([a([Component.text('Item 1')], href: '#')]),
        li([a([Component.text('Item 2')], href: '#')]),
      ],
      classes: 'menu bg-base-100 rounded-box z-1 w-52 p-2 shadow-sm',
      attributes: {'tabindex': '-1'},
    ),
  ],
  modifier: DropdownModifier.open,
);

Force close

<div class="dropdown dropdown-close">
  <div tabindex="0" role="button" class="btn m-1">Button</div>
  <ul tabindex="-1" class="dropdown-content menu bg-base-100 rounded-box z-1 w-52 p-2 shadow-sm">
    <li><a>Item 1</a></li>
    <li><a>Item 2</a></li>
  </ul>
</div>
Dropdown(
  [
    div(
      [Component.text('Button')],
      classes: 'btn m-1',
      attributes: {'tabindex': '0', 'role': 'button'},
    ),
    DropdownContent(
      [
        li([a([Component.text('Item 1')], href: '#')]),
        li([a([Component.text('Item 2')], href: '#')]),
      ],
      classes: 'menu bg-base-100 rounded-box z-1 w-52 p-2 shadow-sm',
      attributes: {'tabindex': '-1'},
    ),
  ],
  modifier: DropdownModifier.close,
);

More examples#

Card as dropdown

<div class="dropdown">
  <div tabindex="0" role="button" class="btn m-1">Click</div>
  <div tabindex="0" class="dropdown-content card card-sm bg-base-100 z-1 w-64 shadow-md">
    <div class="card-body">
      <p>This is a card. You can use any element as a dropdown.</p>
    </div>
  </div>
</div>
Dropdown(
  [
    div(
      [Component.text('Click')],
      classes: 'btn m-1',
      attributes: {'tabindex': '0', 'role': 'button'},
    ),
    Card(
      [
        CardBody([p([Component.text('This is a card. You can use any element as a dropdown.')])]),
      ],
      classes: 'dropdown-content bg-base-100 z-1 w-64 shadow-md',
      size: CardSize.sm,
      attributes: {'tabindex': '0'},
    ),
  ],
);

Helper dropdown

A normal text and a helper dropdown
<div class="dropdown dropdown-end">
  <div tabindex="0" role="button" class="btn btn-circle btn-ghost btn-xs text-info">
    <svg tabindex="0" aria-label="More info" xmlns="http://www.w3.org/2000/svg" fill="none" viewbox="0 0 24 24" class="h-4 w-4 stroke-current">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
    </svg>
  </div>
  <div tabindex="0" class="card card-sm dropdown-content bg-base-100 rounded-box z-1 w-64 shadow-sm">
    <div tabindex="0" class="card-body">
      <h2 class="card-title">You needed more info?</h2>
      <p>Here is a description!</p>
    </div>
  </div>
</div>
Dropdown(
  [
    div(
      [
        RawText(r'''<svg tabindex="0" aria-label="More info" xmlns="http://www.w3.org/2000/svg" fill="none" viewbox="0 0 24 24" class="h-4 w-4 stroke-current">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
    </svg>'''),
      ],
      classes: 'btn btn-circle btn-ghost btn-xs text-info',
      attributes: {'tabindex': '0', 'role': 'button'},
    ),
    Card(
      [
        CardBody(
          [
            CardTitle([Component.text('You needed more info?')]),
            p([Component.text('Here is a description!')]),
          ],
          attributes: {'tabindex': '0'},
        ),
      ],
      classes: 'dropdown-content bg-base-100 rounded-box z-1 w-64 shadow-sm',
      size: CardSize.sm,
      attributes: {'tabindex': '0'},
    ),
  ],
  placement: DropdownPlacement.end,
);