Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(storybook): support app themes and product accents (#8227)

Andrew Huangccd5690e2cb5
diff --git a/frontend/.storybook/ThemeDocsContainer.tsx b/frontend/.storybook/ThemeDocsContainer.tsx
new file mode 100644
index 0000000000..a939bac42f
--- /dev/null
+++ b/frontend/.storybook/ThemeDocsContainer.tsx
@@ -0,0 +1,39 @@
+import { PropsWithChildren, useEffect, useState } from "react";
+import { DocsContainer, DocsContainerProps } from "@storybook/addon-docs/blocks";
+import { GLOBALS_UPDATED } from "storybook/internal/core-events";
+import { GlobalsUpdatedPayload } from "storybook/internal/types";
+import { themes } from "storybook/theming";
+
+import { useTheme } from "../src/components/v3/platform/ThemeProvider";
+import { ThemePreview } from "./decorators/DocumentDecorator";
+
+const ThemedDocs = ({ children, context }: PropsWithChildren<DocsContainerProps>) => {
+ const { resolvedTheme } = useTheme();
+
+ return (
+ <DocsContainer context={context} theme={themes[resolvedTheme]}>
+ {children}
+ </DocsContainer>
+ );
+};
+
+export const ThemeDocsContainer = ({
+ children,
+ context
+}: PropsWithChildren<DocsContainerProps>) => {
+ const [globals, setGlobals] = useState(
+ () => context.getStoryContext(context.storyById()).globals
+ );
+
+ useEffect(() => {
+ const onGlobalsUpdated = (event: GlobalsUpdatedPayload) => setGlobals(event.globals);
+ context.channel.on(GLOBALS_UPDATED, onGlobalsUpdated);
+ return () => context.channel.off(GLOBALS_UPDATED, onGlobalsUpdated);
+ }, [context.channel]);
+
+ return (
+ <ThemePreview theme={globals.theme} productAccent={globals.productAccent}>
+ <ThemedDocs context={context}>{children}</ThemedDocs>
+ </ThemePreview>
+ );
+};
diff --git a/frontend/.storybook/decorators/DocumentDecorator.tsx b/frontend/.storybook/decorators/DocumentDecorator.tsx
index b0d817c6e1..f305ed44df 100644
--- a/frontend/.storybook/decorators/DocumentDecorator.tsx
+++ b/frontend/.storybook/decorators/DocumentDecorator.tsx
@@ -1,12 +1,44 @@
-import { useEffect } from "react";
+import { PropsWithChildren, useLayoutEffect } from "react";
import type { Decorator } from "@storybook/react-vite";
-export const DocumentDecorator: Decorator = (Story) => {
- useEffect(() => {
- const root = document.documentElement;
+import { ThemeProvider } from "../../src/components/v3/platform/ThemeProvider";
+
+export const productAccents = [
+ { value: "sm", title: "Secrets Management" },
+ { value: "pki", title: "Certificates" },
+ { value: "kms", title: "KMS" },
+ { value: "ss", title: "Secret Scanning" },
+ { value: "pam", title: "Privileged Access" },
+ { value: "av", title: "Agent Vault" }
+];
- root.setAttribute("class", "overflow-visible");
- }, []);
+export const ThemePreview = ({
+ children,
+ theme,
+ productAccent
+}: PropsWithChildren<{ theme: string; productAccent: string }>) => {
+ const resolvedAccent = productAccents.some(({ value }) => value === productAccent)
+ ? productAccent
+ : "sm";
- return <Story />;
+ useLayoutEffect(() => {
+ const root = document.documentElement;
+ root.classList.add("overflow-visible");
+ root.style.setProperty("--color-project", `var(--color-product-${resolvedAccent})`);
+ }, [resolvedAccent]);
+
+ return (
+ <ThemeProvider
+ pathname="/"
+ forcedTheme={theme === "light" || theme === "system" ? theme : "dark"}
+ >
+ {children}
+ </ThemeProvider>
+ );
};
+
+export const DocumentDecorator: Decorator = (Story, { globals }) => (
+ <ThemePreview theme={globals.theme} productAccent={globals.productAccent}>
+ <Story />
+ </ThemePreview>
+);
diff --git a/frontend/.storybook/preview.tsx b/frontend/.storybook/preview.tsx
index e1f3470217..b221603256 100644
--- a/frontend/.storybook/preview.tsx
+++ b/frontend/.storybook/preview.tsx
@@ -3,7 +3,9 @@ import lottieWasmUrl from "@lottiefiles/dotlottie-web/dist/dotlottie-player.wasm
import type { Preview } from "@storybook/react-vite";
import { initializePlatform } from "../src/lib/fn/platform";
+import { productAccents } from "./decorators/DocumentDecorator";
import { DocumentDecorator, RouterDecorator } from "./decorators";
+import { ThemeDocsContainer } from "./ThemeDocsContainer";
import "@fontsource/inter/400.css";
import "@fontsource/inter/500.css";
@@ -18,6 +20,28 @@ setWasmUrl(lottieWasmUrl);
const preview: Preview = {
decorators: [DocumentDecorator, RouterDecorator],
+ globalTypes: {
+ theme: {
+ description: "Application theme",
+ toolbar: {
+ icon: "circlehollow",
+ dynamicTitle: true,
+ items: [
+ { value: "dark", title: "Dark" },
+ { value: "light", title: "Light" },
+ { value: "system", title: "System" }
+ ]
+ }
+ },
+ productAccent: {
+ description: "Project accent color",
+ toolbar: {
+ icon: "paintbrush",
+ dynamicTitle: true,
+ items: productAccents
+ }
+ }
+ },
parameters: {
options: {
storySort: {
@@ -31,22 +55,24 @@ const preview: Preview = {
}
},
docs: {
- backgroundColor: "var(--background)"
+ container: ThemeDocsContainer
},
a11y: {
test: "todo"
},
backgrounds: {
- default: "dark",
options: {
- dark: { name: "Dark", value: "var(--background)" },
+ page: { name: "Page", value: "var(--color-page)" },
+ background: { name: "Background", value: "var(--color-background)" },
card: { name: "Card", value: "var(--color-card)" }
}
}
},
initialGlobals: {
+ theme: "dark",
+ productAccent: "sm",
backgrounds: {
- value: "dark"
+ value: "page"
}
}
};
diff --git a/frontend/src/components/v3/generic/Toast/Toast.stories.tsx b/frontend/src/components/v3/generic/Toast/Toast.stories.tsx
index 3d8fd37be7..9f957d6319 100644
--- a/frontend/src/components/v3/generic/Toast/Toast.stories.tsx
+++ b/frontend/src/components/v3/generic/Toast/Toast.stories.tsx
@@ -2,6 +2,7 @@ import { createPortal } from "react-dom";
import type { Meta, StoryObj } from "@storybook/react-vite";
import { toast } from "sonner";
+import { useTheme } from "../../platform/ThemeProvider";
import { Button, type ButtonProps } from "../Button";
import { Toaster } from "./Toast";
@@ -30,14 +31,18 @@ const ToastExample = ({
variant: ButtonProps["variant"];
label: string;
onShow: (toasterId: string) => void;
-}) => (
- <>
- <Button variant={variant} onClick={() => onShow(toasterId)}>
- {label}
- </Button>
- {createPortal(<Toaster id={toasterId} />, document.body)}
- </>
-);
+}) => {
+ const { resolvedTheme } = useTheme();
+
+ return (
+ <>
+ <Button variant={variant} onClick={() => onShow(toasterId)}>
+ {label}
+ </Button>
+ {createPortal(<Toaster id={toasterId} theme={resolvedTheme} />, document.body)}
+ </>
+ );
+};
const meta = {
title: "Generic/Toast",
diff --git a/frontend/src/components/v3/platform/ThemeProvider/ThemeProvider.tsx b/frontend/src/components/v3/platform/ThemeProvider/ThemeProvider.tsx
index 9c7446316a..74a5ad5da3 100644
--- a/frontend/src/components/v3/platform/ThemeProvider/ThemeProvider.tsx
+++ b/frontend/src/components/v3/platform/ThemeProvider/ThemeProvider.tsx
@@ -64,9 +64,11 @@ export const initializeTheme = () => {
export const ThemeProvider = ({
children,
- pathname
-}: React.PropsWithChildren<{ pathname: string }>) => {
- const [theme, setThemeState] = React.useState<Theme>(readStoredTheme);
+ pathname,
+ forcedTheme
+}: React.PropsWithChildren<{ pathname: string; forcedTheme?: Theme }>) => {
+ const [storedTheme, setThemeState] = React.useState<Theme>(readStoredTheme);
+ const theme = forcedTheme ?? storedTheme;
const [systemTheme, setSystemTheme] = React.useState<ResolvedTheme>(getSystemTheme);
const resolvedTheme = resolveTheme(theme, pathname, systemTheme);