Skip to content

Commit

Permalink
adds quick start guide, fixes linting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcsapo committed Oct 3, 2018
1 parent 02db5c8 commit 2d7235e
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ For additional help, join us [in our Slack](https://now-examples-slackin-rrirkqo
| [React Native](app/react-native) | - | - | [![React Native](https://img.shields.io/npm/dt/@storybook/react-native.svg)](app/react-native) |
| [Vue](app/vue) | [v3.4.x](https://release-3-4--storybooks-vue.netlify.com/), [v3.3.x](https://release-3-3--storybooks-vue.netlify.com/) | [v4.0.0-alpha](https://storybooks-vue.netlify.com/) | [![Vue](https://img.shields.io/npm/dt/@storybook/vue.svg)](app/vue) |
| [Angular](app/angular) | [v3.4.x](https://release-3-4--storybooks-angular.netlify.com/), [v3.3.x](https://release-3-3--storybooks-angular.netlify.com/) | [v4.0.0-alpha](https://storybooks-angular.netlify.com/) | [![Angular](https://img.shields.io/npm/dt/@storybook/angular.svg)](app/angular) |
| [Ember](app/ember) | - | [v4.0.0-alpha](https://storybooks-ember.netlify.com/) | [![Ember](https://img.shields.io/npm/dt/@storybook/ember.svg)](app/angular) |
| [Polymer](app/polymer) | [v3.4.x](https://release-3-4--storybooks-polymer.netlify.com/) | [v4.0.0-alpha](https://storybooks-polymer.netlify.com/) | [![Polymer](https://img.shields.io/npm/dt/@storybook/polymer.svg)](app/polymer) |
| [Mithril](app/mithril) <sup>alpha</sup> | - | [v4.0.0-alpha](https://storybooks-mithril.netlify.com/) | [![Mithril](https://img.shields.io/npm/dt/@storybook/mithril.svg)](app/mithril) |
| [Marko](app/marko) <sup>alpha</sup> | - | [v4.0.0-alpha](https://storybooks-marko.netlify.com/) | [![Marko](https://img.shields.io/npm/dt/@storybook/marko.svg)](app/marko) |
| [HTML](app/html) <sup>alpha</sup> | - | [v4.0.0-alpha](https://storybooks-html.netlify.com/) | [![HTML](https://img.shields.io/npm/dt/@storybook/html.svg)](app/html) |
| [Svelte](app/svelte) <sup>alpha</sup> | - | [v4.0.0-alpha](https://storybooks-svelte.netlify.com/) | [![Svelte](https://img.shields.io/npm/dt/@storybook/svelte.svg)](app/svelte) |
| [Riot](app/riot) <sup>alpha</sup> | - | [v4.0.0-alpha](https://storybooks-riot.netlify.com/) | [![Riot](https://img.shields.io/npm/dt/@storybook/riot.svg)](app/riot) |
| [Ember](app/ember) | - | [v4.0.0-alpha](https://storybooks-ember.netlify.com/) | [![Ember](https://img.shields.io/npm/dt/@storybook/ember.svg)](app/ember) |

### Sub Projects

Expand Down
157 changes: 157 additions & 0 deletions docs/src/pages/basics/guide-ember/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
---
id: 'guide-ember'
title: 'Storybook for Ember'
---

You may have tried to use our quick start guide to setup your project for Storybook. If you want to set up Storybook manually, this is the guide for you.

> This will also help you to understand how Storybook works.
## Starter Guide Ember

Storybook has its own Webpack setup and a dev server.

In this guide, we will set up Storybook for your Ember project.

## Table of contents

- [Add @storybook/ember](#add-storybookember)
- [Setup environment](#setup-environment)
- [Create the config file](#create-the-config-file)
- [Write your stories](#write-your-stories)
- [Run your Storybook](#run-your-storybook)

## Add @storybook/ember

First of all, you need to add `@storybook/ember` to your project. To do that, simply run:

```sh
npm i --save-dev @storybook/ember
```

If you don't have `package.json` in your project, you'll need to init it first:

```sh
npm init
```

Then add the following NPM script to your package json in order to start the storybook later in this guide:

> In order for your storybook to run properly be sure to be either run `ember serve` or `ember build` before running any storybook commands.
```json
{
"scripts": {
"storybook": "start-storybook -p 9001 -c .storybook"
}
}
```

## Setup environment

### Adding preview-head.html

In order for storybook to register your ember application you must add the following file to `.storybook/preview-head.html`

> These scripts may not contain everything you need, a good point of reference is to look at what is in the head tag in your applications `dist/index.html` file when you build.
```
<meta name="{ember-app-name}/config/environment" content="%7B%22modulePrefix%22%3A%22{ember-app-name}%22%2C%22environment%22%3A%22test%22%2C%22rootURL%22%3A%22/%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%7D%2C%22APP%22%3A%7B%22name%22%3A%22{ember-app-name}%22%2C%22version%22%3A%224.0.0-alpha.23+4f61a2fb%22%7D%7D" />
<link integrity="" rel="stylesheet" href="./assets/vendor.css">
<link integrity="" rel="stylesheet" href="./assets/{ember-app-name}.css">
<script src="./assets/vendor.js"></script>
<script>
runningTests = true;
</script>
<script src="./assets/{ember-app-name}.js"></script>
```

> Adding the runningTests script is extremely important don't forget to add this as it will result in your application binding multiple times.
Substitute `ember-app-name` with the name of your ember application.

> This is found by going to `package.json` and referencing the name field
### Adding .env

A file named `.env` is needed in the root directory with the following contents:

```
STORYBOOK_NAME={ember-app-name}
```

## Create the config file

Storybook can be configured in several different ways.
That’s why we need a config directory. We've added a `-c` option to the above NPM script mentioning `.storybook` as the config directory.

For the basic Storybook configuration file, you don't need to do much, but simply tell Storybook where to find stories.

To do that, simply create a file at `.storybook/config.js` with the following content:

```js
import { configure } from '@storybook/ember';

function loadStories() {
require('../stories/index.js');
// You can require as many stories as you need.
}

configure(loadStories, module);
```

That'll load stories in `../stories/index.js`.

## Write your stories

Now you can write some stories inside the `../stories/index.js` file, like this:

> It is import that you import the `hbs` function that is provided by a babel plugin in `@storybook/ember`
```js
import hbs from 'htmlbars-inline-precompile';
import { storiesOf } from '@storybook/ember';

storiesOf('Demo', module)
.add('heading', () => hbs`<h1>Hello World</h1>`)
.add('button', () => {
return {
template: hbs`<button {{action onClick}}>
Hello Button
</button>`
},
context: {
onClick: (e) => console.log(e)
}
})
.add('component', () => {
return {
template: hbs`{{foo-bar
click=onClick
}}`
},
context: {
onClick: (e) => console.log(e)
}
});

```

A story is either:

1. A single handlebars fragment generated using the `hbs` function
2. An object that contains template and context that will be bound to the resulting element

> In order to get your storybook to get new changes made to the `foo-bar` or any other components that are defined in your Ember app you must run `ember serve` as a sidecar for the build files to get generated.
## Run your Storybook

Now everything is ready. Simply run your storybook with:

```sh
npm run storybook
```

Now you can change components and write stories whenever you need to.
1 change: 1 addition & 0 deletions docs/src/pages/basics/quick-start-guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ To learn more about what `getstorybook` command does, have a look at the slow st
* [Marko](/basics/guide-marko/)
* [HTML](/basics/guide-html/)
* [Svelte](/basics/guide-svelte/)
* [Ember](/basics/guide-ember/)

A tutorial is also available at [Learn Storybook](https://www.learnstorybook.com) for a step-by-step guide (only available for React).
2 changes: 1 addition & 1 deletion examples/ember-cli/config/environment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function(environment) {
module.exports = function buildEnvironment(environment) {
const ENV = {
modulePrefix: 'ember-example',
environment,
Expand Down
2 changes: 1 addition & 1 deletion examples/ember-cli/ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
module.exports = function build(defaults) {
const app = new EmberApp(defaults, {
// Add options here
});
Expand Down
5 changes: 2 additions & 3 deletions examples/ember-cli/stories/addon-knobs.stories.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hbs from 'htmlbars-inline-precompile';
import { storiesOf } from '@storybook/ember';
import { withKnobs, text, color } from '@storybook/addon-knobs';
import { action } from '@storybook/addon-actions';

storiesOf('Addon|Knobs', module)
.addDecorator(withKnobs)
Expand All @@ -22,8 +23,6 @@ storiesOf('Addon|Knobs', module)
subTitleColor: color('subTitleColor', '#B8854F'),
title: text('title', 'Welcome to storybook'),
subtitle: text('subtitle', 'This environment is completely editable'),
onClick: e => {
console.log(e);
},
onClick: action('clicked'),
},
}));

0 comments on commit 2d7235e

Please sign in to comment.