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

272 lines (271 loc) 10.2 kB
import { h, Host, } from "@stencil/core"; import { convertPropsToClasses } from "./modus-wc-alert.tailwind"; import { inheritAriaAttributes } from "../utils"; /** * A customizable alert component used to inform the user about important events */ export class ModusWcAlert { constructor() { this.inheritedAttributes = {}; /** Custom CSS class to apply to the outer div element. */ this.customClass = ''; /** Whether the alert has a dismiss button */ this.dismissible = false; /** The variant of the alert. */ this.variant = 'info'; } componentWillLoad() { // Set default role if none provided if (!this.el.hasAttribute('role')) { this.el.setAttribute('role', 'status'); } // Then inherit all ARIA attributes normally this.inheritedAttributes = inheritAriaAttributes(this.el); } getClasses() { const classList = ['modus-wc-alert']; const propClasses = convertPropsToClasses({ variant: this.variant, }); // 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(' '); } getLeadingIcon() { if (this.icon) { return (h("modus-wc-icon", { "custom-class": "modus-wc-alert-icon", name: this.icon, variant: "outlined" })); } switch (this.variant) { case 'error': return (h("modus-wc-icon", { "custom-class": "modus-wc-alert-icon", name: "alert", variant: "outlined" })); case 'success': return (h("modus-wc-icon", { "custom-class": "modus-wc-alert-icon", name: "check_circle" })); case 'warning': return (h("modus-wc-icon", { "custom-class": "modus-wc-alert-icon", name: "warning", variant: "outlined" })); case 'info': default: return (h("modus-wc-icon", { "custom-class": "modus-wc-alert-icon", name: "info", variant: "outlined" })); } } delayChanged(newDelay) { clearTimeout(this.timerId); this.timerId = setTimeout(() => { this.dismissElement(); }, newDelay); } dismissElement() { this.dismissClick.emit(); this.el.remove(); } componentDidLoad() { if (this.delay && this.delay > 0) { this.timerId = setTimeout(() => { this.dismissElement(); }, this.delay); } } disconnectedCallback() { clearTimeout(this.timerId); } elementKeyupHandler(event) { switch (event.code) { case 'Escape': if (!this.dismissible) { return; } this.dismissElement(); break; } } render() { return (h(Host, { key: '4fba2bccde6a15c0d6431a1c593ac4b6a3de9a2c' }, h("div", Object.assign({ key: '6ecb7d3d6643b3323da231775595447812faacfa', class: this.getClasses() }, this.inheritedAttributes), this.getLeadingIcon(), h("div", { key: '7ba3c57d00f326da4be078db4920960128c8159d' }, h("div", { key: '69e1c5848fb512f65320abf87818213f5fda0127', class: "title" }, this.alertTitle), this.alertDescription && (h("div", { key: '053789b7eef0bea2e510b5a483ea09a340ce712f', class: "description" }, this.alertDescription)), !this.alertTitle && !this.alertDescription && (h("slot", { key: 'f94f1ded917dcdf0fe0c29f3b4c4e03b8556ed87', name: "content" }))), h("slot", { key: '7007b90ccffc25123265d1b4ad35bca5ff205a5c', name: "button" }), this.dismissible && (h("modus-wc-button", { key: '2466b7e16d87ba8dd6bf2bc5611c546e42790d51', "aria-label": "notification button", color: "tertiary", size: "sm", slot: "button", variant: "borderless", onButtonClick: () => this.dismissElement() }, h("modus-wc-icon", { key: 'de2c7f0ec8db5b689a36536a4fa8b1080f071b21', "custom-class": "modus-wc-alert-close-icon", name: "close", variant: "outlined" })))))); } static get is() { return "modus-wc-alert"; } static get originalStyleUrls() { return { "$": ["modus-wc-alert.scss"] }; } static get styleUrls() { return { "$": ["modus-wc-alert.css"] }; } static get properties() { return { "alertDescription": { "type": "string", "attribute": "alert-description", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The description of the alert." }, "getter": false, "setter": false, "reflect": false }, "alertTitle": { "type": "string", "attribute": "alert-title", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "The title of the alert." }, "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 outer div element." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" }, "delay": { "type": "number", "attribute": "delay", "mutable": false, "complexType": { "original": "number", "resolved": "number | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Time taken to dismiss the alert in milliseconds" }, "getter": false, "setter": false, "reflect": false }, "dismissible": { "type": "boolean", "attribute": "dismissible", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Whether the alert has a dismiss button" }, "getter": false, "setter": false, "reflect": false, "defaultValue": "false" }, "icon": { "type": "string", "attribute": "icon", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The Modus icon to render." }, "getter": false, "setter": false, "reflect": false }, "variant": { "type": "string", "attribute": "variant", "mutable": false, "complexType": { "original": "'error' | 'info' | 'success' | 'warning'", "resolved": "\"error\" | \"info\" | \"success\" | \"warning\" | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The variant of the alert." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'info'" } }; } static get events() { return [{ "method": "dismissClick", "name": "dismissClick", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "An event that fires when the alert is dismissed" }, "complexType": { "original": "any", "resolved": "any", "references": {} } }]; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "delay", "methodName": "delayChanged" }]; } static get listeners() { return [{ "name": "keyup", "method": "elementKeyupHandler", "target": undefined, "capture": false, "passive": false }]; } }