Skip to content

Dropdown Button

The Dropdown Button component combines the functionality of the Button and Dropdown components to create a button that toggles a dropdown menu. It provides a convenient way to create dropdown menus triggered by button clicks.

  • Metro UI Core
  • Button Component
  • Dropdown Component
<!-- Basic dropdown button -->
<div class="dropdown-button">
<button class="button dropdown-toggle">
Options
</button>
<ul data-role="dropdown" class="d-menu">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 3</a></li>
</ul>
</div>
<!-- With different button styles -->
<div class="dropdown-button">
<button class="button outline dropdown-toggle">
Outline Button
</button>
<ul data-role="dropdown" class="d-menu">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</div>
<!-- Dropdown that opens upward -->
<div class="dropdown-button">
<button class="button dropdown-toggle">
Options
</button>
<ul data-role="dropdown" class="d-menu drop-up">
<li><a href="#">Content appears above the button</a></li>
</ul>
</div>
<!-- Dropdown aligned to the right -->
<div class="dropdown-button">
<button class="button dropdown-toggle">
Options
</button>
<ul data-role="dropdown" class="d-menu drop-down-right" >
<li><a href="#">Content aligned to the right</a></li>
</ul>
</div>
<!-- Colored dropdown buttons -->
<div class="dropdown-button">
<button class="button button-primary dropdown-toggle">
Primary
</button>
<ul data-role="dropdown" class="d-menu">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</div>
<div class="dropdown-button">
<button class="button button-success dropdown-toggle">
Success
</button>
<ul data-role="dropdown" class="d-menu">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</div>

The Dropdown Button component inherits all configuration options from the Dropdown component:

OptionTypeDefaultDescription
dropdownDeferredNumber0Delay in milliseconds before the dropdown appears
dropFilterStringnullCSS selector to filter which dropdowns should be closed when another is opened
toggleElementStringnullCSS selector for the toggle element (if not automatically detected)
alignString”left”Alignment of the dropdown (“left” or “right”)
noCloseBooleanfalseIf true, the dropdown won’t close when clicking outside
durationNumber50Animation duration in milliseconds
openModeString”auto”How the dropdown opens (“auto”, “up”)
openFuncString”show”Function to use for opening animation
closeFuncString”hide”Function to use for closing animation
heightString”auto”Height of the dropdown
// Global configuration
Metro.dropdownSetup({
duration: 100,
noClose: true,
openMode: "up"
});
// Component-specific configuration
const dropdown = Metro.makePlugin("#myDropdownButton ul", "dropdown", {
align: "right",
height: "300px",
onDrop: function() {
console.log("Dropdown opened");
}
});

The Dropdown Button component provides the following API methods (inherited from the Dropdown component):

  • open(immediate, el) - Opens the dropdown. If immediate is true, opens without animation. el is optional element to open.
  • close(immediate, el) - Closes the dropdown. If immediate is true, closes without animation. el is optional element to close.
  • toggle() - Toggles the dropdown between open and closed states.
// Get the dropdown component instance
const dropdown = Metro.getPlugin("#myDropdownButton ul", "dropdown");
// Open the dropdown
dropdown.open();
// Close the dropdown immediately (without animation)
dropdown.close(true);
// Toggle the dropdown state
dropdown.toggle();

The Dropdown Button component provides the following callback events (inherited from the Dropdown component):

EventDescription
onDropTriggered when the dropdown is opened
onUpTriggered when the dropdown is closed
onDropdownCreateTriggered when the dropdown component is created

The Dropdown Button component can be styled using the following CSS variables:

VariableDefault (Light)Dark ModeDescription
--dropdown-button-toggle-color#000#fffColor of the dropdown toggle icon

Additionally, all CSS variables from the Button component can be used to style the button part:

VariableDefault (Light)Dark ModeDescription
--button-border-radius4px4pxBorder radius for standard buttons
--button-pill-radius36px36pxBorder radius for pill buttons
--button-size36px36pxHeight of the button
--button-font-size14px14pxFont size of the button text
--button-background#ebebeb#2b2d30Background color of the button
--button-background-hover#dadada#333439Background color on hover
--button-color#191919#f3fcffText color of the button
--button-border-color#ebebeb#4e5157Border color of the button
--button-disabled-opacity.65.25Opacity for disabled buttons
/* Custom styling for dropdown button */
.custom-dropdown-button {
--dropdown-button-toggle-color: #2196F3;
--button-background: #e0f7fa;
--button-background-hover: #b2ebf2;
--button-color: #006064;
}
  • .dropdown-button - Main class for the dropdown button component
  • All button classes from the Button component can be used
  • See the Button component documentation for the full list of available classes
  • All dropdown positioning and state classes from the Dropdown component can be used
  • See the Dropdown component documentation for the full list of available classes
  1. Use clear and descriptive labels for dropdown buttons
  2. Keep dropdown menus concise and organized
  3. Use appropriate button styles to indicate the type of action
  4. Consider using icons alongside text for better visual recognition
  5. Use appropriate positioning based on the location of the dropdown button in the UI
  6. Ensure dropdown content is accessible and navigable via keyboard