Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSF: Add addon-themes for testing to sandboxes & main storybook #28765

Merged
merged 13 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion code/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const config: StorybookConfig = {
directory: '../addons/toolbars/template/stories',
titlePrefix: 'addons/toolbars',
},
{
directory: '../addons/themes/template/stories',
titlePrefix: 'addons/themes',
},
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
{
directory: '../addons/onboarding/src',
titlePrefix: 'addons/onboarding',
Expand All @@ -83,6 +87,7 @@ const config: StorybookConfig = {
],
addons: [
'@storybook/addon-links',
'@storybook/addon-themes',
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/addon-storysource',
Expand Down Expand Up @@ -120,7 +125,6 @@ const config: StorybookConfig = {
features: {
viewportStoryGlobals: true,
backgroundsStoryGlobals: true,
// themesStoryGlobals: true,
},
viteFinal: (viteConfig, { configType }) =>
mergeConfig(viteConfig, {
Expand Down
54 changes: 39 additions & 15 deletions code/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ThemeBlock = styled.div<{ side: 'left' | 'right'; layout: string }>(
overflow: 'auto',
},
({ layout }) => ({
padding: layout === 'fullscreen' ? 0 : 10,
padding: layout === 'fullscreen' ? 0 : '1rem',
}),
({ theme }) => ({
background: theme.background.content,
Expand All @@ -55,14 +55,14 @@ const ThemeBlock = styled.div<{ side: 'left' | 'right'; layout: string }>(
const ThemeStack = styled.div<{ layout: string }>(
{
position: 'relative',
minHeight: 'calc(50vh - 15px)',
flex: 1,
},
({ theme }) => ({
background: theme.background.content,
color: theme.color.defaultText,
}),
({ layout }) => ({
padding: layout === 'fullscreen' ? 0 : 10,
padding: layout === 'fullscreen' ? 0 : '1rem',
})
);

Expand All @@ -86,6 +86,25 @@ const PlayFnNotice = styled.div(
})
);

const StackContainer = ({ children, layout }) => (
<div
style={{
height: '100%',
display: 'flex',
flexDirection: 'column',
// margin: layout === 'fullscreen' ? 0 : '-1rem',
}}
>
<style dangerouslySetInnerHTML={{ __html: 'html, body, #storybook-root { height: 100%; }' }} />
{layout === 'fullscreen' ? null : (
<style
dangerouslySetInnerHTML={{ __html: 'html, body { padding: 0!important; margin: 0; }' }}
/>
)}
{children}
</div>
);

const ThemedSetRoot = () => {
const theme = useTheme();

Expand Down Expand Up @@ -166,7 +185,7 @@ export const decorators = [
* This decorator renders the stories side-by-side, stacked or default based on the theme switcher in the toolbar
*/
(StoryFn, { globals, playFunction, args, storyGlobals, parameters }) => {
let theme = globals.theme;
let theme = globals.sb_theme;
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
let showPlayFnNotice = false;

// this makes the decorator be out of 'phase' with the actually selected theme in the toolbar
Expand All @@ -176,7 +195,7 @@ export const decorators = [
if (playFunction && args.autoplay !== false && !(theme === 'light' || theme === 'dark')) {
theme = 'light';
showPlayFnNotice = true;
} else if (isChromatic() && !storyGlobals.theme && !playFunction) {
} else if (isChromatic() && !storyGlobals.sb_theme && !playFunction) {
theme = 'stacked';
}

Expand Down Expand Up @@ -206,16 +225,18 @@ export const decorators = [
<ThemeProvider theme={convert(themes.light)}>
<Global styles={createReset} />
</ThemeProvider>
<ThemeProvider theme={convert(themes.light)}>
<ThemeStack data-side="left" layout={parameters.layout}>
<StoryFn />
</ThemeStack>
</ThemeProvider>
<ThemeProvider theme={convert(themes.dark)}>
<ThemeStack data-side="right" layout={parameters.layout}>
<StoryFn />
</ThemeStack>
</ThemeProvider>
<StackContainer layout={parameters.layout}>
<ThemeProvider theme={convert(themes.light)}>
<ThemeStack data-side="left" layout={parameters.layout}>
<StoryFn />
</ThemeStack>
</ThemeProvider>
<ThemeProvider theme={convert(themes.dark)}>
<ThemeStack data-side="right" layout={parameters.layout}>
<StoryFn />
</ThemeStack>
</ThemeProvider>
</StackContainer>
</Fragment>
);
}
Expand Down Expand Up @@ -315,6 +336,9 @@ export const parameters = {
viewport: {
options: MINIMAL_VIEWPORTS,
},
themes: {
disable: true,
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
},
backgrounds: {
options: {
light: { name: 'light', value: '#edecec' },
Expand Down
8 changes: 4 additions & 4 deletions code/addons/controls/template/stories/conditional.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export const ToggleExpandCollapse = {

export const GlobalBased = {
argTypes: {
ifThemeExists: { control: 'text', if: { global: 'theme' } },
ifThemeNotExists: { control: 'text', if: { global: 'theme', exists: false } },
ifLightTheme: { control: 'text', if: { global: 'theme', eq: 'light' } },
ifNotLightTheme: { control: 'text', if: { global: 'theme', neq: 'light' } },
ifThemeExists: { control: 'text', if: { global: 'sb_theme' } },
ifThemeNotExists: { control: 'text', if: { global: 'sb_theme', exists: false } },
ifLightTheme: { control: 'text', if: { global: 'sb_theme', eq: 'light' } },
ifNotLightTheme: { control: 'text', if: { global: 'sb_theme', neq: 'light' } },
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const Disabled: Story = {

export const Hovered: Story = {
...Done,
globals: { theme: 'light' },
globals: { sb_theme: 'light' },
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.hover(canvas.getByRole('button'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const meta = {
),
],
parameters: { layout: 'fullscreen' },
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
globals: { theme: 'light' },
args: {
calls: new Map(getCalls(CallStates.DONE).map((call) => [call.id, call])),
controls: SubnavStories.args.controls,
Expand All @@ -58,35 +57,35 @@ export const Passing: Story = {
args: {
interactions: getInteractions(CallStates.DONE),
},
};
Passing.play = async ({ args, canvasElement }) => {
if (isChromatic()) return;
const canvas = within(canvasElement);

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go to start'));
await expect(args.controls.start).toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go back'));
await expect(args.controls.back).toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go forward'));
await expect(args.controls.next).not.toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go to end'));
await expect(args.controls.end).not.toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Rerun'));
await expect(args.controls.rerun).toHaveBeenCalled();
});
play: async ({ args, canvasElement }) => {
if (isChromatic()) return;
const canvas = within(canvasElement);

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go to start'));
await expect(args.controls.start).toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go back'));
await expect(args.controls.back).toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go forward'));
await expect(args.controls.next).not.toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Go to end'));
await expect(args.controls.end).not.toHaveBeenCalled();
});

await waitFor(async () => {
await userEvent.click(canvas.getByLabelText('Rerun'));
await expect(args.controls.rerun).toHaveBeenCalled();
});
},
};

export const Paused: Story = {
Expand Down
4 changes: 4 additions & 0 deletions code/addons/interactions/src/components/Subnav.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { action } from '@storybook/addon-actions';
import { CallStates } from '@storybook/instrumenter';
import { Subnav } from './Subnav';
import { parameters } from '../preview';

export default {
title: 'Subnav',
component: Subnav,
parameters: {
layout: 'fullscreen',
},
args: {
controls: {
start: action('start'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
onSuccess: fn(),
},
globals: {
theme: 'light',
sb_theme: 'light',
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
},
};

Expand Down
14 changes: 4 additions & 10 deletions code/addons/themes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ import { Button } from './Button';
export default {
title: 'Example/Button',
component: Button,
parameters: {
themes: {
themeOverride: 'light', // component level override
},
},
// meta level override
globals: { theme: 'dark' },
};

export const Primary = {
Expand All @@ -64,10 +61,7 @@ export const PrimaryDark = {
primary: true,
label: 'Button',
},
parameters: {
themes: {
themeOverride: 'dark', // Story level override
},
},
// story level override
globals: { theme: 'dark' },
};
```
1 change: 1 addition & 0 deletions code/addons/themes/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const DEFAULT_ADDON_STATE: ThemeAddonState = {

export interface ThemeParameters {
themeOverride?: string;
disable?: boolean;
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
}

export const DEFAULT_THEME_PARAMETERS: ThemeParameters = {};
Expand Down
4 changes: 2 additions & 2 deletions code/addons/themes/src/decorators/class-name.decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DEFAULT_ELEMENT_SELECTOR = 'html';
const classStringToArray = (classString: string) => classString.split(' ').filter(Boolean);

// TODO check with @kasperpeulen: change the types so they can be correctly inferred from context e.g. <Story extends (...args: any[]) => any>
export const withThemeByClassName = <TRenderer extends Renderer = any>({
export const withThemeByClassName = <TRenderer extends Renderer = Renderer>({
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
themes,
defaultTheme,
parentSelector = DEFAULT_ELEMENT_SELECTOR,
Expand Down Expand Up @@ -47,7 +47,7 @@ export const withThemeByClassName = <TRenderer extends Renderer = any>({
if (newThemeClasses.length > 0) {
parentElement.classList.add(...newThemeClasses);
}
}, [themeOverride, selected, parentSelector]);
}, [themeOverride, selected]);
ndelangen marked this conversation as resolved.
Show resolved Hide resolved

return storyFn();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const withThemeByDataAttribute = <TRenderer extends Renderer = any>({
if (parentElement) {
parentElement.setAttribute(attributeName, themes[themeKey]);
}
}, [themeOverride, selected, parentSelector, attributeName]);
}, [themeOverride, selected]);
ndelangen marked this conversation as resolved.
Show resolved Hide resolved

return storyFn();
};
Expand Down
2 changes: 1 addition & 1 deletion code/addons/themes/src/decorators/provider.decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const withThemeFromJSXProvider = <TRenderer extends Renderer = any>({
const pairs = Object.entries(themes);

return pairs.length === 1 ? pluckThemeFromKeyPairTuple(pairs[0]) : themes[selectedThemeName];
}, [themes, selected, themeOverride]);
}, [selected, themeOverride]);
ndelangen marked this conversation as resolved.
Show resolved Hide resolved

if (!Provider) {
return (
Expand Down
7 changes: 3 additions & 4 deletions code/addons/themes/src/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Renderer, ProjectAnnotations } from 'storybook/internal/types';
import { GLOBAL_KEY } from './constants';
import { GLOBAL_KEY as KEY } from './constants';

export const globals: ProjectAnnotations<Renderer>['globals'] = {
// Required to make sure SB picks this up from URL params
[GLOBAL_KEY]: '',
export const initialGlobals: ProjectAnnotations<Renderer>['initialGlobals'] = {
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
[KEY]: '',
};
Loading