Skip to content

Commit 8cbbe54

Browse files
components: Add theme provider (codesandbox#3294)
* add theme provider * add box sizing for storybook Co-authored-by: Sara Vieira <[email protected]>
1 parent 60684d1 commit 8cbbe54

File tree

4 files changed

+51
-38
lines changed

4 files changed

+51
-38
lines changed

packages/components/.storybook/config.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { themes } from '@storybook/theming';
77
import { ThemeDecorator } from './decorators';
88
import { createGlobalStyle } from 'styled-components';
99
import global from '@codesandbox/common/lib/global.css';
10-
import theme from './theme';
1110

1211
const viewports = {
1312
mobile: {
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import React from 'react';
2-
import theme from '../theme';
3-
import { ThemeProvider } from 'styled-components';
2+
3+
// black is the default, it would be helpful to use storybook-addon-themes
4+
// to test our components across multiple themes
5+
import vsCodeTheme from '@codesandbox/common/lib/themes/codesandbox-black';
6+
7+
import { ThemeProvider } from '../../src/components/ThemeProvider';
48

59
export const ThemeDecorator = (fn: () => JSX.Element) => (
6-
<ThemeProvider theme={theme}>{fn()}</ThemeProvider>
10+
<ThemeProvider vsCodeTheme={vsCodeTheme}>{fn()}</ThemeProvider>
711
);

packages/components/.storybook/theme.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* There are 3 layers to our component styles.
3+
*
4+
* design language - spacing, fontsizes, radii, etc.
5+
* vscode theme - color tokens
6+
* polyfill - color tokens missing from vscode
7+
*/
8+
import React from 'react';
9+
import dot from 'dot-object';
10+
import deepmerge from 'deepmerge';
11+
import css from '@styled-system/css';
12+
import styled, { ThemeProvider as StyledProvider } from 'styled-components';
13+
import designLanguage from '@codesandbox/common/lib/design-language';
14+
import polyfillTheme from '../../utils/polyfill-theme';
15+
16+
const ThemeBoundary = styled.div(
17+
css({
18+
fontFamily: 'Inter, sans-serif',
19+
'input, textarea, button': {
20+
fontFamily: 'Inter, sans-serif',
21+
},
22+
})
23+
);
24+
25+
export const ThemeProvider = ({ vsCodeTheme = { colors: {} }, children }) => {
26+
// black is the default, it would be helpful to use storybook-addon-themes
27+
// to test our components across multiple themes
28+
// convert vscode colors to dot notation so that we can use them in tokens
29+
const vsCodeColors = dot.object({ ...vsCodeTheme.colors });
30+
31+
// Our interface does not map 1-1 with vscode.
32+
// To add styles that remain themeable, we add
33+
// some polyfills to the theme tokens.
34+
const polyfilledVSCodeColors = polyfillTheme(vsCodeColors);
35+
36+
// merge the design language and vscode theme
37+
const theme = deepmerge(designLanguage, { colors: polyfilledVSCodeColors });
38+
39+
return (
40+
<StyledProvider theme={theme}>
41+
<ThemeBoundary>{children}</ThemeBoundary>
42+
</StyledProvider>
43+
);
44+
};

0 commit comments

Comments
 (0)