@storybook/tanstack-react 0.0.0 → 10.4.0-alpha.18

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,54 @@
1
+ import type { Meta, StoryObj } from '@storybook/tanstack-react';
2
+
3
+ import { fn } from 'storybook/test';
4
+
5
+ import { Button } from './Button';
6
+
7
+ // More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
8
+ const meta = {
9
+ title: 'Example/Button',
10
+ component: Button,
11
+ parameters: {
12
+ // Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
13
+ layout: 'centered',
14
+ },
15
+ // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
16
+ tags: ['autodocs'],
17
+ // More on argTypes: https://storybook.js.org/docs/api/argtypes
18
+ argTypes: {
19
+ backgroundColor: { control: 'color' },
20
+ },
21
+ // Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#story-args
22
+ args: { onClick: fn() },
23
+ } satisfies Meta<typeof Button>;
24
+
25
+ export default meta;
26
+ type Story = StoryObj<typeof meta>;
27
+
28
+ // More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
29
+ export const Primary: Story = {
30
+ args: {
31
+ primary: true,
32
+ label: 'Button',
33
+ },
34
+ };
35
+
36
+ export const Secondary: Story = {
37
+ args: {
38
+ label: 'Button',
39
+ },
40
+ };
41
+
42
+ export const Large: Story = {
43
+ args: {
44
+ size: 'large',
45
+ label: 'Button',
46
+ },
47
+ };
48
+
49
+ export const Small: Story = {
50
+ args: {
51
+ size: 'small',
52
+ label: 'Button',
53
+ },
54
+ };
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+ import './button.css';
3
+
4
+ export interface ButtonProps {
5
+ /** Is this the principal call to action on the page? */
6
+ primary?: boolean;
7
+ /** What background color to use */
8
+ backgroundColor?: string;
9
+ /** How large should the button be? */
10
+ size?: 'small' | 'medium' | 'large';
11
+ /** Button contents */
12
+ label: string;
13
+ /** Optional click handler */
14
+ onClick?: () => void;
15
+ }
16
+
17
+ /** Primary UI component for user interaction */
18
+ export const Button = ({
19
+ primary = false,
20
+ size = 'medium',
21
+ backgroundColor,
22
+ label,
23
+ ...props
24
+ }: ButtonProps) => {
25
+ const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
26
+ return (
27
+ <button
28
+ type="button"
29
+ className={['storybook-button', `storybook-button--${size}`, mode].join(' ')}
30
+ style={{ backgroundColor }}
31
+ {...props}
32
+ >
33
+ {label}
34
+ </button>
35
+ );
36
+ };
@@ -0,0 +1,34 @@
1
+ import type { Meta, StoryObj } from '@storybook/tanstack-react';
2
+
3
+ import { fn } from 'storybook/test';
4
+
5
+ import { Header } from './Header';
6
+
7
+ const meta = {
8
+ title: 'Example/Header',
9
+ component: Header,
10
+ // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
11
+ tags: ['autodocs'],
12
+ parameters: {
13
+ // More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
14
+ layout: 'fullscreen',
15
+ },
16
+ args: {
17
+ onLogin: fn(),
18
+ onLogout: fn(),
19
+ onCreateAccount: fn(),
20
+ },
21
+ } satisfies Meta<typeof Header>;
22
+
23
+ export default meta;
24
+ type Story = StoryObj<typeof meta>;
25
+
26
+ export const LoggedIn: Story = {
27
+ args: {
28
+ user: {
29
+ name: 'Jane Doe',
30
+ },
31
+ },
32
+ };
33
+
34
+ export const LoggedOut: Story = {};
@@ -0,0 +1,56 @@
1
+ import React from 'react';
2
+
3
+ import { Button } from './Button';
4
+ import './header.css';
5
+
6
+ type User = {
7
+ name: string;
8
+ };
9
+
10
+ export interface HeaderProps {
11
+ user?: User;
12
+ onLogin?: () => void;
13
+ onLogout?: () => void;
14
+ onCreateAccount?: () => void;
15
+ }
16
+
17
+ export const Header = ({ user, onLogin, onLogout, onCreateAccount }: HeaderProps) => (
18
+ <header>
19
+ <div className="storybook-header">
20
+ <div>
21
+ <svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
22
+ <g fill="none" fillRule="evenodd">
23
+ <path
24
+ d="M10 0h12a10 10 0 0110 10v12a10 10 0 01-10 10H10A10 10 0 010 22V10A10 10 0 0110 0z"
25
+ fill="#FFF"
26
+ />
27
+ <path
28
+ d="M5.3 10.6l10.4 6v11.1l-10.4-6v-11zm11.4-6.2l9.7 5.5-9.7 5.6V4.4z"
29
+ fill="#555AB9"
30
+ />
31
+ <path
32
+ d="M27.2 10.6v11.2l-10.5 6V16.5l10.5-6zM15.7 4.4v11L6 10l9.7-5.5z"
33
+ fill="#91BAF8"
34
+ />
35
+ </g>
36
+ </svg>
37
+ <h1>Acme</h1>
38
+ </div>
39
+ <div>
40
+ {user ? (
41
+ <>
42
+ <span className="welcome">
43
+ Welcome, <b>{user.name}</b>!
44
+ </span>
45
+ <Button size="small" onClick={onLogout} label="Log out" />
46
+ </>
47
+ ) : (
48
+ <>
49
+ <Button size="small" onClick={onLogin} label="Log in" />
50
+ <Button primary size="small" onClick={onCreateAccount} label="Sign up" />
51
+ </>
52
+ )}
53
+ </div>
54
+ </div>
55
+ </header>
56
+ );
@@ -0,0 +1,41 @@
1
+ import type { Meta, StoryObj } from '@storybook/tanstack-react';
2
+
3
+ import { expect, userEvent, within } from 'storybook/test';
4
+
5
+ import { Route } from './Page';
6
+ import './page.css';
7
+
8
+ const meta = {
9
+ title: 'Example/Page',
10
+ parameters: {
11
+ // More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
12
+ layout: 'fullscreen',
13
+ tanstack: {
14
+ // Example of providing a custom route for a story.
15
+ // The page component is extracted if a component is not set for the story.
16
+ // More on mocking Tanstack Router at: https://storybook.js.org/docs/get-started/tanstack-react#routing
17
+ router: {
18
+ route: Route,
19
+ },
20
+ },
21
+ },
22
+ } satisfies Meta<typeof Route>;
23
+
24
+ export default meta;
25
+ type Story = StoryObj<typeof meta>;
26
+
27
+ export const LoggedOut: Story = {};
28
+
29
+ // More on component testing: https://storybook.js.org/docs/writing-tests/interaction-testing
30
+ export const LoggedIn: Story = {
31
+ play: async ({ canvasElement }) => {
32
+ const canvas = within(canvasElement);
33
+ const loginButton = canvas.getByRole('button', { name: /Log in/i });
34
+ await expect(loginButton).toBeInTheDocument();
35
+ await userEvent.click(loginButton);
36
+ await expect(loginButton).not.toBeInTheDocument();
37
+
38
+ const logoutButton = canvas.getByRole('button', { name: /Log out/i });
39
+ await expect(logoutButton).toBeInTheDocument();
40
+ },
41
+ };
@@ -0,0 +1,79 @@
1
+ import React from 'react';
2
+
3
+ import { createFileRoute } from '@tanstack/react-router';
4
+
5
+ import { Header } from './Header';
6
+
7
+ type User = {
8
+ name: string;
9
+ };
10
+
11
+ export const Route = createFileRoute('/')({
12
+ component: () => <Page />,
13
+ });
14
+
15
+ const Page: React.FC = () => {
16
+ const [user, setUser] = React.useState<User>();
17
+
18
+ return (
19
+ <article>
20
+ <Header
21
+ user={user}
22
+ onLogin={() => setUser({ name: 'Jane Doe' })}
23
+ onLogout={() => setUser(undefined)}
24
+ onCreateAccount={() => setUser({ name: 'Jane Doe' })}
25
+ />
26
+
27
+ <section className="storybook-page">
28
+ <h2>Pages in Storybook</h2>
29
+ <p>
30
+ We recommend building UIs with a{' '}
31
+ <a href="https://componentdriven.org" target="_blank" rel="noopener noreferrer">
32
+ <strong>component-driven</strong>
33
+ </a>{' '}
34
+ process starting with atomic components and ending with pages.
35
+ </p>
36
+ <p>
37
+ Render pages with mock data. This makes it easy to build and review page states without
38
+ needing to navigate to them in your app. Here are some handy patterns for managing page
39
+ data in Storybook:
40
+ </p>
41
+ <ul>
42
+ <li>
43
+ Use a higher-level connected component. Storybook helps you compose such data from the
44
+ "args" of child component stories
45
+ </li>
46
+ <li>
47
+ Assemble data in the page component from your services. You can mock these services out
48
+ using Storybook.
49
+ </li>
50
+ </ul>
51
+ <p>
52
+ Get a guided tutorial on component-driven development at{' '}
53
+ <a href="https://storybook.js.org/tutorials/" target="_blank" rel="noopener noreferrer">
54
+ Storybook tutorials
55
+ </a>
56
+ . Read more in the{' '}
57
+ <a href="https://storybook.js.org/docs" target="_blank" rel="noopener noreferrer">
58
+ docs
59
+ </a>
60
+ .
61
+ </p>
62
+
63
+ <div className="tip-wrapper">
64
+ <span className="tip">Tip</span> Adjust the width of the canvas with the{' '}
65
+ <svg width="10" height="10" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg">
66
+ <g fill="none" fillRule="evenodd">
67
+ <path
68
+ d="M1.5 5.2h4.8c.3 0 .5.2.5.4v5.1c-.1.2-.3.3-.4.3H1.4a.5.5 0 01-.5-.4V5.7c0-.3.2-.5.5-.5zm0-2.1h6.9c.3 0 .5.2.5.4v7a.5.5 0 01-1 0V4H1.5a.5.5 0 010-1zm0-2.1h9c.3 0 .5.2.5.4v9.1a.5.5 0 01-1 0V2H1.5a.5.5 0 010-1zm4.3 5.2H2V10h3.8V6.2z"
69
+ id="a"
70
+ fill="#999"
71
+ />
72
+ </g>
73
+ </svg>
74
+ Viewports addon in the toolbar
75
+ </div>
76
+ </section>
77
+ </article>
78
+ );
79
+ };