Skip to content
Closed
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
10 changes: 6 additions & 4 deletions .storybook/ThemeSwapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

const ThemeSwapper = ({ context, children }: ThemeSwapperProps) => {
const { mode, setMode } = useColorScheme();
//if( !mode ) return

useEffect(() => {
const selectedThemeMode = context.globals.themeMode || TextLight;
setMode(selectedThemeMode == TextLight ? "light" : "dark");
}, [context.globals.themeMode]);
const nextMode = selectedThemeMode === TextLight ? "light" : "dark";

setMode(nextMode);
document.documentElement.setAttribute("data-mode", nextMode);
}, [context.globals.themeMode, setMode]);

return (
<div style={{ backgroundColor: mode === "light" ? "#fafafaaa" : "#000a" }}>
Expand All @@ -36,4 +38,4 @@
};

export { ThemeSwapper };
export type { Context };
export type { Context };

Check failure on line 41 in .storybook/ThemeSwapper.tsx

View workflow job for this annotation

GitHub Actions / build (22.20.0)

Insert `⏎`
72 changes: 51 additions & 21 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
import React from "react";
import React, { useLayoutEffect } from "react";
import { CssBaseline } from "@mui/material";
import type { Preview } from "@storybook/react";

import { ThemeProvider } from "../src";
import { GenericTheme, DiamondTheme } from "../src";

import {
GenericTheme,
DiamondTheme,
DiamondDSTheme,
DiamondDSThemeDark,
} from "../src";
import { Context, ThemeSwapper, TextLight, TextDark } from "./ThemeSwapper";

const TextThemeBase = "Theme: Generic";
const TextThemeDiamond = "Theme: Diamond";

const TextThemeDiamondDS = "Theme: DiamondDS";

function resolveTheme(selectedTheme: string, mode: "light" | "dark") {
switch (selectedTheme) {
case TextThemeBase:
return GenericTheme;
case TextThemeDiamondDS:
return mode === "dark" ? DiamondDSThemeDark : DiamondDSTheme;
case TextThemeDiamond:
default:
return DiamondTheme;
}
}

function ApplyModeToPreviewDoc({
mode,
doc,
}: {
mode: "light" | "dark";
doc: Document;
}) {
useLayoutEffect(() => {
const root = doc.documentElement; // <html>
root.setAttribute("data-mode", mode);

// Optional: keep class too if your CSS supports it
root.classList.toggle("dark", mode === "dark");
root.classList.toggle("light", mode === "light");

root.style.colorScheme = mode;
}, [mode, doc]);

return null;
}

export const decorators = [
(StoriesWithPadding: React.FC) => {
return (
Expand All @@ -28,12 +66,16 @@ export const decorators = [
(StoriesWithThemeProvider: React.FC, context: Context) => {
const selectedTheme = context.globals.theme || TextThemeBase;
const selectedThemeMode = context.globals.themeMode || TextLight;
const mode = selectedThemeMode === TextLight ? "light" : "dark";

// ensure we target the preview iframe document
const doc: Document = context?.canvasElement?.ownerDocument ?? document;
return (
<ThemeProvider
theme={selectedTheme == TextThemeBase ? GenericTheme : DiamondTheme}
defaultMode={selectedThemeMode == TextLight ? "light" : "dark"}
theme={resolveTheme(selectedTheme, mode)}
defaultMode={mode}
>
<ApplyModeToPreviewDoc mode={mode} doc={doc} />
<CssBaseline />
<StoriesWithThemeProvider />
</ThemeProvider>
Expand All @@ -48,7 +90,7 @@ const preview: Preview = {
toolbar: {
title: "Theme",
icon: "cog",
items: [TextThemeBase, TextThemeDiamond],
items: [TextThemeBase, TextThemeDiamond, TextThemeDiamondDS],
dynamicTitle: true,
},
},
Expand All @@ -63,8 +105,8 @@ const preview: Preview = {
},
},
initialGlobals: {
theme: "Theme: Diamond",
themeMode: "Mode: Light",
theme: TextThemeDiamondDS,
themeMode: TextLight,
},
parameters: {
controls: {
Expand All @@ -75,18 +117,6 @@ const preview: Preview = {
},
backgrounds: { disable: true },
layout: "fullscreen",
options: {
storySort: {
order: [
"Introduction",
"Components",
"Theme",
"Theme/Logos",
"Theme/Colours",
"Helpers",
],
},
},
},
argTypes: {
linkComponent: {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export * from "./components/helpers/jsonforms";
// themes
export * from "./themes/BaseTheme";
export * from "./themes/DiamondTheme";
export * from "./themes/DiamondOldTheme";
export * from "./themes/DiamondDSTheme";
export * from "./themes/GenericTheme";
export * from "./themes/ThemeProvider";
export * from "./themes/ThemeManager";
Expand Down
Loading
Loading