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

Allow setting global args in SB 6.5+ #399

Merged
merged 3 commits into from
Jun 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/react-ts/.storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export const parameters = {
},
},
};

export const argTypes = { globalArg: { options: ['A', 'B'], control: 'select' } };

export const args = { globalArg: 'A' };
2 changes: 2 additions & 0 deletions examples/react-ts/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# React TypesScript

This example demonstrates storybook in a React v16 project using TypeScript.

It also uses the v7 story store, and demonstrates the use of global args.
4 changes: 4 additions & 0 deletions examples/react/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export const parameters = {
},
},
};

export const argTypes = { globalArg: { options: ['A', 'B'], control: 'select' } };

export const args = { globalArg: 'A' };
2 changes: 2 additions & 0 deletions examples/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
This example demonstrates storybook in a React v16 project.

It uses autotitle for some stories, but without a configuration object, a `titlePrefix` cannot be defined.

This example also demonstrates global arg configuration in a v6 story store project.
34 changes: 25 additions & 9 deletions packages/builder-vite/codegen-iframe-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,44 @@ export async function generateIframeScriptCode(options: ExtendedOptions) {
// is loaded. That way our client-apis can assume the existence of the API+store
import { configure } from '${frameworkImportPath}';

import {
import * as clientApi from "@storybook/client-api";
import { logger } from '@storybook/client-logger';
${absoluteFilesToImport(configEntries, 'config')}
import * as preview from '${virtualPreviewFile}';
import { configStories } from '${virtualStoriesFile}';

const {
addDecorator,
addParameters,
addLoader,
addArgTypesEnhancer,
addArgsEnhancer,
setGlobalRender
} from '@storybook/client-api';
import { logger } from '@storybook/client-logger';
${absoluteFilesToImport(configEntries, 'config')}
import * as preview from '${virtualPreviewFile}';
import { configStories } from '${virtualStoriesFile}';
setGlobalRender,
} = clientApi;

const configs = [${importArray('config', configEntries.length).concat('preview.default').join(',')}].filter(Boolean)

configs.forEach(config => {
Object.keys(config).forEach((key) => {
const value = config[key];
switch (key) {
case 'args':
case 'args': {
if (typeof clientApi.addArgs !== "undefined") {
return clientApi.addArgs(value);
} else {
return logger.warn(
"Could not add global args. Please open an issue in storybookjs/builder-vite."
);
}
}
case 'argTypes': {
return logger.warn('Invalid args/argTypes in config, ignoring.', JSON.stringify(value));
if (typeof clientApi.addArgTypes !== "undefined") {
return clientApi.addArgTypes(value);
} else {
return logger.warn(
"Could not add global argTypes. Please open an issue in storybookjs/builder-vite."
);
}
}
case 'decorators': {
return value.forEach((decorator) => addDecorator(decorator, false));
Expand Down