Skip to main content

Dropdown

Overview​

The Dropdown component provides Bulma's versatile dropdown menu for your Bulma React UI.

It supports custom triggers, menu alignment, hover or click activation, right/up direction, disabled state, controlled/uncontrolled open state, menu dividers, and Bulma/utility helper props. Use it for navigation menus, actions, or custom pop-up lists.

info

Dropdowns can be fully controlled, used as hoverable, or triggered by click. Menu items and dividers are included.


Import​

import {
Dropdown,
Button,
Field,
Control,
Span,
} from '@allxsmith/bestax-bulma';

Usage​

Default Dropdown​

To create a dropdown menu, use the Dropdown component with a label for the trigger and Dropdown.Item children for each menu option. You can add a Dropdown.Divider to separate groups of items. This pattern is ideal for navigation menus, action lists, or custom pop-up menus in your UI.

import { Dropdown } from '@allxsmith/bestax-bulma';

function Example() {
  return (
    <Dropdown label="Dropdown Menu">
      <Dropdown.Item>First Item</Dropdown.Item>
      <Dropdown.Item>Second Item</Dropdown.Item>
      <Dropdown.Divider />
      <Dropdown.Item>Third Item</Dropdown.Item>
      <Dropdown.Item>Fourth Item</Dropdown.Item>
      <Dropdown.Item>Fifth Item</Dropdown.Item>
    </Dropdown>
  );
}


Custom Tags (button, div, anchor)​

Use the as prop on Dropdown.Item to render different HTML elements, such as a, div, or button. This allows you to customize the behavior and semantics of each dropdown item, supporting links, actions, or custom content.

<Dropdown label="Custom Dropdown Content">
  <Dropdown.Item as="a" href="https://example.com" target="_blank">
    Anchor Item
  </Dropdown.Item>
  <Dropdown.Item as="div">Div Item</Dropdown.Item>
  <Dropdown.Item as="button" onClick={() => alert('Clicked!')}>
    Button Item
  </Dropdown.Item>
</Dropdown>


Hoverable and Always Active​

Add the hoverable prop to open the dropdown on hover, and the active prop to keep it always open. This is useful for menus that should remain visible or for previewing dropdown content without a click.

<Dropdown label="Hoverable + Active" hoverable active>
  <Dropdown.Item>Hover or Always Open</Dropdown.Item>
  <Dropdown.Item>Second</Dropdown.Item>
  <Dropdown.Divider />
  <Dropdown.Item>Another</Dropdown.Item>
</Dropdown>


Right-Aligned Dropdown​

Set the right prop to align the dropdown menu to the right edge of its trigger. This is useful for menus in toolbars or when space is limited on the left.

<Dropdown label="Dropdown Right" right>
  <Dropdown.Item>Right 1</Dropdown.Item>
  <Dropdown.Item>Right 2</Dropdown.Item>
</Dropdown>


Upward Dropdown​

Use the up prop to make the dropdown menu open upward instead of downward. This is helpful when the dropdown is near the bottom of the viewport or container.

<Dropdown label="Dropdown Up" up>
  <Dropdown.Item>Up 1</Dropdown.Item>
  <Dropdown.Item>Up 2</Dropdown.Item>
</Dropdown>


Controlled Dropdown Example​

Control the open/close state of the dropdown by setting the active prop and handling state changes with onActiveChange. This pattern is useful for advanced interactions or integrating with other UI state.

function example() {
  const [open, setOpen] = useState(false);

  return (
    <Dropdown
      label="Controlled Dropdown"
      active={open}
      onActiveChange={setOpen}
    >
      <Dropdown.Item>Item A</Dropdown.Item>
      <Dropdown.Item>Item B</Dropdown.Item>
    </Dropdown>
  );
}


Forwarded ref​

Dropdown forwards a ref to the root .dropdown element. It is merged with the ref the component uses internally for outside-click and keyboard handling, so attaching your own does not disable either.

function example() {
  const dropdownRef = React.useRef<HTMLDivElement>(null);
  const [tag, setTag] = React.useState<string>();

  return (
    <>
      <Dropdown label="Pick one" ref={dropdownRef}>
        <Dropdown.Item>First</Dropdown.Item>
        <Dropdown.Item>Second</Dropdown.Item>
      </Dropdown>
      <Button mt="3" onClick={() => setTag(dropdownRef.current?.className)}>
        Read the root from its ref
      </Button>
      <p>Root class: {tag ?? '—'}</p>
    </>
  );
}

Callback refs behave the same on React 18 and 19: a cleanup function you return runs on detach instead of the ref being called with null.


Split Button​

A split button pairs a main action with a menu of related ones. Put a Button and a Dropdown in a Field hasAddons, one Control each. Field hasAddons squares the inner corners of the button inside each Control, so it joins the Dropdown's trigger as well. Buttons hasAddons can't: it joins buttons that sit side by side as siblings, and the trigger sits alone inside the Dropdown's own wrapper.

The trigger shows only the Dropdown's arrow, so its label is visually hidden text that names the menu for screen readers. Both halves are in the tab order, and the menu keeps the keyboard support described under Accessibility. Keep the main button in the default color: the trigger takes no color prop, so a colored main button can't match it.

function SplitButton() {
  const [lastAction, setLastAction] = useState('none yet');

  return (
    <>
      <Field hasAddons>
        <Control>
          <Button onClick={() => setLastAction('saved')}>Save</Button>
        </Control>
        <Control>
          <Dropdown label={<Span visibility="sr-only">More save options</Span>}>
            <Dropdown.Item
              as="button"
              onClick={() => setLastAction('saved as a draft')}
            >
              Save as draft
            </Dropdown.Item>
            <Dropdown.Item
              as="button"
              onClick={() => setLastAction('saved a copy')}
            >
              Save a copy
            </Dropdown.Item>
          </Dropdown>
        </Control>
      </Field>
      <p role="status">Last action: {lastAction}</p>
    </>
  );
}


Compound (dot-notation) usage​

DropdownItem and DropdownDivider are also available as Dropdown.Item and Dropdown.Divider, so the whole menu can be composed from the single Dropdown import.

<Dropdown label="Dropdown Menu" active>
  <Dropdown.Item>First Item</Dropdown.Item>
  <Dropdown.Item>Second Item</Dropdown.Item>
  <Dropdown.Divider />
  <Dropdown.Item>Third Item</Dropdown.Item>
</Dropdown>


Accessibility​

  • The trigger is a <button> with aria-haspopup="true" and aria-expanded, and its aria-controls points at the menu, with or without an id on the Dropdown.
  • The menu is role="menu", and aria-labelledby names it after the trigger, so a screen reader moving into it hears the trigger's label. That makes the label the menu's name too, so a trigger that shows only an icon needs text a screen reader can read, as in the split button.
  • Each item is role="menuitem". An item given role="menuitemcheckbox" or role="menuitemradio" stays in the arrow-key order.
  • Dropdown.Item active sets aria-current="true", so the current item isn't only a color. An aria-current you pass wins: a menu of pages can say "page", and aria-current={false} turns it off, as on an item that says its state with aria-checked.
  • Forced colors: Bulma draws Dropdown.Divider as a background color, which forced colors (Windows' contrast themes) paints in the menu's own color, so bestax's styles draw it in the system's text color there. Forced colors also replaces the fill and text color Bulma marks an active item with, so bestax's styles mark it with a bar in the system's highlight color along its start edge, as Menu does, and every item keeps the bar's width so no label moves. Forced colors also drops the shadow Bulma draws the open menu's box with, so bestax's styles give the box an edge in the system's text color. Those rules ship in bestax.css and extras.css, not in Bulma's own stylesheet.
  • A click between items, on a divider say, focuses the menu itself, so the arrow keys still reach the items from there.
  • Dropdown.Item as="button" defaults to type="button", so a menu item inside a form doesn't submit it. Pass type="submit" or type="reset" and yours is used instead; any other value renders type="button".
  • Pressing outside an open menu closes it.
  • On a hoverable Dropdown, Escape puts away a menu the pointer is showing, so it can be dismissed without moving the pointer, as WCAG 1.4.13 asks. It stays away until the pointer leaves the Dropdown. With focus on the trigger, the key is used there (preventDefault()), so a Modal around the Dropdown waits for the next one. With focus elsewhere on the page, the key goes on to what has focus too, as a tooltip's does.
  • A click the browser dispatches with no pointer behind it (detail 0), as a screen reader's browse mode can send when you activate the trigger, opens the menu and focuses its first enabled item, as Enter does. A pointer's click on the trigger, and a script's click(), open it and move no focus into it.

The keyboard follows the WAI-ARIA Menu Button pattern. Unlike the pattern's menu items, each item is also a tab stop, so Tab from the trigger of a hoverable menu the pointer holds open goes through its items.

  • On the trigger: ArrowDown or Enter/Space open the menu and focus the first item; ArrowUp opens the menu and focuses the last item; Escape closes an open menu. On the trigger of a menu that's already open, ArrowDown/ArrowUp focus the first/last item and Enter/Space close it.
  • Inside the menu: ArrowDown/ArrowUp move between items and wrap around; Home/End jump to the first/last item; Escape closes the menu and returns focus to the trigger.
  • Tab in an open menu closes it and moves focus to the next tab stop after the Dropdown, and Shift+Tab closes it and focuses the trigger. Inside a focus trap, such as a Modal's, a Dropdown with nothing after it sends Tab to the trap's first stop, as Tab from the trap's last stop does.
  • Typing a letter moves to the next item whose text starts with it, and typing it again cycles through the items that do. Letters typed in quick succession add up to one search, so d then u lands on "Duplicate" past "Download". Text hidden with aria-hidden, such as an icon's ligature name, doesn't count, and a new search starts each time the menu opens. Typing searches once focus is in the menu, where the trigger's keys above put it. A letter typed on the trigger itself does nothing.
  • On an item: Enter or Space activates it the way a click does, so its onClick runs and the menu closes unless closeOnClick is false. That includes the default anchor without an href and as="div", which the browser would not activate on its own. Holding the key down runs it once.
  • When running an item closes the menu, by click or by key, focus goes back to the trigger, as it does for Escape. An item whose onClick moves focus somewhere else keeps it there.
  • Disabled items (a Dropdown.Item rendered with a native disabled attribute or aria-disabled="true") and dividers are skipped by the arrow keys and by typing. An item that turns disabled while it has focus, such as one whose action is loading, keeps its place: the arrows and typing go on from where it sits. An as="button" item given disabled loses focus to the page instead, as any disabled button does.
  • For a panel built around fields, Popover fits better: role="menu" owns menu items, and the default closeOnClick shuts the menu on a click anywhere in it, a field included. Anything focusable you put in the menu anyway that isn't an item (a field, a button, a checkbox, a link without a menu-item role) keeps its own keys, so its arrows, Home, End and letters don't move to the items. A field needs closeOnClick={false}, or a click into it closes the menu. Escape and Tab still close the menu from any of them, except an Escape the control used itself by calling preventDefault() in its onKeyDown, such as an Autocomplete closing its list or a date picker closing its calendar. The next Escape closes the menu.

  • Button: Use Bulma/Bestax buttons as triggers if needed.
  • Popover: A panel of interactive content, such as a form, rather than a menu of actions.
  • ContextMenu: The same items in a menu that opens where you right-click an element, or long press it, rather than from a button.
  • Helper Props: Bulma helper props for spacing and other utilities.

Additional Resources​

Pro Tip

You can use Bulma helper props with <Dropdown /> and <Dropdown.Item /> for utility-based styling.


Props​

PropTypeDefaultDescription
labelReact.ReactNode—Required. The dropdown button/trigger content.
childrenReact.ReactNode—Required. Dropdown menu items and dividers.
classNamestring—Additional CSS classes for root.
menuClassNamestring—Additional CSS classes for the dropdown menu.
activeboolean—Whether the dropdown is open (controlled).
upbooleanfalseDropdown menu opens upward.
rightbooleanfalseMenu is right-aligned.
hoverablebooleanfalseOpen on hover instead of click. Escape puts away a menu the pointer is showing, and it stays away until the pointer leaves the Dropdown.
disabledbooleanfalseDisables the dropdown trigger.
onActiveChange(active: boolean) => void—Callback when dropdown active state changes.
closeOnClickbooleantrueClose dropdown when a menu item is clicked, or activated with Enter or Space.
idReact.HTMLAttributes<HTMLElement>['id']—Root element ID. The trigger gets <id>-trigger and the menu <id>-menu, which the trigger's aria-controls and the menu's aria-labelledby point at. Without one, both ids come from useId.
refReact.Ref<HTMLDivElement>—Ref forwarded to the root dropdown element.
...All standard <div> attributes and Bulma helper props—See Helper Props

Subcomponents:

  • Dropdown.Item: Bulma Dropdown item.
  • Dropdown.Divider: Bulma Dropdown divider.
PropTypeDefaultDescription
as'a' | 'div' | 'button''a'The element type to render. 'button' renders type="button" unless you pass type="submit" or type="reset", so an item inside a form does not submit it.
activebooleanfalseMarks the item as the current one. It adds Bulma's is-active class, and aria-current="true", so assistive tech can tell which item is current too. An aria-current you pass wins, false and undefined included, so an item that says its state another way, such as a menuitemradio with aria-checked, takes aria-current={false}.
classNamestring—Additional CSS classes.
disabledbooleanfalseMarks the item as disabled; disabled items are skipped during keyboard navigation. Use with as="button" for a native disabled control, or pair with aria-disabled on a link.
childrenReact.ReactNode—Item content.
...Remaining props of the element selected by as and Bulma helper props—See Helper Props

No props. Renders as a menu divider (<hr>).


CSS & Sass Variables​

Dropdown registers these variables on its own .dropdown element. Override them there (or via className) — a value set on an ancestor is only inherited, and loses to the component-level declaration. See Theme.

CSS VariableSass VariableDefault
--bulma-dropdown-menu-min-width$dropdown-menu-min-width12rem
--bulma-dropdown-content-background-color$dropdown-content-background-colorvar(--bulma-scheme-main)
--bulma-dropdown-content-offset$dropdown-content-offset0.25rem
--bulma-dropdown-content-padding-bottom$dropdown-content-padding-bottom0.5rem
--bulma-dropdown-content-padding-top$dropdown-content-padding-top0.5rem
--bulma-dropdown-content-radius$dropdown-content-radiusvar(--bulma-radius)
--bulma-dropdown-content-shadow$dropdown-content-shadowvar(--bulma-shadow)
--bulma-dropdown-content-z$dropdown-content-z20
--bulma-dropdown-item-h$dropdown-item-hvar(--bulma-scheme-h)
--bulma-dropdown-item-s$dropdown-item-svar(--bulma-scheme-s)
--bulma-dropdown-item-l$dropdown-item-lvar(--bulma-scheme-main-l)
--bulma-dropdown-item-background-l$dropdown-item-background-lvar(--bulma-scheme-main-l)
--bulma-dropdown-item-background-l-delta$dropdown-item-background-l-delta0%
--bulma-dropdown-item-hover-background-l-delta$dropdown-item-hover-background-l-deltavar(--bulma-hover-background-l-delta)
--bulma-dropdown-item-active-background-l-delta$dropdown-item-active-background-l-deltavar(--bulma-active-background-l-delta)
--bulma-dropdown-item-color-l$dropdown-item-color-lvar(--bulma-text-strong-l)
--bulma-dropdown-item-selected-h$dropdown-item-selected-hvar(--bulma-link-h)
--bulma-dropdown-item-selected-s$dropdown-item-selected-svar(--bulma-link-s)
--bulma-dropdown-item-selected-l$dropdown-item-selected-lvar(--bulma-link-l)
--bulma-dropdown-item-selected-background-l$dropdown-item-selected-background-lvar(--bulma-link-l)
--bulma-dropdown-item-selected-color-l$dropdown-item-selected-color-lvar(--bulma-link-invert-l)
--bulma-dropdown-divider-background-color$dropdown-divider-background-colorvar(--bulma-border-weak)