Skip to content

Commit 7486e93

Browse files
committed
feat(switch): use localstorage to save the switch state at refresh
1 parent 8b377ae commit 7486e93

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"node-sass": "^8.0.0",
3535
"prettier": "^2.8.1",
3636
"riot": "^7.1.0",
37-
"riot-mui": "github:joxit/riot-5-mui#b892806",
37+
"riot-mui": "github:joxit/riot-5-mui#a97f2d3",
3838
"rollup": "^3.9.0",
3939
"rollup-plugin-app-utils": "^1.0.6",
4040
"rollup-plugin-copy": "^3.4.0",

src/scripts/theme.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const DARK_THEME = {
2323
'footer-background': '#555',
2424
};
2525

26+
const LOCAL_STORAGE_THEME = 'registryUiTheme';
27+
2628
let THEME;
2729

2830
const normalizeKey = (k) =>
@@ -33,9 +35,16 @@ const normalizeKey = (k) =>
3335

3436
const preferDarkMode = ({ theme }) => {
3537
if (theme === 'auto') {
36-
if (typeof window.matchMedia === 'function') {
37-
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
38-
return prefersDarkScheme && prefersDarkScheme.matches;
38+
switch (localStorage.getItem(LOCAL_STORAGE_THEME)) {
39+
case 'dark':
40+
return true;
41+
case 'light':
42+
return false;
43+
default:
44+
if (typeof window.matchMedia === 'function') {
45+
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
46+
return prefersDarkScheme && prefersDarkScheme.matches;
47+
}
3948
}
4049
}
4150
return theme === 'dark';
@@ -49,5 +58,7 @@ export const loadTheme = (props, style) => {
4958
.map(([k, v]) => [normalizeKey(k), v])
5059
.forEach(([k, v]) => (THEME[k] = v));
5160
Object.entries(THEME).forEach(([k, v]) => style.setProperty(`--${k}`, v));
52-
return isDarkMode ? 'dark' : 'light';
61+
const theme = isDarkMode ? 'dark' : 'light';
62+
localStorage.setItem(LOCAL_STORAGE_THEME, theme);
63+
return theme;
5364
};

src/style.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ main {
341341

342342
material-footer {
343343
padding: 0.5em 1em;
344+
li {
345+
align-self: center;
346+
}
344347
}
345348

346349
.copy-to-clipboard {

0 commit comments

Comments
 (0)