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
148 changes: 139 additions & 9 deletions docs/_snippets/decorator-parameterized-in-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const preview: Preview = {
const { pageLayout } = parameters;
switch (pageLayout) {
case 'page':
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
return `<div class="page-layout">${story}</div>`;
case 'page-mobile':
return `<div class="page-mobile-layout">${story}</div>`;
Expand All @@ -37,7 +37,7 @@ export default {
switch (pageLayout) {
case 'page':
return (
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
<div className="page-layout">
<Story />
</div>
Expand All @@ -60,7 +60,7 @@ export default {
```tsx filename=".storybook/preview.tsx" renderer="react" language="ts" tabTitle="CSF 3"
import React from 'react';

// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
// Replace your-framework with the framework you are using (e.g., react-vite, nextjs, nextjs-vite)
import type { Preview } from '@storybook/your-framework';

const preview: Preview = {
Expand All @@ -72,7 +72,7 @@ const preview: Preview = {
switch (pageLayout) {
case 'page':
return (
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
<div className="page-layout">
<Story />
</div>
Expand Down Expand Up @@ -104,7 +104,7 @@ export default {
switch (pageLayout) {
case 'page':
return (
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
<div className="page-layout">
<Story />
</div>
Expand Down Expand Up @@ -136,7 +136,7 @@ const preview: Preview = {
switch (pageLayout) {
case 'page':
return (
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
<div className="page-layout">
<Story />
</div>
Expand Down Expand Up @@ -167,7 +167,7 @@ export default {
const { pageLayout } = parameters;
switch (pageLayout) {
case 'page':
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
return { template: '<div class="page-layout"><story/></div>' };
case 'page-mobile':
return { template: '<div class="page-mobile-layout"><story/></div>' };
Expand Down Expand Up @@ -206,6 +206,136 @@ const preview: Preview = {
export default preview;
```

```ts filename=".storybook/preview.ts" renderer="svelte" language="ts" tabTitle="Preview"
// Replace your-framework with svelte-vite or sveltekit
import type { Preview } from '@storybook/your-framework';

import PageLayout from './PageLayout.svelte';

const preview: Preview = {
decorators: [
// 👇 Defining the decorator in the preview file applies it to all stories
(story, { parameters }) => {
// 👇 Make it configurable by reading from parameters
const { pageLayout } = parameters;
return {
Component: PageLayout,
props: {
layout: pageLayout || 'default',
children: story,
},
};
},
],
};

export default preview;
```

```svelte filename=".storybook/PageLayout.svelte" renderer="svelte" language="ts" tabTitle="Layout component"
<script lang="ts">
interface Props {
layout?: 'page' | 'page-mobile' | 'default';
children?: import('svelte').Snippet;
}

let { layout = 'default', children }: Props = $props();
</script>

<!-- Your page layout is probably a little more complex than this -->
<div class={layout}>
{@render children?.()}
</div>
```

```js filename=".storybook/preview.js" renderer="svelte" language="js" tabTitle="Preview"
import PageLayout from './PageLayout.svelte';

const preview = {
decorators: [
// 👇 Defining the decorator in the preview file applies it to all stories
(story, { parameters }) => {
// 👇 Make it configurable by reading from parameters
const { pageLayout } = parameters;
return {
Component: PageLayout,
props: {
layout: pageLayout || 'default',
children: story,
},
};
},
],
};

export default preview;
```

```svelte filename=".storybook/PageLayout.svelte" renderer="svelte" language="js" tabTitle="Layout component"
<script>
let { layout = 'default', children } = $props();
</script>

<!-- Your page layout is probably a little more complex than this -->
<div class={layout}>
{@render children?.()}
</div>
```

```ts filename=".storybook/preview.ts" renderer="web-components" language="ts"
import type { Preview } from '@storybook/web-components-vite';

import { html } from 'lit';

const preview: Preview = {
decorators: [
// 👇 Defining the decorator in the preview file applies it to all stories
(story, { parameters }) => {
// 👇 Make it configurable by reading from parameters
const { pageLayout } = parameters;
switch (pageLayout) {
case 'page':
// Your page layout is probably a little more complex than this
return html`<div class="page-layout">${story()}</div>`;
case 'page-mobile':
return html`<div class="page-mobile-layout">${story()}</div>`;
default:
// In the default case, don't apply a layout
return story();
}
},
],
};

export default preview;
```

```js filename=".storybook/preview.js" renderer="web-components" language="js" tabTitle="Preview"
import { html } from 'lit';

const preview = {
decorators: [
// 👇 Defining the decorator in the preview file applies it to all stories
(story, { parameters }) => {
// 👇 Make it configurable by reading from parameters
const { pageLayout } = parameters;
switch (pageLayout) {
case 'page':
// Your page layout is probably a little more complex than this
return html`<div class="page-layout">${story()}</div>`;
case 'page-mobile':
return html`<div class="page-mobile-layout">${story()}</div>`;
default:
// In the default case, don't apply a layout
return story();
}
},
],
};

export default preview;
```

```tsx filename=".storybook/preview.tsx" renderer="react" language="ts" tabTitle="CSF Next 🧪"
import React from 'react';

Expand All @@ -221,7 +351,7 @@ export default definePreview({
switch (pageLayout) {
case 'page':
return (
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
<div className="page-layout">
<Story />
</div>
Expand Down Expand Up @@ -258,7 +388,7 @@ export default definePreview({
switch (pageLayout) {
case 'page':
return (
// Your page layout is probably a little more complex than this ;)
// Your page layout is probably a little more complex than this
<div className="page-layout">
<Story />
</div>
Expand Down
Loading