Skip to content

Commit

Permalink
Allow pages to be exempted from Dark Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
davenquinn committed Jan 27, 2024
1 parent 8da271d commit 6cf932b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/pages/sift/+Page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Sift } from "@macrostrat-web/sift";
import h from "@macrostrat/hyper";

export const supportsDarkMode = false;

export function Page() {
return h(Sift);
}
2 changes: 1 addition & 1 deletion src/renderer/+config.h.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Config } from "vike/types";

export default {
passToClient: ["pageProps", "urlPathname", "pageStyle"],
passToClient: ["pageProps", "urlPathname", "pageStyle", "supportsDarkMode"],
clientRouting: true,
hydrationCanBeAborted: true,
} satisfies Config;
1 change: 0 additions & 1 deletion src/renderer/+onRenderClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ async function render(pageContext: PageContextClient) {

const page = h(PageShell, { pageContext }, h(Page, pageProps));

console.log("Rendering on client");
const container = document.getElementById("app-container")!;

// TODO: we might be able to switch to vike-react's internal renderer
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/+onRenderHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import { PageShell } from "./page-shell";
import type { PageContextServer } from "./types";

async function render(pageContext: PageContextServer) {
const { Page, pageProps, exports } = pageContext;
const pageStyle = exports?.pageStyle || "fullscreen";
const { Page, pageProps } = pageContext;
// This render() hook only supports SSR, see https://vike.dev/render-modes for how to modify render() to support SPA
let pageHtml = "";
if (Page != null) {
pageHtml = ReactDOMServer.renderToString(
h(PageShell, { pageContext, pageStyle }, h(Page, pageProps))
h(PageShell, { pageContext }, h(Page, pageProps))
);
}

Expand Down
13 changes: 10 additions & 3 deletions src/renderer/page-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ const h = hyper.styled(styles);
export function PageShell({
children,
pageContext,
pageStyle = "fullscreen",
}: {
children: React.ReactNode;
pageContext: PageContext;
pageStyle?: PageStyle;
supportsDarkMode?: boolean;
}) {
const { exports } = pageContext;
const supportsDarkMode = exports?.supportsDarkMode || true;
const pageStyle = exports?.pageStyle || "fullscreen";

return h(
PageContextProvider,
{ pageContext },
h(
DarkModeProvider,
supportsDarkMode ? DarkModeProvider : NoOpDarkModeProvider,
{ followSystem: true },
h("div.app-shell", { className: pageStyle + "-page" }, children)
)
);
}

function NoOpDarkModeProvider(props) {
return props.children;
}
1 change: 1 addition & 0 deletions src/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type PageContextCustom = {
urlPathname: string;
exports: {
pageStyle?: PageStyle;
supportsDarkMode?: boolean;
documentProps?: {
title?: string;
description?: string;
Expand Down

0 comments on commit 6cf932b

Please sign in to comment.