-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout-theme.js
More file actions
87 lines (74 loc) · 2.28 KB
/
Copy pathlayout-theme.js
File metadata and controls
87 lines (74 loc) · 2.28 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const appGridLayouts = ['default', 'preview-right', 'preview-left']
export const createLayoutThemeController = ({
appGrid,
appGridLayoutButtons,
appThemeButtons,
syncPreviewBackgroundPickerFromTheme,
appGridLayoutStorageKey = 'knighted-develop:app-grid-layout',
appThemeStorageKey = 'knighted-develop:theme',
}) => {
const applyAppGridLayout = (layout, { persist = true } = {}) => {
if (!appGrid || !appGridLayouts.includes(layout)) {
return
}
appGrid.classList.toggle('app-grid--preview-right', layout === 'preview-right')
appGrid.classList.toggle('app-grid--preview-left', layout === 'preview-left')
for (const button of appGridLayoutButtons) {
const isActive = button.dataset.appGridLayout === layout
button.setAttribute('aria-pressed', isActive ? 'true' : 'false')
}
if (persist) {
try {
localStorage.setItem(appGridLayoutStorageKey, layout)
} catch {
/* Ignore storage write errors in restricted browsing modes. */
}
}
}
const getInitialAppGridLayout = () => {
try {
const value = localStorage.getItem(appGridLayoutStorageKey)
if (appGridLayouts.includes(value)) {
return value
}
} catch {
/* Ignore storage read errors in restricted browsing modes. */
}
return 'default'
}
const applyTheme = (theme, { persist = true } = {}) => {
if (!['dark', 'light'].includes(theme)) {
return
}
document.documentElement.dataset.theme = theme
syncPreviewBackgroundPickerFromTheme()
for (const button of appThemeButtons) {
const isActive = button.dataset.appTheme === theme
button.setAttribute('aria-pressed', isActive ? 'true' : 'false')
}
if (persist) {
try {
localStorage.setItem(appThemeStorageKey, theme)
} catch {
/* Ignore storage write errors in restricted browsing modes. */
}
}
}
const getInitialTheme = () => {
try {
const value = localStorage.getItem(appThemeStorageKey)
if (value === 'dark' || value === 'light') {
return value
}
} catch {
/* Ignore storage read errors in restricted browsing modes. */
}
return 'dark'
}
return {
applyAppGridLayout,
applyTheme,
getInitialAppGridLayout,
getInitialTheme,
}
}