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: Rollback v7 breaking change and keep reactive v6-compatible API #22229

Merged
merged 12 commits into from
Apr 28, 2023
Merged
13 changes: 3 additions & 10 deletions code/renderers/vue3/src/decorateStory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { ConcreteComponent, Component, ComponentOptions } from 'vue';
import type { Component, ComponentOptions, ConcreteComponent } from 'vue';
import { h } from 'vue';
import type { DecoratorFunction, StoryContext, LegacyStoryFn, Args } from '@storybook/types';
import type { DecoratorFunction, LegacyStoryFn, StoryContext } from '@storybook/types';
import { sanitizeStoryContextUpdate } from '@storybook/preview-api';
// eslint-disable-next-line import/no-extraneous-dependencies
import { looseEqual } from '@vue/shared';
import type { VueRenderer } from './types';

/*
Expand Down Expand Up @@ -45,7 +43,6 @@ export function decorateStory(
storyFn: LegacyStoryFn<VueRenderer>,
decorators: DecoratorFunction<VueRenderer>[]
): LegacyStoryFn<VueRenderer> {
let updatedArgs: Args;
return decorators.reduce(
(decorated: LegacyStoryFn<VueRenderer>, decorator) => (context: StoryContext<VueRenderer>) => {
let story: VueRenderer['storyResult'] | undefined;
Expand All @@ -55,20 +52,16 @@ export function decorateStory(
...context,
...sanitizeStoryContextUpdate(update),
});

if (update && update.args && !looseEqual(update.args, context.args))
updatedArgs ??= update.args;
return story;
}, context);

context.args = updatedArgs ?? context.args;
if (!story) story = decorated(context);

if (decoratedStory === story) {
return story;
}

const innerStory = () => h(story!, context.args);
const innerStory = () => h(story!);
return prepare(decoratedStory, innerStory) as VueRenderer['storyResult'];
},
(context) => prepare(storyFn(context)) as LegacyStoryFn<VueRenderer>
Expand Down
17 changes: 10 additions & 7 deletions code/renderers/vue3/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const render: ArgsStoryFn<VueRenderer> = (props, context) => {
);
}

return h(Component, props, createOrUpdateSlots(context));
return () => h(Component, props, createOrUpdateSlots(context));
};

// set of setup functions that will be called when story is created
Expand Down Expand Up @@ -82,7 +82,9 @@ export function renderToCanvas(
map.set(canvasElement, appState);

return () => {
return h(rootElement, appState.reactiveArgs);
// not passing args here as props
// treat the rootElement as a component without props
return h(rootElement);
};
},
});
Expand Down Expand Up @@ -161,11 +163,12 @@ function teardown(
function createOrUpdateSlots(context: StoryContext<VueRenderer, Args>) {
const { id: storyID, component } = context;
const slots = generateSlots(context);
if (slotsMap.has(storyID)) {
const app = slotsMap.get(storyID);
if (app?.reactiveSlots) updateArgs(app.reactiveSlots, slots);
return app?.reactiveSlots;
}
// this seem to cause recursive updates, and vue errors
// if (slotsMap.has(storyID)) {
// const app = slotsMap.get(storyID);
// if (app?.reactiveSlots) updateArgs(app.reactiveSlots, slots);
// return app?.reactiveSlots;
// }
slotsMap.set(storyID, { component, reactiveSlots: slots });
Copy link
Contributor Author

@kasperpeulen kasperpeulen Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chakAs3 I think I can remove the whole slotsMap. All the reactive test passes without it.
My gut feeling is that line 25, fixes this.
Without it, the slots are basically static, and need to updated forcefully.
But now we fix the render function to be lazy, we don't need this code anymore.

return slots;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export {
NoDecorators,
DecoratorFunctionalComponent,
DecoratorFunctionalComponentArgsFromContext,
DecoratorFunctionalComponentArgsFromProps,
DecoratorComponentOptions,
DecoratorComponentOptionsArgsFromData,
DecoratorComponentOptionsArgsFromProps,
} from './ReactiveDecorators.stories';
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export {
NoDecorators,
DecoratorFunctionalComponent,
DecoratorFunctionalComponentArgsFromContext,
DecoratorFunctionalComponentArgsFromProps,
DecoratorComponentOptions,
DecoratorComponentOptionsArgsFromData,
DecoratorComponentOptionsArgsFromProps,
} from './ReactiveDecorators.stories';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ export const ReactiveHtmlWrapper: Story = {
};

// to test that Simple html Decorators in CSF2 format are applied correctly in reactive mode
const ReactiveCSF2WrapperTempl: StoryFn = (args, { argTypes }) => ({
const ReactiveCSF2WrapperTempl: StoryFn = (args) => ({
components: { ReactiveArgs },
props: Object.keys(argTypes),
template: '<ReactiveArgs v-bind="$props" />',
setup() {
return { args };
},
template: '<ReactiveArgs v-bind="args" />',
});

export const ReactiveCSF2Wrapper = ReactiveCSF2WrapperTempl.bind({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ export const DecoratorFunctionalComponentArgsFromContext: Story = {
],
};

export const DecoratorFunctionalComponentArgsFromProps: Story = {
decorators: [
(storyFn, context) => {
const story = storyFn();
return (args) =>
h('div', [h('h2', `Decorator using args.label: ${args.label}`), h(story, context.args)]);
},
],
};

export const DecoratorComponentOptions: Story = {
decorators: [
(storyFn, context) => {
Expand All @@ -99,14 +89,3 @@ export const DecoratorComponentOptionsArgsFromData: Story = {
},
],
};

export const DecoratorComponentOptionsArgsFromProps: Story = {
decorators: [
(storyFn, context) => {
return {
props: ['label'],
template: '<div><h2>Decorator using label: {{label}}</h2><story /></div>',
};
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const CustomRender: Story = {
setup() {
return { args };
},
template: `<MySlotComponent :label="args.label" v-slot="slotProps">
template: `<MySlotComponent v-bind="args" v-slot="slotProps">
{{ slotProps.text }}, {{ slotProps.year }}
</MySlotComponent>`,
}),
Expand All @@ -73,7 +73,7 @@ export const CustomRenderUsingFunctionSlot: Story = {
setup() {
return { args };
},
template: `<MySlotComponent :label="args.label" v-slot="slotProps">
template: `<MySlotComponent v-bind="args" v-slot="slotProps">
{{args.default(slotProps)}}
</MySlotComponent>`,
}),
Expand Down