Skip to content

Commit

Permalink
Add Storybook
Browse files Browse the repository at this point in the history
The latest stable release doesn't support Svelte 5, so we have to use
the v8 pre-release.
  • Loading branch information
bradleyayers committed Jan 29, 2024
1 parent 49cef3a commit de6bb22
Show file tree
Hide file tree
Showing 32 changed files with 8,117 additions and 705 deletions.
1 change: 1 addition & 0 deletions projects/frontend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = /** @satisfies { import("eslint").Linter.Config } */ ({
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:storybook/recommended",
"plugin:svelte/recommended",
"prettier",
],
Expand Down
27 changes: 27 additions & 0 deletions projects/frontend/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { StorybookConfig } from "@storybook/sveltekit";

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: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
addons: [
getAbsolutePath("@storybook/addon-links"),
getAbsolutePath("@storybook/addon-essentials"),
getAbsolutePath("@storybook/addon-interactions"),
],
framework: {
name: getAbsolutePath("@storybook/sveltekit"),
options: {},
},
docs: {
autodocs: "tag",
},
};
export default config;
15 changes: 15 additions & 0 deletions projects/frontend/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Preview } from "@storybook/svelte";

const preview: Preview = {
parameters: {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
};

export default preview;
14 changes: 14 additions & 0 deletions projects/frontend/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ tasks:
- --check
- .
platform: node
storybook:
command:
- "yarn"
- "storybook"
- "dev"
- "-p"
- "6006"
platform: node
storybook-build:
command:
- "yarn"
- "storybook"
- "build"
platform: node
11 changes: 11 additions & 0 deletions projects/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"private": true,
"type": "module",
"devDependencies": {
"@storybook/addon-essentials": "^8.0.0-alpha.15",
"@storybook/addon-interactions": "^8.0.0-alpha.15",
"@storybook/addon-links": "^8.0.0-alpha.15",
"@storybook/blocks": "^8.0.0-alpha.15",
"@storybook/svelte": "^8.0.0-alpha.15",
"@storybook/sveltekit": "^8.0.0-alpha.15",
"@storybook/test": "^8.0.0-alpha.15",
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
Expand All @@ -11,9 +18,13 @@
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-storybook": "^0.6.15",
"eslint-plugin-svelte": "^2.36.0-next.4",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"storybook": "^8.0.0-alpha.15",
"svelte": "^5.0.0-next.1",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
Expand Down
48 changes: 48 additions & 0 deletions projects/frontend/src/stories/Button.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { Meta, StoryObj } from '@storybook/svelte';

import Button from './Button.svelte';

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories
const meta = {
title: 'Example/Button',
component: Button,
tags: ['autodocs'],
argTypes: {
backgroundColor: { control: 'color' },
size: {
control: { type: 'select' },
options: ['small', 'medium', 'large'],
},
},
} satisfies Meta<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: {
primary: true,
label: 'Button',
},
};

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

export const Large: Story = {
args: {
size: 'large',
label: 'Button',
},
};

export const Small: Story = {
args: {
size: 'small',
label: 'Button',
},
};
34 changes: 34 additions & 0 deletions projects/frontend/src/stories/Button.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script lang="ts">
import './button.css';
/**
* Is this the principal call to action on the page?
*/
export let primary = false;
/**
* What background color to use
*/
export let backgroundColor: string | undefined = undefined;
/**
* How large should the button be?
*/
export let size: 'small' | 'medium' | 'large' = 'medium';
/**
* Button contents
*/
export let label: string = '';
$: mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
$: style = backgroundColor ? `background-color: ${backgroundColor}` : '';
</script>

<button
type="button"
class={['storybook-button', `storybook-button--${size}`, mode].join(' ')}
{style}
on:click
>
{label}
</button>
Loading

0 comments on commit de6bb22

Please sign in to comment.