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

92 wiersze
2.5 KiB
TypeScript
Czysty Zwykły widok Historia

import { html, LitElement } from 'lit';
2021-07-08 21:23:47 +00:00
import { customElement, property, query } from 'lit/decorators.js';
2021-09-29 12:40:26 +00:00
import { classMap } from 'lit/directives/class-map.js';
2022-03-24 11:48:03 +00:00
import '~/components/icon/icon';
import { watch } from '~/internal/watch';
2021-07-10 00:45:44 +00:00
import styles from './menu-item.styles';
2021-07-12 14:36:06 +00:00
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
*
2021-06-25 20:25:46 +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.
2020-07-15 21:30:37 +00:00
*
2022-03-09 20:54:18 +00:00
* @csspart base - The component's internal wrapper.
2021-06-25 20:25:46 +00:00
* @csspart prefix - The prefix container.
* @csspart label - The menu item label.
* @csspart suffix - The suffix container.
2020-07-15 21:30:37 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-menu-item')
2021-03-09 00:14:32 +00:00
export default class SlMenuItem extends LitElement {
2021-07-10 00:45:44 +00:00
static styles = styles;
2020-07-15 21:30:37 +00:00
2021-03-06 17:01:39 +00:00
@query('.menu-item') menuItem: HTMLElement;
2021-02-26 14:09:13 +00:00
/** Draws the item in a checked state. */
2021-07-01 00:04:46 +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-07-01 00:04:46 +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-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2021-07-08 21:23:47 +00:00
firstUpdated() {
this.setAttribute('role', 'menuitem');
2020-10-22 17:41:09 +00:00
}
2021-07-08 21:23:47 +00:00
@watch('checked')
handleCheckedChange() {
this.setAttribute('aria-checked', this.checked ? 'true' : 'false');
2020-10-22 17:41:09 +00:00
}
2021-07-08 21:23:47 +00:00
@watch('disabled')
handleDisabledChange() {
this.setAttribute('aria-disabled', this.disabled ? 'true' : 'false');
2020-10-22 17:41:09 +00:00
}
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,
2022-03-03 22:15:58 +00:00
'menu-item--disabled': this.disabled,
2022-03-04 15:08:59 +00:00
'menu-item--has-submenu': false // reserved for future use
2021-02-26 14:09:13 +00:00
})}
2020-07-15 21:30:37 +00:00
>
2022-03-03 22:15:58 +00:00
<span class="menu-item__check">
<sl-icon name="check-lg" library="default" aria-hidden="true"></sl-icon>
</span>
2020-07-15 21:30:37 +00:00
<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>
2022-03-03 22:15:58 +00:00
<span class="menu-item__chevron">
<sl-icon name="chevron-right" library="default" aria-hidden="true"></sl-icon>
</span>
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:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-menu-item': SlMenuItem;
}
}