dees-domtools/ts/domtools.classes.thememanager.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-03-16 12:39:50 +00:00
import { DomTools } from './domtools.classes.domtools.js';
import * as plugins from './domtools.plugins.js';
2020-11-23 20:41:26 +00:00
export class ThemeManager {
public domtoolsRef: DomTools;
2020-12-07 03:47:39 +00:00
2020-11-24 19:48:14 +00:00
public goBrightBoolean = false;
2020-11-25 15:42:55 +00:00
public preferredColorSchemeMediaMatch = window.matchMedia('(prefers-color-scheme: light)');
2020-11-23 20:41:26 +00:00
2020-12-07 03:13:34 +00:00
public themeObservable = new plugins.smartrx.rxjs.ReplaySubject<boolean>(1);
2020-11-23 20:41:26 +00:00
constructor(domtoolsRefArg: DomTools) {
this.domtoolsRef = domtoolsRefArg;
2020-11-25 15:42:55 +00:00
// lets care
this.goBrightBoolean = this.preferredColorSchemeMediaMatch.matches;
2020-12-07 03:47:39 +00:00
this.preferredColorSchemeMediaMatch.addEventListener('change', (eventArg) => {
2020-11-25 15:42:55 +00:00
this.goBrightBoolean = eventArg.matches;
this.updateAllConnectedElements();
});
2020-12-07 03:13:34 +00:00
this.updateAllConnectedElements();
2020-11-23 20:41:26 +00:00
}
2020-11-24 19:23:06 +00:00
private async updateAllConnectedElements() {
2020-12-07 03:47:39 +00:00
if (document.body && document.body.style) {
document.body.style.background = this.goBrightBoolean ? '#fff' : '#000';
}
2020-12-07 03:13:34 +00:00
this.themeObservable.next(this.goBrightBoolean);
2020-11-23 20:41:26 +00:00
}
2021-11-26 15:21:11 +00:00
/**
* set the theme of the website to bright
*/
2020-11-24 19:23:06 +00:00
public goBright() {
this.goBrightBoolean = true;
this.updateAllConnectedElements();
}
2021-11-26 15:21:11 +00:00
/**
* set the theme of the website to dark
*/
2020-11-23 20:41:26 +00:00
public goDark() {
2020-11-24 19:23:06 +00:00
this.goBrightBoolean = false;
this.updateAllConnectedElements();
}
2021-11-26 15:21:11 +00:00
/**
* simply toggle between bright and dark
*/
2020-11-24 19:23:06 +00:00
public toggleDarkBright() {
this.goBrightBoolean = !this.goBrightBoolean;
this.updateAllConnectedElements();
2020-11-23 20:41:26 +00:00
}
2020-12-07 03:47:39 +00:00
}