-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDarkModeModule.ts
49 lines (41 loc) · 1.56 KB
/
DarkModeModule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import {DarkModeModuleState} from '@klipper/bow/stores/darkMode/DarkModeModuleState';
import {DarkModeState} from '@klipper/bow/stores/darkMode/DarkModeState';
import {Module, MutationTree} from 'vuex';
/**
* @author François Pluchino <[email protected]>
*/
export class DarkModeModule<R extends DarkModeModuleState = DarkModeModuleState> implements Module<DarkModeState, R> {
private readonly storage: Storage;
public constructor(storage?: Storage) {
this.storage = storage ? storage : localStorage;
}
public get namespaced(): boolean {
return true;
}
public get state(): DarkModeState {
const darkMode: string | null = this.storage.getItem('darkMode:enabled');
return {
enabled: null === darkMode ? false : 'true' === darkMode,
};
}
public get mutations(): MutationTree<DarkModeState> {
const self = this;
return {
toggle(state: DarkModeState, enabled?: boolean): void {
state.enabled = undefined === enabled ? !state.enabled : enabled;
self.storage.setItem('darkMode:enabled', state.enabled ? 'true' : 'false');
},
syncState(state: DarkModeState, newState: DarkModeState): void {
state.enabled = newState.enabled;
},
};
}
}