UNPKG

@trimble-oss/moduswebcomponents

Version:

Modus Web Components is a modern, accessible UI library built with Stencil JS that provides reusable web components following Trimble's Modus design system. This updated version focuses on improved flexibility, enhanced theming options, comprehensive cust

292 lines (291 loc) 10.9 kB
import { h, Host, } from "@stencil/core"; import { convertPropsToClasses } from "./modus-wc-menu-item.tailwind"; import { inheritAriaAttributes } from "../utils"; /** * A customizable menu item component used to display the item portion of a menu */ export class ModusWcMenuItem { constructor() { this.inheritedAttributes = {}; /** Custom CSS class to apply to the li element. */ this.customClass = ''; /** The text rendered in the menu item. */ this.label = ''; /** The size of the menu item. */ this.size = 'md'; /** The unique identifying value of the menu item. */ this.value = ''; this.handleItemSelect = () => { this.itemSelect.emit({ value: this.value }); }; } componentWillLoad() { this.inheritedAttributes = inheritAriaAttributes(this.el); } getClasses() { const classList = ['modus-wc-menu-item']; const propClasses = convertPropsToClasses({ bordered: this.bordered, disabled: this.disabled, selected: this.selected, focused: this.focused, size: this.size, }); // The order CSS classes are added matters to CSS specificity if (propClasses) classList.push(propClasses); if (this.customClass) classList.push(this.customClass); return classList.join(' '); } getIconSize() { switch (this.size) { case 'sm': return 'xs'; case 'md': return 'sm'; case 'lg': return 'md'; // istanbul ignore next (unreachable code) default: return 'sm'; } } render() { return (h(Host, { key: '57687be825113001277c6262f8e9a68a79f48eb0' }, h("li", Object.assign({ key: 'd283508c2df245b3c5d9c36c38d355c96c41719d', "aria-current": this.selected, "aria-disabled": this.disabled, class: this.getClasses(), role: "menuitem" }, this.inheritedAttributes), h("button", { key: '78f5a9b41da06c955f4c57ab1574057463045846', disabled: this.disabled, onClick: this.handleItemSelect, type: "button" }, h("div", { key: '167e81fa176e156fe6055726454cfb302d27afad', class: "modus-wc-menu-item-content" }, this.startIcon && (h("modus-wc-icon", { key: '8915b9c86bfc59e15520190ec4e712e4e00588ff', customClass: "modus-wc-menu-item-start-icon", decorative: true, name: this.startIcon, size: this.getIconSize() })), h("div", { key: '0a7b9ec715202e093832784951757a7ce5b35fab', class: "modus-wc-menu-item-labels" }, h("div", { key: 'ff731dc9d04f1a984e25e0b4f7a56e22cce8d307' }, this.label), this.subLabel && (h("div", { key: 'bf58571b5dfdfb908dc4402401e2231ff23eadde', class: "modus-wc-menu-item-sublabel" }, this.subLabel))), this.selected && (h("div", { key: 'e84e9dae595a5e2486e44bc34f460fdb534f8b80', class: "modus-wc-menu-item-selected-icon" }, h("modus-wc-icon", { key: 'cdbdd45dd149269f7e34e806eaf6a6ff6e798d79', decorative: true, name: "check", size: this.getIconSize() })))))))); } static get is() { return "modus-wc-menu-item"; } static get originalStyleUrls() { return { "$": ["modus-wc-menu-item.scss"] }; } static get styleUrls() { return { "$": ["modus-wc-menu-item.css"] }; } static get properties() { return { "bordered": { "type": "boolean", "attribute": "bordered", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "" }, "getter": false, "setter": false, "reflect": false }, "customClass": { "type": "string", "attribute": "custom-class", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Custom CSS class to apply to the li element." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" }, "disabled": { "type": "boolean", "attribute": "disabled", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The disabled state of the menu item." }, "getter": false, "setter": false, "reflect": false }, "label": { "type": "string", "attribute": "label", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The text rendered in the menu item." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" }, "startIcon": { "type": "string", "attribute": "start-icon", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The modus icon name to render on the start of the menu item." }, "getter": false, "setter": false, "reflect": false }, "selected": { "type": "boolean", "attribute": "selected", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The selected state of the menu item." }, "getter": false, "setter": false, "reflect": false }, "focused": { "type": "boolean", "attribute": "focused", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The focused state of the menu item." }, "getter": false, "setter": false, "reflect": false }, "size": { "type": "string", "attribute": "size", "mutable": false, "complexType": { "original": "ModusSize", "resolved": "\"lg\" | \"md\" | \"sm\" | undefined", "references": { "ModusSize": { "location": "import", "path": "../types", "id": "src/components/types.ts::ModusSize" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "The size of the menu item." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'md'" }, "subLabel": { "type": "string", "attribute": "sub-label", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The text rendered beneath the label." }, "getter": false, "setter": false, "reflect": false }, "value": { "type": "string", "attribute": "value", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The unique identifying value of the menu item." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" } }; } static get events() { return [{ "method": "itemSelect", "name": "itemSelect", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Event emitted when a menu item is selected." }, "complexType": { "original": "{ value: string }", "resolved": "{ value: string; }", "references": {} } }]; } static get elementRef() { return "el"; } }