Skip to content

Commit

Permalink
fix(theme): set color-scheme css property in theme context (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz authored Jun 6, 2022
1 parent 163c66b commit 35342a3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion sandpack-react/src/contexts/themeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { standardizeTheme } from "../styles";
import { defaultLight } from "../themes";
import type { SandpackTheme, SandpackThemeProp } from "../types";
import { classNames } from "../utils/classNames";
import { isDarkColor } from "../utils/stringUtils";

const wrapperClassName = css({
all: "initial",
Expand All @@ -22,6 +23,13 @@ const wrapperClassName = css({
WebkitTapHighlightColor: "transparent",
WebkitFontSmoothing: "subpixel-antialiased",

variants: {
variant: {
dark: { colorScheme: "dark" },
light: { colorScheme: "light" },
},
},

"@media screen and (min-resolution: 2dppx)": {
WebkitFontSmoothing: "antialiased",
MozOsxFontSmoothing: "grayscale",
Expand Down Expand Up @@ -57,13 +65,15 @@ const SandpackThemeProvider: React.FC<
return createTheme(id, standardizeStitchesTheme(theme));
}, [theme, id]);

const isDarkTheme = isDarkColor(theme.colors.surface1);

return (
<SandpackThemeContext.Provider value={{ theme, id }}>
<div
className={classNames(
c("wrapper"),
themeClassName.toString(),
wrapperClassName,
wrapperClassName({ variant: isDarkTheme ? "dark" : "light" }),
className
)}
{...props}
Expand Down
2 changes: 1 addition & 1 deletion sandpack-react/src/presets/Sandpack.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("getSandpackCssText", () => {
</SandpackProvider>
);

expect(getSandpackCssText().length).toBe(3456);
expect(getSandpackCssText().length).toBe(3564);
expect(getSandpackCssText()).not.toContain(componentClassName);
});

Expand Down

0 comments on commit 35342a3

Please sign in to comment.