|
1 | 1 | # React dat.GUI
|
2 | 2 |
|
3 |
| -Currently very much under construction. |
| 3 | +This is a fully[*](#whats-missing) featured React port of Google's esteemed [dat.GUI](https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage) controller library. It comes packed with all the core components you'll need to cleanly integrate dat.GUIs into your React app. |
| 4 | + |
| 5 | +For those that haven't used or seen dat.GUI before, it's basically a GUI for updating and interacting with objects in real time. It's used extensively in canvas or WebGL rendering demos/apps for libraries such as [three.js](http://threejs.org) but it can also be used to build browser based editing software. |
| 6 | + |
| 7 | +## TOC |
| 8 | + |
| 9 | +- [Demo](#demo) |
| 10 | +- [Installation](#installation) |
| 11 | +- [Usage](#usage) |
| 12 | +- [Docs](#docs) |
| 13 | + * [`DatGui`](#datgui) |
| 14 | + + [props](#props) |
| 15 | + * [Components](#components) |
| 16 | + + [Common props](#common-props) |
| 17 | + + [`DatBoolean`](#datboolean) |
| 18 | + + [`DatButton`](#datbutton) |
| 19 | + + [`DatColor`](#datcolor) |
| 20 | + + [`DatFolder`](#datfolder) |
| 21 | + + [`DatNumber`](#datnumber) |
| 22 | + + [`DatPresets`](#datpresets) |
| 23 | + + [`DatSelect`](#datselect) |
| 24 | + + [`DatString`](#datstring) |
| 25 | +- [Scripts](#scripts) |
| 26 | + + [`build`](#build) |
| 27 | + + [`dev`](#dev) |
| 28 | + + [`dev:migrate`](#devmigrate) |
| 29 | + + [`dev:promote`](#devpromote) |
| 30 | + + [`example`](#example) |
| 31 | + + [`example:deploy`](#exampledeploy) |
| 32 | + + [`test`](#test) |
| 33 | + + [`test:watch`](#testwatch) |
| 34 | + + [`lint`](#lint) |
| 35 | + + [`lint:fix`](#lintfix) |
| 36 | + + [`toc`](#toc) |
| 37 | +- [What's missing](#whats-missing) |
| 38 | +- [Roadmap](#roadmap) |
4 | 39 |
|
5 | 40 | ## Demo
|
6 | 41 |
|
7 |
| -https://react-dat-gui.herokuapp.com/ |
| 42 | +[Checkout the demo!](https://rohan-deshpande.github.io/react-dat-gui/) |
| 43 | + |
| 44 | +The demo is a deployed version of the latest production build of `./example`. There's also a `dev` directory where you can prototype changes to the source code easily. Both of these have been bootstrapped with `create-react-app`. |
8 | 45 |
|
9 | 46 | ## Installation
|
10 | 47 |
|
11 | 48 | ```
|
12 |
| -npm install react-dat-gui --save |
| 49 | +npm i -S react-dat-gui |
13 | 50 | ```
|
14 | 51 |
|
15 | 52 | ## Usage
|
16 | 53 |
|
| 54 | +First you'll need a wrapper component which will handle the updates from your dat.GUI, this component should pass the data for the GUI to control as well as an `onUpdate` function to the `DatGui` container component as props. Here's how you might do that: |
| 55 | + |
17 | 56 | ```
|
18 |
| -<Dat data={this.state.data} onUpdate={this.handleUpdate}> |
19 |
| - <DatString path="string" label="String" /> |
20 |
| - <DatNumber path="number" label="Number" min={0} max={100} step={1} /> |
21 |
| - <DatBoolean path="boolean" label="Boolean" /> |
22 |
| - <DatButton label="Button" onClick={this.handleClick} /> |
23 |
| -</Dat> |
| 57 | +import '../node_modules/react-dat-gui/build/react-dat-gui.css'; |
| 58 | +import React, { Component } from 'react'; |
| 59 | +
|
| 60 | +import DatGui, { DatBoolean, DatButton, DatNumber, DatString } from 'react-dat-gui'; |
| 61 | +
|
| 62 | +class App extends Component { |
| 63 | + state = { |
| 64 | + data: { |
| 65 | + package: 'react-dat-gui', |
| 66 | + power: 9000, |
| 67 | + isAwesome: true, |
| 68 | + feelsLike: '#2FA1D6', |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | + update = data => this.setState({ data }) |
| 73 | +
|
| 74 | + render() { |
| 75 | + const { data } = this.state; |
| 76 | +
|
| 77 | + return ( |
| 78 | + <DatGui data={data} onUpdate={this.update}> |
| 79 | + <DatString path='package' label='Package' /> |
| 80 | + <DatNumber path='power' label='Power' min={9000} max={9999} step={1} /> |
| 81 | + <DatBoolean path='isAwesome' label='Awesome?' /> |
| 82 | + <DatColor path='feelsLike' label='Feels Like' /> |
| 83 | + </DatGui> |
| 84 | + ) |
| 85 | + } |
24 | 86 | ```
|
25 | 87 |
|
26 |
| -## CSS |
| 88 | +This will give you a dat.GUI controller which can perform live mutations to the `data` in the `App` component's state. |
| 89 | + |
| 90 | +## Docs |
| 91 | + |
| 92 | +### `DatGui` |
| 93 | + |
| 94 | +This is the main container component for your GUI and is the default export from the package. |
| 95 | + |
| 96 | +#### props |
| 97 | + |
| 98 | +##### required |
| 99 | + |
| 100 | +* `data` - The data your dat.GUI controller will mutate |
| 101 | +* `onUpdate` - The method which will be called whenever an update is handled by the controller |
| 102 | +* `children` - The dat.GUI components that make up the controller |
| 103 | + |
| 104 | +##### optional |
| 105 | + |
| 106 | +* `liveUpdate` - Determines if live updates should occur, defaults to `true` |
| 107 | +* `labelWidth` - The width of the labels in pixels, defaults to `40` |
| 108 | +* `className` - The class name to set on the `DatGui` div |
| 109 | +* `style` - The style object to set on the `DatGui` div |
| 110 | + |
| 111 | +### Components |
| 112 | + |
| 113 | +All of the `react-dat-gui` components should be rendered as children of your `DatGui` parent component. |
| 114 | + |
| 115 | +#### Common props |
| 116 | + |
| 117 | +These components will have a number of props implicitly passed to them via the `DatGui` parent component's `renderChildren` method, but can also require other props to be passed explicitly to them. |
| 118 | + |
| 119 | +Below are docs for the required and optional props you can pass to each component. Check the `renderChildren` method of `src/index.js` to see which other props are passed down implicitly. |
| 120 | + |
| 121 | +##### required |
| 122 | + |
| 123 | +* `path: string` - the path to the value within the `data` object which the component will control, eg., considering your object was `{ foo: 'bar' }`: `<DatString path='foo' />` |
| 124 | +* Note, this prop is not required for the following components |
| 125 | + * `DatButton` |
| 126 | + * `DatFolder` |
| 127 | + * `DatPresets` |
| 128 | + |
| 129 | +##### optional |
| 130 | + |
| 131 | +* `label: string` - the label for the controller eg., `<DatString path='message' label='Message' />` |
| 132 | + |
| 133 | +#### `DatBoolean` |
| 134 | + |
| 135 | +Used for controlling boolean values. Renders a checkbox input element. |
| 136 | + |
| 137 | +#### `DatButton` |
| 138 | + |
| 139 | +Can be used for performing any kind of function. Simply pass an `onClick` prop to the component and it will fire whenever the rendered element is clicked. |
| 140 | + |
| 141 | +##### props |
| 142 | + |
| 143 | +###### required |
| 144 | + |
| 145 | +* `onClick :func` - the function to perform with the rendered element is clicked |
| 146 | + |
| 147 | +#### `DatColor` |
| 148 | + |
| 149 | +Uses [`react-color`](https://github.com/casesandberg/react-color/) to render a color picker component that will control color values. |
| 150 | + |
| 151 | +#### `DatFolder` |
| 152 | + |
| 153 | +Component which wraps other components to render them within an expandable/collapsable nested folder. |
| 154 | + |
| 155 | +##### props |
| 156 | + |
| 157 | +###### required |
| 158 | + |
| 159 | + * `title: string` - The folder title eg., `<DatFolder title='MyAwesomeFolder' />` |
| 160 | + * `children: array` - The child components to render |
| 161 | + |
| 162 | +#### `DatNumber` |
| 163 | + |
| 164 | +A number component for updating numeric values. Will render a slider if `min`, `max` and `step` props are supplied. |
| 165 | + |
| 166 | +##### props |
| 167 | + |
| 168 | +###### optional |
| 169 | + |
| 170 | +* `min: number` - The minimum range for the number |
| 171 | +* `max: number` - The maximum range for the number |
| 172 | +* `step: number` - The amount the number should increment each tick |
| 173 | + |
| 174 | +#### `DatPresets` |
| 175 | + |
| 176 | +Presets for the object which your `DatGui` is controlling can be supplied to this component as items in its `options` prop. A select field will be rendered which will allow you to easily switch between the presets. |
| 177 | + |
| 178 | +Each item in this array will need to be in the format `{ 'presetName': ...data, ...preset }` where `...data` is your initial data and `...preset` is your preset. |
| 179 | + |
| 180 | +##### props |
| 181 | + |
| 182 | +###### required |
| 183 | + |
| 184 | +* `options: array` - An array of objects, each in the format `{ 'presetName': ...data, ...preset }` |
| 185 | + |
| 186 | +#### `DatSelect` |
| 187 | + |
| 188 | +A select component for updating a value with one of the options supplied via the `options` prop. The original value from the `path` will always be added to the passed options array as the first item. |
| 189 | + |
| 190 | +##### props |
| 191 | + |
| 192 | +###### required |
| 193 | + |
| 194 | +* `options: array` - A simple array of options to select from eg., `<DatSelect path='fruits' options={['apple', 'orange', 'pear']} />` |
| 195 | + |
| 196 | +#### `DatString` |
| 197 | + |
| 198 | +A simple text input component that can be used to mutate strings. |
| 199 | + |
| 200 | +## Scripts |
| 201 | + |
| 202 | +There are a few NPM scripts in the root `package.json` for developing changes to the repo's source code as well as running tests and deploying the demo. |
| 203 | + |
| 204 | +#### `build` |
| 205 | + |
| 206 | +Builds the package for publishing. |
| 207 | + |
| 208 | +#### `dev` |
| 209 | + |
| 210 | +Runs the app in `./dev`. |
| 211 | + |
| 212 | +#### `dev:migrate` |
| 213 | + |
| 214 | +Migrates the code in `./src` to `./dev/src/react-dat-gui`. Handy for making sure you're developing with the latest source code. |
| 215 | + |
| 216 | +#### `dev:promote` |
| 217 | + |
| 218 | +Promotes the code in `./dev/src/react-dat-gui` back up to the root of the repo. Use this when you're happy with the changes you've been developing. |
| 219 | + |
| 220 | +#### `example` |
| 221 | + |
| 222 | +Runs the app in `./example`. |
| 223 | + |
| 224 | +#### `example:deploy` |
| 225 | + |
| 226 | +Deploys the production build of the app in `./example` to the `gh-pages` branch of this repo. |
| 227 | + |
| 228 | +#### `test` |
| 229 | + |
| 230 | +Runs unit tests. |
| 231 | + |
| 232 | +#### `test:watch` |
| 233 | + |
| 234 | +Runs unit tests and watches for changes. |
| 235 | + |
| 236 | +#### `lint` |
| 237 | + |
| 238 | +Runs `eslint` on the supplied path. |
| 239 | + |
| 240 | +#### `lint:fix` |
| 241 | + |
| 242 | +Runs `eslint --fix` on the supplied path. |
| 243 | + |
| 244 | +#### `toc` |
| 245 | + |
| 246 | +Prints the `README.md` table of contents into the console. |
| 247 | + |
| 248 | +## What's missing |
| 249 | + |
| 250 | +There are still a few features from the original implementation missing from this package. These are mainly related to saving and loading data as well as local storage. Animations for folder expanding/collapsing is also not currently implemented, but shouldn't be too hard to do. |
| 251 | + |
| 252 | +For the first, I think the fact that this is now an NPM module sort of goes against it handling this sort of stuff. Google's original concept was basically a plug and play controller that could do everything if you just slam it into the browser and pass it an object. However, in module form, it's expected that you'll most likely be integrating this with an existing application. In that case, you'll probably have pretty specific needs around how you would like to save/load data into your GUI and so it's been left out for now. |
| 253 | + |
| 254 | +Local storage however is in the roadmap and will probably be done very soon. |
27 | 255 |
|
28 |
| - * SCSS: `./src/Dat.scss` |
29 |
| - * CSS: `./lib/Dat.css` |
30 |
| - * CSS minified: `./build/react-dat-gui.min.css` |
| 256 | +## Roadmap |
| 257 | +* Loading and storing both default and preset data via `localStorage` |
| 258 | +* Time travel with undo/redo buttons (edited) |
0 commit comments