@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
294 lines (293 loc) • 11 kB
JavaScript
import { h, Host, } from "@stencil/core";
import { convertPropsToClasses } from "./modus-wc-alert.tailwind";
import { AlertSolidIcon } from "../../icons/alert-solid.icon";
import { CheckCircleSolidIcon } from "../../icons/check-circle-solid.icon";
import { CloseSolidIcon } from "../../icons/close-solid.icon";
import { InfoSolidIcon } from "../../icons/info-solid.icon";
import { WarningSolidIcon } from "../../icons/warning-solid.icon";
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';
/** Role taken by the alert. Defaults to 'status' */
this.role = 'status';
}
componentWillLoad() {
this.inheritedAttributes = inheritAriaAttributes(this.el, ['role']);
}
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 }));
}
switch (this.variant) {
case 'error':
return h(AlertSolidIcon, { className: "modus-wc-alert-icon" });
case 'success':
return h(CheckCircleSolidIcon, { className: "modus-wc-alert-icon" });
case 'warning':
return h(WarningSolidIcon, { className: "modus-wc-alert-icon" });
case 'info':
default:
return h(InfoSolidIcon, { className: "modus-wc-alert-icon" });
}
}
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: '53bf0c060956aec86bbb41ff9c5367ae3757070d' }, h("div", Object.assign({ key: '9932f1c3e714eaa85c4cfaea462f22bac6cfe622', class: this.getClasses(), role: this.role }, this.inheritedAttributes), this.getLeadingIcon(), h("div", { key: '6514a604258e9190708d7eabfabe1c6ed2449218' }, h("div", { key: '9535218f2a5252d70f5b6008489c04c75e2982d2', class: "title" }, this.alertTitle), this.alertDescription && (h("div", { key: 'c412b0f7735aac130ad8ca647ab3af07cf536c10', class: "description" }, this.alertDescription)), !this.alertTitle && !this.alertDescription && (h("slot", { key: '7796cb5d4f7e9c6141618db4d8db91053dd1cea1', name: "content" }))), h("slot", { key: '2b2722869563a768fe537f37b077377fb98b45b4', name: "button" }), this.dismissible && (h("modus-wc-button", { key: '5f913216d03ae975c6468f0f5ff5c311c812d92e', "aria-label": "notification button", color: "secondary", size: "sm", slot: "button", variant: "borderless", onButtonClick: () => this.dismissElement() }, h(CloseSolidIcon, { key: '0aa4e8db4057b286dc299d0376b0443486fae761', className: "modus-wc-alert-close-icon" }))))));
}
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'"
},
"role": {
"type": "string",
"attribute": "role",
"mutable": false,
"complexType": {
"original": "'alert' | 'log' | 'marquee' | 'status' | 'timer'",
"resolved": "\"alert\" | \"log\" | \"marquee\" | \"status\" | \"timer\"",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Role taken by the alert. Defaults to 'status'"
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'status'"
}
};
}
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
}];
}
}