Skip to content

Commit f778ba0

Browse files
SaraVieirasiddharthkp
authored andcommitted
add ability to change themes (codesandbox#3308)
* add ability to change themes * remove theme decorator * move themesd
1 parent 955e30f commit f778ba0

24 files changed

+17543
-37
lines changed

packages/components/.storybook/addons.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ import '@storybook/addon-knobs/register';
33
import '@storybook/addon-storysource/register';
44
import '@storybook/addon-viewport/register';
55
import '@storybook/addon-a11y/register';
6+
import 'storybook-addon-styled-component-theme/dist/register';

packages/components/.storybook/config.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { withKnobs } from '@storybook/addon-knobs';
44
import { withA11y } from '@storybook/addon-a11y';
55
import { addDecorator, addParameters, configure } from '@storybook/react';
66
import { themes } from '@storybook/theming';
7-
import { ThemeDecorator } from './decorators';
87
import { createGlobalStyle } from 'styled-components';
8+
// @ts-ignore
99
import global from '@codesandbox/common/lib/global.css';
10+
import { makeTheme, getThemes } from '../src/components/ThemeProvider';
11+
import { withThemesProvider } from 'storybook-addon-styled-component-theme';
1012

1113
const viewports = {
1214
mobile: {
@@ -33,31 +35,38 @@ const GlobalStyle = createGlobalStyle`
3335
${global};
3436
html body {
3537
font-family: 'Inter', sans-serif;
38+
margin: 0;
3639
background-color: ${props =>
3740
// @ts-ignore
3841
props.theme.colors.sideBar.background} !important;
3942
color: ${props =>
4043
// @ts-ignore
4144
props.theme.colors.sideBar.foreground} !important;
42-
margin: 0;
43-
4445
* {
4546
box-sizing: border-box;
4647
}
48+
4749
}
4850
`;
4951

50-
export const withGlobal = cb => (
52+
export const withGlobal = (cb: any) => (
5153
<>
5254
<GlobalStyle />
5355
{cb()}
5456
</>
5557
);
5658

59+
const allThemes = getThemes();
60+
const vsCodeThemes = allThemes.map(b => makeTheme(b, b.name));
61+
const blackCodesandbox = vsCodeThemes.find(
62+
theme => theme.name === 'CodeSandbox Black'
63+
);
64+
65+
const rest = vsCodeThemes.filter(theme => theme.name !== 'CodeSandbox Black');
66+
addDecorator(withGlobal);
67+
addDecorator(withThemesProvider([blackCodesandbox, ...rest]));
5768
addDecorator(withA11y);
5869
addDecorator(withKnobs);
59-
addDecorator(withGlobal);
60-
addDecorator(ThemeDecorator);
6170
addParameters({ viewport: { viewports } });
6271

6372
// Option defaults.

packages/components/.storybook/decorators/ThemeDecorator.tsx

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export * from './ThemeDecorator';
21
export * from './LayoutDecorator';

packages/components/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"if-env": "^1.0.4",
5858
"np": "^5.2.1",
5959
"rimraf": "^3.0.0",
60+
"storybook-addon-styled-component-theme": "^1.3.0",
6061
"storybook-chromatic": "^3.4.1",
6162
"typescript": "3.7.4",
6263
"yarn": "^1.21.1"

packages/components/src/components/ThemeProvider/index.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,21 @@
55
* vscode theme - color tokens
66
* polyfill - color tokens missing from vscode
77
*/
8-
import React from 'react';
98
import dot from 'dot-object';
109
import deepmerge from 'deepmerge';
11-
import css from '@styled-system/css';
12-
import styled, { ThemeProvider as StyledProvider } from 'styled-components';
1310
import designLanguage from '@codesandbox/common/lib/design-language';
11+
import VSCodeThemes from '../../themes';
1412
import polyfillTheme from '../../utils/polyfill-theme';
1513

16-
const ThemeBoundary = styled.div(
17-
css({
18-
fontFamily: 'Inter, sans-serif',
19-
'input, textarea, button': {
20-
fontFamily: 'Inter, sans-serif',
21-
},
22-
})
23-
);
14+
export const getThemes = () => {
15+
const results = VSCodeThemes.map(theme => ({
16+
name: theme.name,
17+
...theme.content,
18+
}));
2419

25-
export const ThemeProvider = ({ vsCodeTheme = { colors: {} }, children }) => {
20+
return results.filter(a => a);
21+
};
22+
export const makeTheme = (vsCodeTheme = { colors: {} }, name: string) => {
2623
// black is the default, it would be helpful to use storybook-addon-themes
2724
// to test our components across multiple themes
2825
// convert vscode colors to dot notation so that we can use them in tokens
@@ -36,9 +33,8 @@ export const ThemeProvider = ({ vsCodeTheme = { colors: {} }, children }) => {
3633
// merge the design language and vscode theme
3734
const theme = deepmerge(designLanguage, { colors: polyfilledVSCodeColors });
3835

39-
return (
40-
<StyledProvider theme={theme}>
41-
<ThemeBoundary>{children}</ThemeBoundary>
42-
</StyledProvider>
43-
);
36+
return {
37+
name,
38+
...theme,
39+
};
4440
};

0 commit comments

Comments
 (0)