npm install -D theme-playground
yarn add -D theme-playgroundYou can use the withThemePlayground HOC
import withThemePlayground from 'theme-playground';
import { ThemeProvider } from 'styled-components';
const options = {
theme: {
colors: {
primary: 'red'
},
fontSize: '80px'
},
hide: process.env.NODE_ENV !== 'development'
};
const App = ({ theme }) => (
<ThemeProvider theme={theme}>
<Components />
</ThemeProvider>
);
export default withThemePlayground(App, options);... or the ThemePlayground component with a render function.
import { ThemePlayground } from 'theme-playground';
import { ThemeProvider } from 'styled-components';
const options = {
theme: {
colors: {
primary: 'red'
},
fontSize: '80px'
}
};
const App = () => (
<ThemePlayground {...options}>
{theme => (
<ThemeProvider theme={theme}>
<Components />
</ThemeProvider>
)}
</ThemePlayground>
);import { useThemePlayground } from 'theme-playground';
import { ThemeProvider } from 'styled-components';
const ChildComponent = () => {
const { theme, name } = useThemePlayground();
return (
<div>
<p>Current theme: {name}</p>
<pre>{JSON.stringify(theme, null, 2)}</pre>
</div>
);
};object | Array<{ name: string, theme: object }> | required
The theme object or multiple themes as an array of objects. Look at the Multiple Themes section for an example.
object | optional
Optional override components of default components. Look at the Overrides section for detailed documentation.
object | optional
An additional config object can be added. Look at the Config section for detailed documentation.
"path" || "startCase" || (path: string[]) => string | default: "startCase"
boolean | default: true
Set to false no code component will be rendered.
To add multiple themes, add an Array to the theme key. Each theme must have a name and a theme key.
import withThemePlayground from 'theme-playground';
import { ThemeProvider } from 'styled-components';
import defaultTheme from 'path/to/default/theme';
import anotherTheme from 'path/to/another/theme';
const options = {
theme: [
{ name: 'Theme', theme: defaultTheme },
{ name: 'Another Theme', theme: anotherTheme }
]
};
const App = ({ theme }) => (
<ThemeProvider theme={theme}>
<Components />
</ThemeProvider>
);
export default withThemePlayground(App, options);Example
import withThemePlayground from 'theme-playground';
import { ThemeProvider } from 'styled-components';
import theme from 'path/to/theme';
const options = {
theme,
config: {
// One of "path"
labelFormat: 'path', // "button.color"
// or "startCase"
labelFormat: 'startCase', // "Button Color"
// or a custom function
labelFormat: path => {
// path is equal to ["button", "color"]
return path.join('-'); // "button-color"
},
showCode: true || false
}
};
const App = ({ theme }) => (
<ThemeProvider theme={theme}>
<Components />
</ThemeProvider>
);
export default withThemePlayground(App, options);theme-playground will render a default component based on the theme value. If you want to customize them, you can override the default components by adding an overrides object to the decorator.
As a key use the theme object path, e.g 'button.spacing'
Example
import withThemePlayground from 'theme-playground';
import { ThemeProvider } from 'styled-components';
import theme from 'path/to/theme';
const config = {
theme,
overrides: {
'button.spacing': {
type: 'counter',
label: 'Button Spacing',
description: 'Spacing for all buttons',
min: 1,
max: 20,
steps: 1
},
'button.color.primary': {
type: 'color',
label: 'Button Primary Color'
}
}
};
const App = ({ theme }) => (
<ThemeProvider theme={theme}>
<Components />
</ThemeProvider>
);
export default withThemePlayground(App, options);It is also possible to hide specific theme values or objects, e.g.:
const overrides = {
breakpoints: {
hidden: true
},
'button.spacing': {
hidden: true
}
};'theme.path': {
type: 'color',
hidden: Boolean,
label: String | 'Theme Path',
description: String | null
}'theme.path': {
type: 'counter',
hidden: Boolean,
label: String | 'Theme Path',
description: String | null,
min: Number | 0,
max: Number | 100,
steps: Number | 1
}'theme.path': {
type: 'select',
hidden: Boolean,
label: String | 'Theme Path',
description: String | null
options: [
{
value: String,
label: String
}
]
}'theme.path': {
type: 'shorthand',
hidden: Boolean,
label: String | 'Theme Path',
description: String | null
}'theme.path': {
type: 'switch',
hidden: Boolean,
label: String | 'Theme Path',
description: String | null
}'theme.path': {
type: 'radio',
hidden: Boolean,
label: String | 'Theme Path',
description: String | null
options: [
{
value: String,
label: String
}
]
}'theme.path': {
type: 'range',
hidden: Boolean,
label: String | 'Theme Path',
description: String | null,
min: Number | 0,
max: Number | 100,
steps: Number | 1
}theme-playground will render the following components based on the value.
boolean
number
string
string&&string.length >= 40
string&&string.endsWith("px" || "rem" || "em" || "%")
string&&string.startsWith("#" || "rgba" || "rgba")||label.includes("color")
object&&Object.keys(object).length === 4&&Object.keys(object).includes("top" && "right" && "bottom" && "left")
