Menu

A menu of items that is hidden until triggered.

Preview
Code

Basic Example

<script>
	import {
		Menu,
		MenuArrow,
		MenuContent,
		MenuItem,
		MenuSub,
		MenuSubContent,
		MenuSubTrigger,
		MenuTrigger
	} from 'lithesome';
</script>

<Menu>
	<MenuTrigger>Menu</MenuTrigger>
	<MenuContent>
		<MenuItem>Item 1</MenuItem>
		<MenuItem>Item 2</MenuItem>
		<MenuItem>Item 3</MenuItem>
		<MenuSub name="submenu">
			<MenuSubTrigger>Item 4</MenuSubTrigger>

			<MenuSubContent>
				<MenuItem>Item 4-1</MenuItem>
				<MenuItem>Item 4-2</MenuItem>
				<MenuItem>Item 4-4</MenuItem>
			</MenuSubContent>
		</MenuSub>
	</MenuContent>
</Menu>

Disabling an item

By passing the disabled prop on the <MenuItem /> component. Lithesome will disallow clicking and skip over via keyboard navigation of that item.

<Menu>
	<MenuTrigger>Menu</MenuTrigger>
	<MenuContent>
		<MenuItem />
		<MenuItem disabled />
		<MenuItem />
	</MenuContent>
</Menu>

Each <MenuItem /> exposes a href prop. This will automatically turn the button into an anchor tag and place said href onto that item.

<Menu>
	<MenuTrigger>Menu</MenuTrigger>
	<MenuContent>
		<MenuItem href="/settings" />
		<MenuItem disabled />
		<MenuItem />
	</MenuContent>
</Menu>

To keep your menus organized, you can group related items together using the <MenuSub /> component and its related elements.

<Menu>
	<MenuTrigger>Menu</MenuTrigger>
	<MenuContent>
		<MenuItem />
		<MenuSub name="sub">
			<MenuSubTrigger>Sub Trigger</MenuSubTrigger>
			<MenuSubContent>
				<MenuItem />
				<MenuItem />
			</MenuSubContent>
		</MenuSub>
		<MenuItem />
	</MenuContent>
</Menu>

Whenever we want a sub menu, place it in the position to where you would want the new sub menu trigger to be.

The <MenuSub /> component must have a name prop with a unique ID.

Disabling a sub menu

Just like disabling an item, passing the disabled prop to the <MenuSub /> component will disable the <MenuSubTrigger /> and the following contents.

<Menu>
	<MenuTrigger>Menu</MenuTrigger>
	<MenuContent>
		<MenuItem />
		<MenuSub name="sub" disabled>
			<MenuSubTrigger>Sub Trigger</MenuSubTrigger>
			<MenuSubContent>
				<MenuItem />
				<MenuItem />
			</MenuSubContent>
		</MenuSub>
		<MenuItem />
	</MenuContent>
</Menu>

Deeply nested sub menus

The <MenuSub /> components can be place inside of one another to achieve a deeply nested menu.

<Menu>
	<MenuTrigger>Menu</MenuTrigger>
	<MenuContent>
		<MenuItem />
		<MenuSub name="sub">
			<MenuSubTrigger>Sub Trigger</MenuSubTrigger>
			<MenuSubContent>
				<MenuItem />
				<MenuSub name="anotherone">
					<MenuSubTrigger>Sub Trigger</MenuSubTrigger>
					<MenuSubContent>
						<MenuItem />
						<MenuItem />
					</MenuSubContent>
				</MenuSub>
				<MenuItem />
			</MenuSubContent>
		</MenuSub>
		<MenuItem />
	</MenuContent>
</Menu>

API Reference

<Menu />

PropertyTypeDescription
children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

visible boolean

The current visibility of the contents.

disabled boolean

Disables the entire component tree.

portalTarget HTMLElement | string

The DOM target to mount the content to.

floatingConfig FloatingConfig

The underlying FloatingUI config.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<MenuTrigger />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<MenuContent />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<MenuArrow />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<MenuItem />

PropertyTypeDescription
href string

Turns the item into an anchor tag with the provided `href` applied.

disabled boolean

Disables the item, skipping mouse/keyboard navigation and stopping events from firing.

closeOnClick boolean

Closes the item on click/keyboard press.

ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
disabledbooleanTrue if the item is disabled.
activebooleanTrue if the item is being hovered.

<MenuSub />

PropertyTypeDescription
name string

The unique ID of the sub menu. This ID must not change on renders. So using an autogenerated id will not work and cause the sub menus to fail.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

visible boolean

The current visibility of the contents.

disabled boolean

Disables the entire component tree.

portalTarget HTMLElement | string

The DOM target to mount the content to.

floatingConfig FloatingConfig

The underlying FloatingUI config.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<MenuSubTrigger />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
disabledbooleanTrue if the sub menu is disabled.
activebooleanTrue if item is hovered.
openedbooleanTrue if the sub menu is opened.

<MenuSubContent />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.