A minimalistic framework for demonstrating your Vue components, inspired by react-storybook.
Table of Contents
Getting Started
The easy way
npm install -g getplay
cd my-vue-project
getplay
npm run play
npm run play:build
The hard way
There're two pages in your play app, one is the app interface which has a sidebar and it can toggle scenarios of your components, the other page is for rendering the examples, this page will be loaded as iframe in app interface.
And only the latter needs to load scenarios that you write in the play entry
, let's say ./play/index.js
:
import {play} from 'vue-play'
import MyButton from './MyButton.vue'
play('MyButton', module)
.add('with text', h => h(MyButton, ['text']))
App interface
import app from 'vue-play/dist/app'
import 'vue-play/dist/app.css'
app()
Preview
import preview from 'vue-play/dist/preview'
import scenarios from './'
preview(scenarios)
Add app interface
and preview
to your webpack entry:
module.exports = {
entry: {
app: './play/app.js',
preview: './play/preview.js'
},
plugins: [
HtmlWebpackPlugin({
filename: 'index.html',
chunks: ['app']
}),
HtmlWebpackPlugin({
filename: 'preview.html',
chunks: ['preview']
})
]
}
That's it, you're all set!
Writing Scenarios
scenario
, a.k.a. story in react-storybook, it's usually an example for demostrating your real component.
Keeping Scenarios
You can keep scenarios anywhere you want, for example you can 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', module)
.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.
import { configure } from 'vue-play'
const load = requireContext => requireContext.keys().map(requireContext)
const scenarios = load(require.context('../src/components', true, /.play.js$/))
configure(scenarios, module)
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:
import MyButton from './MyButton.vue'
module.exports.components = {
MyButton
}
play('MyButton', module)
.add('with text', '<my-button>text</my-button>')
You can also put the example '<my-button>text</my-button>'
in a seperate file, like .vue
file and register components there, locally.
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:
play('Button', module)
.add('template shorthand', '<my-button>text</my-button>')
.add('render function shorthand', h => h(MyButton, ['text']))
.add('full component', {
data() {},
methods: {},
render(h) {}
})
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', module)
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.
Showcase
Feel free to add your projects here:
Development
npm run play
npm run build
License
MIT © EGOIST