Back to Storybook

List Story Template

docs/_snippets/list-story-template.md

10.3.615.2 KB
Original Source
ts
import { type Meta, type StoryObj, moduleMetadata } from '@storybook/angular';

import { CommonModule } from '@angular/common';

import { List } from './list.component';
import { ListItem } from './list-item.component';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta: Meta<List> = {
  component: List,
  decorators: [
    moduleMetadata({
      declarations: [List, ListItem],
      imports: [CommonModule],
    }),
  ],
};

export default meta;
type Story = StoryObj<List>;

/*
 *๐Ÿ‘‡ Render functions are a framework specific feature to allow you control on how the component renders.
 * See https://storybook.js.org/docs/api/csf
 * to learn how to use render functions.
 */
const ListTemplate: Story = {
  render: (args) => ({
    props: args,
    template: `
      <app-list>
        <div *ngFor="let item of items">
          <app-list-item [item]="item"></app-list-item>
        </div>
      </app-list>
    `,
  }),
};

export const Empty: Story = {
  ...ListTemplate,
  args: { items: [] },
};

export const OneItem: Story = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
ts
import { CommonModule } from '@angular/common';

import { moduleMetadata } from '@storybook/angular';

import preview from '../.storybook/preview';

import { List } from './list.component';
import { ListItem } from './list-item.component';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = preview.meta({
  component: List,
  decorators: [
    moduleMetadata({
      declarations: [List, ListItem],
      imports: [CommonModule],
    }),
  ],
});

/*
 *๐Ÿ‘‡ Render functions are a framework specific feature to allow you control on how the component renders.
 * See https://storybook.js.org/docs/api/csf
 * to learn how to use render functions.
 */
const ListTemplate = {
  render: (args) => ({
    props: args,
    template: `
      <app-list>
        <div *ngFor="let item of items">
          <app-list-item [item]="item"></app-list-item>
        </div>
      </app-list>
    `,
  }),
};

export const Empty = meta.story({
  ...ListTemplate,
  args: { items: [] },
});

export const OneItem = Empty.extend({
  args: {
    items: [{ ...Unchecked.input.args }],
  },
});
jsx
import { List } from './List';
import { ListItem } from './ListItem';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

export default {
  component: List,
};

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
const ListTemplate = {
  render: ({ items, ...args }) => {
    return (
      <List>
        {items.map((item) => (
          <ListItem {...item} />
        ))}
      </List>
    );
  },
};

export const Empty = {
  ...ListTemplate,
  args: {
    items: [],
  },
};

export const OneItem = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
tsx
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';

import { List } from './List';
import { ListItem } from './ListItem';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = {
  component: List,
} satisfies Meta<typeof List>;

export default meta;
type Story = StoryObj<typeof meta>;

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
const ListTemplate: Story = {
  render: ({ items, ...args }) => {
    return (
      <List>
        {items.map((item) => (
          <ListItem {...item} />
        ))}
      </List>
    );
  },
};

export const Empty = {
  ...ListTemplate,
  args: {
    items: [],
  },
};

export const OneItem = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
jsx
import { List } from './List';
import { ListItem } from './ListItem';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

export default {
  component: List,
};

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
const ListTemplate = {
  render: ({ items, ...args }) => {
    return (
      <List>
        {items.map((item) => (
          <ListItem {...item} />
        ))}
      </List>
    );
  },
};

export const Empty = {
  ...ListTemplate,
  args: {
    items: [],
  },
};

export const OneItem = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
tsx
import type { Meta, StoryObj } from 'storybook-solidjs-vite';

import { List } from './List';
import { ListItem } from './ListItem';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = {
  component: List,
} satisfies Meta<typeof List>;

export default meta;
type Story = StoryObj<typeof meta>;

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
const ListTemplate: Story = {
  render: ({ items, ...args }) => {
    return (
      <List>
        {items.map((item) => (
          <ListItem {...item} />
        ))}
      </List>
    );
  },
};

export const Empty = {
  ...ListTemplate,
  args: {
    items: [],
  },
};

export const OneItem = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
svelte
<script module>
  import { defineMeta } from '@storybook/addon-svelte-csf';

  import List from './List.svelte';
  import ListItem from './ListItem.svelte';

  //๐Ÿ‘‡ Imports a specific story from ListItem stories
  import { Unchecked } from './ListItem.stories.svelte';

  const { Story } = defineMeta({
    component: List,
  });
</script>

<!--
  The template construct will be spread to the existing stories.
  It's based on Svelte's snippet syntax allowing you share the same UI with small variations.
-->
{#snippet template(args)}
  <List {...args}>
    {#each args.items as item}
      <ListItem {...item} />
    {/each}
  </List>
{/snippet}

<Story name="Empty" args={{ items: [] }} {template} />

<Story
  name="One Item"
  args={{
    items: [{ ...Unchecked.args }],
  }}
  {template}
/>
svelte
<script module>
  import { defineMeta } from '@storybook/addon-svelte-csf';

  import List from './List.svelte';
  import ListItem from './ListItem.svelte';

  //๐Ÿ‘‡ Imports a specific story from ListItem stories
  import { Unchecked } from './ListItem.stories.svelte';

  const { Story } = defineMeta({
    component: List,
  });
</script>

<!--
  The template construct will be spread to the existing stories.
  It's based on Svelte's snippet syntax allowing you share the same UI with small variations.
-->
{#snippet template(args)}
  <List {...args}>
    {#each args.items as item}
      <ListItem {...item} />
    {/each}
  </List>
{/snippet}

<Story name="Empty" args={{ items: [] }} {template} />

<Story
  name="One Item"
  args={{
    items: [{ ...Unchecked.args }],
  }}
  {template}
/>
js
import List from './List.vue';
import ListItem from './ListItem.vue';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

export default {
  component: List,
};

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
const ListTemplate = {
  render: (args) => ({
    components: { List, ListItem },
    setup() {
      return { ...args };
    },
    template: `
      <List v-bind="args">
        <div v-for="item in items" :key="item.title">
          <ListItem :item="item"/>
        </div>
      </List>
    `,
  }),
};

export const Empty = {
  ...ListTemplate,
  args: {
    items: [],
  },
};
export const OneItem = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
ts
import type { Meta, StoryObj } from '@storybook/vue3-vite';

import List from './List.vue';
import ListItem from './ListItem.vue';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = {
  component: List,
} satisfies Meta<typeof List>;

export default meta;
type Story = StoryObj<typeof meta>;

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
export const ListTemplate: Story = {
  render: (args) => ({
    components: { List, ListItem },
    setup() {
      return { ...args };
    },
    template: `
      <List v-bind="args">
        <div v-for="item in items" :key="item.title">
          <ListItem :item="item"/>
        </div>
      </List>
    `,
  }),
};

export const Empty: Story = {
  ...ListTemplate,
  args: {
    items: [],
  },
};
export const OneItem: Story = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
ts
import preview from '../.storybook/preview';

import List from './List.vue';
import ListItem from './ListItem.vue';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = preview.meta({
  component: List,
});

export const Empty = meta.story({
  render: (args) => ({
    components: { List, ListItem },
    setup() {
      return { ...args };
    },
    template: `
      <List v-bind="args">
        <div v-for="item in items" :key="item.title">
          <ListItem :item="item"/>
        </div>
      </List>
    `,
  }),
  args: {
    items: [],
  },
});

export const OneItem = Empty.extend({
  args: {
    items: [{ ...Unchecked.input.args }],
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import preview from '../.storybook/preview';

import List from './List.vue';
import ListItem from './ListItem.vue';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = preview.meta({
  component: List,
});

export const Empty = meta.story({
  render: (args) => ({
    components: { List, ListItem },
    setup() {
      return { ...args };
    },
    template: `
      <List v-bind="args">
        <div v-for="item in items" :key="item.title">
          <ListItem :item="item"/>
        </div>
      </List>
    `,
  }),
  args: {
    items: [],
  },
});

export const OneItem = Empty.extend({
  args: {
    items: [{ ...Unchecked.input.args }],
  },
});
js
import { html } from 'lit';
import { repeat } from 'lit/directives/repeat.js';

import { Unchecked } from './ListItem.stories';

export default {
  component: 'demo-list',
};

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
const ListTemplate = {
  render: ({ items, ...args }) => {
    return html`
      <demo-list>
        ${repeat(items, (item) => html`<demo-list-item>${item}</demo-list-item>`)}
      </demo-list>
    `;
  },
};
export const Empty = {
  ...ListTemplate,
  args: {
    items: [],
  },
};

export const OneItem = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
ts
import type { Meta, StoryObj } from '@storybook/web-components-vite';

import { html } from 'lit';
import { repeat } from 'lit/directives/repeat.js';

const meta: Meta = {
  component: 'demo-list',
};

export default meta;
type Story = StoryObj;

//๐Ÿ‘‡ The ListTemplate construct will be spread to the existing stories.
const ListTemplate = {
  render: ({ items, ...args }) => {
    return html`
      <demo-list>
        ${repeat(items, (item) => html`<demo-list-item>${item}</demo-list-item>`)}
      </demo-list>
    `;
  },
};

export const Empty: Story = {
  ...ListTemplate,
  args: {
    items: [],
  },
};

export const OneItem: Story = {
  ...ListTemplate,
  args: {
    items: [{ ...Unchecked.args }],
  },
};
js
import { html } from 'lit';
import { repeat } from 'lit/directives/repeat.js';

import preview from '../.storybook/preview';

import { Unchecked } from './ListItem.stories';

const meta = preview.meta({
  component: 'demo-list',
});

export const Empty = meta.story({
  render: ({ items, ...args }) => {
    return html`
      <demo-list>
        ${repeat(items, (item) => html`<demo-list-item>${item}</demo-list-item>`)}
      </demo-list>
    `;
  },
  args: {
    items: [],
  },
});

export const OneItem = Empty.extend({
  args: {
    items: [{ ...Unchecked.input.args }],
  },
});
ts
import { html } from 'lit';
import { repeat } from 'lit/directives/repeat.js';

import preview from '../.storybook/preview';

import { Unchecked } from './ListItem.stories';

const meta = preview.meta({
  component: 'demo-list',
});

export const Empty = meta.story({
  render: ({ items, ...args }) => {
    return html`
      <demo-list>
        ${repeat(items, (item) => html`<demo-list-item>${item}</demo-list-item>`)}
      </demo-list>
    `;
  },
  args: {
    items: [],
  },
});

export const OneItem = Empty.extend({
  args: {
    items: [{ ...Unchecked.input.args }],
  },
});
tsx
import preview from '../.storybook/preview';

import { List } from './List';
import { ListItem } from './ListItem';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = preview.meta({
  component: List,
});

export const Empty = meta.story({
  render: ({ items, ...args }) => {
    return (
      <List>
        {items.map((item) => (
          <ListItem {...item} />
        ))}
      </List>
    );
  },
  args: {
    items: [],
  },
});

export const OneItem = Empty.extend({
  args: {
    items: [{ ...Unchecked.input.args }],
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
jsx
import preview from '../.storybook/preview';

import { List } from './List';
import { ListItem } from './ListItem';

//๐Ÿ‘‡ Imports a specific story from ListItem stories
import { Unchecked } from './ListItem.stories';

const meta = preview.meta({
  component: List,
});

export const Empty = meta.story({
  render: ({ items, ...args }) => {
    return (
      <List>
        {items.map((item) => (
          <ListItem {...item} />
        ))}
      </List>
    );
  },
  args: {
    items: [],
  },
});

export const OneItem = Empty.extend({
  args: {
    items: [{ ...Unchecked.input.args }],
  },
});