mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-07 07:21:17 +08:00
646 lines
16 KiB
Markdown
646 lines
16 KiB
Markdown
```ts filename="Button.stories.ts" renderer="angular" language="ts"
|
|
import type { Meta, StoryObj } from '@storybook/angular';
|
|
|
|
import { Button } from './button.component';
|
|
|
|
const meta: Meta<Button> = {
|
|
component: Button,
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj<Button>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => ({
|
|
props: {
|
|
label: 'Button',
|
|
primary: true,
|
|
},
|
|
}),
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js" renderer="html" language="js"
|
|
export default {
|
|
/* 👇 The title prop is optional.
|
|
* See https://storybook.js.org/docs/configure/#configure-story-loading
|
|
* to learn how to generate automatic titles
|
|
*/
|
|
title: 'Button',
|
|
};
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary = {
|
|
render: () => {
|
|
const btn = document.createElement('button');
|
|
btn.innerText = 'Button';
|
|
|
|
btn.className = [
|
|
'storybook-button',
|
|
'storybook-button--medium',
|
|
'storybook-button--primary',
|
|
].join(' ');
|
|
|
|
return btn;
|
|
},
|
|
};
|
|
```
|
|
|
|
```ts filename="Button.stories.ts" renderer="html" language="ts"
|
|
import type { Meta, StoryObj } from '@storybook/html';
|
|
|
|
const meta: Meta = {
|
|
/* 👇 The title prop is optional.
|
|
* See https://storybook.js.org/docs/configure/#configure-story-loading
|
|
* to learn how to generate automatic titles
|
|
*/
|
|
title: 'Button',
|
|
};
|
|
|
|
export default meta;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: StoryObj = {
|
|
render: () => {
|
|
const btn = document.createElement('button');
|
|
btn.innerText = 'Button';
|
|
|
|
btn.className = [
|
|
'storybook-button',
|
|
'storybook-button--medium',
|
|
'storybook-button--primary',
|
|
].join(' ');
|
|
|
|
return btn;
|
|
},
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js|jsx" renderer="preact" language="js"
|
|
/** @jsx h */
|
|
import { h } from 'preact';
|
|
|
|
import { Button } from './Button';
|
|
|
|
export default {
|
|
component: Button,
|
|
};
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary = {
|
|
render: () => <Button primary label="Button" />,
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js|jsx" renderer="react" language="js"
|
|
import { Button } from './Button';
|
|
|
|
export default {
|
|
component: Button,
|
|
};
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary = {
|
|
render: () => <Button primary label="Button" />,
|
|
};
|
|
```
|
|
|
|
```ts filename="Button.stories.ts|tsx" renderer="react" language="ts-4-9"
|
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta = {
|
|
component: Button,
|
|
} satisfies Meta<typeof Button>;
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => <Button primary label="Button" />,
|
|
};
|
|
```
|
|
|
|
```ts filename="Button.stories.ts|tsx" renderer="react" language="ts"
|
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta: Meta<typeof Button> = {
|
|
component: Button,
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof Button>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => <Button primary label="Button" />,
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js|jsx" renderer="react" language="js" tabTitle="with-hooks"
|
|
import React, { useState } from 'react';
|
|
|
|
import { Button } from './Button';
|
|
|
|
export default {
|
|
component: Button,
|
|
};
|
|
|
|
/*
|
|
* Example Button story with React Hooks.
|
|
* See note below related to this example.
|
|
*/
|
|
const ButtonWithHooks = () => {
|
|
// Sets the hooks for both the label and primary props
|
|
const [value, setValue] = useState('Secondary');
|
|
const [isPrimary, setIsPrimary] = useState(false);
|
|
|
|
// Sets a click handler to change the label's value
|
|
const handleOnChange = () => {
|
|
if (!isPrimary) {
|
|
setIsPrimary(true);
|
|
setValue('Primary');
|
|
}
|
|
};
|
|
return <Button primary={isPrimary} onClick={handleOnChange} label={value} />;
|
|
};
|
|
|
|
export const Primary = {
|
|
render: () => <ButtonWithHooks />,
|
|
};
|
|
```
|
|
|
|
```tsx filename="Button.stories.ts|tsx" renderer="react" language="ts-4-9" tabTitle="with-hooks"
|
|
import React, { useState } from 'react';
|
|
|
|
import { Meta, StoryObj } from '@storybook/react';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta = {
|
|
component: Button,
|
|
} satisfies Meta<typeof Button>;
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
/*
|
|
* Example Button story with React Hooks.
|
|
* See note below related to this example.
|
|
*/
|
|
const ButtonWithHooks = () => {
|
|
// Sets the hooks for both the label and primary props
|
|
const [value, setValue] = useState('Secondary');
|
|
const [isPrimary, setIsPrimary] = useState(false);
|
|
|
|
// Sets a click handler to change the label's value
|
|
const handleOnChange = () => {
|
|
if (!isPrimary) {
|
|
setIsPrimary(true);
|
|
setValue('Primary');
|
|
}
|
|
};
|
|
return <Button primary={isPrimary} onClick={handleOnChange} label={value} />;
|
|
};
|
|
|
|
export const Primary = {
|
|
render: () => <ButtonWithHooks />,
|
|
} satisfies Story;
|
|
```
|
|
|
|
```tsx filename="Button.stories.ts|tsx" renderer="react" language="ts" tabTitle="with-hooks"
|
|
import React, { useState } from 'react';
|
|
|
|
import { Meta, StoryObj } from '@storybook/react';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta: Meta<typeof Button> = {
|
|
component: Button,
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof Button>;
|
|
|
|
/*
|
|
* Example Button story with React Hooks.
|
|
* See note below related to this example.
|
|
*/
|
|
const ButtonWithHooks = () => {
|
|
// Sets the hooks for both the label and primary props
|
|
const [value, setValue] = useState('Secondary');
|
|
const [isPrimary, setIsPrimary] = useState(false);
|
|
|
|
// Sets a click handler to change the label's value
|
|
const handleOnChange = () => {
|
|
if (!isPrimary) {
|
|
setIsPrimary(true);
|
|
setValue('Primary');
|
|
}
|
|
};
|
|
return <Button primary={isPrimary} onClick={handleOnChange} label={value} />;
|
|
};
|
|
|
|
export const Primary: Story = {
|
|
render: () => <ButtonWithHooks />,
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js|jsx" renderer="solid" language="js"
|
|
import { Button } from './Button';
|
|
|
|
export default {
|
|
component: Button,
|
|
};
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary = {
|
|
render: () => <Button primary label="Button" />,
|
|
};
|
|
```
|
|
|
|
```tsx filename="Button.stories.ts|tsx" renderer="solid" language="ts-4-9"
|
|
import type { Meta, StoryObj } from 'storybook-solidjs';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta = {
|
|
component: Button,
|
|
} satisfies Meta<typeof Button>;
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => <Button primary label="Button" />,
|
|
};
|
|
```
|
|
|
|
```tsx filename="Button.stories.ts|tsx" renderer="solid" language="ts"
|
|
import type { Meta, StoryObj } from 'storybook-solidjs';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta: Meta<typeof Button> = {
|
|
component: Button,
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof Button>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => <Button primary label="Button" />,
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js|jsx" renderer="solid" language="js" tabTitle="with-hooks"
|
|
import { createSignal } from 'solid-js';
|
|
|
|
import { Button } from './Button';
|
|
|
|
export default {
|
|
component: Button,
|
|
};
|
|
|
|
/*
|
|
* Example Button story with Solid Hooks.
|
|
* See note below related to this example.
|
|
*/
|
|
const ButtonWithHooks = () => {
|
|
// Sets the hooks for both the label and primary props
|
|
const [value, setValue] = createSignal('Secondary');
|
|
const [isPrimary, setIsPrimary] = createSignal(false);
|
|
|
|
// Sets a click handler to change the label's value
|
|
const handleOnChange = () => {
|
|
if (!isPrimary()) {
|
|
setIsPrimary(true);
|
|
setValue('Primary');
|
|
}
|
|
};
|
|
return <Button primary={isPrimary()} onClick={handleOnChange} label={value()} />;
|
|
};
|
|
|
|
export const Primary = {
|
|
render: () => <ButtonWithHooks />,
|
|
};
|
|
```
|
|
|
|
```tsx filename="Button.stories.ts|tsx" renderer="solid" language="ts-4-9" tabTitle="with-hooks"
|
|
import type { Meta, StoryObj } from 'storybook-solidjs';
|
|
|
|
import { createSignal } from 'solid-js';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta = {
|
|
component: Button,
|
|
} satisfies Meta<typeof Button>;
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
/*
|
|
* Example Button story with Solid Hooks.
|
|
* See note below related to this example.
|
|
*/
|
|
const ButtonWithHooks = () => {
|
|
// Sets the hooks for both the label and primary props
|
|
const [value, setValue] = createSignal('Secondary');
|
|
const [isPrimary, setIsPrimary] = createSignal(false);
|
|
|
|
// Sets a click handler to change the label's value
|
|
const handleOnChange = () => {
|
|
if (!isPrimary()) {
|
|
setIsPrimary(true);
|
|
setValue('Primary');
|
|
}
|
|
};
|
|
return <Button primary={isPrimary()} onClick={handleOnChange} label={value()} />;
|
|
};
|
|
|
|
export const Primary = {
|
|
render: () => <ButtonWithHooks />,
|
|
} satisfies Story;
|
|
```
|
|
|
|
```tsx filename="Button.stories.ts|tsx" renderer="solid" language="ts" tabTitle="with-hooks"
|
|
import type { Meta, StoryObj } from 'storybook-solidjs';
|
|
|
|
import { createSignal } from 'solid-js';
|
|
|
|
import { Button } from './Button';
|
|
|
|
const meta: Meta<typeof Button> = {
|
|
component: Button,
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof Button>;
|
|
|
|
/*
|
|
* Example Button story with Solid Hooks.
|
|
* See note below related to this example.
|
|
*/
|
|
const ButtonWithHooks = () => {
|
|
// Sets the hooks for both the label and primary props
|
|
const [value, setValue] = createSignal('Secondary');
|
|
const [isPrimary, setIsPrimary] = createSignal(false);
|
|
|
|
// Sets a click handler to change the label's value
|
|
const handleOnChange = () => {
|
|
if (!isPrimary()) {
|
|
setIsPrimary(true);
|
|
setValue('Primary');
|
|
}
|
|
};
|
|
return <Button primary={isPrimary()} onClick={handleOnChange} label={value()} />;
|
|
};
|
|
|
|
export const Primary: Story = {
|
|
render: () => <ButtonWithHooks />,
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js" renderer="svelte" language="js"
|
|
import Button from './Button.svelte';
|
|
|
|
export default {
|
|
component: Button,
|
|
};
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary = {
|
|
render: () => ({
|
|
Component: Button,
|
|
props: {
|
|
primary: true,
|
|
label: 'Button',
|
|
},
|
|
}),
|
|
};
|
|
```
|
|
|
|
```html renderer="svelte" language="ts" tabTitle="native-format"
|
|
{/* Button.stories.svelte */}
|
|
|
|
<script>
|
|
import { Meta, Story } from '@storybook/addon-svelte-csf';
|
|
|
|
import Button from './Button.svelte';
|
|
</script>
|
|
|
|
<meta title="Button" component="{Button}" />
|
|
|
|
<Story name="Primary">
|
|
<button primary="true" label="Button" />
|
|
</Story>
|
|
```
|
|
|
|
```ts filename="Button.stories.ts" renderer="svelte" language="ts-4-9"
|
|
import type { Meta, StoryObj } from '@storybook/svelte';
|
|
|
|
import Button from './Button.svelte';
|
|
|
|
const meta = {
|
|
component: Button,
|
|
} satisfies Meta<typeof Button>;
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => ({
|
|
Component: Button,
|
|
props: {
|
|
primary: true,
|
|
label: 'Button',
|
|
},
|
|
}),
|
|
};
|
|
```
|
|
|
|
```ts filename="Button.stories.ts" renderer="svelte" language="ts"
|
|
import type { Meta, StoryObj } from '@storybook/svelte';
|
|
|
|
import Button from './Button.svelte';
|
|
|
|
const meta: Meta<typeof Button> = {
|
|
component: Button,
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => ({
|
|
Component: Button,
|
|
props: {
|
|
primary: true,
|
|
label: 'Button',
|
|
},
|
|
}),
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js" renderer="vue" language="js"
|
|
import Button from './Button.vue';
|
|
|
|
export default {
|
|
component: Button,
|
|
};
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary = {
|
|
render: () => ({
|
|
components: { Button },
|
|
template: '<Button primary label="Button" />',
|
|
}),
|
|
};
|
|
```
|
|
|
|
```ts filename="Button.stories.ts" renderer="vue" language="ts-4-9"
|
|
import type { Meta, StoryObj } from '@storybook/vue3';
|
|
|
|
import Button from './Button.vue';
|
|
|
|
const meta = {
|
|
component: Button,
|
|
} satisfies Meta<typeof Button>;
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => ({
|
|
components: { Button },
|
|
template: '<Button primary label="Button" />',
|
|
}),
|
|
};
|
|
```
|
|
|
|
```ts filename="Button.stories.ts" renderer="vue" language="ts"
|
|
import type { Meta, StoryObj } from '@storybook/vue3';
|
|
|
|
import Button from './Button.vue';
|
|
|
|
const meta: Meta<typeof Button> = {
|
|
component: Button,
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof Button>;
|
|
|
|
/*
|
|
*👇 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.
|
|
*/
|
|
export const Primary: Story = {
|
|
render: () => ({
|
|
components: { Button },
|
|
template: '<Button primary label="Button" />',
|
|
}),
|
|
};
|
|
```
|
|
|
|
```js filename="Button.stories.js" renderer="web-components" language="js"
|
|
import { html } from 'lit';
|
|
|
|
export default {
|
|
component: 'demo-button',
|
|
};
|
|
|
|
export const Primary = {
|
|
render: () => html`<demo-button primary></demo-button>`,
|
|
};
|
|
```
|
|
|
|
```ts filename="Button.stories.ts" renderer="web-components" language="ts"
|
|
import type { Meta, StoryObj } from '@storybook/web-components';
|
|
|
|
import { html } from 'lit';
|
|
|
|
const meta: Meta = {
|
|
component: 'demo-button',
|
|
};
|
|
|
|
export default meta;
|
|
type Story = StoryObj;
|
|
|
|
export const Primary: Story = {
|
|
render: () => html`<demo-button primary></demo-button>`,
|
|
};
|
|
```
|
|
|