-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from jsdelivr/map-theme
feat: persist dark theme in localstorage
- Loading branch information
Showing
8 changed files
with
151 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
import { useAppearance } from '~/store/appearance'; | ||
import { useAuth } from '~/store/auth'; | ||
|
||
export default defineNuxtPlugin(() => { | ||
const auth = useAuth(); | ||
const appearance = useAppearance(); | ||
const userTheme = computed(() => auth.user.appearance); | ||
|
||
const userAppearance = computed(() => auth.user.appearance); | ||
const updateTheme = () => { | ||
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | ||
const prevUserTheme = localStorage.getItem('theme:prevUserTheme') as 'dark' | 'light' | null; | ||
|
||
const updateAppearance = () => { | ||
const systemAppearance = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | ||
const appearance = userAppearance.value ?? systemAppearance; | ||
document.documentElement.className = appearance; | ||
}; | ||
let theme: 'light' | 'dark' = systemTheme; | ||
|
||
if (auth.isLoggedIn && userTheme.value) { | ||
theme = userTheme.value; | ||
} else if (!auth.isLoggedIn && prevUserTheme) { | ||
theme = prevUserTheme; | ||
} | ||
|
||
updateAppearance(); | ||
document.documentElement.className = theme; | ||
appearance.setTheme(theme); | ||
|
||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateAppearance); | ||
if (auth.isLoggedIn && userTheme.value) { | ||
localStorage.setItem('theme:prevUserTheme', userTheme.value); | ||
} else if (auth.isLoggedIn && userTheme.value === null) { | ||
localStorage.removeItem('theme:prevUserTheme'); | ||
} | ||
}; | ||
|
||
watch(userAppearance, updateAppearance); | ||
updateTheme(); | ||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme); | ||
watch(userTheme, updateTheme); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { defineStore } from 'pinia'; | ||
|
||
interface AppearanceState { | ||
theme: 'light' | 'dark', | ||
} | ||
|
||
export const useAppearance = defineStore('appearance', { | ||
state: (): AppearanceState => ({ | ||
theme: 'light', | ||
}), | ||
actions: { | ||
setTheme (theme: AppearanceState['theme']) { | ||
this.theme = theme; | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
[ | ||
{ | ||
"featureType": "water", | ||
"elementType": "geometry", | ||
"stylers": [ | ||
{ | ||
"color": "#131728" | ||
}, | ||
{ | ||
"lightness": 0 | ||
} | ||
] | ||
}, | ||
{ | ||
"featureType": "landscape", | ||
"elementType": "geometry", | ||
"stylers": [ | ||
{ | ||
"color": "#2d3c59" | ||
}, | ||
{ | ||
"lightness": 0 | ||
} | ||
] | ||
}, | ||
{ | ||
"featureType": "administrative", | ||
"elementType": "geometry.stroke", | ||
"stylers": [ | ||
{ | ||
"color": "#17233a" | ||
}, | ||
{ | ||
"lightness": 0 | ||
}, | ||
{ | ||
"weight": 1.2 | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters