-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15748 from pzuraq/add-dynamic-source-renderer-to-…
…html HTML: Dynamic source snippets
- Loading branch information
Showing
9 changed files
with
201 additions
and
27 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,16 @@ | ||
import { sourceDecorator } from './sourceDecorator'; | ||
import { prepareForInline } from './prepareForInline'; | ||
import { SourceType } from '../../shared'; | ||
|
||
export const decorators = [sourceDecorator]; | ||
|
||
export const parameters = { | ||
docs: { | ||
inlineStories: true, | ||
prepareForInline, | ||
source: { | ||
type: SourceType.DYNAMIC, | ||
language: 'html', | ||
}, | ||
}, | ||
}; |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import { StoryFn } from '@storybook/addons'; | ||
|
||
export function prepareForInline(storyFn: StoryFn<string>) { | ||
const html = storyFn(); | ||
if (typeof html === 'string') { | ||
// eslint-disable-next-line react/no-danger | ||
return <div dangerouslySetInnerHTML={{ __html: html }} />; | ||
} | ||
return ( | ||
<div ref={(node?: HTMLDivElement): never | null => (node ? node.appendChild(html) : null)} /> | ||
); | ||
} |
113 changes: 113 additions & 0 deletions
113
addons/docs/src/frameworks/html/sourceDecorator.test.ts
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,113 @@ | ||
import { addons, StoryContext } from '@storybook/addons'; | ||
import { sourceDecorator } from './sourceDecorator'; | ||
import { SNIPPET_RENDERED } from '../../shared'; | ||
|
||
jest.mock('@storybook/addons'); | ||
const mockedAddons = addons as jest.Mocked<typeof addons>; | ||
|
||
expect.addSnapshotSerializer({ | ||
print: (val: any) => val, | ||
test: (val) => typeof val === 'string', | ||
}); | ||
|
||
const makeContext = (name: string, parameters: any, args: any, extra?: object): StoryContext => ({ | ||
id: `html-test--${name}`, | ||
kind: 'js-text', | ||
name, | ||
parameters, | ||
args, | ||
argTypes: {}, | ||
globals: {}, | ||
...extra, | ||
}); | ||
|
||
describe('sourceDecorator', () => { | ||
let mockChannel: { on: jest.Mock; emit?: jest.Mock }; | ||
beforeEach(() => { | ||
mockedAddons.getChannel.mockReset(); | ||
|
||
mockChannel = { on: jest.fn(), emit: jest.fn() }; | ||
mockedAddons.getChannel.mockReturnValue(mockChannel as any); | ||
}); | ||
|
||
it('should render dynamically for args stories', () => { | ||
const storyFn = (args: any) => `<div>args story</div>`; | ||
const context = makeContext('args', { __isArgsStory: true }, {}); | ||
sourceDecorator(storyFn, context); | ||
expect(mockChannel.emit).toHaveBeenCalledWith( | ||
SNIPPET_RENDERED, | ||
'html-test--args', | ||
'<div>args story</div>' | ||
); | ||
}); | ||
|
||
it('should dedent source by default', () => { | ||
const storyFn = (args: any) => ` | ||
<div> | ||
args story | ||
</div> | ||
`; | ||
const context = makeContext('args', { __isArgsStory: true }, {}); | ||
sourceDecorator(storyFn, context); | ||
expect(mockChannel.emit).toHaveBeenCalledWith( | ||
SNIPPET_RENDERED, | ||
'html-test--args', | ||
['<div>', ' args story', '</div>'].join('\n') | ||
); | ||
}); | ||
|
||
it('should skip dynamic rendering for no-args stories', () => { | ||
const storyFn = () => `<div>classic story</div>`; | ||
const context = makeContext('classic', {}, {}); | ||
sourceDecorator(storyFn, context); | ||
expect(mockChannel.emit).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should use the originalStoryFn if excludeDecorators is set', () => { | ||
const storyFn = (args: any) => `<div>args story</div>`; | ||
const decoratedStoryFn = (args: any) => ` | ||
<div style="padding: 25px; border: 3px solid red;">${storyFn(args)}</div> | ||
`; | ||
const context = makeContext( | ||
'args', | ||
{ | ||
__isArgsStory: true, | ||
docs: { | ||
source: { | ||
excludeDecorators: true, | ||
}, | ||
}, | ||
}, | ||
{}, | ||
{ originalStoryFn: storyFn } | ||
); | ||
sourceDecorator(decoratedStoryFn, context); | ||
expect(mockChannel.emit).toHaveBeenCalledWith( | ||
SNIPPET_RENDERED, | ||
'html-test--args', | ||
'<div>args story</div>' | ||
); | ||
}); | ||
|
||
it('allows the snippet output to be modified by transformSource', () => { | ||
const storyFn = (args: any) => `<div>args story</div>`; | ||
const transformSource = (dom: string) => `<p>${dom}</p>`; | ||
const docs = { transformSource }; | ||
const context = makeContext('args', { __isArgsStory: true, docs }, {}); | ||
sourceDecorator(storyFn, context); | ||
expect(mockChannel.emit).toHaveBeenCalledWith( | ||
SNIPPET_RENDERED, | ||
'html-test--args', | ||
'<p><div>args story</div></p>' | ||
); | ||
}); | ||
|
||
it('provides the story context to transformSource', () => { | ||
const storyFn = (args: any) => `<div>args story</div>`; | ||
const transformSource = jest.fn((x) => x); | ||
const docs = { transformSource }; | ||
const context = makeContext('args', { __isArgsStory: true, docs }, {}); | ||
sourceDecorator(storyFn, context); | ||
expect(transformSource).toHaveBeenCalledWith('<div>args story</div>', context); | ||
}); | ||
}); |
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,44 @@ | ||
/* global window */ | ||
import { addons, StoryContext, StoryFn } from '@storybook/addons'; | ||
import dedent from 'ts-dedent'; | ||
import { SNIPPET_RENDERED, SourceType } from '../../shared'; | ||
|
||
function skipSourceRender(context: StoryContext) { | ||
const sourceParams = context?.parameters.docs?.source; | ||
const isArgsStory = context?.parameters.__isArgsStory; | ||
|
||
// always render if the user forces it | ||
if (sourceParams?.type === SourceType.DYNAMIC) { | ||
return false; | ||
} | ||
|
||
// never render if the user is forcing the block to render code, or | ||
// if the user provides code, or if it's not an args story. | ||
return !isArgsStory || sourceParams?.code || sourceParams?.type === SourceType.CODE; | ||
} | ||
|
||
// By default, just remove indentation | ||
function defaultTransformSource(source: string) { | ||
// Have to wrap dedent so it doesn't serialize the context | ||
return dedent(source); | ||
} | ||
|
||
function applyTransformSource(source: string, context: StoryContext): string { | ||
const docs = context.parameters.docs ?? {}; | ||
const transformSource = docs.transformSource ?? defaultTransformSource; | ||
return transformSource(source, context); | ||
} | ||
|
||
export function sourceDecorator(storyFn: StoryFn, context: StoryContext) { | ||
const story = context?.parameters.docs?.source?.excludeDecorators | ||
? context.originalStoryFn(context.args) | ||
: storyFn(); | ||
|
||
if (typeof story === 'string' && !skipSourceRender(context)) { | ||
const source = applyTransformSource(story, context); | ||
|
||
addons.getChannel().emit(SNIPPET_RENDERED, context.id, source); | ||
} | ||
|
||
return story; | ||
} |
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
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