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

Vue2 Compatibility #469

Closed
wants to merge 8 commits into from
Closed
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
File renamed without changes.
22 changes: 22 additions & 0 deletions examples/vue2/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { resolve } = require('path');
const { mergeConfig } = require('vite');

export default {
framework: '@storybook/vue',
stories: ['../stories/**/*.stories.mdx', '../stories/**/*.stories.@(js|jsx|ts|tsx)'],
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
core: {
builder: '@storybook/builder-vite',
// we don't want to muck up the data when we're working on the builder
disableTelemetry: true,
},
features: {
previewMdx2: true,
},
async viteFinal(config, { configType }) {
// Demonstrates use of mergeConfig and resolve.alias as an array
return mergeConfig(config, {
resolve: { alias: [{ find: '@assets', replacement: resolve(__dirname, '..', 'stories', 'assets') }] },
});
},
};
File renamed without changes.
5 changes: 5 additions & 0 deletions examples/vue2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Vue

This example demonstrates storybook in a Vue 2 project.

This example also demonstrates MDX2 support.
34 changes: 34 additions & 0 deletions examples/vue2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "example-vue2",
"private": true,
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"storybook": "start-storybook --port 6010",
"build-storybook": "build-storybook",
"preview-storybook": "http-server storybook-static --port 6010 --silent",
"test": "wait-on tcp:6010 && test-storybook --url 'http://localhost:6010'",
"test-ci": "run-p --race test preview-storybook"
},
"author": "",
"license": "MIT",
"dependencies": {
"vue": "^2.7"
},
"devDependencies": {
"@storybook/addon-a11y": "^6.5.9",
"@storybook/addon-essentials": "^6.5.9",
"@storybook/builder-vite": "workspace:*",
"@storybook/mdx2-csf": "^0.0.3",
"@storybook/test-runner": "0.1.0",
"@storybook/vue": "^6.5.9",
"@vitejs/plugin-vue2": "^1.1.2",
"http-server": "^14.1.0",
"jest": "^27.5.1",
"npm-run-all": "^4.1.5",
"vite": "^3.0.0-beta.9",
"vue-loader": "^15.9.8",
"wait-on": "^6.0.1"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions examples/vue3/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
STORYBOOK_ENV_VAR=included
VITE_ENV_VAR=included
ENV_VAR=should_not_be_included
File renamed without changes.
10 changes: 10 additions & 0 deletions examples/vue3/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
expanded: true,
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/vue/package.json → examples/vue3/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example-vue",
"name": "example-vue3",
"private": true,
"version": "0.0.0",
"description": "",
Expand Down
47 changes: 47 additions & 0 deletions examples/vue3/stories/Button.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import MyButton from './Button.vue';

export default {
title: 'Example/Button',
component: MyButton,
argTypes: {
backgroundColor: { control: 'color' },
size: {
control: { type: 'select', options: ['small', 'medium', 'large'] },
},
onClick: {},
},
};

const Template = (args) => ({
// Components used in your story `template` are defined in the `components` object
components: { MyButton },
// The story's `args` need to be mapped into the template through the `setup()` method
setup() {
return { args };
},
// And then the `args` are bound to your component with `v-bind="args"`
template: '<my-button v-bind="args" />',
});

export const Primary = Template.bind({});
Primary.args = {
primary: true,
label: 'Button',
};

export const Secondary = Template.bind({});
Secondary.args = {
label: 'Button',
};

export const Large = Template.bind({});
Large.args = {
size: 'large',
label: 'Button',
};

export const Small = Template.bind({});
Small.args = {
size: 'small',
label: 'Button',
};
69 changes: 69 additions & 0 deletions examples/vue3/stories/Button.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<template>
<button type="button" :class="classes" @click="onClick" :style="style">
{{ label }}
</button>
</template>

<script>
import './button.css';
import { reactive, computed } from 'vue';

export default {
name: 'my-button',

props: {
/**
* The label of the button
*/
label: {
type: String,
required: true,
},

/**
* Whether the button is primary
*/
primary: {
type: Boolean,
default: false,
},

/**
* The size of the button
*/
size: {
type: String,
validator: function (value) {
return ['small', 'medium', 'large'].indexOf(value) !== -1;
},
},

/**
* The background colour of the button
*/
backgroundColor: {
type: String,
},
},

emits: ['click'],

setup(props, { emit }) {
props = reactive(props);
return {
classes: computed(() => ({
'storybook-button': true,
'storybook-button--primary': props.primary,
'storybook-button--secondary': !props.primary,
[`storybook-button--${props.size || 'medium'}`]: true,
})),
style: computed(() => ({
backgroundColor: props.backgroundColor,
})),
onClick() {
emit('click');
},
};
},
};
</script>
22 changes: 22 additions & 0 deletions examples/vue3/stories/EnvironmentVariables.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import EnvVars from './EnvironmentVariables.vue';

export default {
title: 'Environment Variables',
component: EnvVars,
};

const Template = (args) => ({
// Components used in your story `template` are defined in the `components` object
components: { EnvVars },
// The story's `args` need to be mapped into the template through the `setup()` method
setup() {
return { args };
},
// And then the `args` are bound to your component with `v-bind="args"`
template: '<env-vars v-bind="args" />',
});

export const Info = Template.bind({});
Info.args = {
dynamic: import.meta.env.VITE_ENV_VAR,
};
30 changes: 30 additions & 0 deletions examples/vue3/stories/EnvironmentVariables.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<template>
<div>
<h1>import​.meta​.env:</h1>
<div>{{ env }}</div>
<h1>import​.meta​.env​.STORYBOOK:</h1>
<div>{{ storybook }}</div>
<h1>dynamic:</h1>
<div>{{ dynamic }}</div>
</div>
</template>

<script>
export default {
name: 'env-vars',
props: {
dynamic: {
type: String,
required: true,
},
},
computed: {
env() {
return JSON.stringify(import.meta.env, null, 2);
},
storybook() {
return import.meta.env.STORYBOOK;
},
},
};
</script>
28 changes: 28 additions & 0 deletions examples/vue3/stories/Header.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import MyHeader from './Header.vue';

export default {
title: 'Example/Header',
component: MyHeader,
};

const Template = (args) => ({
// Components used in your story `template` are defined in the `components` object
components: { MyHeader },
// The story's `args` need to be mapped into the template through the `setup()` method
setup() {
// Story args can be spread into the returned object
return { ...args };
},
// Then, the spread values can be accessed directly in the template
template: '<my-header :user="user" />',
});

export const LoggedIn = Template.bind({});
LoggedIn.args = {
user: {},
};

export const LoggedOut = Template.bind({});
LoggedOut.args = {
user: null,
};
40 changes: 40 additions & 0 deletions examples/vue3/stories/Header.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<template>
<header>
<div class="wrapper">
<div>
<svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
<g fill="none" fill-rule="evenodd">
<path d="M10 0h12a10 10 0 0110 10v12a10 10 0 01-10 10H10A10 10 0 010 22V10A10 10 0 0110 0z" fill="#FFF" />
<path d="M5.3 10.6l10.4 6v11.1l-10.4-6v-11zm11.4-6.2l9.7 5.5-9.7 5.6V4.4z" fill="#555AB9" />
<path d="M27.2 10.6v11.2l-10.5 6V16.5l10.5-6zM15.7 4.4v11L6 10l9.7-5.5z" fill="#91BAF8" />
</g>
</svg>
<h1>Acme</h1>
</div>
<div>
<my-button size="small" @click="$emit('logout')" label="Log out" v-if="user" />
<my-button size="small" @click="$emit('login')" label="Log in" v-if="!user" />
<my-button primary size="small" @click="$emit('createAccount')" label="Sign up" v-if="!user" />
</div>
</div>
</header>
</template>

<script>
import './header.css';
import MyButton from './Button.vue';

export default {
name: 'my-header',

components: { MyButton },

props: {
user: {
type: Object,
},
},

emits: ['login', 'logout', 'createAccount'],
};
</script>
Loading