Menu

Provides quick context menus when you tap the trigger element.

javascript
import { menu } from '@brainandbones/skeleton';
javascript
// Included in 'all.css' and 'elements.css'
import '@brainandbones/skeleton/styles/elements/menus.css';

Examples

This menu uses default settings. The position will auto-update depending on the trigger's page location.

This menu is arbitrarily positioned in the top-right corner of the page. We've made it a bright color to catch you attention.

Usage

Menus make use of both Tailwind Element styles as well as a Svelte Action. Below is a basic example with minimal styles.

html
<!-- Use a wrapping .relative class to confine the menu position -->
<span class="relative">

	<!-- Trigger: apply the 'use:menu' action and supply the unique menu ID -->
	<button use:menu={{ menu: 'example' }}>Trigger</button>

	<!-- Menu: set a matching 'data-menu-[menuId]' attribute -->
	<div data-menu="example">(menu)</div>
	
</span>

Menu Styles

Use .card classes to alter the appearance of the menu element.

html
<div class="card card-body" data-menu="example">(menu)</div>

Pair this with Tailwind utility classes to customize the look and feel.

html
<div class="card p-2 w-64 shadow-xl" data-menu="example">(menu)</div>

Combine .list-nav, and .card Tailwind Element classes to create a navigation menu.

html
<nav class="list-nav card p-4 w-64 shadow-xl" data-menu="example">
	<ul>
		<li><a href="/">Home</a></li>
		<li><a href="/">About</a></li>
		<li><a href="/">Blog</a></li>
	</ul>
</nav>

Fixed Origin

Use fixed: true and apply the desired origin via the menu-[tl|tr|bl|br] class.

html
<button use:menu={{ menu: 'example', fixed: true }}>Trigger</button>
html
<div class="menu-tl hidden" data-menu="example">(menu)</div>

Interactive Menus

By default menus will self-close when clicking within the menu body. Set interactive: true to alter this behavior.

html
<button use:menu={{ menu: 'example', interactive: true }}>Trigger</button>

Custom Positioning

Remove the wrapping .relative element, set fixed: true, and position the menu with utility classes.

html
<button use:menu={{ menu: 'example', fixed: true }}>Trigger</button>
html
<div class="absolute top-2 right-2" data-menu="example">(menu)</div>

Data Attributes

Name Description
data-menu="{menuId}"Provide a unique identifier for the menu element. This pairs with menu action param.

Action Params

Key Type Required Description
menustringAccepts the matching menu ID specified in the data attribute.
fixedboolean-When enabled, a fixed origin position can be specified via a menu class.
interactiveboolean-When enabled, keeps the menu open while interacting with the contents.

Classes

Class Description
.menu-tlSpecifies a fixed origin position of top-left.
.menu-trSpecifies a fixed origin position of top-right.
.menu-blSpecifies a fixed origin position of bottom-left.
.menu-brSpecifies a fixed origin position of bottom-right.

Accessibility

The attribute role="menu" will be applied to the menu automatically. However, please review the menus guidelines for handling menu bars or menu items.

Keyboard Interactions

Keys Description
EnterWhen menu button in focus, toggles the menu open/close.
SpaceWhen menu button in focus, toggles the menu open/close.
EscClose the open menu.