GithubHelp home page GithubHelp logo

dvingo / om-storybook-example Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 412 KB

Example of using om.next components in React Storybook

Home Page: https://dvingo.github.io/om-storybook-example

JavaScript 17.08% HTML 8.37% Clojure 72.02% CSS 0.21% Shell 2.31%
om clojurescript

om-storybook-example's Introduction

Example of using om.next components in React Storybook.

This example also uses the JS dependencies solution as described here: http://blob.tomerweller.com/reagent-import-react-components-from-npm

This way we can use the latest versions of JS libs without worrying about externs.

The trade off is that you must use string names for properties, but for that we can use: https://github.com/binaryage/cljs-oops

Overview of steps to get things setup:

This project was created using a figwheel template:

lein new figwheel cljs-storybook -- --om

Then updating the project.clj, and creating a simple component:

(defui ^:export Hello
  Object
  (render [this]
    (let [{:keys [name]} (om/props this)]
      (dom/div nil (str " your name is: " name)))))

(def ^:export hello (om/factory Hello))

Next step is to install all the JS deps using yarn.

This assumes yarn, npm, and node are installed already.

yarn init -y
yarn add -D \
react \
react-dom \
webpack \
@kadira/storybook

Setup React Storybook

Following the Storybook "slow start" guide

https://storybooks.js.org/docs/react-storybook/basics/slow-start-guide/

Add npm/yarn script:

  "scripts": {
    "storybook": "start-storybook -p 9001 -c .storybook -s resources/public"
  }

-p is the port to run the server on, -c is where to find the config directory and -s adds more directories for the server to serve static files from.

resources/public is where our compiled CLJS and JS files are output to.

Create .storybook dir.

Edit .storybook/config.js

We use webpack's require.context feature for dynamic require statements.

https://webpack.js.org/guides/dependency-management/#require-context

This will treat any file ending in .story.js as a file that contains stories. You are free to change how stories are included.

import { configure } from '@kadira/storybook'
const r = require.context('../src', true, /\.story\.js$/)
configure(() => r.keys().forEach(r), module)

Produce JS deps:

 ./node_modules/.bin/webpack -p

Add head.html in .storybook dir:

This includes our CLJS on the page in a blocking manner so that our namespaces will be available globally before the story code runs.

<script src="js/compiled/out/goog/base.js"></script>
<script src="js/compiled/cljs_storybook.js"></script>
<script>goog.require('cljs_storybook.core')</script>

Now we can construct a story:

import React from 'react'
import { storiesOf } from '@kadira/storybook'
const {hello} = cljs_storybook.core
const props = cljs.core.hash_map(cljs.core.keyword('name'), 'hello')

storiesOf('OmComponent', module)
  .add('Default', () => <div>{hello(vals)}</div>)

In this example I'm constructing the CLJS map in JS, another solution would be to export your props from CLJS so they are easier to write.

Start storybook:

yarn run storybook

Either produce a CLJS build one time or for dev use figwheel:

lein cljsbuild once dev
lein cljsbuild once min
# or
rlwrap lein figwheel

open http://localhost:9001 to view the stories. and http://localhost:3449 for normal figwheel dev.

Now you can edit your code and use figwheel development workflow while viewing your components in React Storybook.

GitHub pages build

Clone the repo to another directory.

Build the assets in this directory:

./scripts/buildProd.sh

Then copy the contents of the storybookBuild directory to the directory where you have the gh-pages branch checked out.

om-storybook-example's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.