-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Stacked layout in Storybook for Chromatic (#2165)
- Loading branch information
Showing
6 changed files
with
101 additions
and
88 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,6 @@ | ||
--- | ||
"@marigold/storybook-config": minor | ||
"@marigold/theme-b2b": minor | ||
--- | ||
|
||
feat: A stacked layout for Chromatic |
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
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
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 |
---|---|---|
@@ -1,49 +1,97 @@ | ||
import React from 'react'; | ||
|
||
import { addDecorator, addParameters } from '@storybook/react'; | ||
// @ts-ignore (no types) | ||
import { withThemes } from 'storybook-addon-themes/react'; | ||
import isChromatic from 'chromatic/isChromatic'; | ||
import type { StoryFn } from '@storybook/react'; | ||
|
||
import { MarigoldProvider } from '@marigold/components'; | ||
import { Box, MarigoldProvider } from '@marigold/components'; | ||
import b2bTheme from '@marigold/theme-b2b'; | ||
import coreTheme from '@marigold/theme-core'; | ||
import unicornTheme from '@marigold/theme-unicorn'; | ||
|
||
// Theme Switch | ||
const themes = { | ||
// Helpers | ||
// --------------- | ||
const THEME = { | ||
b2b: b2bTheme, | ||
core: coreTheme, | ||
unicorn: unicornTheme, | ||
} as const; | ||
}; | ||
|
||
type ThemeNames = keyof typeof themes; | ||
type ThemeNames = keyof typeof THEME; | ||
|
||
addDecorator(withThemes); | ||
addParameters({ | ||
const Frame = ({ children, title }: any) => ( | ||
<Box css={{ p: '16px' }}> | ||
<Box css={{ color: '#495057', fontSize: '0.75rem' }}>{title}</Box> | ||
<Box | ||
css={{ | ||
p: '16px', | ||
border: '1px solid #dee2e6', | ||
borderRadius: 'clamp(0px, calc(100vw - 100%) * 1e5, 5px)', | ||
boxShadow: 'inset 0 1px 4px 0 hsl(220 3% 15% / 10%);', | ||
}} | ||
> | ||
{children} | ||
</Box> | ||
</Box> | ||
); | ||
|
||
// Parameters | ||
// --------------- | ||
export const parameters = { | ||
a11y: { | ||
element: '#root', | ||
config: {}, | ||
options: {}, | ||
}, | ||
controls: { expanded: true }, | ||
themes: { | ||
default: localStorage.getItem('marigold-storybook-current-theme') || 'b2b', | ||
clearable: false, | ||
onChange: ({ name }: { name: ThemeNames }) => { | ||
localStorage.setItem('marigold-storybook-current-theme', name); | ||
}; | ||
|
||
// Decorators | ||
// --------------- | ||
export const decorators = [ | ||
(Story: StoryFn, { globals, parameters }: any) => { | ||
const theme = isChromatic() | ||
? 'stacked' | ||
: globals.theme || parameters.theme || 'b2b'; | ||
|
||
switch (theme) { | ||
case 'stacked': { | ||
return ( | ||
<> | ||
{Object.keys(THEME).map(key => ( | ||
<Frame key={key} title={`Theme "${key}"`}> | ||
<MarigoldProvider theme={THEME[key as ThemeNames]}> | ||
<Story /> | ||
</MarigoldProvider> | ||
</Frame> | ||
))} | ||
</> | ||
); | ||
} | ||
default: { | ||
return ( | ||
<MarigoldProvider theme={THEME[theme as ThemeNames]}> | ||
<Story /> | ||
</MarigoldProvider> | ||
); | ||
} | ||
} | ||
}, | ||
]; | ||
|
||
// Global Types | ||
// --------------- | ||
export const globalTypes = { | ||
// Add theme selector to toolbar | ||
theme: { | ||
name: 'Theme', | ||
description: 'Global theme for components', | ||
toolbar: { | ||
icon: 'paintbrush', | ||
items: [ | ||
...Object.keys(THEME).map(key => ({ | ||
value: key, | ||
title: key, | ||
})), | ||
{ value: 'stacked', title: 'stacked' }, | ||
], | ||
}, | ||
Decorator: ({ | ||
themeName, | ||
children, | ||
}: { | ||
themeName: ThemeNames; | ||
children: React.ReactChild; | ||
}) => ( | ||
<MarigoldProvider theme={themes[themeName]}>{children}</MarigoldProvider> | ||
), | ||
list: (Object.keys(themes) as ThemeNames[]).map(name => ({ | ||
name, | ||
color: themes[name].colors!.primary, | ||
})), | ||
}, | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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