Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion apps/ai-dial-admin/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,27 @@ export default async function RootLayout({ children }: { children: ReactNode })
? getIconPath(themesConfig?.images['admin-favicon'] || themesConfig?.images.favicon)
: undefined;

// Runs synchronously in <head> before first paint to apply saved CSS variables,
// preventing a flash of the default dark theme on page refresh.
const themeInitScript = `
(function () {
try {
var colors = localStorage.getItem('theme-colors');
if (colors) {
var parsed = JSON.parse(colors);
var root = document.documentElement;
Object.keys(parsed).forEach(function (key) {
root.style.setProperty('--' + key, parsed[key]);
});
}
} catch (e) {}
})();
`;

return (
<html>
<html suppressHydrationWarning>
<head>
<script dangerouslySetInnerHTML={{ __html: themeInitScript }} />
<link rel="icon" href={faviconUrl || '/'} sizes="any" type="image/png" />
<link rel="apple-touch-icon" href={faviconUrl || '/'} type="image/png" />
</head>
Expand Down
5 changes: 4 additions & 1 deletion apps/ai-dial-admin/src/utils/themes/apply-theme-colors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Theme } from '@/src/models/theme';
import { setToLocalStorage } from '@/src/utils/local-storage';

export const THEME_COLORS_STORAGE_KEY = 'theme-colors';

export const applyThemeColors = (div: HTMLElement, theme?: Theme) => {
if (theme) {
const themeColors = theme.colors;
Expand All @@ -9,6 +11,7 @@ export const applyThemeColors = (div: HTMLElement, theme?: Theme) => {
div.style.setProperty(`--${key}`, value);
});

setToLocalStorage('theme', theme.id); // Persist the theme
setToLocalStorage('theme', theme.id);
setToLocalStorage(THEME_COLORS_STORAGE_KEY, JSON.stringify(themeColors));
}
};
Loading