Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set theme attributes #1264

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 42 additions & 32 deletions packages/ui-next/src/hooks/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,65 @@ export function useThemeEffect({
useEffect(() => {
if (!theme) return;

const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
const appliedColorMode = (
mutation.target as HTMLElement
).classList.contains("dark")
? "dark"
: "light";

document.documentElement.style.setProperty(
"--theme-icon-url",
theme.icon.startsWith("http")
? `url("${theme.icon}")`
: `url("${assetUrl}${theme.icon}")`,
);
const coverValue =
typeof theme.cover === "string"
? theme.cover
: theme.cover[appliedColorMode];
const coverUrl = coverValue.startsWith("http")
? `url("${coverValue}")`
: `url("${assetUrl}${coverValue}")`;
document.documentElement.style.setProperty(
"--theme-cover-url",
coverUrl,
);
// Helper function to update theme properties
const updateThemeProperties = (colorMode: "light" | "dark") => {
// Set icon
document.documentElement.style.setProperty(
"--theme-icon-url",
theme.icon.startsWith("http")
? `url("${theme.icon}")`
: `url("${assetUrl}${theme.icon}")`,
);

if (!theme.colors) return;
// Set cover
const coverValue =
typeof theme.cover === "string" ? theme.cover : theme.cover[colorMode];
const coverUrl = coverValue.startsWith("http")
? `url("${coverValue}")`
: `url("${assetUrl}${coverValue}")`;
document.documentElement.style.setProperty("--theme-cover-url", coverUrl);

if (theme.colors?.primary) {
// Set colors if they exist
if (theme.colors) {
if (theme.colors.primary) {
const val =
typeof theme.colors.primary === "string"
? theme.colors?.primary
: theme.colors?.primary[appliedColorMode];
? theme.colors.primary
: theme.colors.primary[colorMode];
document.documentElement.style.setProperty(
"--primary",
hexToHsl(val),
);
}

if (theme.colors?.primaryForeground) {
if (theme.colors.primaryForeground) {
const val =
typeof theme.colors.primaryForeground === "string"
? theme.colors?.primaryForeground
: theme.colors?.primaryForeground[appliedColorMode];
? theme.colors.primaryForeground
: theme.colors.primaryForeground[colorMode];
document.documentElement.style.setProperty(
"--primary-foreground",
hexToHsl(val),
);
}
}
};

// Set initial values based on current color mode
const initialColorMode = document.documentElement.classList.contains("dark")
? "dark"
: "light";
updateThemeProperties(initialColorMode);

// Watch for changes
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
const colorMode = (mutation.target as HTMLElement).classList.contains(
"dark",
)
? "dark"
: "light";
updateThemeProperties(colorMode);
});
});

Expand Down
Loading