shoelace/src/components/menu-item/menu-item.ts

103 wiersze
2.7 KiB
TypeScript
Czysty Zwykły widok Historia

2021-02-26 14:09:13 +00:00
import { classMap, html, Shoemaker } from '@shoelace-style/shoemaker';
import styles from 'sass:./menu-item.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
*
2021-02-26 14:09:13 +00:00
* @dependency sl-icon
*
2020-07-15 21:30:37 +00:00
* @slot - The menu item's label.
* @slot prefix - Used to prepend an icon or similar element to the menu item.
* @slot suffix - Used to append an icon or similar element to the menu item.
*
* @part base - The component's base wrapper.
* @part checked-icon - The container that wraps the checked icon.
* @part prefix - The prefix container.
* @part label - The menu item label.
* @part suffix - The suffix container.
*/
2021-02-26 14:09:13 +00:00
export default class SlMenuItem extends Shoemaker {
static tag = 'sl-menu-item';
static props = ['hasFocus', 'checked', 'value', 'disabled'];
static reflect = ['checked', 'disabled'];
static styles = styles;
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
private hasFocus = false;
private menuItem: HTMLElement;
2020-10-22 17:41:09 +00:00
2021-02-26 14:09:13 +00:00
/** Draws the item in a checked state. */
checked = false;
2020-07-15 21:30:37 +00:00
2020-12-30 18:32:42 +00:00
/** A unique value to store in the menu item. This can be used as a way to identify menu items when selected. */
2021-02-26 14:09:13 +00:00
value = '';
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
/** Draws the menu item in a disabled state. */
disabled = false;
2020-10-22 17:41:09 +00:00
/** Sets focus on the button. */
2021-02-26 14:09:13 +00:00
setFocus(options?: FocusOptions) {
2021-01-07 15:13:08 +00:00
this.menuItem.focus(options);
2020-10-22 17:41:09 +00:00
}
2020-10-22 17:41:09 +00:00
/** Removes focus from the button. */
2021-02-26 14:09:13 +00:00
removeFocus() {
2020-10-22 17:41:09 +00:00
this.menuItem.blur();
}
handleBlur() {
this.hasFocus = false;
}
handleFocus() {
this.hasFocus = true;
}
2020-12-11 22:09:28 +00:00
handleMouseEnter() {
2020-10-22 17:41:09 +00:00
this.setFocus();
}
2020-12-11 22:09:28 +00:00
handleMouseLeave() {
2020-10-22 17:41:09 +00:00
this.removeFocus();
}
2020-07-15 21:30:37 +00:00
render() {
2021-02-26 14:09:13 +00:00
return html`
2020-07-15 21:30:37 +00:00
<div
2021-02-26 14:09:13 +00:00
ref=${(el: HTMLElement) => (this.menuItem = el)}
2020-07-15 21:30:37 +00:00
part="base"
2021-02-26 14:09:13 +00:00
class=${classMap({
2020-07-15 21:30:37 +00:00
'menu-item': true,
'menu-item--checked': this.checked,
2020-10-22 17:41:09 +00:00
'menu-item--disabled': this.disabled,
'menu-item--focused': this.hasFocus
2021-02-26 14:09:13 +00:00
})}
2020-07-15 21:30:37 +00:00
role="menuitem"
2021-02-26 14:09:13 +00:00
aria-disabled=${this.disabled ? 'true' : 'false'}
aria-checked=${this.checked ? 'true' : 'false'}
tabindex=${!this.disabled ? '0' : null}
onfocus=${this.handleFocus.bind(this)}
onblur=${this.handleBlur.bind(this)}
onmouseenter=${this.handleMouseEnter.bind(this)}
onmouseleave=${this.handleMouseLeave.bind(this)}
2020-07-15 21:30:37 +00:00
>
<span part="checked-icon" class="menu-item__check">
2020-10-27 12:21:04 +00:00
<sl-icon name="check2" aria-hidden="true" />
2020-07-15 21:30:37 +00:00
</span>
<span part="prefix" class="menu-item__prefix">
<slot name="prefix" />
</span>
<span part="label" class="menu-item__label">
<slot />
</span>
<span part="suffix" class="menu-item__suffix">
<slot name="suffix" />
</span>
</div>
2021-02-26 14:09:13 +00:00
`;
2020-07-15 21:30:37 +00:00
}
}