-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(react-storybook-addon-codesandbox): support various providers for "export to" functionality #29536
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
Merged
Hotell
merged 6 commits into
microsoft:master
from
Hotell:hotell/storybook/make-codesandbox-addon-pluggable
Oct 24, 2023
Merged
feat(react-storybook-addon-codesandbox): support various providers for "export to" functionality #29536
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
00dfce2
feat(storybook-addon-codesandbox): make addon configurable for both C…
Hotell d71bd72
feat(.storybook): use react-storybook-addon-codesandbox types directl…
Hotell bc103c7
feat(react-storybook-addon-codesandbox): make css style overrides a d…
Hotell 17a6c55
refactor: rename files/tokens and expose only addDemoActionButton fro…
Hotell 87ad2da
test(storybook-addon-codesandbox): add factory coverage
Hotell 354f22b
style: remove commented code in tests
Hotell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
2 changes: 1 addition & 1 deletion
2
...ok-addon-codesandbox/src/getDepdencies.ts → ...-addon-codesandbox/src/getDependencies.ts
This file contains hidden or 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
4 changes: 2 additions & 2 deletions
4
packages/react-components/react-storybook-addon-codesandbox/src/preset/preview.ts
This file contains hidden or 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,3 +1,3 @@ | ||
| import { withCodeSandboxButton } from '../withCodeSandboxButton'; | ||
| import { withSandboxButton } from '../withCodeSandboxButton'; | ||
|
|
||
| export const decorators = [withCodeSandboxButton]; | ||
| export const decorators = [withSandboxButton]; |
16 changes: 16 additions & 0 deletions
16
packages/react-components/react-storybook-addon-codesandbox/src/public-types.ts
This file contains hidden or 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,16 @@ | ||
| /** | ||
| * NOTE: | ||
| * Don't import anything from source code in this file !! | ||
| * | ||
| * only pure API definitions of addon are allowed to live here, that are used both internal and for external storybook `Parameter` type extensions | ||
| */ | ||
| interface ParametersConfig { | ||
| optionalDependencies?: Record<string, string>; | ||
| requiredDependencies?: Record<string, string>; | ||
| provider: 'codesandbox-cloud' | 'codesandbox-browser' | 'stackblitz-cloud'; | ||
| bundler: 'vite' | 'cra'; | ||
| } | ||
|
|
||
| export interface ParametersExtension { | ||
| exportToSandbox?: ParametersConfig; | ||
| } |
117 changes: 117 additions & 0 deletions
117
packages/react-components/react-storybook-addon-codesandbox/src/sandbox-factory.spec.ts
This file contains hidden or 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,117 @@ | ||
| import { ParametersExtension, StoryContext } from './types'; | ||
| import { addDemoActionButton } from './sandbox-factory'; | ||
| describe(`sandbox-factory`, () => { | ||
Hotell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| describe(`#addDemoActionButton`, () => { | ||
| let submitSpy: ReturnType<typeof jest.fn>; | ||
| beforeEach(() => { | ||
| // https://github.com/jsdom/jsdom/issues/1937 | ||
| submitSpy = window.HTMLFormElement.prototype.submit = jest.fn(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| function setup(config: Pick<NonNullable<ParametersExtension['exportToSandbox']>, 'bundler' | 'provider'>) { | ||
| const context = { | ||
| story: 'Showcase', | ||
| componentId: '', | ||
| name: 'Showcase', | ||
| title: 'DefaultTitle', | ||
| kind: '', | ||
| id: 'default', | ||
| originalStoryFn: { | ||
| name: 'DefaultTitle', | ||
| }, | ||
| viewMode: 'docs', | ||
| parameters: { | ||
| fullSource: ` | ||
| import * as React from 'react'; | ||
| import { Text } from '@proj/react-components'; | ||
| export const Default = () => <Text>This is an example of the Text component's usage.</Text>; | ||
| `, | ||
| exportToSandbox: { | ||
| ...config, | ||
| requiredDependencies: {}, | ||
| }, | ||
| }, | ||
| } as unknown as StoryContext; | ||
| const canvas = createCanvas(context.id); | ||
|
|
||
| return { canvas, context }; | ||
|
|
||
| function createCanvas(id: string) { | ||
| const root = document.createElement('div'); | ||
| root.id = 'docs-root'; | ||
|
|
||
| const content = ` | ||
| <div id="anchor--${id}"> | ||
| <div class="docs-story"> | ||
| <div class="css-x12m3"> | ||
| <button class="docblock-code-toggle">Show Code</button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| `; | ||
|
|
||
| root.innerHTML = content; | ||
|
|
||
| document.body.appendChild(root); | ||
|
|
||
| return { | ||
| getActionButton: () => document.querySelector('.with-code-sandbox-button') as HTMLButtonElement, | ||
| getActionButtonsContainer: () => document.querySelector('.css-x12m3'), | ||
| cleanup: () => root.remove(), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| it.each([ | ||
| { | ||
| bundler: 'cra', | ||
| provider: 'codesandbox-browser', | ||
| expected: `<button class="docblock-code-toggle with-code-sandbox-button">Open in CodeSandbox</button>`, | ||
| }, | ||
| { | ||
| bundler: 'cra', | ||
| provider: 'codesandbox-cloud', | ||
| expected: `<button class="docblock-code-toggle with-code-sandbox-button">Open in CodeSandbox</button>`, | ||
| }, | ||
| { | ||
| bundler: 'vite', | ||
| provider: 'codesandbox-cloud', | ||
| expected: `<button class="docblock-code-toggle with-code-sandbox-button">Open in CodeSandbox</button>`, | ||
| }, | ||
| { | ||
| bundler: 'cra', | ||
| provider: 'stackblitz-cloud', | ||
| expected: `<button class="docblock-code-toggle with-code-sandbox-button">Open in Stackblitz</button>`, | ||
| }, | ||
| { | ||
| bundler: 'vite', | ||
| provider: 'stackblitz-cloud', | ||
| expected: `<button class="docblock-code-toggle with-code-sandbox-button">Open in Stackblitz</button>`, | ||
| }, | ||
| ] as const)(`should add action button based on configuration ($bundler, $provider)`, ({ expected, ...config }) => { | ||
| const { canvas, context } = setup(config); | ||
| addDemoActionButton(context); | ||
|
|
||
| expect(canvas.getActionButtonsContainer()?.innerHTML).toEqual(expect.stringContaining(expected)); | ||
|
|
||
| canvas.cleanup(); | ||
| }); | ||
|
|
||
| it(`should submit form on click`, () => { | ||
| const { canvas, context } = setup({ bundler: 'cra', provider: 'codesandbox-browser' }); | ||
| addDemoActionButton(context); | ||
|
|
||
| const actionButton = canvas.getActionButton(); | ||
| actionButton.click(); | ||
|
|
||
| expect(submitSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| canvas.cleanup(); | ||
| }); | ||
| }); | ||
| }); | ||
97 changes: 97 additions & 0 deletions
97
packages/react-components/react-storybook-addon-codesandbox/src/sandbox-factory.ts
This file contains hidden or 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,97 @@ | ||
| import { getParameters } from 'codesandbox-import-utils/lib/api/define'; | ||
|
|
||
| import { scaffold } from './sandbox-scaffold'; | ||
| import { addHiddenInput, prepareData, prepareSandboxContainer, type Data } from './sandbox-utils'; | ||
| import { StoryContext } from './types'; | ||
|
|
||
| const defaultFileToPreview = encodeURIComponent('src/example.tsx'); | ||
|
|
||
| const actionConfig = { | ||
| 'codesandbox-cloud': { | ||
| label: 'CodeSandbox', | ||
| factory: (files: Record<string, string>, config: Data) => openCodeSandbox({ files, ...config }), | ||
| }, | ||
| 'codesandbox-browser': { | ||
| label: 'CodeSandbox', | ||
| factory: (files: Record<string, string>, config: Data) => openCodeSandbox({ files, ...config }), | ||
| }, | ||
| 'stackblitz-cloud': { | ||
| label: 'Stackblitz', | ||
| factory: (files: Record<string, string>, config: Data) => openStackblitz({ files, ...config }), | ||
| }, | ||
| }; | ||
|
|
||
| export function addDemoActionButton(context: StoryContext) { | ||
| const { container, cssClasses } = prepareSandboxContainer(context); | ||
| const config = prepareData(context); | ||
| if (!config) { | ||
| throw new Error('issues with data'); | ||
| } | ||
|
|
||
| addActionButton(container, config, cssClasses); | ||
| } | ||
|
|
||
| function addActionButton(container: HTMLElement, config: Data, classList: string[]) { | ||
| const files = scaffold[config.bundler](config); | ||
| const action = actionConfig[config.provider]; | ||
|
|
||
| const button = document.createElement('button'); | ||
| button.classList.add(...classList); | ||
| button.textContent = `Open in ${action.label}`; | ||
|
|
||
| container?.prepend(button); | ||
|
|
||
| button.addEventListener('click', _ev => { | ||
| action.factory(files, config); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @see https://developer.stackblitz.com/docs/platform/post-api/ | ||
| */ | ||
| function openStackblitz(data: { files: Record<string, string> } & Data) { | ||
| const { files, description, title } = data; | ||
| const form = document.createElement('form'); | ||
| form.method = 'post'; | ||
| form.target = '_blank'; | ||
| form.action = `https://stackblitz.com/run?file=${defaultFileToPreview}`; | ||
| // node template - boots web-container | ||
| addHiddenInput(form, 'project[template]', 'node'); | ||
| addHiddenInput(form, 'project[title]', title); | ||
| addHiddenInput(form, 'project[description]', `# ${description}`); | ||
|
|
||
| Object.keys(files).forEach(key => { | ||
| const value = files[key]; | ||
| addHiddenInput(form, `project[files][${key}]`, value); | ||
| }); | ||
|
|
||
| document.body.appendChild(form); | ||
| form.submit(); | ||
| document.body.removeChild(form); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @see https://codesandbox.io/docs/learn/sandboxes/cli-api#define-api | ||
| */ | ||
| function openCodeSandbox({ files, provider }: { files: Record<string, string> } & Data) { | ||
| const normalizedFilesApi = Object.entries(files).reduce((acc, current) => { | ||
| acc[current[0]] = { isBinary: false, content: current[1] }; | ||
| return acc; | ||
| }, {} as Record<string, { content: string; isBinary: boolean }>); | ||
|
|
||
| const env = provider === 'codesandbox-cloud' ? 'server' : 'browser'; | ||
| const parameters = getParameters({ files: normalizedFilesApi }); | ||
|
|
||
| const form = document.createElement('form'); | ||
| form.method = 'POST'; | ||
| form.target = '_blank'; | ||
| form.action = `https://codesandbox.io/api/v1/sandboxes/define?environment=${env}`; | ||
|
|
||
| addHiddenInput(form, 'parameters', parameters); | ||
| addHiddenInput(form, 'query', `file=${defaultFileToPreview}`); | ||
| document.body.appendChild(form); | ||
| form.submit(); | ||
| document.body.removeChild(form); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.