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

Vue3: Add default render function CSF3 #17068

Merged
merged 4 commits into from
Jan 3, 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: 2 additions & 2 deletions app/vue3/src/client/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IStorybookSection } from './types';
import { VueFramework } from './types-6-0';
import { decorateStory } from './decorateStory';

import { renderToDOM, storybookApp } from './render';
import { render, renderToDOM, storybookApp } from './render';

const framework = 'vue3';

Expand All @@ -22,7 +22,7 @@ interface ClientApi extends ClientStoryApi<VueFramework['storyResult']> {
app: App;
}

const api = start(renderToDOM, { decorateStory });
const api = start(renderToDOM, { decorateStory, render });

export const storiesOf: ClientApi['storiesOf'] = (kind, m) => {
return (api.clientApi.storiesOf(kind, m) as ReturnType<ClientApi['storiesOf']>).addParameters({
Expand Down
13 changes: 13 additions & 0 deletions app/vue3/src/client/preview/render.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import dedent from 'ts-dedent';
import { createApp, h, shallowRef, ComponentPublicInstance } from 'vue';
import { RenderContext } from '@storybook/store';
import { ArgsStoryFn } from '@storybook/csf';

import { StoryFnVueReturnType } from './types';
import { VueFramework } from './types-6-0';

export const render: ArgsStoryFn<VueFramework> = (props, context) => {
const { id, component: Component } = context;
if (!Component) {
throw new Error(
`Unable to render story ${id} as the component annotation is missing from the default export`
);
}

return h(Component, props);
};

export const activeStoryComponent = shallowRef<StoryFnVueReturnType | null>(null);

let root: ComponentPublicInstance | null = null;
Expand Down
6 changes: 3 additions & 3 deletions examples/vue-3-cli/src/stories/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

<script lang="typescript">
import './button.css';
import { reactive, computed } from 'vue';
import { reactive, computed, defineComponent } from 'vue';

export default {
export default defineComponent({
name: 'my-button',

props: {
Expand Down Expand Up @@ -55,5 +55,5 @@ export default {
}
}
},
};
});
</script>
21 changes: 21 additions & 0 deletions examples/vue-3-cli/src/stories/CSF3.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { userEvent, within } from '@storybook/testing-library';
import { Story, Meta } from '@storybook/vue3/types-7-0';
import Button from './Button.vue';

export default {
title: 'Example/ButtonCSF3',
component: Button,
} as Meta;

export const Default: Story = { args: { label: 'Default' } };

export const Primary: Story = {
args: {
label: 'Primary',
primary: true,
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.click(canvas.getByRole('button'));
},
};