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: improve system color scheme support and documentation #1050

Merged
merged 2 commits into from
Jan 3, 2024
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
2 changes: 1 addition & 1 deletion sandpack-react/src/components/ReactDevTools/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const devToolClassName = css({
width: "100%",
});

type DevToolsTheme = "dark" | "light";
type DevToolsTheme = "dark" | "light" | "auto";

export const SandpackReactDevTools = ({
clientId,
Expand Down
2 changes: 1 addition & 1 deletion sandpack-react/src/hooks/useSandpackTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { SandpackTheme } from "../types";
export const useSandpackTheme = (): {
theme: SandpackTheme;
themeId: string;
themeMode: "dark" | "light";
themeMode: "dark" | "light" | "auto";
} => {
const { theme, id, mode } = React.useContext(SandpackThemeContext);
return { theme, themeId: id, themeMode: mode };
Expand Down
25 changes: 23 additions & 2 deletions sandpack-react/src/styles/themeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const wrapperClassName = css({
const SandpackThemeContext = React.createContext<{
theme: SandpackTheme;
id: string;
mode: "dark" | "light";
mode: "dark" | "light" | "auto";
}>({
theme: defaultLight,
id: "light",
Expand All @@ -51,13 +51,34 @@ const SandpackThemeProvider: React.FC<
children?: React.ReactNode;
}
> = ({ theme: themeFromProps, children, className, ...props }) => {
const { theme, id, mode } = standardizeTheme(themeFromProps);
const [prefferedTheme, setPreferredTheme] = React.useState<
SandpackThemeProp | undefined
>(themeFromProps);
const { theme, id, mode } = standardizeTheme(prefferedTheme);
const classNames = useClassNames();

const themeClassName = React.useMemo(() => {
return createTheme(id, standardizeStitchesTheme(theme));
}, [theme, id]);

React.useEffect(() => {
if (themeFromProps !== "auto") return;

const colorSchemeChange = ({ matches }: MediaQueryListEvent) => {
setPreferredTheme(matches ? "dark" : "light");
};

window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", colorSchemeChange);

return () => {
window
.matchMedia("(prefers-color-scheme: dark)")
.removeEventListener("change", colorSchemeChange);
};
}, [themeFromProps]);

return (
<SandpackThemeContext.Provider value={{ theme, id, mode }}>
<div
Expand Down
17 changes: 13 additions & 4 deletions website/docs/src/components/Themes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ export default function Themes() {
};

const predefinedThemes = ["auto", "dark", "light"].includes(current);
const codeBlock = predefinedThemes
? `// Predefined theme
const codeBlock = (() => {
if (current === "auto") {
return `// System color scheme (light or dark)

<Sandpack theme="${current}" />`;
}

if (predefinedThemes)
return `// Predefined theme

<Sandpack theme="${current}" />`
: `import { ${current} } from "@codesandbox/sandpack-themes";
<Sandpack theme="${current}" />`;

return `import { ${current} } from "@codesandbox/sandpack-themes";

<Sandpack theme={${current}} />;`;
})();

return (
<div>
Expand Down
Loading