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

181 lines (180 loc) 6.55 kB
import { h, Host, } from "@stencil/core"; import { convertPropsToClasses } from "./modus-wc-menu.tailwind"; import { inheritAriaAttributes } from "../utils"; /** * A customizable menu component used to display a list of li elements vertically or horizontally. * * The component supports a `<slot>` for injecting custom li elements inside the ul */ export class ModusWcMenu { constructor() { this.inheritedAttributes = {}; /** Custom CSS class to apply to the ul element. */ this.customClass = ''; /** The orientation of the menu. */ this.orientation = 'vertical'; /** The size of the menu. */ this.size = 'md'; this.handleFocusout = (e) => { // Check if the new focus target is still within this menu if (!this.el.contains(e.relatedTarget)) { // Focus has left the menu entirely this.menuFocusout.emit(e); } }; this.getMenuRole = () => this.orientation === 'horizontal' ? 'menubar' : 'menu'; } componentWillLoad() { if (!this.el.ariaLabel) { this.el.ariaLabel = 'Menu'; } this.inheritedAttributes = inheritAriaAttributes(this.el); } getClasses() { const classList = ['modus-wc-menu modus-wc-w-full']; const propClasses = convertPropsToClasses({ bordered: this.bordered, orientation: this.orientation, 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(' '); } render() { return (h(Host, { key: 'a966dcdb4ee793f76ed1b4d768a10feac67b4348' }, h("ul", Object.assign({ key: '10ea9a00e23bf2e1123ba2ceef274f4bf5ad0bee', "aria-orientation": this.orientation, class: this.getClasses(), onFocusout: this.handleFocusout, role: this.getMenuRole() }, this.inheritedAttributes), h("slot", { key: 'cae15f842402b499d55d4ba4933ac242a35369a4' })))); } static get is() { return "modus-wc-menu"; } static get originalStyleUrls() { return { "$": ["modus-wc-menu.scss"] }; } static get styleUrls() { return { "$": ["modus-wc-menu.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": "Indicates that the menu should have a border." }, "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 ul element." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" }, "orientation": { "type": "string", "attribute": "orientation", "mutable": false, "complexType": { "original": "Orientation", "resolved": "\"horizontal\" | \"vertical\" | undefined", "references": { "Orientation": { "location": "import", "path": "../types", "id": "src/components/types.ts::Orientation" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "The orientation of the menu." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'vertical'" }, "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." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'md'" } }; } static get events() { return [{ "method": "menuFocusout", "name": "menuFocusout", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Event emitted when the menu loses focus." }, "complexType": { "original": "FocusEvent", "resolved": "FocusEvent", "references": { "FocusEvent": { "location": "global", "id": "global::FocusEvent" } } } }]; } static get elementRef() { return "el"; } }