Skip to content

Commit

Permalink
feat: add theme support to shoelace ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Belar committed Jan 5, 2025
1 parent 1dc7666 commit 35d2a09
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/base/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<html>
<head>
<title>Penpot Desktop</title>
<link rel="stylesheet" href="./styles/index.css" />
<link
rel="stylesheet"
href="../../node_modules/@shoelace-style/shoelace/cdn/themes/light.css"
/>
<link
rel="stylesheet"
href="../../node_modules/@shoelace-style/shoelace/cdn/themes/dark.css"
onload="document.documentElement.classList.add('sl-theme-dark')"
/>
<link rel="stylesheet" href="./styles/index.css" />
<script src="./scripts/main.js" type="module"></script>
</head>
<body>
Expand Down
33 changes: 33 additions & 0 deletions src/base/scripts/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const THEME_TAB_EVENTS = Object.freeze({
REQUEST_UPDATE: "theme-request-update",
UPDATE: "theme-update",
});
const THEME_MEDIA = Object.freeze({
LIGHT: "(prefers-color-scheme: light)",
DARK: "(prefers-color-scheme: dark)",
});

/** @type {ThemeSetting | null} */
let currentThemeSetting = null;
Expand All @@ -27,6 +31,35 @@ export function initTheme() {
}

prepareForm(currentThemeSetting);
syncThemeClass();
}

function syncThemeClass() {
/**
* @function
* @param {MediaQueryListEvent} arg0
*/
const mediaMatchListener = ({ matches, media }) => {
if (!matches) {
return;
}

if (media === THEME_MEDIA.LIGHT) {
document.documentElement.classList.remove("sl-theme-dark");
document.documentElement.classList.add("sl-theme-light");
return;
}

if (media === THEME_MEDIA.DARK) {
document.documentElement.classList.remove("sl-theme-light");
document.documentElement.classList.add("sl-theme-dark");
}
};

Object.values(THEME_MEDIA).forEach((media) => {
const match = matchMedia(media);
match.addEventListener("change", mediaMatchListener);
});
}

/**
Expand Down

0 comments on commit 35d2a09

Please sign in to comment.