Skip to content

DisclosureMenu

An accessible disclosure menu in the DOM.

Note

This is a subclass of BaseMenu.

Constructor

Constructs a new DisclosureMenu.

js
new DisclosureMenu({
  menuElement,
  menuItemSelector,
  menuLinkSelector,
  submenuItemSelector,
  submenuToggleSelector,
  submenuSelector,
  controllerElement,
  containerElement,
  openClass,
  closeClass,
  transitionClass,
  transitionDuration,
  openDuration,
  closeDuration,
  isTopLevel,
  parentMenu,
  hoverType,
  hoverDelay,
  enterDelay,
  leaveDelay,
  optionalKeySupport,
  prefix,
  initialize,
});

The constructor will call BaseMenu's constructor with the provided options. It will also populate the optionalSupport property and initialize the menu if the initialize flag is set to true.

Parameters

NameTypeDescriptionDefault
optionsobjectThe options for generating the menu.undefined
options.menuElementHTMLElementThe menu element in the DOM.undefined
options.menuItemSelectorstringThe query selector string for menu items."li"
options.menuLinkSelectorstringThe query selector string for menu links."a"
options.submenuItemSelectorstringThe query selector string for menu items containing submenus.li:has(ul)
options.submenuToggleSelectorstringThe query selector string for submenu toggle buttons/links."button"
options.submenuSelectorstringThe query selector string for submenus."ul"
options.controllerElementHTMLElement, nullThe element controlling the menu in the DOM.null
options.containerElementHTMLElement, nullThe element containing the menu in the DOM.null
options.openClassstring, string[], nullThe class to apply when a menu is "open"."show"
options.closeClassstring, string[], nullThe class to apply when a menu is "closed"."hide"
options.transitionClassstring, string[], nullThe class to apply when a menu is transitioning between "open" and "closed" states.transitioning
options.transitionDurationnumberThe duration of the transition between "open" and "closed" states (in milliseconds).250
options.openDurationnumberThe duration of the transition from "closed" to "open" states (in milliseconds).250
options.closeDurationnumberThe duration of the transition from "open" to "closed" states (in milliseconds).250
options.isTopLevelbooleanA flag to mark the root menu.true
options.parentMenuDisclosureMenu, nullThe parent menu to this menu.null
options.hoverTypestringThe type of hoverability a menu has."off"
options.hoverDelaynumberThe delay for opening and closing menus if the menu is hoverable (in milliseconds).250
options.enterDelaynumberThe delay for opening a menu if the menu is focusable (in milliseconds).-1
options.leaveDelaynumberThe delay for closing a menu if the menu is focusable (in milliseconds).-1
options.optionalKeySupportbooleanA flag to add optional keyboard support (Arrow keys, Home, and End) to the menu.false
options.prefixstring, nullThe prefix for the CSS custom properties."am-"
options.initializebooleanA flag to initialize the menu immediately upon creation.true

Initialize

Initializes the menu.

js
DisclosureMenu.initialize();

Initialize will call BaseMenu's initialize method as well as set up focus (inherited), click, hover (inherited), keydown, and keyup events for the menu.

If the BaseMenu's initialize method throws an error, this will catch it and log it to the console.

Properties

Properties are inherited from the BaseMenu class. The following properties are unique to or overwritten in the DisclosureMenu class.

_MenuType protected

The class to use when generating submenus.

js
DisclosureMenu._MenuType; // Default: `DisclosureMenu`.

Type

Class

_MenuItemType protected

The class to use when generating menu items.

js
DisclosureMenu._MenuItemType; // Default: `DisclosureMenuItem`.

Type

Class

_MenuToggleType protected

The class to use when generating menu toggles.

js
DisclosureMenu._MenuToggleType; // Default: `DisclosureMenuToggle`.

Type

Class

_currentChild protected

The index of the currently selected menu item in the menu.

js
DisclosureMenu._currentChild; // Default: `-1`.

Type

number

_optionalSupport protected

A flag to add optional keyboard support (Arrow keys, "Home", and "End") to the menu.

js
DisclosureMenu._optionalSupport; // Default: `false`.

Type

boolean

Getters and Setters

Getters and setters are inherited from the BaseMenu class. The following getters and setters are unique to or overwritten in the DisclosureMenu class.

optionalKeySupport

A flag to add optional keyboard support (Arrow keys, "Home", and "End") to the menu.

js
/**
 * @type {boolean}
 *
 * @see _optionalSupport
 */
DisclosureMenu.optionalKeySupport;
js
/**
 * @type {boolean}
 */
DisclosureMenu.optionalKeySupport = true;

This functions differently for root vs. submenus. Submenus will always inherit their root menu's optionalKeySupport.

Methods

Methods are inherited from the BaseMenu class. The following methods are unique to or overwritten in the DisclosureMenu class.

_validate protected

Validates all aspects of the menu to ensure proper functionality.

js
DisclosureMenu._validate();

The validation method will call BaseMenu's validation method as well as validate the optionalKeySupport property.

Returns

TypeDescription
booleanThe result of the validation.

_handleClick protected

Handles click events throughout the menu for proper use.

js
DisclosureMenu._handleClick();

This method will do the following:

  • Adds all event listeners listed in BaseMenu's _handleClick method.
  • Adds a pointerup listener to the document so if the user clicks outside of the menu it will close if it is open.

_handleKeydown protected

Handles keydown events throughout the menu for proper menu use.

js
DisclosureMenu._handleKeydown();

This method exists to assist the _handleKeyup method.

This method will do the following:

  • Adds all keydown listeners from BaseMenu's _handleKeydown method.
  • Adds a keydown listener to the menu/all submenus.
    • Blocks propagation on the following keys: "Space", "Enter", and "Escape".
    • If optional keyboard support is enabled, blocks propagation on the following keys: "ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft", "Home", and "End".

_handleKeyup protected

Handles keyup events throughout the menu for proper menu use.

js
DisclosureMenu._handleKeyup();

Adds all keyup listeners from BaseMenu's _handleKeyup method.

Adds the following keybindings (explanations are taken from the WAI ARIA Practices Example Disclosure for Navigation Menus:

KeyFunction
Tab or Shift + TabMove keyboard focus among top-level buttons, and if a dropdown is open, into and through links in the dropdown.
Space or Enter
  • If focus is on a disclosure button, activates the button, which toggles the visibility of the dropdown.
  • If focus is on a link:
    • If any link has aria-current set, removes it.
    • Sets aria-current="page" on the focused link.
    • Activates the focused link.
EscapeIf a dropdown is open, closes it and sets focus on the button that controls that dropdown.
Down Arrow or Right Arrow (Optional})
  • If focus is on a button and its dropdown is collapsed, and it is not the last button, moves focus to the next button.
  • if focus is on a button and its dropdown is expanded, moves focus to the first link in the dropdown.
  • If focus is on a link, and it is not the last link, moves focus to the next link.
Up Arrow or Left Arrow (Optional})
  • If focus is on a button, and it is not the first button, moves focus to the previous button.
  • If focus is on a link, and it is not the first link, moves focus to the previous link.
Home (Optional)
  • If focus is on a button, and it is not the first button, moves focus to the first button.
  • If focus is on a link, and it is not the first link, moves focus to the first link.
End (Optional)
  • If focus is on a button, and it is not the last button, moves focus to the last button.
  • If focus is on a link, and it is not the last link, moves focus to the last link.

The optional keybindings are controlled by the menu's optionalKeySupport value.