2021-03-24 14:21:21 +00:00
|
|
|
import { LitElement, html, unsafeCSS } from 'lit';
|
2021-05-27 21:00:43 +00:00
|
|
|
import { customElement, query } from 'lit/decorators.js';
|
2021-03-18 13:04:23 +00:00
|
|
|
import { event, EventEmitter } from '../../internal/decorators';
|
2021-02-26 14:09:13 +00:00
|
|
|
import { getTextContent } from '../../internal/slot';
|
2021-05-27 20:29:10 +00:00
|
|
|
import type SlMenuItem from '../menu-item/menu-item';
|
2021-05-03 19:08:17 +00:00
|
|
|
import styles from 'sass:./menu.scss';
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-17 10:09:10 +00:00
|
|
|
* @since 2.0
|
2020-07-15 21:30:37 +00:00
|
|
|
* @status stable
|
|
|
|
*
|
|
|
|
* @slot - The menu's content, including menu items, menu dividers, and menu labels.
|
|
|
|
*
|
|
|
|
* @part base - The component's base wrapper.
|
|
|
|
*/
|
2021-03-18 13:04:23 +00:00
|
|
|
@customElement('sl-menu')
|
2021-03-09 00:14:32 +00:00
|
|
|
export default class SlMenu extends LitElement {
|
2021-03-06 17:01:39 +00:00
|
|
|
static styles = unsafeCSS(styles);
|
|
|
|
|
|
|
|
@query('.menu') menu: HTMLElement;
|
2021-04-15 18:27:35 +00:00
|
|
|
@query('slot') defaultSlot: HTMLSlotElement;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2021-04-15 18:27:35 +00:00
|
|
|
private items: SlMenuItem[] = [];
|
2021-02-26 14:09:13 +00:00
|
|
|
private typeToSelectString = '';
|
|
|
|
private typeToSelectTimeout: any;
|
2020-07-21 19:18:58 +00:00
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
/** Emitted when a menu item is selected. */
|
|
|
|
@event('sl-select') slSelect: EventEmitter<{ item: SlMenuItem }>;
|
|
|
|
|
2020-07-24 19:28:14 +00:00
|
|
|
/**
|
|
|
|
* Initiates type-to-select logic, which automatically selects an option based on what the user is currently typing.
|
|
|
|
* The key passed will be appended to the internal query and the selection will be updated. After a brief period, the
|
|
|
|
* internal query is cleared automatically. This method is intended to be used with the keydown event. Useful for
|
|
|
|
* enabling type-to-select when the menu doesn't have focus.
|
|
|
|
*/
|
2021-02-26 14:09:13 +00:00
|
|
|
typeToSelect(key: string) {
|
2020-07-24 19:28:14 +00:00
|
|
|
clearTimeout(this.typeToSelectTimeout);
|
|
|
|
this.typeToSelectTimeout = setTimeout(() => (this.typeToSelectString = ''), 750);
|
|
|
|
this.typeToSelectString += key.toLowerCase();
|
2021-04-15 18:27:35 +00:00
|
|
|
for (const item of this.items) {
|
2021-02-26 14:09:13 +00:00
|
|
|
const slot = item.shadowRoot!.querySelector('slot:not([name])') as HTMLSlotElement;
|
2020-07-24 19:28:14 +00:00
|
|
|
const label = getTextContent(slot).toLowerCase().trim();
|
|
|
|
if (label.substring(0, this.typeToSelectString.length) === this.typeToSelectString) {
|
2021-03-29 11:16:40 +00:00
|
|
|
item.focus();
|
2020-07-24 19:28:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:27:35 +00:00
|
|
|
syncItems() {
|
|
|
|
this.items = [...this.defaultSlot.assignedElements({ flatten: true })].filter(
|
2021-05-27 20:29:10 +00:00
|
|
|
(el: any) => el.tagName.toLowerCase() === 'sl-menu-item' && !el.disabled
|
2021-02-26 14:09:13 +00:00
|
|
|
) as [SlMenuItem];
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getActiveItem() {
|
2021-04-15 18:27:35 +00:00
|
|
|
return this.items.filter(i => i.shadowRoot!.querySelector('.menu-item--focused'))[0];
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
setActiveItem(item: SlMenuItem) {
|
2021-03-29 11:16:40 +00:00
|
|
|
item.focus();
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(event: MouseEvent) {
|
|
|
|
const target = event.target as HTMLElement;
|
2021-02-26 14:09:13 +00:00
|
|
|
const item = target.closest('sl-menu-item') as SlMenuItem;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
if (item && !item.disabled) {
|
2021-03-06 17:01:39 +00:00
|
|
|
this.slSelect.emit({ detail: { item } });
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown(event: KeyboardEvent) {
|
|
|
|
// Make a selection when pressing enter
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
const item = this.getActiveItem();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (item) {
|
2021-03-06 17:01:39 +00:00
|
|
|
this.slSelect.emit({ detail: { item } });
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent scrolling when space is pressed
|
|
|
|
if (event.key === ' ') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the selection when pressing down or up
|
|
|
|
if (['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
|
|
|
|
const selectedItem = this.getActiveItem();
|
2021-04-15 18:27:35 +00:00
|
|
|
let index = selectedItem ? this.items.indexOf(selectedItem) : 0;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2021-04-15 18:27:35 +00:00
|
|
|
if (this.items.length) {
|
2020-07-15 21:30:37 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (event.key === 'ArrowDown') {
|
|
|
|
index++;
|
|
|
|
} else if (event.key === 'ArrowUp') {
|
|
|
|
index--;
|
|
|
|
} else if (event.key === 'Home') {
|
|
|
|
index = 0;
|
|
|
|
} else if (event.key === 'End') {
|
2021-04-15 18:27:35 +00:00
|
|
|
index = this.items.length - 1;
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index < 0) index = 0;
|
2021-04-15 18:27:35 +00:00
|
|
|
if (index > this.items.length - 1) index = this.items.length - 1;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2021-04-15 18:27:35 +00:00
|
|
|
this.setActiveItem(this.items[index]);
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 12:21:39 +00:00
|
|
|
this.typeToSelect(event.key);
|
2020-07-24 14:41:52 +00:00
|
|
|
}
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2021-04-15 18:27:35 +00:00
|
|
|
handleSlotChange() {
|
|
|
|
this.syncItems();
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
render() {
|
2021-02-26 14:09:13 +00:00
|
|
|
return html`
|
2021-03-06 17:01:39 +00:00
|
|
|
<div part="base" class="menu" role="menu" @click=${this.handleClick} @keydown=${this.handleKeyDown} tabindex="0">
|
2021-04-15 18:27:35 +00:00
|
|
|
<slot @slotchange=${this.handleSlotChange}></slot>
|
2020-07-15 21:30:37 +00:00
|
|
|
</div>
|
2021-02-26 14:09:13 +00:00
|
|
|
`;
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:07:38 +00:00
|
|
|
|
2021-03-12 14:09:08 +00:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-menu': SlMenu;
|
|
|
|
}
|
|
|
|
}
|