Skip to content

Commit

Permalink
Migrate OC.Themes
Browse files Browse the repository at this point in the history
darkmode toggler
  • Loading branch information
Skrypt committed Jan 11, 2025
1 parent 9f5d234 commit 8d56b55
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 225 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 0 additions & 22 deletions src/OrchardCore.Modules/OrchardCore.Themes/Assets.json

This file was deleted.

28 changes: 0 additions & 28 deletions src/OrchardCore.Modules/OrchardCore.Themes/Assets/js/constants.js

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@orchardcore/themes",
"version": "1.0.0",
"type": "module",
"dependencies": {
"@popperjs/core": "2.11.8",
"bootstrap": "5.3.3"
Expand Down
59 changes: 59 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Themes/Assets/ts/constants.ts
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,
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { getPreferredTheme, setTheme } from "./constants";

// We need to apply the classes BEFORE the page is rendered.
// That is why we use a MutationObserver instead of document.Ready().
const themeObserver = new MutationObserver(function (mutations) {

for (let i = 0; i < mutations.length; i++) {
for (let j = 0; j < mutations[i].addedNodes.length; j++) {

if (mutations[i].addedNodes[j].tagName == 'BODY') {
const addedNode = mutations[i].addedNodes[j] as HTMLElement;
if (addedNode.tagName === 'BODY') {
setTheme(getPreferredTheme());

// we're done:
themeObserver.disconnect();
};
}
}
}
});
Expand Down
Loading

0 comments on commit 8d56b55

Please sign in to comment.