@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
472 lines (471 loc) • 18.8 kB
JavaScript
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 position of the tooltip relative to the menu item. */
this.tooltipPosition = 'auto';
/** The unique identifying value of the menu item. */
this.value = '';
/** Internal state to track if submenu is expanded */
this.isExpanded = false;
this.handleKeyDown = (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
this.handleItemSelect();
}
};
this.handleItemSelect = () => {
// For submenu items, handle the toggle
if (this.hasSubmenu) {
// Check if side nav is expanded (if this menu is inside a side nav)
const sideNav = this.el.closest('modus-wc-side-navigation');
if (sideNav &&
!sideNav.expanded) {
// Don't allow submenu expansion when side nav is collapsed
// Still emit the event for consistency
this.itemSelect.emit({ value: this.value });
return;
}
// The submenu should be inside this menu-item element (slotted content)
const submenu = this.el.querySelector('.modus-wc-menu-dropdown');
const liElement = this.el.querySelector('li');
if (submenu && liElement) {
submenu.classList.toggle('modus-wc-menu-dropdown-show');
const buttonElement = liElement.querySelector('button');
// Update internal expanded state and add/remove class
this.isExpanded = submenu.classList.contains('modus-wc-menu-dropdown-show');
if (this.isExpanded) {
liElement.classList.add('modus-wc-menu-item-expanded');
if (buttonElement) {
buttonElement.classList.add('modus-wc-menu-dropdown-show');
}
}
else {
liElement.classList.remove('modus-wc-menu-item-expanded');
if (buttonElement) {
buttonElement.classList.remove('modus-wc-menu-dropdown-show');
}
}
}
}
// For checkbox items, provide immediate visual feedback
else if (this.checkbox) {
const liElement = this.el.querySelector('li');
const checkboxElement = this.el.querySelector('modus-wc-checkbox');
if (liElement) {
// Toggle based on current state
const isSelected = liElement.classList.contains('modus-wc-menu-item-selected');
if (isSelected) {
liElement.classList.remove('modus-wc-menu-item-selected');
}
else {
liElement.classList.add('modus-wc-menu-item-selected');
}
// Update checkbox visual state
if (checkboxElement) {
checkboxElement.setAttribute('value', (!isSelected).toString());
}
this.selected = true;
}
}
// For regular menu items, set selected to true
else {
this.selected = true;
}
// Always emit the event with current selection state
this.itemSelect.emit({ value: this.value, selected: this.selected });
};
}
componentWillLoad() {
this.inheritedAttributes = inheritAriaAttributes(this.el);
}
/**
* Public method to collapse the submenu if it's expanded
*/
async collapseSubmenu() {
if (this.hasSubmenu && this.isExpanded) {
const submenu = this.el.querySelector('.modus-wc-menu-dropdown');
const liElement = this.el.querySelector('li');
if (submenu && liElement) {
submenu.classList.remove('modus-wc-menu-dropdown-show');
liElement.classList.remove('modus-wc-menu-item-expanded');
this.isExpanded = false;
}
}
return Promise.resolve();
}
getClasses() {
const classList = ['modus-wc-menu-item'];
const propClasses = convertPropsToClasses({
bordered: this.bordered,
disabled: this.disabled,
selected: this.hasSubmenu ? false : 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(' ');
}
getButtonClasses() {
return this.hasSubmenu ? 'modus-wc-menu-dropdown-toggle' : '';
}
render() {
return (h(Host, { key: '7d8f11c2661bb48fea599296919753a9afcc6b8b' }, h("li", Object.assign({ key: 'ff0f32cce009f017d91450639ad518bbbd75eec2', "aria-current": this.selected, "aria-disabled": this.disabled, class: this.getClasses(), onKeyDown: this.handleKeyDown, role: "menuitem", tabIndex: this.disabled ? -1 : 0 }, this.inheritedAttributes), h("button", { key: '549d1b8758345179772632fa671dafe0208e8f20', class: this.getButtonClasses(), disabled: this.disabled, onClick: this.handleItemSelect, tabIndex: -1, type: "button" }, h("div", { key: '7aa8b7bd9677b5061e3c5b5878a4dc78579dc131', class: "modus-wc-menu-item-content" }, this.checkbox && (h("modus-wc-checkbox", { key: 'ef1f3f365b85665839fd2a7e46b09457a6ae077c', "aria-label": "Checkbox", disabled: this.disabled, size: this.size, value: !!this.selected })), h("slot", { key: '077efb3c770fc31ab16e29bdcbeac5c15db86b3b', name: "start-icon" }), h("div", { key: '22ae0acc390dddcfa9927a69ee695aadce4ffde9', class: "modus-wc-menu-item-labels" }, this.tooltipContent ? (h("modus-wc-tooltip", { content: this.tooltipContent, position: this.tooltipPosition, customClass: "modus-wc-menu-item-tooltip" }, h("div", null, this.label))) : (h("div", null, this.label)), this.subLabel && (h("div", { key: '683e67e731296b116d92944ef0d109748bf3fcf5', class: "modus-wc-menu-item-sublabel" }, this.subLabel))))), h("slot", { key: '10dfea85bd31f8da1f43ad8e72bf83b4f6293514' }))));
}
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
},
"checkbox": {
"type": "boolean",
"attribute": "checkbox",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "If true, renders a checkbox at the start of the menu item."
},
"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
},
"tooltipContent": {
"type": "string",
"attribute": "tooltip-content",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The tooltip text to display when hovering over the menu item."
},
"getter": false,
"setter": false,
"reflect": false
},
"tooltipPosition": {
"type": "string",
"attribute": "tooltip-position",
"mutable": false,
"complexType": {
"original": "'auto' | 'top' | 'right' | 'bottom' | 'left'",
"resolved": "\"auto\" | \"bottom\" | \"left\" | \"right\" | \"top\" | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The position of the tooltip relative to the menu item."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'auto'"
},
"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": "''"
},
"hasSubmenu": {
"type": "boolean",
"attribute": "has-submenu",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Whether this menu item has a collapsible submenu. When true, the item will show a caret and handle toggle behavior."
},
"getter": false,
"setter": false,
"reflect": false
}
};
}
static get states() {
return {
"isExpanded": {}
};
}
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": "{\n value: string;\n selected?: boolean;\n }",
"resolved": "{ value: string; selected?: boolean | undefined; }",
"references": {}
}
}];
}
static get methods() {
return {
"collapseSubmenu": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global",
"id": "global::Promise"
},
"HTMLElement": {
"location": "global",
"id": "global::HTMLElement"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Public method to collapse the submenu if it's expanded",
"tags": []
}
}
};
}
static get elementRef() { return "el"; }
}