-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2326 from lindapaiste/feature/storybook-themes
Add theme switching to Storybook
- Loading branch information
Showing
2 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { upperFirst } from 'lodash'; | ||
import React from 'react'; | ||
import styled, { ThemeProvider } from 'styled-components'; | ||
import theme, { prop } from '../client/theme'; | ||
|
||
const PreviewArea = styled.div` | ||
background: ${prop('backgroundColor')}; | ||
flex-grow: 1; | ||
padding: 2rem; | ||
& > h4 { | ||
margin-top: 0; | ||
color: ${prop('primaryTextColor')}; | ||
} | ||
`; | ||
|
||
const themeKeys = Object.keys(theme); | ||
|
||
export const withThemeProvider = (Story, context) => { | ||
const setting = context.globals.theme; | ||
if (setting === 'all') { | ||
return ( | ||
<div style={{ display: 'flex', flexWrap: 'wrap' }}> | ||
{Object.keys(theme).map((themeName) => ( | ||
<ThemeProvider theme={theme[themeName]} key={themeName}> | ||
<PreviewArea className={themeName}> | ||
<h4>{upperFirst(themeName)}</h4> | ||
<Story /> | ||
</PreviewArea> | ||
</ThemeProvider> | ||
))} | ||
</div> | ||
); | ||
} else { | ||
const themeName = setting; | ||
return ( | ||
<ThemeProvider theme={theme[themeName]}> | ||
<PreviewArea className={themeName}> | ||
<Story /> | ||
</PreviewArea> | ||
</ThemeProvider> | ||
); | ||
} | ||
}; | ||
|
||
export const themeToolbarItem = { | ||
description: 'Global theme for components', | ||
defaultValue: 'all', | ||
toolbar: { | ||
title: 'Theme', | ||
icon: 'mirror', | ||
items: [...themeKeys, 'all'] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters