@stencil/storybook-plugin 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ import type { Renderer, ArgsEnhancer } from "@storybook/types";
2
+ /**
3
+ * Automatically add action args for argTypes whose name
4
+ * matches a regex, such as `^on.*` for react-style `onClick` etc.
5
+ */
6
+ export declare const inferActionsFromArgTypesRegex: ArgsEnhancer<Renderer>;
7
+ /**
8
+ * Add action args for list of strings.
9
+ */
10
+ export declare const addActionsFromArgTypes: ArgsEnhancer<Renderer>;
@@ -0,0 +1,37 @@
1
+ // This file is entirely copied from @storybook/addon-actions (changing the action import)
2
+ import { action } from "@storybook/addon-actions";
3
+ const isInInitialArgs = (name, initialArgs) => typeof initialArgs[name] === "undefined" && !(name in initialArgs);
4
+ /**
5
+ * Automatically add action args for argTypes whose name
6
+ * matches a regex, such as `^on.*` for react-style `onClick` etc.
7
+ */
8
+ export const inferActionsFromArgTypesRegex = (context) => {
9
+ const { initialArgs, argTypes, parameters: { actions }, } = context;
10
+ if (!actions || actions.disable || !actions.argTypesRegex || !argTypes) {
11
+ return {};
12
+ }
13
+ const argTypesRegex = new RegExp(actions.argTypesRegex);
14
+ const argTypesMatchingRegex = Object.entries(argTypes).filter(([name]) => !!argTypesRegex.test(name));
15
+ return argTypesMatchingRegex.reduce((acc, [name, argType]) => {
16
+ if (isInInitialArgs(name, initialArgs)) {
17
+ acc[name] = action(name);
18
+ }
19
+ return acc;
20
+ }, {});
21
+ };
22
+ /**
23
+ * Add action args for list of strings.
24
+ */
25
+ export const addActionsFromArgTypes = (context) => {
26
+ const { initialArgs, argTypes, parameters: { actions }, } = context;
27
+ if (actions?.disable || !argTypes) {
28
+ return {};
29
+ }
30
+ const argTypesWithAction = Object.entries(argTypes).filter(([name, argType]) => !!argType["action"]);
31
+ return argTypesWithAction.reduce((acc, [name, argType]) => {
32
+ if (isInInitialArgs(name, initialArgs)) {
33
+ acc[name] = action(typeof argType["action"] === "string" ? argType["action"] : name);
34
+ }
35
+ return acc;
36
+ }, {});
37
+ };
@@ -0,0 +1,2 @@
1
+ import { Args } from "./types.js";
2
+ export declare const componentToJSX: (tagName: string, args: Args) => any;
@@ -0,0 +1,5 @@
1
+ import { h } from '@stencil/core';
2
+ export const componentToJSX = (tagName, args) => {
3
+ const Component = `${tagName}`;
4
+ return h(Component, { ...args });
5
+ };
@@ -0,0 +1,3 @@
1
+ import './globals';
2
+ export * from './types.js';
3
+ export * from './portable-stories.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import './globals';
2
+ export * from './types.js';
3
+ export * from './portable-stories.js';
@@ -0,0 +1,2 @@
1
+ import type { StorybookConfig } from '../types';
2
+ export declare function defineMain(config: StorybookConfig): StorybookConfig;
@@ -0,0 +1,3 @@
1
+ export function defineMain(config) {
2
+ return config;
3
+ }
@@ -0,0 +1,22 @@
1
+ import type { NamedOrDefaultProjectAnnotations, NormalizedProjectAnnotations } from '@storybook/types';
2
+ import type { StencilRenderer } from './types';
3
+ /**
4
+ * Function that sets the globalConfig of your storybook. The global config is the preview module of
5
+ * your .storybook folder.
6
+ *
7
+ * It should be run a single time, so that your global config (e.g. decorators) is applied to your
8
+ * stories when using `composeStories` or `composeStory`.
9
+ *
10
+ * Example:
11
+ *
12
+ * ```jsx
13
+ * // setup-file.js
14
+ * import { setProjectAnnotations } from '@storybook/web-components';
15
+ * import projectAnnotations from './.storybook/preview';
16
+ *
17
+ * setProjectAnnotations(projectAnnotations);
18
+ * ```
19
+ *
20
+ * @param projectAnnotations - E.g. (import projectAnnotations from '../.storybook/preview')
21
+ */
22
+ export declare function setProjectAnnotations(projectAnnotations: NamedOrDefaultProjectAnnotations<any> | NamedOrDefaultProjectAnnotations<any>[]): NormalizedProjectAnnotations<StencilRenderer<unknown>>;
@@ -0,0 +1,25 @@
1
+ import { setProjectAnnotations as originalSetProjectAnnotations, setDefaultProjectAnnotations, } from '@storybook/preview-api';
2
+ import * as stencilAnnotations from './preview';
3
+ /**
4
+ * Function that sets the globalConfig of your storybook. The global config is the preview module of
5
+ * your .storybook folder.
6
+ *
7
+ * It should be run a single time, so that your global config (e.g. decorators) is applied to your
8
+ * stories when using `composeStories` or `composeStory`.
9
+ *
10
+ * Example:
11
+ *
12
+ * ```jsx
13
+ * // setup-file.js
14
+ * import { setProjectAnnotations } from '@storybook/web-components';
15
+ * import projectAnnotations from './.storybook/preview';
16
+ *
17
+ * setProjectAnnotations(projectAnnotations);
18
+ * ```
19
+ *
20
+ * @param projectAnnotations - E.g. (import projectAnnotations from '../.storybook/preview')
21
+ */
22
+ export function setProjectAnnotations(projectAnnotations) {
23
+ setDefaultProjectAnnotations(stencilAnnotations);
24
+ return originalSetProjectAnnotations(projectAnnotations);
25
+ }
@@ -0,0 +1,4 @@
1
+ import { StorybookConfig } from './types.js';
2
+ export declare const core: StorybookConfig['core'];
3
+ export declare const viteFinal: StorybookConfig["viteFinal"];
4
+ export declare const previewAnnotations: StorybookConfig["previewAnnotations"];
package/dist/preset.js ADDED
@@ -0,0 +1,33 @@
1
+ import { dirname, join, resolve } from "node:path";
2
+ import { createRequire } from "node:module";
3
+ import { mergeConfig } from 'vite';
4
+ import stencil from 'unplugin-stencil/vite';
5
+ const require = createRequire(import.meta.url);
6
+ const getAbsolutePath = (input) => dirname(require.resolve(join(input, 'package.json')));
7
+ const renderer = join(__dirname, 'preview.js');
8
+ export const core = {
9
+ builder: getAbsolutePath('@storybook/builder-vite'),
10
+ renderer,
11
+ };
12
+ export const viteFinal = async (defaultConfig) => {
13
+ const config = mergeConfig(defaultConfig, {
14
+ build: {
15
+ target: "es2020",
16
+ rollupOptions: {
17
+ external: ['@stencil/core'],
18
+ },
19
+ },
20
+ plugins: [stencil({
21
+ rootPath: defaultConfig.root,
22
+ })],
23
+ });
24
+ return config;
25
+ };
26
+ export const previewAnnotations = async (input = [], options) => {
27
+ const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
28
+ const result = [];
29
+ return result
30
+ .concat(input)
31
+ .concat([renderer])
32
+ .concat(docsEnabled ? [resolve(getAbsolutePath('@storybook/html'), 'dist', 'entry-preview-docs.mjs')] : []);
33
+ };
@@ -0,0 +1,4 @@
1
+ export declare const parameters: {
2
+ renderer: 'stencil';
3
+ };
4
+ export { render, renderToCanvas } from './render.js';
@@ -0,0 +1,2 @@
1
+ export const parameters = { renderer: 'stencil' };
2
+ export { render, renderToCanvas } from './render.js';
@@ -0,0 +1,4 @@
1
+ import { ArgsStoryFn, RenderContext } from '@storybook/types';
2
+ import type { StencilRenderer } from './types';
3
+ export declare const render: ArgsStoryFn<StencilRenderer<unknown>>;
4
+ export declare function renderToCanvas({ storyFn, showMain }: RenderContext<StencilRenderer<unknown>>, canvasElement: StencilRenderer<unknown>['canvasElement']): void;
package/dist/render.js ADDED
@@ -0,0 +1,25 @@
1
+ import { simulatePageLoad } from '@storybook/preview-api';
2
+ import { render as renderStencil } from '@stencil/core';
3
+ import { componentToJSX } from './component-to-jsx';
4
+ export const render = (args, context) => {
5
+ const { component } = context;
6
+ if (Array.isArray(component)) {
7
+ throw new Error('If your story does not contain a render function, you must provide a component property!');
8
+ }
9
+ const cmpName = customElements.getName(component);
10
+ if (!cmpName) {
11
+ throw new Error('Component is not registered!');
12
+ }
13
+ return componentToJSX(cmpName, args);
14
+ };
15
+ export function renderToCanvas({ storyFn, showMain }, canvasElement) {
16
+ const vdom = storyFn();
17
+ showMain();
18
+ if (canvasElement.firstChild) {
19
+ canvasElement.removeChild(canvasElement.firstChild);
20
+ }
21
+ const element = document.createElement('div');
22
+ canvasElement.appendChild(element);
23
+ renderStencil(vdom, element);
24
+ simulatePageLoad(element);
25
+ }
@@ -0,0 +1,54 @@
1
+ import { VNode } from '@stencil/core';
2
+ import { StorybookConfigVite } from '@storybook/builder-vite';
3
+ import { WebRenderer } from '@storybook/types';
4
+ export type { Args, ArgTypes, Parameters, StrictArgs } from '@storybook/types';
5
+ import type { AnnotatedStoryFn, Args, ComponentAnnotations, StoryAnnotations, DecoratorFunction, LoaderFunction, StoryContext as GenericStoryContext, StrictArgs, ProjectAnnotations, StorybookConfig as StorybookConfigBase } from '@storybook/types';
6
+ interface DevJSX {
7
+ fileName: string;
8
+ lineNumber: number;
9
+ columnNumber: number;
10
+ stack?: string;
11
+ }
12
+ type JSXChildren = string | number | boolean | null | undefined | Function | RegExp | JSXChildren[] | Promise<JSXChildren> | VNode;
13
+ type ComponentChildren<PROPS> = PROPS extends {
14
+ children: any;
15
+ } ? never : {
16
+ children?: JSXChildren;
17
+ };
18
+ type PublicProps<PROPS> = (PROPS extends Record<any, any> ? Omit<PROPS, `${string}$`> : unknown extends PROPS ? {} : PROPS) & ComponentChildren<PROPS>;
19
+ type FunctionComponent<P = unknown> = {
20
+ renderFn(props: P, key: string | null, flags: number, dev?: DevJSX): VNode;
21
+ }['renderFn'];
22
+ type Component<PROPS = unknown> = FunctionComponent<PublicProps<PROPS>>;
23
+ export interface StencilRenderer<T> extends WebRenderer {
24
+ component: Component<T> | any;
25
+ storyResult: ReturnType<Component<T>>;
26
+ args: T;
27
+ }
28
+ export type Preview = ProjectAnnotations<StencilRenderer<unknown>>;
29
+ /**
30
+ * Metadata to configure the stories for a component.
31
+ *
32
+ * @see [Default export](https://storybook.js.org/docs/formats/component-story-format/#default-export)
33
+ */
34
+ export type Meta<TArgs = Args> = ComponentAnnotations<StencilRenderer<TArgs>, TArgs>;
35
+ /**
36
+ * Story function that represents a CSFv2 component example.
37
+ *
38
+ * @see [Named Story exports](https://storybook.js.org/docs/formats/component-story-format/#named-story-exports)
39
+ */
40
+ export type StoryFn<TArgs = Args> = AnnotatedStoryFn<StencilRenderer<TArgs>, TArgs>;
41
+ /**
42
+ * Story function that represents a CSFv3 component example.
43
+ *
44
+ * @see [Named Story exports](https://storybook.js.org/docs/formats/component-story-format/#named-story-exports)
45
+ */
46
+ export type StoryObj<TArgs = Args> = StoryAnnotations<StencilRenderer<TArgs>, TArgs>;
47
+ export type Decorator<TArgs = StrictArgs> = DecoratorFunction<StencilRenderer<TArgs>, TArgs>;
48
+ export type Loader<TArgs = StrictArgs> = LoaderFunction<StencilRenderer<TArgs>, TArgs>;
49
+ export type StoryContext<TArgs = StrictArgs> = GenericStoryContext<StencilRenderer<TArgs>, TArgs>;
50
+ export type StorybookConfig = Omit<StorybookConfigBase, 'framework'> & {
51
+ framework: '@stencil/storybook-plugin' | {
52
+ name: '@stencil/storybook-plugin';
53
+ };
54
+ } & StorybookConfigVite;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stencil/storybook-plugin",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Storybook plugin for Stencil",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/stenciljs/storybook",
@@ -25,7 +25,9 @@
25
25
  "module": "dist/index.js",
26
26
  "types": "dist/index.d.ts",
27
27
  "files": [
28
- "dist"
28
+ "dist",
29
+ "template",
30
+ "preset.js"
29
31
  ],
30
32
  "//": "not sure why dist/preview is needed, but it is",
31
33
  "exports": {
package/preset.js ADDED
@@ -0,0 +1,2 @@
1
+ // This file seems to only be necessary for Windows
2
+ module.exports = require("./dist/preset.js");
@@ -0,0 +1,3 @@
1
+ :host {
2
+ display: block;
3
+ }
@@ -0,0 +1,28 @@
1
+ import type { Meta, StoryObj } from '@stencil/storybook-plugin';
2
+ import { MyComponent } from './MyComponent';
3
+
4
+ const meta = {
5
+ title: 'MyComponent',
6
+ component: MyComponent,
7
+ parameters: {
8
+ layout: 'centered',
9
+ },
10
+ tags: ['autodocs'],
11
+ argTypes: {
12
+ first: { control: 'text' },
13
+ last: { control: 'text' },
14
+ middle: { control: 'text' },
15
+ },
16
+ args: { first: 'John', last: 'Doe', middle: 'Michael' },
17
+ } satisfies Meta<MyComponent>;
18
+
19
+ export default meta;
20
+ type Story = StoryObj<MyComponent>;
21
+
22
+ export const Primary: Story = {
23
+ args: {
24
+ first: 'John',
25
+ last: 'Doe',
26
+ middle: 'Michael',
27
+ }
28
+ };
@@ -0,0 +1,35 @@
1
+ import { Component, Prop, h } from '@stencil/core';
2
+
3
+ @Component({
4
+ tag: 'my-component',
5
+ styleUrl: 'my-component.css',
6
+ shadow: true,
7
+ })
8
+ export class MyComponent {
9
+ /**
10
+ * The first name
11
+ */
12
+ @Prop() first: string;
13
+
14
+ /**
15
+ * The middle name
16
+ */
17
+ @Prop() middle: string;
18
+
19
+ /**
20
+ * The last name
21
+ */
22
+ @Prop() last: string;
23
+
24
+ private getText(): string {
25
+ return format(this.first, this.middle, this.last);
26
+ }
27
+
28
+ render() {
29
+ return <div>Hello, World! I'm {this.getText()}</div>;
30
+ }
31
+ }
32
+
33
+ function format(first?: string, middle?: string, last?: string): string {
34
+ return (first || '') + (middle ? ` ${middle}` : '') + (last ? ` ${last}` : '');
35
+ }