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

feat(embed): override theme variables #152

Merged
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
22 changes: 20 additions & 2 deletions src/app/(theme)/embed/sqlite/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import ThemeLayout from "../../theme_layout";
import EmbedPageClient from "./page-client";

export default async function EmbedPage(props: {
searchParams: { theme?: string; disableThemeToggle?: string };
searchParams: {
theme?: string;
disableThemeToggle?: string;
[key: string]: any;
};
}) {
let overrideTheme: "dark" | "light" | undefined = undefined;
const disableToggle = props.searchParams.disableThemeToggle === "1";
Expand All @@ -11,8 +15,22 @@ export default async function EmbedPage(props: {
overrideTheme = props.searchParams.theme === "dark" ? "dark" : "light";
}

const overrideThemeVariables: Record<string, string> = {};

for (const key in props.searchParams) {
if (!key.startsWith("themeVariables[")) {
continue;
}

overrideThemeVariables[key.slice(15, -1)] = props.searchParams[key];
}

return (
<ThemeLayout overrideTheme={overrideTheme} disableToggle={disableToggle}>
<ThemeLayout
overrideTheme={overrideTheme}
disableToggle={disableToggle}
overrideThemeVariables={overrideThemeVariables}
>
<EmbedPageClient />
</ThemeLayout>
);
Expand Down
9 changes: 8 additions & 1 deletion src/app/(theme)/theme_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@ export default async function ThemeLayout({
children,
overrideTheme,
disableToggle,
overrideThemeVariables,
}: PropsWithChildren<{
overrideTheme?: "dark" | "light";
disableToggle?: boolean;
overrideThemeVariables?: Record<string, string>;
}>) {
const cookieStore = cookies();
const theme =
overrideTheme ??
(cookieStore.get("theme")?.value === "dark" ? "dark" : "light");
const style = overrideThemeVariables ?? {};

return (
<body className={cn(inter.className, theme)} suppressHydrationWarning>
<body
className={cn(inter.className, theme)}
style={style}
suppressHydrationWarning
>
<ThemeProvider defaultTheme={theme} disableToggle={disableToggle}>
<Fragment>{children}</Fragment>
<Toaster />
Expand Down
Loading