tethys.backend/resources/js/Stores/style.service.ts

77 lines
2.8 KiB
TypeScript
Raw Permalink Normal View History

2023-03-03 15:54:28 +00:00
import { defineStore } from 'pinia';
import styles from '@/styles';
2023-03-03 15:54:28 +00:00
import { darkModeKey, styleKey } from '@/config';
interface StyleState {
[key: string]: string | boolean;
asideStyle: string;
asideScrollbarsStyle: string;
asideBrandStyle: string;
asideMenuItemStyle: string;
asideMenuItemActiveStyle: string;
asideMenuDropdownStyle: string;
navBarItemLabelStyle: string;
navBarItemLabelHoverStyle: string;
navBarItemLabelActiveColorStyle: string;
overlayStyle: string;
darkMode: boolean;
}
// Define StyleService store
2023-03-03 15:54:28 +00:00
export const StyleService = defineStore('style', {
state: (): StyleState => ({
// Styles
asideStyle: '',
asideScrollbarsStyle: '',
asideBrandStyle: '',
asideMenuItemStyle: '',
asideMenuItemActiveStyle: '',
asideMenuDropdownStyle: '',
navBarItemLabelStyle: '',
navBarItemLabelHoverStyle: '',
navBarItemLabelActiveColorStyle: '',
overlayStyle: '',
// Dark mode default false
darkMode: false,
}),
actions: {
// Set style based on payload value ('basic' or 'white')
setStyle(payload: 'basic' | 'white') {
if (!styles[payload]) {
return;
}
if (typeof localStorage !== 'undefined') {
localStorage.setItem(styleKey, payload);
}
const style = styles[payload] as Record<string, string>;
for (const key in style) {
// let keyStyle: string = `${key}Style`;//key as keyof typeof style;
this[`${key}Style` as keyof StyleState] = style[key];
}
},
// Toggle dark mode
setDarkMode(payload?: boolean) {
this.darkMode = payload !== undefined ? payload : !this.darkMode;
if (typeof localStorage !== 'undefined') {
localStorage.setItem(darkModeKey, this.darkMode ? '1' : '0');
}
if (typeof document !== 'undefined') {
document.body.classList[this.darkMode ? 'add' : 'remove']('dark-scrollbars');
document.documentElement.classList[this.darkMode ? 'add' : 'remove']('dark-scrollbars-compat');
}
},
// Toggle dark mode
setLocale(payload?: boolean) {
this.darkMode = payload !== undefined ? payload : !this.darkMode;
if (typeof localStorage !== 'undefined') {
localStorage.setItem(darkModeKey, this.darkMode ? '1' : '0');
}
if (typeof document !== 'undefined') {
document.body.classList[this.darkMode ? 'add' : 'remove']('dark-scrollbars');
document.documentElement.classList[this.darkMode ? 'add' : 'remove']('dark-scrollbars-compat');
}
},
},
});