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

103 wiersze
2.7 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, internalProperty, property, query, unsafeCSS } from 'lit-element';
import { tag } from '../../internal/decorators';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
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.
*/
@tag('sl-menu-item')
2021-03-06 17:01:39 +00:00
export class SlMenuItem extends LitElement {
static styles = unsafeCSS(styles);
2020-07-15 21:30:37 +00:00
2021-03-06 17:01:39 +00:00
@query('.menu-item') menuItem: HTMLElement;
@internalProperty() private hasFocus = false;
2020-10-22 17:41:09 +00:00
2021-02-26 14:09:13 +00:00
/** Draws the item in a checked state. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) 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-03-06 17:01:39 +00:00
@property() 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. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) 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
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}
2021-03-06 17:01:39 +00:00
@focus=${this.handleFocus}
@blur=${this.handleBlur}
@mouseenter=${this.handleMouseEnter}
@mouseleave=${this.handleMouseLeave}
2020-07-15 21:30:37 +00:00
>
<span part="checked-icon" class="menu-item__check">
2021-03-06 17:01:39 +00:00
<sl-icon name="check2" aria-hidden="true"></sl-icon>
2020-07-15 21:30:37 +00:00
</span>
<span part="prefix" class="menu-item__prefix">
2021-03-06 17:01:39 +00:00
<slot name="prefix"></slot>
2020-07-15 21:30:37 +00:00
</span>
<span part="label" class="menu-item__label">
2021-03-06 17:01:39 +00:00
<slot></slot>
2020-07-15 21:30:37 +00:00
</span>
<span part="suffix" class="menu-item__suffix">
2021-03-06 17:01:39 +00:00
<slot name="suffix"></slot>
2020-07-15 21:30:37 +00:00
</span>
</div>
2021-02-26 14:09:13 +00:00
`;
2020-07-15 21:30:37 +00:00
}
}