@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
118 lines (117 loc) • 5.68 kB
JavaScript
// TODO - add coverage once finalized
/* istanbul ignore file */
import { h, } from "@stencil/core";
import { themeStore } from "../../providers/theme/theme.store";
import { handleShadowDOMStyles } from "../base-component";
import { inheritAriaAttributes } from "../utils";
/**
* A theme switcher component used to toggle the application theme and/or mode.
*
* Allows consumers to set the initial theme (Modus Classic, Modus Modern, etc.) and end-users to toggle modes (Light, Dark).
*/
export class ModusWcThemeSwitcher {
constructor() {
this.inheritedAttributes = {};
/** Custom CSS class to apply to the theme switcher element. */
this.customClass = '';
this.isDarkMode = themeStore.state.mode === 'dark';
}
componentWillLoad() {
handleShadowDOMStyles(this.el);
if (!this.el.ariaLabel) {
this.el.ariaLabel = 'Switch between light and dark theme';
}
this.inheritedAttributes = inheritAriaAttributes(this.el);
}
connectedCallback() {
this.modeUnsubscribe = themeStore.onChange('mode', (mode) => {
this.isDarkMode = mode === 'dark';
});
}
disconnectedCallback() {
var _a;
(_a = this.modeUnsubscribe) === null || _a === void 0 ? void 0 : _a.call(this);
}
getClasses() {
const classList = [
'modus-wc-inline-grid modus-wc-cursor-pointer modus-wc-justify-center modus-wc-place-items-center',
];
if (this.customClass)
classList.push(this.customClass);
return classList.join(' ');
}
handleModeToggle(event) {
const checkbox = event.target;
const newMode = checkbox.checked ? 'dark' : 'light';
themeStore.setMode(newMode);
this.themeChange.emit(themeStore.state);
}
render() {
return (h("label", Object.assign({ key: '7a314d7f49edd370c77ececc7cedd196e28e1e6f', class: this.getClasses() }, this.inheritedAttributes), h("input", { key: 'c2d2bd964cdfe760da97458a60889a401d931af5', "aria-checked": this.isDarkMode, "aria-label": this.isDarkMode ? 'Dark theme' : 'Light theme', checked: this.isDarkMode, class: "modus-wc-toggle modus-wc-theme-controller modus-wc-bg-base-content modus-wc-col-span-2 modus-wc-col-start-1 modus-wc-row-start-1", onChange: (event) => this.handleModeToggle(event), type: "checkbox", value: "default" }), h("svg", { key: '4f953d356333a7b5826b3769335e13b30aeedbf3', "aria-hidden": "true", class: "modus-wc-stroke-base-100 modus-wc-fill-base-100 modus-wc-col-start-1 modus-wc-row-start-1", fill: "none", height: "14", role: "presentation", stroke: "currentColor", "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", viewBox: "0 0 24 24", width: "14", xmlns: "http://www.w3.org/2000/svg" }, h("circle", { key: 'a0bc1219989132a49515e4ff73b7a8a99b275f3c', cx: "12", cy: "12", r: "5" }), h("path", { key: '9a8d9a888071920c53a0e269dfc0893774d64eb4', d: "M12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4" })), h("svg", { key: 'b1cfd24ffeae078952e9d1a9aaf808ac315ec828', "aria-hidden": "true", class: "modus-wc-stroke-base-100 modus-wc-fill-base-100 modus-wc-col-start-2 modus-wc-row-start-1", fill: "none", height: "14", role: "presentation", stroke: "currentColor", "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", viewBox: "0 0 24 24", width: "14", xmlns: "http://www.w3.org/2000/svg" }, h("path", { key: '51cf8732436e6f450bdabea0de64144d1a784f86', d: "M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" }))));
}
static get is() { return "modus-wc-theme-switcher"; }
static get originalStyleUrls() {
return {
"$": ["modus-wc-theme-switcher.scss"]
};
}
static get styleUrls() {
return {
"$": ["modus-wc-theme-switcher.css"]
};
}
static get properties() {
return {
"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 theme switcher element."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "''"
}
};
}
static get states() {
return {
"isDarkMode": {}
};
}
static get events() {
return [{
"method": "themeChange",
"name": "themeChange",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "An event that fires when the theme is changed."
},
"complexType": {
"original": "IThemeConfig",
"resolved": "IThemeConfig",
"references": {
"IThemeConfig": {
"location": "import",
"path": "../../providers/theme/theme.types",
"id": "src/providers/theme/theme.types.ts::IThemeConfig"
}
}
}
}];
}
static get elementRef() { return "el"; }
}