@nent/core
Version:
80 lines (75 loc) • 3 kB
JavaScript
/*!
* NENT 2022
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-1829aebc.js');
const state = require('./state-d62be6f1.js');
require('./index-96f3ab3f.js');
const AppTheme = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
/**
* Change the element that is decorated with
* the dark-mode class
*/
this.targetElement = 'body';
/**
* Change the class name that is added to the
* target element when the theme is determined to
* be dark.
*/
this.darkClass = 'dark';
/**
* Display the user's system preference.
*/
this.display = false;
/**
* This element displays the current theme,
* unless in switch-mode, it will show the opposite.
*/
this.switch = false;
}
componentWillLoad() {
this.subscribeToSystem();
}
subscribeToSystem() {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
if ((prefersDark === null || prefersDark === void 0 ? void 0 : prefersDark.addEventListener) && state.state.darkMode == null) {
prefersDark.addEventListener('change', ev => {
state.state.darkMode = ev.matches;
});
state.state.darkMode = prefersDark.matches;
}
this.stateSubscription = state.onChange('darkMode', () => {
this.toggleDarkTheme();
});
}
componentWillRender() {
this.toggleDarkTheme();
}
toggleDarkTheme() {
const element = this.targetElement == 'body'
? this.el.ownerDocument.body
: this.el.ownerDocument.querySelector(this.targetElement);
if (state.state.darkMode)
element === null || element === void 0 ? void 0 : element.classList.toggle(this.darkClass, true);
else
element === null || element === void 0 ? void 0 : element.classList.remove(this.darkClass);
}
render() {
if (this.display) {
const swap = this.switch
? !state.state.darkMode
: state.state.darkMode;
return (index.h(index.Host, null, index.h("svg", { class: "themeIcon", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", width: "24", height: "24" }, index.h("path", { fill: "none", d: "M0 0h24v24H0z" }), swap ? (index.h("path", { d: "M11.38 2.019a7.5 7.5 0 1 0 10.6 10.6C21.662 17.854 17.316 22 12.001 22 6.477 22 2 17.523 2 12c0-5.315 4.146-9.661 9.38-9.981z" })) : (index.h("path", { d: "M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z" })))));
}
return null;
}
disconnectedCallback() {
var _a;
(_a = this.stateSubscription) === null || _a === void 0 ? void 0 : _a.call(this);
}
get el() { return index.getElement(this); }
};
exports.n_app_theme = AppTheme;