@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
242 lines (241 loc) • 9.3 kB
JavaScript
import { h, Host, } from "@stencil/core";
import { convertPropsToClasses } from "./modus-wc-side-navigation.tailwind";
import { inheritAriaAttributes } from "../utils";
/**
* A customizable side navigation component for organizing primary navigation and content areas in an application.
*/
export class ModusWcSideNavigation {
constructor() {
this.inheritedAttributes = {};
this.minWidth = '4rem';
/** Whether the side navigation should collapse when clicking outside of it. */
this.collapseOnClickOutside = true;
/** Custom CSS class to apply to the inner div. */
this.customClass = '';
/** Whether the side navigation is expanded. */
this.expanded = false;
/** Maximum width of the side navigation panel in an expanded state. */
this.maxWidth = '256px';
/** Mode to make side navigation either overlay or push the content for the selector specified in targetContent */
this.mode = 'overlay';
/** (optional) Specify the selector for the page's content for which paddings and margins will be set by side navigation based on the mode. */
this.targetContent = '';
this.handleClickOutside = (event) => {
if (!this.expanded || !this.collapseOnClickOutside || !this.navRef)
return;
const path = event.composedPath ? event.composedPath() : [event.target];
if (!path.includes(this.navRef)) {
this.expanded = false;
}
};
}
handleExpandedChange() {
this.setTargetContentMargin(this.expanded, this.mode, this.targetContent);
this.expandedChange.emit(this.expanded);
}
handleModeChange(mode) {
this.setTargetContentMargin(this.expanded, mode, this.targetContent);
}
handleTargetContentChange(target) {
this.setTargetContentMargin(this.expanded, this.mode, target);
}
componentWillLoad() {
this.inheritedAttributes = inheritAriaAttributes(this.el);
}
connectedCallback() {
if (this.collapseOnClickOutside) {
document.addEventListener('click', this.handleClickOutside, true);
}
}
disconnectedCallback() {
document.removeEventListener('click', this.handleClickOutside, true);
}
getClasses() {
const classList = ['modus-wc-side-navigation'];
const propClasses = convertPropsToClasses({
expanded: this.expanded,
});
// 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(' ');
}
setTargetContentMargin(isExpanded, mode, target) {
const content = document.querySelector(target);
if (content && 'style' in content) {
content.style.marginLeft =
isExpanded && mode === 'push' ? this.maxWidth : this.minWidth;
}
}
render() {
return (h(Host, { key: '6bce6f85c55103144d1afeec06f484cc9babb6e6' }, h("nav", Object.assign({ key: 'cb621050963ab5b06053ca2a37cf0af495b75571' }, this.inheritedAttributes, { class: this.getClasses(), ref: (el) => (this.navRef = el), style: { width: this.expanded ? this.maxWidth : this.minWidth } }), h("div", { key: '49a2fead0e7231c45928e8d1b995bd8c47851955', class: "side-navigation-content" }, h("slot", { key: 'b53246d439a92e9e8354ef2ca5619dc3bd002b06' })))));
}
static get is() { return "modus-wc-side-navigation"; }
static get originalStyleUrls() {
return {
"$": ["modus-wc-side-navigation.scss"]
};
}
static get styleUrls() {
return {
"$": ["modus-wc-side-navigation.css"]
};
}
static get properties() {
return {
"collapseOnClickOutside": {
"type": "boolean",
"attribute": "collapse-on-click-outside",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Whether the side navigation should collapse when clicking outside of it."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "true"
},
"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 inner div."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "''"
},
"expanded": {
"type": "boolean",
"attribute": "expanded",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Whether the side navigation is expanded."
},
"getter": false,
"setter": false,
"reflect": true,
"defaultValue": "false"
},
"maxWidth": {
"type": "string",
"attribute": "max-width",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Maximum width of the side navigation panel in an expanded state."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'256px'"
},
"mode": {
"type": "string",
"attribute": "mode",
"mutable": false,
"complexType": {
"original": "'overlay' | 'push'",
"resolved": "\"overlay\" | \"push\"",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Mode to make side navigation either overlay or push the content for the selector specified in targetContent"
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'overlay'"
},
"targetContent": {
"type": "string",
"attribute": "target-content",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "(optional) Specify the selector for the page's content for which paddings and margins will be set by side navigation based on the mode."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "''"
}
};
}
static get events() {
return [{
"method": "expandedChange",
"name": "expandedChange",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Event emitted when the expanded state changes (expanded/collapsed)."
},
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
}
}];
}
static get elementRef() { return "el"; }
static get watchers() {
return [{
"propName": "expanded",
"methodName": "handleExpandedChange"
}, {
"propName": "mode",
"methodName": "handleModeChange"
}, {
"propName": "targetContent",
"methodName": "handleTargetContentChange"
}];
}
}