Playroom

Design with JSX, powered by your own component library.

README

PlayroomPlayroom
npm Build Status


Playroom Demo

Simultaneously design across a variety of themes and screen sizes, powered by JSX and your own component library.

Playroom allows you to create a zero-install code-oriented design environment, built into a standalone bundle that can be deployed alongside your existing design system documentation.

- Iterate on your designs in the final medium.
- Create quick mock-ups and interactive prototypes with real code.
- Exercise and evaluate the flexibility of your design system.
- Share your work with others by simply copying the URL.

Demos





Cubes (Themed)




Send us a PR if you'd like to be in this list!

Getting Started


  1. ``` sh
  2. $ npm install --save-dev playroom
  3. ```

Add the following scripts to your package.json:

  1. ``` json
  2. {
  3.   "scripts": {
  4.     "playroom:start": "playroom start",
  5.     "playroom:build": "playroom build"
  6.   }
  7. }
  8. ```

Add a playroom.config.js file to the root of your project:

  1. ``` js
  2. module.exports = {
  3.   components: './src/components',
  4.   outputPath: './dist/playroom',

  5.   // Optional:
  6.   title: 'My Awesome Library',
  7.   themes: './src/themes',
  8.   snippets: './playroom/snippets.js',
  9.   frameComponent: './playroom/FrameComponent.js',
  10.   scope: './playroom/useScope.js',
  11.   widths: [320, 768, 1024],
  12.   port: 9000,
  13.   openBrowser: true,
  14.   paramType: 'search', // default is 'hash'
  15.   exampleCode: `
  16.     <Button>
  17.       Hello World!
  18.     </Button>
  19.   `,
  20.   baseUrl: '/playroom/',
  21.   webpackConfig: () => ({
  22.     // Custom webpack config goes here...
  23.   }),
  24.   iframeSandbox: 'allow-scripts',
  25. };
  26. ```

_Note: port and openBrowser options will be set to 9000 and true (respectively) by default whenever they are omitted from the config above._

Your components file is expected to export a single object or a series of named exports. For example:

  1. ``` js
  2. export { default as Text } from '../Text'; // Re-exporting a default export
  3. export { Button } from '../Button'; // Re-exporting a named export
  4. // etc...
  5. ```

The iframeSandbox option can be used to set the [sandbox attribute](https://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/) on Playroom's iframe. A minimum of allow-scripts is required for Playroom to work.

Now that your project is configured, you can start a local development server:

  1. ``` sh
  2. $ npm run playroom:start
  3. ```

To build your assets for production:

  1. ``` sh
  2. $ npm run playroom:build
  3. ```

Snippets


Playroom allows you to quickly insert predefined snippets of code, providing live previews across themes and viewports as you navigate the list. These snippets can be configured via a snippets file that looks like this:

  1. ``` js
  2. export default [
  3.   {
  4.     group: 'Button',
  5.     name: 'Strong',
  6.     code: `
  7.       <Button weight="strong">
  8.         Button
  9.       </Button>
  10.     `,
  11.   },
  12.   // etc...
  13. ];
  14. ```

Custom Frame Component


If your components need to be nested within custom provider components, you can provide a custom React component file via the frameComponent option, which is a path to a file that exports a component. For example, if your component library has multiple themes:

  1. ``` js
  2. import React from 'react';
  3. import { ThemeProvider } from '../path/to/your/theming-system';

  4. export default function FrameComponent({ theme, children }) {
  5.   return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
  6. }
  7. ```

Custom Scope


You can provide extra variables within the scope of your JSX via the scope option, which is a path to a file that exports a useScope Hook that returns a scope object. For example, if you wanted to expose a context-based theme variable to consumers of your Playroom:

  1. ``` js
  2. import { useTheme } from '../path/to/your/theming-system';

  3. export default function useScope() {
  4.   return {
  5.     theme: useTheme(),
  6.   };
  7. ```

Theme Support


If your component library has multiple themes, you can customise Playroom to render every theme simultaneously via the themes configuration option.

Similar to your components file, your themes file is expected to export a single object or a series of named exports. For example:

  1. ``` js
  2. export { themeA } from './themeA';
  3. export { themeB } from './themeB';
  4. // etc...
  5. ```

TypeScript Support


If a tsconfig.json file is present in your project, static prop types are parsed using react-docgen-typescript to provide better autocompletion in the Playroom editor.

By default, all .ts and .tsx files in the current working directory are included, excluding node_modules.

If you need to customise this behaviour, you can provide a typeScriptFiles option in playroom.config.js, which is an array of globs.

  1. ``` js
  2. module.exports = {
  3.   // ...
  4.   typeScriptFiles: ['src/components/**/*.{ts,tsx}', '!**/node_modules'],
  5. };
  6. ```

If you need to customise the parser options, you can provide areactDocgenTypescriptConfig option in playroom.config.js.

For example:

  1. ``` js
  2. module.exports = {
  3.   // ...
  4.   reactDocgenTypescriptConfig: {
  5.     propFilter: (prop, component) => {
  6.       // ...
  7.     },
  8.   },
  9. };
  10. ```

Storybook Integration


If you are interested in integrating Playroom into Storybook, check out storybook-addon-playroom.

License


MIT.