Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions code/e2e-tests/framework-vue3.spec.ts

This file was deleted.

29 changes: 4 additions & 25 deletions code/renderers/vue3/src/render.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { describe, expect, it } from 'vitest';

import type { Args, Globals } from 'storybook/internal/types';

import { expectTypeOf } from 'expect-type';
import { computed, reactive } from 'vue';
import { reactive } from 'vue';

import { updateArgs } from './render';

Expand All @@ -25,7 +23,7 @@ describe('Render Story', () => {
expectTypeOf(reactiveArgs).toEqualTypeOf<{ objectArg: { argFoo: string; argBar: string } }>();

const newArgs = { argFoo: 'foo2', argBar: 'bar2' };
updateArgs<Args>(reactiveArgs, newArgs);
updateArgs(reactiveArgs, newArgs);
expectTypeOf(reactiveArgs).toEqualTypeOf<{ objectArg: { argFoo: string; argBar: string } }>();
expect(reactiveArgs).toEqual({
argFoo: 'foo2',
Expand All @@ -39,7 +37,7 @@ describe('Render Story', () => {
expectTypeOf(reactiveArgs).toEqualTypeOf<{ objectArg: { argFoo: string } }>();

const newArgs = { argFoo: 'foo2', argBar: 'bar2' };
updateArgs<Args>(reactiveArgs, newArgs);
updateArgs(reactiveArgs, newArgs);
expect(reactiveArgs).toEqual({ argFoo: 'foo2', argBar: 'bar2' });
});

Expand All @@ -55,7 +53,7 @@ describe('Render Story', () => {
}>();

const newArgs = { argFoo: 'foo2', argBar: 'bar2' };
updateArgs<Args>(reactiveArgs, newArgs);
updateArgs(reactiveArgs, newArgs);

expect(reactiveArgs).toEqual({
argFoo: 'foo2',
Expand Down Expand Up @@ -90,23 +88,4 @@ describe('Render Story', () => {

expect(reactiveArgs).toEqual({ objectArg: { argFoo: 'bar' } });
});

it('update reactive Globals', async () => {
const reactiveGlobals = reactive<Globals>({ theme: 'light', locale: 'en' });

let observedTheme: string | undefined;
const watcher = computed(() => {
observedTheme = reactiveGlobals.theme as string;
return reactiveGlobals.theme;
});

expect(watcher.value).toBe('light');
expect(observedTheme).toBe('light');

updateArgs<Globals>(reactiveGlobals, { theme: 'dark', locale: 'en' });

expect(watcher.value).toBe('dark');
expect(observedTheme).toBe('dark');
expect(reactiveGlobals).toEqual({ theme: 'dark', locale: 'en' });
});
});
23 changes: 6 additions & 17 deletions code/renderers/vue3/src/render.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/* eslint-disable local-rules/no-uncategorized-errors */
import type { Globals } from 'storybook/internal/types';
import {
type Args,
type ArgsStoryFn,
type RenderContext,
type StoryContext,
} from 'storybook/internal/types';
import type { Args, ArgsStoryFn, RenderContext, StoryContext } from 'storybook/internal/types';

import type { PreviewWeb } from 'storybook/preview-api';
import type { App } from 'vue';
Expand Down Expand Up @@ -45,7 +39,6 @@ const map = new Map<
{
vueApp: ReturnType<typeof createApp>;
reactiveArgs: Args;
reactiveGlobals: Globals;
}
>();

Expand All @@ -63,8 +56,7 @@ export async function renderToCanvas(
const element = storyFn(); // call the story function to get the root element with all the decorators
const args = getArgs(element, storyContext); // get args in case they are altered by decorators otherwise use the args from the context

updateArgs<Args>(existingApp.reactiveArgs, args);
updateArgs<Globals>(existingApp.reactiveGlobals, storyContext.globals);
updateArgs(existingApp.reactiveArgs, args);
return () => {
teardown(existingApp.vueApp, canvasElement);
};
Expand All @@ -74,19 +66,20 @@ export async function renderToCanvas(
teardown(existingApp.vueApp, canvasElement);
}

// create vue app for the story

// create vue app for the story
const vueApp = createApp({
setup() {
storyContext.args = reactive(storyContext.args);
storyContext.globals = reactive(storyContext.globals);
const rootElement = storyFn(); // call the story function to get the root element with all the decorators
const args = getArgs(rootElement, storyContext); // get args in case they are altered by decorators otherwise use the args from the context
const appState = {
vueApp,
reactiveArgs: reactive(args),
reactiveGlobals: storyContext.globals,
};
map.set(canvasElement, appState);

return () => {
// not passing args here as props
// treat the rootElement as a component without props
Expand Down Expand Up @@ -148,11 +141,7 @@ function getArgs(element: StoryFnVueReturnType, storyContext: StoryContext<VueRe
* @param nextArgs
* @returns
*/
export function updateArgs<
T extends {
[name: string]: unknown;
},
>(reactiveArgs: T, nextArgs: T) {
export function updateArgs(reactiveArgs: Args, nextArgs: Args) {
if (Object.keys(nextArgs).length === 0) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import type { DecoratorFunction } from 'storybook/internal/types';
import { global as globalThis } from '@storybook/global';
import type { Meta, StoryObj, VueRenderer } from '@storybook/vue3';

import { useArgs } from 'storybook/preview-api';
import { h } from 'vue';
import { computed } from 'vue';

const { Button, Pre } = (globalThis as any).__TEMPLATE_COMPONENTS__;

Expand Down Expand Up @@ -49,62 +47,6 @@ const DynamicWrapperWrapper: DecoratorFunction<VueRenderer> = (storyFn, { args }
computed: { level: () => `${args.level}px` },
});

const getCaptionForLocale = (locale: string) => {
switch (locale) {
case 'es':
return 'Hola!';
case 'kr':
return '안녕하세요!';
case 'zh':
return '你好!';
case 'en':
return 'Hello!';
default:
return undefined;
}
};

const updateArgsDecorator: DecoratorFunction<VueRenderer> = (story, { args }) => {
const [, updateArgs] = useArgs();
return {
components: { story },
setup() {
return {
args,
updateArgs,
};
},
template: `
<div>
<button @click="() => updateArgs({ label: Number(args.label) + 1 })">Add 1</button>
<hr />
<story />
</div>
`,
};
};

const localeDecorator: DecoratorFunction<VueRenderer> = (story, { globals }) => {
return {
components: { story },
setup() {
const ctxGreeting = computed(() => getCaptionForLocale(globals?.locale) || 'Hello!');

return {
ctxGreeting,
globals,
};
},
template: `
<div>
<p>Greeting: {{ctxGreeting}}</p>
<p>Locale: {{globals?.locale}}</p>
<story />
</div>
`,
};
};

export const ComponentTemplate: Story = {
args: { label: 'With component' },
decorators: [ComponentTemplateWrapper],
Expand Down Expand Up @@ -142,13 +84,3 @@ export const MultipleWrappers = {
DynamicWrapperWrapper,
],
};

export const UpdateArgs = {
args: { label: '0' },
decorators: [updateArgsDecorator],
};

export const ReactiveGlobalDecorator = {
args: { label: 'With reactive global decorator' },
decorators: [localeDecorator],
};
52 changes: 0 additions & 52 deletions docs/_snippets/decorator-with-reactive-globals.md

This file was deleted.

58 changes: 0 additions & 58 deletions docs/_snippets/decorator-with-updateArgs.md

This file was deleted.

Loading