Convert Figma logo to code with AI

react-cosmos logoreact-cosmos

Sandbox for developing and testing UI components in isolation

8,371
352
8,371
10

Top Related Projects

Set up a modern web app by running one command.

85,173

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation

Isolated React component development environment with a living style guide

23,690

✍ It has never been so easy to document your things!

47,669

Fast, easy and reliable testing for anything that runs in a browser.

Quick Overview

React Cosmos is a development environment for building reusable React components. It allows developers to create, test, and document components in isolation, providing a sandbox environment for rapid prototyping and visual testing. React Cosmos aims to improve component-driven development workflows and enhance collaboration between developers and designers.

Pros

  • Enables isolated component development and testing
  • Supports various React renderers (e.g., React Native)
  • Integrates well with existing React projects
  • Provides a powerful plugin system for customization

Cons

  • Learning curve for new users
  • Setup process can be complex for some projects
  • Limited documentation for advanced use cases
  • May add overhead to smaller projects

Code Examples

  1. Creating a simple fixture:
// Button.fixture.jsx
import Button from './Button';

export default {
  component: Button,
  props: {
    label: 'Click me',
    onClick: () => console.log('Button clicked'),
  },
};
  1. Using decorators to wrap fixtures:
// withReduxProvider.js
import { Provider } from 'react-redux';
import store from './store';

export default ({ children }) => (
  <Provider store={store}>{children}</Provider>
);
  1. Configuring Cosmos in a project:
// cosmos.config.json
{
  "staticPath": "public",
  "fileMatch": ["**/*.fixture.{js,jsx,ts,tsx}"],
  "watchDirs": ["src"],
  "experimentalRendererUrl": true
}

Getting Started

  1. Install React Cosmos in your project:

    npm install --save-dev react-cosmos
    
  2. Create a fixture file for a component:

    // src/components/Button.fixture.jsx
    import Button from './Button';
    
    export default {
      component: Button,
      props: { label: 'Hello Cosmos' },
    };
    
  3. Run React Cosmos:

    npx react-cosmos
    
  4. Open your browser and navigate to http://localhost:5000 to see your component in the Cosmos UI.

Competitor Comparisons

Set up a modern web app by running one command.

Pros of Create React App

  • Simpler setup and configuration for beginners
  • Widely adopted and well-maintained by Facebook
  • Comprehensive documentation and large community support

Cons of Create React App

  • Less flexibility for advanced configurations
  • Harder to customize build process without ejecting
  • Can lead to larger bundle sizes due to included dependencies

Code Comparison

Create React App:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

React Cosmos:

import React from 'react';
import { createFixture } from 'react-cosmos';
import MyComponent from './MyComponent';

export default createFixture({
  component: MyComponent,
  props: { message: 'Hello, Cosmos!' }
});

React Cosmos focuses on component development and testing, providing a more granular approach to building React applications. It offers powerful fixture-based development, allowing developers to isolate and test components in various states.

Create React App, on the other hand, provides a complete boilerplate for React applications, handling the build process and initial setup. It's more suitable for quickly starting new projects and prototyping.

While Create React App is excellent for beginners and rapid prototyping, React Cosmos shines in component-driven development and testing scenarios, offering more flexibility for experienced developers working on complex applications.

85,173

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation

Pros of Storybook

  • Larger ecosystem with more addons and integrations
  • Better documentation and community support
  • More flexible configuration options for advanced use cases

Cons of Storybook

  • Steeper learning curve for beginners
  • Heavier setup and potentially slower build times
  • Can be overkill for smaller projects

Code Comparison

React Cosmos:

// cosmos.decorator.js
import React from 'react';

export default ({ children }) => (
  <div style={{ padding: '20px' }}>{children}</div>
);

Storybook:

// preview.js
export const decorators = [
  (Story) => (
    <div style={{ padding: '20px' }}>
      <Story />
    </div>
  ),
];

Both React Cosmos and Storybook are powerful tools for component development and testing. React Cosmos focuses on simplicity and ease of use, making it a great choice for smaller projects or teams new to component development tools. It offers a straightforward setup process and a clean, minimalistic interface.

Storybook, on the other hand, provides a more comprehensive set of features and a vast ecosystem of addons. It's well-suited for larger projects and teams that require advanced customization options. While it may have a steeper learning curve, Storybook's extensive documentation and community support make it easier to overcome initial challenges.

In terms of code, both tools allow for similar functionality, such as adding decorators to wrap components. The syntax and implementation may differ slightly, but the core concepts remain similar across both platforms.

Isolated React component development environment with a living style guide

Pros of React Styleguidist

  • More focused on documentation and style guide generation
  • Easier to set up and configure for simple use cases
  • Better support for Markdown documentation alongside components

Cons of React Styleguidist

  • Less flexible for complex testing scenarios
  • Limited support for state management and mocking
  • Fewer options for customizing the development environment

Code Comparison

React Styleguidist:

// Button.md
A simple button component:

```jsx
<Button>Click me</Button>

// Button.js import React from 'react';

const Button = ({ children }) => ( );

export default Button;


React Cosmos:

```jsx
// Button.fixture.js
import Button from './Button';

export default {
  component: Button,
  props: {
    children: 'Click me'
  }
};

// cosmos.config.json
{
  "staticPath": "public",
  "publicUrl": "/",
  "watchDirs": ["src"]
}

React Styleguidist is more suitable for projects that prioritize documentation and style guide generation, while React Cosmos offers more flexibility for complex testing scenarios and customization of the development environment. The choice between the two depends on the specific needs of your project and team preferences.

23,690

✍ It has never been so easy to document your things!

Pros of docz

  • Focused on documentation generation with built-in MDX support
  • Easy to set up and use, with a minimal configuration required
  • Provides a customizable theme system for styling documentation

Cons of docz

  • Less flexible for component development and testing compared to React Cosmos
  • May require additional setup for advanced component showcasing scenarios
  • Limited to documentation use cases, while React Cosmos offers broader development tools

Code Comparison

docz example:

---
name: Button
---

import { Playground, Props } from 'docz'
import { Button } from './Button'

# Button

<Props of={Button} />

## Basic usage

<Playground>
  <Button>Click me</Button>
</Playground>

React Cosmos example:

// Button.fixture.jsx
import { Button } from './Button';

export default {
  component: Button,
  props: { children: 'Click me' }
};

Both tools serve different primary purposes: docz focuses on documentation generation, while React Cosmos is geared towards component development and testing. docz excels in creating documentation websites with minimal setup, while React Cosmos provides a more comprehensive environment for component development, testing, and showcasing.

47,669

Fast, easy and reliable testing for anything that runs in a browser.

Pros of Cypress

  • Comprehensive end-to-end testing framework with built-in assertion library
  • Real-time test execution with interactive debugging capabilities
  • Extensive documentation and active community support

Cons of Cypress

  • Limited to testing web applications; not suitable for native mobile apps
  • Can be slower for large test suites compared to unit testing frameworks
  • Learning curve for developers new to end-to-end testing concepts

Code Comparison

React Cosmos:

import { fixture } from 'react-cosmos';

fixture({
  component: Button,
  props: { label: 'Click me' },
  state: { clicked: false }
});

Cypress:

describe('Button', () => {
  it('should display correct label', () => {
    cy.visit('/button-page');
    cy.get('button').should('have.text', 'Click me');
  });
});

React Cosmos focuses on component-level testing and visualization, while Cypress provides a more comprehensive end-to-end testing solution. React Cosmos allows developers to create fixtures for isolated component testing, whereas Cypress simulates user interactions in a full browser environment. The choice between the two depends on the specific testing needs of the project, with React Cosmos being more suitable for component-level testing and Cypress excelling in full application testing scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Cosmos

React Cosmos

Getting Started

Choose one of the Getting Started guides to dive into React Cosmos.

Check out the Next.js integration to get started with React Server Components.

Visit reactcosmos.org/docs to view the full documentation.

Community

To chat with other community members you can join the React Cosmos Discord.

You can also ask questions, voice ideas, and share your projects on GitHub Discussions.

Our Code of Conduct applies to all React Cosmos community channels.

Contributing

Please see our CONTRIBUTING.md.

Become a Sponsor to support the ongoing development of React Cosmos.

Live Demo

Visit reactcosmos.org/demo/ for a live demo of React Cosmos.

React Cosmos

NPM DownloadsLast 30 Days