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

165 lines (164 loc) 6.54 kB
import { h, Host, } from "@stencil/core"; import { convertPropsToClasses } from "./modus-wc-breadcrumbs.tailwind"; import { handleShadowDOMStyles } from "../base-component"; import { inheritAriaAttributes, sanitizeUrl } from "../utils"; /** * A customizable breadcrumbs component used to help users navigate through a website. */ export class ModusWcBreadcrumbs { constructor() { this.inheritedAttributes = {}; /** The breadcrumbs to render. */ this.items = []; /** Custom CSS class to apply to the inner div. */ this.customClass = ''; /** The size of the breadcrumbs. */ this.size = 'md'; } componentWillLoad() { handleShadowDOMStyles(this.el); if (!this.el.ariaLabel) { this.el.ariaLabel = 'Breadcrumbs'; } if (!this.items || this.items.length === 0) { console.error('ModusWcBreadcrumbs: breadcrumb items data is required.'); } this.inheritedAttributes = inheritAriaAttributes(this.el); } getClasses() { const classList = ['modus-wc-breadcrumbs']; const propClasses = convertPropsToClasses({ 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(' '); } handleClick(event, crumb) { if (!sanitizeUrl(crumb.url)) { event.preventDefault(); } this.breadcrumbClick.emit(crumb); } render() { return (h(Host, { key: '7ec02ea2af19e523552cbf582e0b0a78055972e7' }, h("nav", Object.assign({ key: '70df2be81ea919967706d0e414b56a73eca8c2f1', class: this.getClasses() }, this.inheritedAttributes), h("ol", { key: 'a770f39cc3ed10579bf72da16cb3e77d88ea8c45' }, this.items.map((item, index) => { const isCurrentPage = index === this.items.length - 1; const sanitizedUrl = sanitizeUrl(item.url); return (h("li", Object.assign({ key: item.label }, (isCurrentPage ? { 'aria-current': 'page' } : {})), isCurrentPage ? (h("span", null, item.label)) : sanitizedUrl ? (h("a", { href: sanitizedUrl, onClick: (event) => this.handleClick(event, item) }, item.label)) : (h("button", { class: "modus-wc-breadcrumb-button", type: "button", onClick: (event) => this.handleClick(event, item) }, item.label)))); }))))); } static get is() { return "modus-wc-breadcrumbs"; } static get originalStyleUrls() { return { "$": ["modus-wc-breadcrumbs.scss"] }; } static get styleUrls() { return { "$": ["modus-wc-breadcrumbs.css"] }; } static get properties() { return { "items": { "type": "unknown", "attribute": "items", "mutable": false, "complexType": { "original": "IBreadcrumb[]", "resolved": "IBreadcrumb[]", "references": { "IBreadcrumb": { "location": "local", "path": "/home/runner/work/modus-wc-2.0/modus-wc-2.0/src/components/modus-wc-breadcrumbs/modus-wc-breadcrumbs.tsx", "id": "src/components/modus-wc-breadcrumbs/modus-wc-breadcrumbs.tsx::IBreadcrumb" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "The breadcrumbs to render." }, "getter": false, "setter": false, "defaultValue": "[]" }, "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": "''" }, "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 breadcrumbs." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'md'" } }; } static get events() { return [{ "method": "breadcrumbClick", "name": "breadcrumbClick", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Event emitted when a breadcrumb is clicked." }, "complexType": { "original": "IBreadcrumb", "resolved": "IBreadcrumb", "references": { "IBreadcrumb": { "location": "local", "path": "/home/runner/work/modus-wc-2.0/modus-wc-2.0/src/components/modus-wc-breadcrumbs/modus-wc-breadcrumbs.tsx", "id": "src/components/modus-wc-breadcrumbs/modus-wc-breadcrumbs.tsx::IBreadcrumb" } } } }]; } static get elementRef() { return "el"; } }