Dropdown dropdown.js
Add a context menu or list of links to a control item. Support for nested lists is included automatically.
Widget Dependencies
Dropdown requires the following:
- The third-party library Popper.js to provide dynamic positioning and viewport detection. Static positioning does not require the use of Popper.js.
Incompatible Widgets
For accessibility reasons, do not mix use of the Tab widget and Dropdown widget in the same nav item. This will cause navigation and usability issues. One or the other, but not both.
Contents
Overview
Wrap the dropdown’s toggle (your button or link) and the dropdown menu within .dropdown
, or another element that declares position: relative;
. Dropdowns can be triggered from <a>
or <button>
elements.
Because of the support for nested dropdown menus, it is currently required to use a ul
element to build your dropdown menus.
There is an expand on hover option available, even though we recommend that you use the default click to toggle mode for consitent usability across devices.
Examples
Dropdown Layout
Here is a static example showing the dropdown layout and content pieces.
Basic Dropdown
Wrap the dropdown’s trigger and the dropdown menu within .dropdown
, or another element that declares position: relative;
. Then, add the menu’s HTML.
Toggle Indicator
Optionally use the .caret
utility icon and add it as an element within the control element. Use spacing utilities as needed.
We use this method instead of using a class placed on the control so that you can use your own icons as needed, and not have interference from hard-coded functionality.
Single Button Dropdown
You can also use <button>
elements in your dropdowns instead of <a>
s. You can also use swap out the .dropdown
class on the parent container with .btn-group
if desired.
Split Button Dropdown
Similarly, create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of .btn-icon
for proper spacing around the caret. We use this extra class to reduce the horizontal padding
on either side of the caret and provide a more appropriately sized hit area next to the main button.
The use of the .btn-group-end
class allows us to place the dropdown within the .btn-group
itself and not reset the border-radius
on the end side of the button.
Within a Navbar
Dropdowns also work in a navbar, but require the use of a wrapping element for positioning. Make sure to use seperate and nested .nav-item
and .nav-link
elements as in the following example.
Components
Menu Headers
Add a header to label sections of actions in any dropdown menu with .dropdown-header
.
Menu Text
Add a non-interactive text item to a dropdown menu with .dropdown-text
.
Menu Dividers
Separate groups of related menu items with a divider by using .dropdown-divider
.
Disabled Menu Items
Add .disabled
to the a
item in the dropdown to make them visually appear disabled.
Disabling Anchors
Please refer to the Accessiblity notes about disabled anchors.
Active Menu Items
Add .active
to the li
item in the dropdown to show a visual emphasis.
Submenus
You can nest submenus by adding a nested list along side it’s toggle.
‘Back’ Menu Items
Using the backlink
option, you can have ‘back’ menu items automatically inserted into all submenus. These links will close the current submenu and move focus back onto the parent menu item. This can be useful if the parent menu/submenu item is being hidden, or obscured by the current submenu.
Special Items
The Dropdown widget in Figuration is primarily designed for menus and navigation, not a container for forms or editable content, such as a login or registration. You might want to consider using the Popover widget instead, or reworking the workflow or interface design.
However, there is some support for handling <button>
, <input>
, and <textarea>
elements within a menu. Each of these special items require the use of either the .dropdown-item
or .dropdown-text
class when inside the menu to adjust their layout .
Buttons
You can optionally use <button>
elements in your dropdowns instead of just <a>
s.
Checkbox and Radio Inputs
Checkbox and radio inputs are allowed, but only one per menu item.
Textual Inputs
Add <input type="text">
or textarea
items to your dropdown menu. Other types of textual inputs have not been tested, and may cause issues. Again, use only one per menu item.
Variants
Dropup
Trigger dropdown menus above elements by adding .dropup
to the parent element. The visual .caret
for the toggle control will reverse direction automatically.
Menu Alignment
By default, a dropdown menu is automatically positioned 100% from the top and aligned to the left side of its parent. While submenu items are aligned 100% from the left and to the top of its parent.
Add .dropend
to a .dropdown-menu
to align the dropdown menu to the right side of the parent. This will also make all submenus open out to the left side. This can also be combined with .dropup
.
Heads up! When using the right-to-left, rtl
, variant of Figuration all horizontal directions will be reversed. Meaning left becomes right, and vice-versa.
Submenu Alignment
The menu alignment class of .dropend
will also work with submenu items, and you can use the available .dropstart
to switch submenu directions if needed. Simply place either class on the li
parent of the submenu list.
Using a Reference
Use the reference
option to help control the location of a dropdown menu.
Usage
Via data attributes or JavaScript, the dropdown widget toggles hidden content (dropdown menus) by toggling the .open
class on the parent list item.
On touch capable devices, the optional expand on hover functionality is forced off in favor of the default click interaction. Also, opening a dropdown adds empty ($.noop
) mouseover
handlers to the immediate children of the <body>
element. This ugly hack is necessary to work around a quirk in iOS’ event delegation, which would otherwise prevent a tap anywhere outside of the dropdown from triggering the code that closes the dropdown. Once the dropdown is closed, these additional empty mouseover
handlers are removed.
Note: The data-cfw="dropdown"
attribute is relied on for closing dropdown menus at an application level, so it’s a good idea to always use it.
Via Data Attributes
Add data-cfw="dropdown"
to the dropdown toggle element, and the widget will automatically link to the sibling .dropdown-menu
list element.
Be sure to add the class dropdown-menu
to the dropdown menu to ensure there is no flash of content at page load.
Via JavaScript
Call the dropdowns via JavaScript:
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-cfw-dropdown
, as in data-cfw-dropdown-backlink="true"
.
Name | Type | Default | Description |
---|---|---|---|
target | string | null |
Either the selector (jQuery style), or the string related to the target dropdown having a data-cfw-dropdown-target attribute. |
delay | integer | 350 |
Delay for hiding menu on loss of focus or hover when not in click only mode (milliseconds). |
hover | boolean | false |
If hover style navigation should be enabled in addition to click/key navigation. If a touch capable device is found, this setting is overruled. |
backlink | boolean | false |
Insert back links into submenus. |
backtop | boolean | false |
If back links should be applied at the top level menu as opposed to only submenus. |
backtext | string | Back |
Text to be used for back links. |
container | element | false | false |
Appends the dropdown menu to a specific element. Example: |
reference | string | element | 'toggle' |
Reference element of the dropdown menu. Accepts the values of |
boundary | string | element | 'scrollParent' |
Overflow constraint boundary of the dropdown menu. Accepts the values of |
flip | boolean | 'true' |
Allow Dropdown to flip in case of an overlapping on the reference element. For more information refer to Popper.js's flip docs. |
display | string | 'dynamic' |
By default, we use Popper.js for dynamic positioning. Disable this with |
popperConfig | null | object | null |
Pass a customized Popper.js configuration that will override the default Popper.js configuration. |
Methods
.CFW_Dropdown(options)
Activates the dropdown menu. Accepts an optional options object
.
.CFW_Dropdown('toggle')
Toggles a root menu to be shown or hidden.
.CFW_Dropdown('show')
Shows the root menu element.
.CFW_Dropdown('hide')
Hides the root menu element.
.CFW_Dropdown('dispose')
Hides the root menu element and disconnect all the event listeners and data from the menu items and the trigger element.
Events
Event callbacks for the root menu happen on the toggle element. Callbacks for the submenus occur on the submenu’s sibling anchor (toggle).
Show and hide, both before and after, events have an added relatedTarget
property, whose value is the toggling anchor element.
Before and after hide events have a clickEvent
property (only when the original event type is click
) that contains an event object for the click event.
Event Type | Description |
---|---|
init.cfw.dropdown | This event fires after the menu item is initialized. |
beforeShow.cfw.dropdown | This event is fired immediately when the internal showMenu method is called. |
afterShow.cfw.dropdown | This event is fired when a menu element has been made visible to the user. |
beforeHide.cfw.dropdown | This event is fired immediately when the internal hideMenu method is called. |
afterHide.cfw.dropdown | This event is fired when a menu element has been hidden from the user. |
Accessibility
General Purpose
While there is an official WAI-ARIA specification for a role="menu"
widget, it is mainly intended for application-style menus that invoke functionality or actions.
The dropdown widget provided by Figuration is intended be generic and apply to a wider number of use-cases. If you require full ARIA compliant menus, then you will need to add the appropriate role
and aria-
attributes as needed.
Keyboard Navigation
-
enter/
space - When the focus is on the main trigger item, the menu is opened, and the menu items can be navigated using the arrow keys.
- esc
- Closes the currently focused menu, and moved focus to the main trigger.
- /
- Moves focus to the previous or next item in the menu list. If current focus is in a textarea, the text caret will move accordingly. If current focus is on a checkbox or radio input, moves focus to the previous or next item in the menu list.
- Opens the submenu if one exists. If current focus is in a text input or textarea, the text caret will move accordingly.
- Closes the currently focused submenu, and returns focus back to the triggering element. If there are no submenus open, focus will be returned to the main trigger. If current focus is in a text input or textarea, the text caret will move accordingly.
SASS Reference
Variables
The available Customization options, or Sass variables, that can be customized for the dropdown component.
Name | Type | Default | Description |
---|---|---|---|
$enable-dropdown |
boolean | true |
Enable the generation of the dropdown component classes.
Smaller segements of the dropdown component classes can be disabled with the following $enable-* variables.
|
$enable-dropdown-header |
boolean | true |
Enable the generation of the dropdown header class. |
$enable-dropdown-text |
boolean | true |
Enable the generation of the dropdown text class. |
$enable-dropdown-divider |
boolean | true |
Enable the generation of the dropdown divider class. |
$enable-dropdown-dropup |
boolean | true |
Enable the generation of the dropdown dropup classes. |
$enable-dropdown-back |
boolean | true |
Enable the generation of the dropdown 'back' caret indicator class. |
$dropdown-min-width |
string | 10rem |
Minimum width for dropdown menus. |
$dropdown-padding-y |
string | .3135rem |
Vertical padding for dropdown menus. |
$dropdown-spacer |
string | .125rem |
Top vertical spacing for dropdown menus. |
$dropdown-font-size |
string | $font-size-base |
Font size for dropdown menus. |
$dropdown-bg |
string | $component-bg |
Background color for dropdown menus. |
$dropdown-border-color |
string | $component-border-color |
Border color for dropdown menus. |
$dropdown-border-width |
string | $border-width |
Border width for dropdown menus. |
$dropdown-border-radius |
string | $border-radius |
Border radius for dropdown menus. |
$dropdown-divider-width |
string | $border-width |
Border width for dropdown menu dividers. |
$dropdown-divider-color |
string | $component-section-border-color |
Border color for dropdown menu dividers. |
$dropdown-divider-spacer |
string | .3125rem |
Vertical spacing for dropdown menu dividers. |
$dropdown-box-shadow |
string | map-get($shadows, "d2") |
Box shadow for dropdown menus. |
$dropdown-link-color |
string | $component-action-color |
Text color for dropdown menu links. |
$dropdown-link-hover-color |
string | $component-action-hover-color |
Text color for dropdown menu links in hover and focus states. |
$dropdown-link-hover-bg |
string | $component-hover-bg |
Background color for dropdown menu links in hover and focus states. |
$dropdown-link-active-color |
string | $component-active-color |
Text color for dropdown menu links in active state. |
$dropdown-link-active-bg |
string | $component-active-bg |
Background color for dropdown menu links in active state. |
$dropdown-link-disabled-color |
string | $component-active-color |
Text color for dropdown menu links in disabled state. |
$dropdown-link-disabled-bg |
string | $component-active-bg |
Background color for dropdown menu links in disabled state. |
$dropdown-item-padding-y |
string | .125rem |
Vertical padding for dropdown menu links. |
$dropdown-item-padding-x |
string | 1.125rem |
Horizontal padding for dropdown menu links. |
$dropdown-header-font-size |
string | ($font-size-base * .875) |
Font size for dropdown menu headers. |
$dropdown-header-font-weight |
string | $font-weight-bold |
Font weight for dropdown menu headers. |
$dropdown-header-color |
string | $uibase-500 |
Text color for dropdown menu headers. |
$dropdown-text-color |
string | $body-color |
Text color for dropdown menu text. |
$dropdown-caret-width |
string | $caret-border-width |
Border width for dropdown submenu indicator. |
$dropdown-caret-color |
string | $uibase-400 |
Border color for dropdown submenu indicator. |
$dropdown-caret-active-color |
string | $component-active-color |
Border color for dropdown submenu indicator in active state. |
$dropdown-caret-spacer-x |
string | .375rem |
Horizontal spacing for dropdown submenu indicator. |
$dropdown-back-width |
string | $caret-border-width |
Border width for dropdown menu 'back' indicator. |
$back-caret-color |
string | $uibase-400 |
Border color for dropdown menu 'back' indicator. |
$dropdown-back-spacer-x |
string | .375rem |
Horizontal spacing for dropdown menu 'back' indicator. |
Mixins
No mixins available.