Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storybook #51

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion frontend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
module.exports = {
plugins: ['react'],
extends: ['next/core-web-vitals', 'plugin:react/recommended', '../.eslintrc.js'],
extends: ['next/core-web-vitals', 'plugin:react/recommended', '../.eslintrc.js', 'plugin:storybook/recommended'],
ignorePatterns: ['node_modules/**/*', '.next/**/*', 'out/**/*', 'pek-api/**/*'],
parserOptions: {
tsconfigRootDir: __dirname,
Expand Down
3 changes: 3 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

*storybook.log
/storybook-static
26 changes: 26 additions & 0 deletions frontend/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { StorybookConfig } from '@storybook/nextjs';

import { join, dirname } from 'path';

/**
* This function is used to resolve the absolute path of a package.
* It is needed in projects that use Yarn PnP or are set up within a monorepo.
*/
function getAbsolutePath(value: string): any {
return dirname(require.resolve(join(value, 'package.json')));
}
const config: StorybookConfig = {
stories: ['../stories/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: [
getAbsolutePath('@storybook/addon-onboarding'),
getAbsolutePath('@storybook/addon-essentials'),
getAbsolutePath('@chromatic-com/storybook'),
getAbsolutePath('@storybook/addon-interactions'),
],
framework: {
name: getAbsolutePath('@storybook/nextjs'),
options: {},
},
staticDirs: ['../public'],
};
export default config;
16 changes: 16 additions & 0 deletions frontend/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import '@/app/globals.css';

import type { Preview } from '@storybook/react';

const preview: Preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
};

export default preview;
14 changes: 13 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"lint:fix": "eslint --fix --ext .ts,.tsx .",
"lint": "eslint --ext .ts,.tsx .",
"generate": "npx kubb generate",
"generate:watch": "npx kubb generate --watch"
"generate:watch": "npx kubb generate --watch",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"dependencies": {
"@kubb/cli": "^2.26.3",
Expand All @@ -36,6 +38,14 @@
"tailwindcss-animate": "^1.0.7"
},
"devDependencies": {
"@chromatic-com/storybook": "^3.2.2",
"@storybook/addon-essentials": "^8.4.7",
"@storybook/addon-interactions": "^8.4.7",
"@storybook/addon-onboarding": "^8.4.7",
"@storybook/blocks": "^8.4.7",
"@storybook/nextjs": "^8.4.7",
"@storybook/react": "^8.4.7",
"@storybook/test": "^8.4.7",
"@types/js-cookie": "^3.0.6",
"@types/node": "^20.16.5",
"@types/react": "^18.3.5",
Expand All @@ -45,7 +55,9 @@
"eslint": "^8.57.0",
"eslint-config-next": "14.2.11",
"eslint-plugin-react": "^7.36.1",
"eslint-plugin-storybook": "^0.11.1",
"postcss": "^8.4.45",
"storybook": "^8.4.7",
"tailwindcss": "^3.4.11",
"typescript": "~5.6.2"
}
Expand Down
63 changes: 63 additions & 0 deletions frontend/stories/Button.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { Meta, StoryObj } from '@storybook/react';
import { expect, fn, userEvent, within } from '@storybook/test';

import { Button } from '@/components/ui/button';

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
title: 'Example/Button',
component: Button,
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
layout: 'centered',
},
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
tags: ['autodocs'],
// More on argTypes: https://storybook.js.org/docs/api/argtypes
argTypes: {
variant: { control: 'select', options: ['default', 'destructive', 'outline', 'secondary', 'ghost', 'link'] },
size: { control: 'select', options: ['default', 'sm', 'lg', 'icon'] },
children: { control: 'text' },
onClick: { action: 'clicked' },
},
// Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
args: { onClick: fn() },
} satisfies Meta<typeof Button>;

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

// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
export const Primary: Story = {
args: {
variant: 'default',
children: 'Button',
},
play: async ({ args, canvasElement }) => {
const canvas = within(canvasElement);
const button = await canvas.findByRole('button');
await expect(button).toBeInTheDocument();
await userEvent.click(button);
expect(args.onClick).toHaveBeenCalled();
},
};

export const Secondary: Story = {
args: {
children: 'Button',
},
};

export const Large: Story = {
args: {
size: 'lg',
children: 'Button',
},
};

export const Small: Story = {
args: {
size: 'sm',
children: 'Button',
},
};
Loading
Loading