GithubHelp home page GithubHelp logo

matteodem / vue-play Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vue-play/vue-play

0.0 2.0 1.0 1.86 MB

๐Ÿญ A minimalistic framework for demonstrating your Vue components

License: MIT License

JavaScript 97.69% CSS 1.95% HTML 0.36%

vue-play's Introduction

vue-play

NPM version NPM downloads Build Status gitter

A minimalistic framework for demonstrating your Vue components, inspired by react-storybook.

preview

Table of Contents

Getting Started

Install it:

cd my-project
npm install --save-dev vue-play vue-play-cli
# vue-play-cli gives you the `vue-play` command
# vue-play is the UI utils.

Add npm scripts:

{
  "scripts": {
    "play": "vue-play start",
    "play:build": "vue-play build"
  }
}

Write play entry to load your examples:

// ./play/index.js
import {play} from 'vue-play'
import MyButton from './components/MyButton.vue'

play('Button', module)
  .add('with text', h => h(MyButton, 'hello'))
  .add('with emoji', h => h(MyButton, '๐Ÿ’ซ'))

Then just run npm run play and go to http://localhost:5000

For more usages on vue-play-cli, please head to vue-play/vue-play-cli.

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

// ./play/app.js
import app from 'vue-play/dist/app'
import 'vue-play/dist/app.css'

// bootstrap app
app()

Preview

// ./play/preview.js
import preview from 'vue-play/dist/preview'
// loads the scenarios at ./play/index.js
import scenarios from './'

// actually render the scenarios in preview page
// when the preview page is ready
// it will tell the app interface what scenarios we have
preview(scenarios)

Add app interface and preview to your webpack entry:

module.exports = {
  // ...
  entry: {
    app: './play/app.js',
    preview: './play/preview.js'
  },
  // don't forget to generate html output for both of them
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      chunks: ['app']
    }),
    new 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'

// Use `play` to describe component title
// use .add to add scenario for that component
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:

// ./play/index.js
import MyButton from './MyButton.vue'

// these components will be registered globally
module.exports.components = {
  MyButton
}

play('MyButton', module)
  .add('with text', {
    template: '<my-button>text</my-button>'
  })

You can also put the example component in a seperate file, like .vue file and register components there, locally.

Use Component as play() argument

import MyButton from './MyButton.vue'

// assuming MyButton.name is 'my-button'
play(MyButton, module)
  // MyButton will be automatially registered in scenarios
  // so you don't have to use module.exports.components = {MyButton}
  .add('with text', '<my-button></my-button>')

// then the app sidebar will look like:
// - my-button
//    - with text

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, module)
  .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', module)
  .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, you should use Vue standalone build as well.

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', {
    // a valid vue component
    ...component,
    // additional
    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

# run example play script
npm run play

# build vue-play
# you don't need this when developing
npm run build

License

MIT ยฉ EGOIST

vue-play's People

Contributors

egoist avatar zephraph avatar qingwei-li avatar

Watchers

James Cloos avatar Matteo Demicheli avatar

Forkers

gaybro8777

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.