1.0.3 • Published 3 years ago

@miraidesigns/menu v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Menus

Menus display a list of choices or options to the user.\ Requires the List module for correct appearance.


HTML

<button id="menu-anchor" class="mdf-button">Show menu</button>

<div class="mdf-menu">
    <ul class="mdf-list" role="menu" aria-orientation="vertical" aria-hidden="true" tabindex="-1">
        <li class="mdf-list__item" role="menuitem" tabindex="-1">Menu item</li>
        <li class="mdf-list__item" role="menuitem" tabindex="-1">Menu item</li>
        <li class="mdf-list__item" role="menuitem" tabindex="-1">Menu item</li>
    </ul>
</div>

Sass

// Include default styles without configuration
@forward '@miraidesigns/menu/styles';
// Configure appearance
@use '@miraidesigns/menu' with (
    $variable: value
);

@include menu.styles();

TypeScript

import { MDFMenu } from '@miraidesigns/menu';

new MDFMenu(document.querySelector('.mdf-menu'), {
    anchor: document.getElementById('menu-anchor')
});

Implementation

Attributes

Please see the WAI-ARIA page for attributes and best practices regarding menus.

NameElementDescription
data-menu-callback<li>Name of the callback has to match the name set in the script options

Classes

NameTypeDescription
mdf-menuParentContains the menu list and its items
mdf-menu--activeModifierFades-in and moves menu on-screen
mdf-menu__captionChildCaption to describe the menu

Events

NameDataDescription
MDFMenu:openednullFires when the menu opens
MDFMenu:activated{callback: string, index: number, item: HTMLElement}Fires whenever the a menu item is activated. Includes the callback name, the item itself and its index
MDFMenu:closednullFires when the menu closes

Properties

NameTypeDescription
.anchorHTMLElementReturns the anchor element
.itemsHTMLElement[]Returns an Array with all menu items
.menuHTMLElementReturns the menu element
.isActive()(): booleanWether or not the menu is visible
.openMenu(setFocus?)(boolean): voidOpen the menu (optionally set focus on the first item)
.closeMenu(setFocus?)(boolean): voidClose the menu (optionally set focus on the anchor)

Options

NameTypeDefaultDescription
anchorElementnullThe anchor element that will call the menu
posXstringcenterThe menu's position on the X-axis. Values are left center right
posYstringbottomThe menu's position on the Y-axis. Values are top center bottom
originstringtop centerOrigin of the menu transition. Same values as the transform-origin CSS property
callbacksRecord<string, () => void>{}Object holding the menu item callbacks
onOpen() => voidnullFunction will run when the menu is opened
onClose() => voidnullFunction will run when the menu is closed
hideOnClickbooleantrueClose the menu when a click is registered outside of it