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

Renderer React: first example of multiframework support using decorators #6826

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
821ac04
Addon React: POC of multiframework support using decorators
Hypnosphi May 19, 2019
3b90941
Update teamcity artifact rules
Hypnosphi May 19, 2019
b613b16
Commit new storyshots
Hypnosphi May 19, 2019
c053196
Merge branch 'next' into addon-react
Hypnosphi May 28, 2019
027c7df
Raise versions
Hypnosphi May 28, 2019
013dee3
Include all `src/server` paths into babelrc node override
Hypnosphi May 28, 2019
babca9c
Use string as "framework" parameter value
Hypnosphi May 28, 2019
5c46ee1
Merge branch 'preview-hooks' into addon-react
Hypnosphi Jun 1, 2019
f9c872e
Merge branch 'preview-hooks' into addon-react
Hypnosphi Jun 1, 2019
8251203
Use preview hooks
Hypnosphi Jun 1, 2019
cf30b8f
Move more stuff from @storybook/react to @storybook/addon-react
Hypnosphi Jun 2, 2019
c159fc8
Add docs
Hypnosphi Jun 2, 2019
33ccea0
Merge remote-tracking branch 'origin/next' into addon-react
Hypnosphi Jul 14, 2019
23d7134
Integrate changes from next
Hypnosphi Jul 14, 2019
a309dfc
Fix stories with theming
Hypnosphi Jul 14, 2019
6f47998
Rename to renderer-react
Hypnosphi Jul 14, 2019
e74e2a3
Update CircleCI cache paths
Hypnosphi Jul 14, 2019
de53897
Merge branch 'next' into pr/Hypnosphi/6826
ndelangen Jul 30, 2019
1780d6e
CLEANUP duplicate snapshots
ndelangen Jul 30, 2019
a4cb580
FIX linting
ndelangen Jul 30, 2019
3bc3952
Merge next
Hypnosphi Aug 17, 2019
6ff74dc
Merge branch 'trigger-effects-after-render' into addon-react
Hypnosphi Aug 17, 2019
85cc081
Support React hooks on story level
Hypnosphi Aug 17, 2019
67cf02f
Merge branch 'trigger-effects-after-render' into addon-react
Hypnosphi Aug 17, 2019
4cd95d3
Fix tests
Hypnosphi Aug 17, 2019
b3afa3b
Merge next
Hypnosphi Oct 13, 2019
0e42647
Update lockfile
Hypnosphi Oct 14, 2019
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
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
- dev-kits
- app
- lib
- renderers
chromatic:
<<: *defaults
steps:
Expand Down Expand Up @@ -157,6 +158,7 @@ jobs:
- addons
- app
- lib
- renderers
smoke-tests:
<<: *defaults
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import shallow from 'react-test-renderer/shallow';

export default (element: any) => {
const renderer = shallow.createRenderer();
renderer.render(element);
return renderer.getRenderOutput();
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import shallow from 'react-test-renderer/shallow';

import getChildren from './getChildren';

function getRenderedTree(story: any, context: any, { renderer, serializer }: any) {
const storyElement = story.render();
const storyElement = getChildren(story.render());
const shallowRenderer = renderer || shallow.createRenderer();
const tree = shallowRenderer.render(storyElement);
return serializer ? serializer(tree) : tree;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import reactTestRenderer from 'react-test-renderer';

import getChildren from './getChildren';

function getRenderedTree(story: any, context: any, { renderer, ...rendererOptions }: any) {
const storyElement = story.render();
const storyElement = getChildren(story.render());
const currentRenderer = renderer || reactTestRenderer.create;
const tree = currentRenderer(storyElement, rendererOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ initStoryshots({

// Assert the expected value and the corresponding snapshot
expect(wrapper.find('AsyncTestComponent').contains(EXPECTED_VALUE)).toBe(true);
expect(toJson(wrapper)).toMatchSpecificSnapshot(snapshotFilename);
expect(toJson(wrapper.children())).toMatchSpecificSnapshot(snapshotFilename);

// finally mark test as done
done();
Expand Down
4 changes: 2 additions & 2 deletions app/html/src/client/preview/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { RenderMainArgs } from './types';

const rootElement = document.getElementById('root');

export default function renderMain({
export default async function renderMain({
storyFn,
selectedKind,
selectedStory,
showMain,
showError,
forceRender,
}: RenderMainArgs) {
const element = storyFn();
const element = await storyFn();

showMain();
if (typeof element === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion app/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
"@storybook/addons": "5.3.0-alpha.17",
"@storybook/core": "5.3.0-alpha.17",
"@storybook/node-logger": "5.3.0-alpha.17",
"@storybook/renderer-react": "5.3.0-alpha.17",
"@svgr/webpack": "^4.0.3",
"@types/webpack-env": "^1.13.7",
"babel-plugin-add-react-displayname": "^0.0.5",
"babel-plugin-named-asset-import": "^0.3.1",
"babel-plugin-react-docgen": "^3.0.0",
"babel-preset-react-app": "^9.0.0",
"core-js": "^3.0.1",
"global": "^4.3.2",
Expand Down
1 change: 1 addition & 0 deletions app/react/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@storybook/renderer-react/dist/client/globals';
import { window } from 'global';

if (window) {
Expand Down
31 changes: 31 additions & 0 deletions app/react/src/client/preview/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { document } from 'global';
import ReactDOM from 'react-dom';
import { render, register } from '@storybook/renderer-react';

import { RenderMainArgs } from './types';

register(true);

const rootEl = document ? document.getElementById('root') : null;

export default async function renderMain({
storyFn,
selectedKind,
selectedStory,
showMain,
forceRender,
}: RenderMainArgs) {
const element = storyFn();

// We need to unmount the existing set of components in the DOM node.
// Otherwise, React may not recreate instances for every story run.
// This could leads to issues like below:
// https://github.com/storybookjs/react-storybook/issues/81
// But forceRender means that it's the same story, so we want too keep the state in that case.
if (!forceRender) {
ReactDOM.unmountComponentAtNode(rootEl);
}

await render(element, rootEl, { name: selectedStory, kind: selectedKind });
showMain();
}
71 changes: 0 additions & 71 deletions app/react/src/client/preview/render.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion app/react/src/client/preview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface ShowErrorArgs {
}

export interface RenderMainArgs {
storyFn: React.FunctionComponent<any>;
storyFn: () => React.ReactElement | null;
selectedKind: string;
selectedStory: string;
showMain: () => void;
Expand Down
4 changes: 2 additions & 2 deletions app/react/src/server/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const packageJson = require('../../package.json');
export default {
packageJson,
frameworkPresets: [
require.resolve('./framework-preset-react.js'),
require.resolve('@storybook/renderer-react/dist/server/framework-preset-react.js'),
require.resolve('./framework-preset-cra.js'),
require.resolve('./framework-preset-react-docgen.js'),
require.resolve('@storybook/renderer-react/dist/server/framework-preset-react-docgen.js'),
],
};
14 changes: 14 additions & 0 deletions examples/html-kitchen-sink/.storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import '@storybook/renderer-react/register';
import { configure, addParameters, addDecorator } from '@storybook/html';
import { withA11y } from '@storybook/addon-a11y';
import { ThemeProvider, themes, convert } from '@storybook/theming';
import { useParameter } from '@storybook/client-api';
import React from 'react';

addDecorator(withA11y);

addDecorator(getStory => {
const story = getStory();
const framework = useParameter('framework');
if (framework !== 'react') {
return story;
}

return <ThemeProvider theme={convert(themes.light)}>{story}</ThemeProvider>;
});

addParameters({
a11y: {
config: {},
Expand Down
6 changes: 5 additions & 1 deletion examples/html-kitchen-sink/.storybook/presets.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = ['@storybook/addon-docs/html/preset'];
module.exports = [
'@storybook/renderer-react/dist/server/framework-preset-react',
'@storybook/renderer-react/dist/server/framework-preset-react-docgen',
'@storybook/addon-docs/html/preset',
];
8 changes: 7 additions & 1 deletion examples/html-kitchen-sink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@
"@storybook/core": "5.3.0-alpha.17",
"@storybook/core-events": "5.3.0-alpha.17",
"@storybook/html": "5.3.0-alpha.17",
"@storybook/renderer-react": "5.3.0-alpha.17",
"@storybook/source-loader": "5.3.0-alpha.17",
"@storybook/theming": "5.3.0-alpha.17",
"eventemitter3": "^4.0.0",
"format-json": "^1.0.3",
"global": "^4.3.2"
"global": "^4.3.2",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"uuid": "^3.3.2"
}
}
76 changes: 76 additions & 0 deletions examples/html-kitchen-sink/stories/react/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React, { Component } from 'react';
import json from 'format-json';
import PropTypes from 'prop-types';

import { styled } from '@storybook/theming';
import EventEmitter from 'eventemitter3';
import uuid from 'uuid/v4';

const Wrapper = styled.div({
padding: 20,
});
const Title = styled.h1({
margin: 0,
});
const Item = styled.div({
listStyle: 'none',
marginBottom: 10,
});

export default class Logger extends Component {
static propTypes = {
emitter: PropTypes.instanceOf(EventEmitter).isRequired,
title: PropTypes.string,
};

static defaultProps = {
title: 'Logger',
};

state = {
events: [],
};

componentDidMount() {
const { emitter } = this.props;

emitter.on(Logger.LOG_EVENT, this.onEventHandler);
}

componentWillUnmount() {
const { emitter } = this.props;

emitter.removeListener(Logger.LOG_EVENT, this.onEventHandler);
}

onEventHandler = ({ name, payload }) => {
this.setState(({ events }) => ({
events: [...events, { name, id: uuid(), payload }],
}));
};

static LOG_EVENT = 'Logger:log';

render() {
const { events } = this.state;
const { title } = this.props;

return (
<Wrapper>
<Title>{title}</Title>
<dl>
{events.map(({ id, name, payload }) => (
<Item key={id}>
<dt>
<b>Event name:</b> {name}
</dt>
<dd>
<b>Event payload:</b> {json.plain(payload)}
</dd>
</Item>
))}
</dl>
</Wrapper>
);
}
}
Loading