@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
298 lines (297 loc) • 11.2 kB
JavaScript
import { h, Host, } from "@stencil/core";
import { convertPropsToClasses } from "./modus-wc-alert.tailwind";
import { handleShadowDOMStyles } from "../base-component";
import { inheritAriaAttributes } from "../utils";
/**
* A customizable alert component used to inform the user about important events.
*
* The component supports `<slot>` elements for injecting custom content and buttons.
*/
export class ModusWcAlert {
constructor() {
this.inheritedAttributes = {};
/** Custom CSS class to apply to the outer div element. */
this.customClass = '';
/** Whether to disable the icon */
this.disableIcon = false;
/** Whether the alert has a dismiss button */
this.dismissible = false;
/** The variant of the alert. */
this.variant = 'info';
}
componentWillLoad() {
handleShadowDOMStyles(this.el);
// 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: '8865feb566e188d4b579f6f28978ab3d9d7050f7' }, h("div", Object.assign({ key: 'e8bf6fa2ef7ee6f69b0af258a26445ead4d075b4', class: this.getClasses() }, this.inheritedAttributes), !this.disableIcon && this.getLeadingIcon(), h("div", { key: 'f49b8f05cce29150caf16e12f35adce2adb16de0', class: "modus-wc-alert-content" }, h("div", { key: '468197f276bc484235068f3c855a2ddf0739d1af', class: "title" }, this.alertTitle), this.alertDescription && (h("div", { key: 'e81f9b673681173fcfeeb29cf001596fdc73bf1e', class: "description" }, this.alertDescription)), !this.alertTitle && !this.alertDescription && (h("slot", { key: '8ae938db71cebcc0e42b2e1ded1d163ad31317cd', name: "content" }))), h("slot", { key: '19a5cd5aba9d4d8eb9c64e76a8b094e03c424079', name: "button" }), this.dismissible && (h("modus-wc-button", { key: 'f77d131e6b138c357c39784541df83ba22d81ef5', "aria-label": "Dismiss alert", color: "tertiary", size: "sm", slot: "button", variant: "borderless", onButtonClick: () => this.dismissElement() }, h("modus-wc-icon", { key: 'ee93c957b78a0eca8a7ffd4d4037df7678051a46', "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
},
"disableIcon": {
"type": "boolean",
"attribute": "disable-icon",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Whether to disable the icon"
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "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' | 'neutral' | 'success' | 'warning'",
"resolved": "\"error\" | \"info\" | \"neutral\" | \"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
}];
}
}