Nested menus
Menus three levels deep, written as a tree. Choosing Settings… opens its menu; the breadcrumb above the input names where you are, and Backspace on an empty search goes back up. A search at the top level still finds every level: type cozy.
// Menus inside menus, written as a tree: `children` holds the child
// actions, and ninja-keys flattens it. An action with children opens its
// menu when chosen; Backspace on an empty search goes back up.
import '@appulsauce/ninja-keys';
import type {INinjaAction, class NinjaKeysA keyboard command palette: a modal list of actions, opened with a hotkey,
searched as you type, with nested menus and per-action hotkeys.
NinjaKeys} from '@appulsauce/ninja-keys';
export default function function setup(ninja: NinjaKeys, report: (message: string) => void, page: HTMLElement): voidsetup(
ninja: NinjaKeysninja: class NinjaKeysA keyboard command palette: a modal list of actions, opened with a hotkey,
searched as you type, with nested menus and per-action hotkeys.
NinjaKeys,
report: (message: string) => voidreport: (message: stringmessage: string) => void,
page: HTMLElementpage: HTMLElement
): void {
const const pick: (choice: string) => () => voidpick = (choice: stringchoice: string) => () => report: (message: string) => voidreport(choice: stringchoice);
const const tree: INinjaAction[]tree: INinjaAction[] = [
{
INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'Projects',
INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Open project…',
INinjaAction.mdIcon?: stringA Material Icons ligature name (settings, dark_mode).
mdIcon: 'folder_open',
INinjaAction.children?: INinjaAction[] | string[]The menu this action opens: the ids of its children in a flat list, or
the child actions themselves in a tree. ninja-keys flattens a tree into
ids, so a searcher always sees ids.
children: [
{INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'atlas', INinjaAction.title: stringThe text shown in the list, and searched.
title: 'atlas', INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Opened atlas')},
{INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'beacon', INinjaAction.title: stringThe text shown in the list, and searched.
title: 'beacon', INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Opened beacon')},
{INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'cinder', INinjaAction.title: stringThe text shown in the list, and searched.
title: 'cinder', INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Opened cinder')},
],
},
{
INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'Settings',
INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Settings…',
INinjaAction.mdIcon?: stringA Material Icons ligature name (settings, dark_mode).
mdIcon: 'settings',
INinjaAction.children?: INinjaAction[] | string[]The menu this action opens: the ids of its children in a flat list, or
the child actions themselves in a tree. ninja-keys flattens a tree into
ids, so a searcher always sees ids.
children: [
{
INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'Theme',
INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Theme…',
INinjaAction.mdIcon?: stringA Material Icons ligature name (settings, dark_mode).
mdIcon: 'palette',
INinjaAction.children?: INinjaAction[] | string[]The menu this action opens: the ids of its children in a flat list, or
the child actions themselves in a tree. ninja-keys flattens a tree into
ids, so a searcher always sees ids.
children: [
{INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'light', INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Light', INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Light theme')},
{INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'dark', INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Dark', INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Dark theme')},
],
},
{
INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'Density',
INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Density…',
INinjaAction.mdIcon?: stringA Material Icons ligature name (settings, dark_mode).
mdIcon: 'density_medium',
INinjaAction.children?: INinjaAction[] | string[]The menu this action opens: the ids of its children in a flat list, or
the child actions themselves in a tree. ninja-keys flattens a tree into
ids, so a searcher always sees ids.
children: [
{INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'compact', INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Compact', INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Compact')},
{INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'cozy', INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Cozy', INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Cozy')},
],
},
],
},
{
INinjaAction.id: stringUnique across all actions. Child actions name their menu by it in
parent, and the breadcrumbs show it while that menu is open.
id: 'sign-out',
INinjaAction.title: stringThe text shown in the list, and searched.
title: 'Sign out',
INinjaAction.mdIcon?: stringA Material Icons ligature name (settings, dark_mode).
mdIcon: 'logout',
INinjaAction.handler?: (action: INinjaAction) => {
keepOpen?: boolean;
} | void
Runs when the action is chosen, by Enter, a click or its hotkey. The
palette closes afterwards unless it returns {keepOpen: true}.
handler: const pick: (choice: string) => () => voidpick('Signed out'),
},
];
ninja: NinjaKeysninja.NinjaKeys.data: INinjaAction[]The actions, as a flat list (children name their menu in parent) or
a tree (menus hold their children in children). Assign a new array to
change it; the hotkeys are rebound each time.
data = const tree: INinjaAction[]tree;
// A button elsewhere on the page can open a menu directly.
page: HTMLElementpage
.ParentNode.querySelector<Element>(selectors: string): Element | null (+4 overloads)Returns the first element that is a descendant of node that matches selectors.
querySelector('[data-open="Theme"]')
?.Element.addEventListener<K>(type: K, listener: (this: Element, ev: ElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void (+1 overload)The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
addEventListener('click', () => ninja: NinjaKeysninja.NinjaKeys.open(options?: {
parent?: string;
search?: string;
} | undefined): void
Show a modal.
search starts the search box with that text, as if typed: a host that
opens the palette on a key press can pass on what the user typed while
it was opening, instead of dropping it or letting it reach the page.
open({parent?: stringparent: 'Theme'}));
}The second button calls ninja.open({parent: 'Theme'}), opening the palette straight into a menu: the pattern for a menu’s own hotkey, or a settings link elsewhere on the page.
The same menus as a flat list, where each child names its menu in parent, behave identically; Actions and menus has both forms side by side.