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

fix: Theme state logic to account for non-prefers-color-scheme usage #1082

Merged
merged 1 commit into from
Feb 12, 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
5 changes: 4 additions & 1 deletion sandpack-react/src/styles/themeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ const SandpackThemeProvider: React.FC<
}, [theme, id]);

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

const colorSchemeChange = ({ matches }: MediaQueryListEvent) => {
setPreferredTheme(matches ? "dark" : "light");
Expand Down
28 changes: 27 additions & 1 deletion sandpack-react/src/themes/Theme.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { storiesOf } from "@storybook/react";
import React from "react";
import { useState } from "react";

import { Sandpack } from "../";

import { SANDPACK_THEMES } from ".";
import { SANDPACK_THEMES, defaultLight, defaultDark } from ".";

const stories = storiesOf("presets/Themes", module);

Expand All @@ -21,3 +22,28 @@ Object.keys(SANDPACK_THEMES).forEach((themeName) =>
/>
))
);

export const ThemeSwitcher = () => {
const [theme, setTheme] = useState("light");

return (
<div>
<select onChange={(e) => setTheme(e.target.value)} value={theme}>
<option value="light">light</option>
<option value="dark">dark</option>
</select>
<Sandpack
options={{
showLineNumbers: true,
showInlineErrors: true,
showNavigator: true,
showTabs: true,
}}
template="react"
theme={theme === "light" ? defaultLight : defaultDark}
/>
</div>
);
};

stories.add("theme-switcher", () => <ThemeSwitcher />);
Loading