-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
darkmode toggler
- Loading branch information
Showing
16 changed files
with
154 additions
and
225 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/OrchardCore.Modules/OrchardCore.Setup/wwwroot/Scripts/setup.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
src/OrchardCore.Modules/OrchardCore.Themes/Assets/js/constants.js
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
src/OrchardCore.Modules/OrchardCore.Themes/Assets/js/theme-toggler.js
This file was deleted.
Oops, something went wrong.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
...e.Modules/OrchardCore.Themes/package.json → ...es/OrchardCore.Themes/Assets/package.json
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
59 changes: 59 additions & 0 deletions
59
src/OrchardCore.Modules/OrchardCore.Themes/Assets/ts/constants.ts
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,59 @@ | ||
const darkThemeName = "dark"; | ||
const lightThemeName = "light"; | ||
const themeStoreKeySuffix = "theme"; | ||
|
||
const getTenantName = () => | ||
document.documentElement.getAttribute("data-tenant") || "default"; | ||
|
||
const getStoreKeySuffix = () => themeStoreKeySuffix || "theme"; | ||
|
||
const getStoreKey = () => `${getTenantName()}-${getStoreKeySuffix()}`; | ||
|
||
const getStoredTheme = () => localStorage.getItem(getStoreKey()); | ||
|
||
const setStoredTheme = (theme: string | null) => { | ||
if (theme) { | ||
localStorage.setItem(getStoreKey(), theme); | ||
} | ||
}; | ||
|
||
const isDarkMedia = () => | ||
window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
|
||
const getPreferredTheme = () => { | ||
const storedTheme = getStoredTheme(); | ||
if (storedTheme) { | ||
return storedTheme; | ||
} | ||
|
||
return isDarkMedia() ? darkThemeName : lightThemeName; | ||
}; | ||
|
||
const setTheme = (theme: string | null) => { | ||
if (!theme) { | ||
return; | ||
} | ||
|
||
if (theme === "auto") { | ||
document.documentElement.setAttribute( | ||
"data-bs-theme", | ||
isDarkMedia() ? darkThemeName : lightThemeName | ||
); | ||
} else { | ||
document.documentElement.setAttribute("data-bs-theme", theme ?? ""); | ||
} | ||
}; | ||
|
||
export { | ||
darkThemeName, | ||
lightThemeName, | ||
themeStoreKeySuffix, | ||
getTenantName, | ||
getStoreKeySuffix, | ||
getStoreKey, | ||
getStoredTheme, | ||
setStoredTheme, | ||
isDarkMedia, | ||
getPreferredTheme, | ||
setTheme, | ||
}; |
7 changes: 5 additions & 2 deletions
7
...hardCore.Themes/Assets/js/theme-loader.js → ...rchardCore.Themes/Assets/ts/theme-head.ts
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
Oops, something went wrong.