vue-play
A minimalistic framework for demonstrating your Vue components, inspired by react-storybook.
Table of Contents
Getting Started
Integrate vue-play
into your project using getplay:
yarn global add getplay
cd my-project
getplay
Then you can run yarn play
and go to http://localhost:5000
So far we got:
- npm scripts
yarn play
& yarn build:play
- A
./play
folder where you write scenarios for your component - A
./play.config.js
file which helps you configure webpack easily using vbuild
The only thing you really need to worry about is ./play/index.js
, since you will write scenarios or dynamically load scenarios there.
Writing Scenarios
scenario
, a.k.a. story in react-storybook, it's usually an example component for demostrating your real component.
Keeping Scenarios
You can keep scenarios anywhere you want, by default keep them all at ./play/index.js
, you can also use separate files for them, or even name them *.play.js
in your component directory and load them dynamically.
Writing Scenarios
import { play } from 'vue-play'
import MyButton from '../src/components/MyButton.vue'
play('MyButton')
.add('with text', h => h(MyButton, ['hello world']))
.add('with emoji', h => h(MyButton, ['😃🍻']))
Loading Scenarios Dynamically
We can use Webpack's require.context to load modules dynamically.
const load = requireContext => requireContext.keys().map(requireContext)
load(require.context('../src/components', true, /.play.js$/))
Register Components
If you are using render function you won't need to register components, you only need this when you are using the template property, and it's same way as you do in other Vue app:
import Vue from 'vue'
import MyButton from './MyButton.vue'
Vue.component('my-button', MyButton)
play('MyButton')
.add('with text', {
template: '<my-button>text</my-button>'
})
You can also register components locally.
Use Component as play()
argument
import MyButton from './MyButton.vue'
play(MyButton)
.add('with text', '<my-button></my-button>')
To customize the displayName
in sidebar and the componentName
which is used to register itself in scenarios, you can simply set them in your component:
<!-- ./MyButton.vue -->
<script>
export default {
name: 'my-other-button',
displayName: 'Show off my cute button'
}
</script>
Or use methods:
play(MyButton)
.name('my-other-button')
.displayName('Show off my cute button')
.add('with text', '<my-other-button>text</my-other-button>')
Component Shorthand
If you only need template
or render
property for your component, you can use component shorthand
, which means you can directly set the value of scenario to a template string or render function:
import Example from './Example.vue'
play('Button')
.add('template shorthand', '<my-button>text</my-button>')
.add('render function shorthand', h => h(MyButton, ['text']))
.add('full component', {
data() {},
methods: {},
render(h) {}
}).
.add('single file', Example)
note: If you are using template
shorthand or template
property in component options, you should use Vue standalone build as well. For vue-play-cli
, it's as simple as using --standalone
option.
Additional Component Properties
The component for each scenario is a typical Vue component, but it can also accept some additional properties for documenting its usage, eg:
play('Button')
.add('with text', {
...component,
example,
})
example
Type: string
The example code of your component.
readme
Type: HTML string
Optionally display a readme tab to show detailed usage.
Component Injection
this.$log(data)
Log data to app console.
Showcase
Feel free to add your projects here:
Development
npm run play
npm run build
License
MIT © EGOIST