GithubHelp home page GithubHelp logo

moretti / cosmos Goto Github PK

View Code? Open in Web Editor NEW

This project forked from react-cosmos/react-cosmos

0.0 2.0 0.0 1.37 MB

DX tool for designing truly encapsulated React components.

Home Page: https://www.youtube.com/watch?v=t9V2oKK83Kg

License: MIT License

JavaScript 96.63% CSS 0.77% HTML 2.60%

cosmos's Introduction

Cosmos Build Status Coverage Status

Cosmos is a JavaScript DX* tool for designing truly encapsulated React components.

It scans your project for React component fixtures and loads them inside ComponentPlayground, enabling you to:

  1. Render your components under any combination of props and state
  2. See component states evolve in real-time as you interact with running instances

Working with ComponentPlayground improves the component design because it surfaces any implicit dependencies. It also forces you to define sane inputs for every component, making them more predictable and easier to debug down the road.

Component Playground

*DX stands for Developer Experience, the counterpart of UX in building a product, system or service.

Requirements

  • You should already be using CommonJS modules to structure your code and webpack to bundle your modules for the browser
  • Your React components should be fully encapsulated. They should have no global dependencies and rely exclusively on props for input. Including styles, which means you need to be using style-loader.
  • You must create component fixtures for ComponentPlayground to load. The component and fixture files should be nested as in the folder structure below. See the example repo for a better picture.

Installing

  • Install the Cosmos package through npm npm install cosmos-js
  • Run the ComponentPlayground executable node_modules/.bin/component-playground
  • Open localhost:8989

Under the hood

Running the ComponentPlayground executable will:

  1. Start a webpack dev server, serving an instance of ComponentPlayground at localhost:8989
  2. Scan the current folder for components and fixtures and feed them to ComponentPlayground

File structure

This is the file structure Cosmos expects:

|
+-- components
|   +-- FooComponent.jsx
|   +-- namespace
|   |   +-- BarComponent.jsx
+-- fixtures
|   +-- FooComponent
|   |   +-- default.js
|   |   +-- active.js
|   +-- namespace
|   |   +-- BarComponent
|   |       +-- default.js
|   |       +-- paused.js

If the components and fixtures folders are not siblings, their paths can be specified via cli args:

node_modules/.bin/component-playground --components-path src/components --fixtures-path tests/fixtures

webpack config

The webpack build bundles modules from both the current folder and the Cosmos package. It is compatible with React classes, ES6 classes, JSX and CSS/LESS modules out of the box, but you can customize the webpack config to support additional loaders and settings by creating a component-playground.config.js file in the project root. E.g.

module.exports.webpack = function(config) {
  config.module.loaders.push(/*...*/);
  return config;
};

You can use this functionality to inject external styles or scripts if your components need e.g. Bootstrap to work.

config.entry.push(path.join(process.cwd(), 'injectBootstrapTags.js'));

Hot loading

Cosmos includes React Hot Loader and has webpack's hot module replacement enabled so you can tweak the components and their styles without refreshing the browser:

React Hot Loader in Cosmos

Thank you for your interest!

Join the chat at https://gitter.im/skidding/cosmos

Explore the Contributing Guide for more information.

cosmos's People

Contributors

adaschevici avatar bbirand avatar cef62 avatar gaearon avatar gitter-badger avatar moretti avatar nighttrax avatar ovidiuch avatar piatra avatar w0rm 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.