This is a fully* featured React port of Google's esteemed dat.GUI controller library. It comes packed with all the core components you'll need to cleanly integrate dat.GUIs into your React app.
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 but it can also be used to build browser based editing software.
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
.
npm install react-dat-gui --save
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:
import 'react-dat-gui/build/react-dat-gui.css';
import React, { Component } from 'react';
import DatGui, { DatBoolean, DatButton, DatNumber, DatString } from 'react-dat-gui';
class App extends Component {
state = {
data: {
package: 'react-dat-gui',
power: 9000,
isAwesome: true,
feelsLike: '#2FA1D6',
}
}
update = data => this.setState({ data })
render() {
const { data } = this.state;
return (
<DatGui data={data} onUpdate={this.update}>
<DatString path='package' label='Package' />
<DatNumber path='power' label='Power' min={9000} max={9999} step={1} />
<DatBoolean path='isAwesome' label='Awesome?' />
<DatColor path='feelsLike' label='Feels Like' />
</DatGui>
)
}
This will give you a dat.GUI controller which can perform live mutations to the data
in the App
component's state.
This is the main container component for your GUI and is the default export from the package.
data
- The data your dat.GUI controller will mutateonUpdate
- The method which will be called whenever an update is handled by the controllerchildren
- The dat.GUI components that make up the controller
liveUpdate
- Determines if live updates should occur, defaults totrue
labelWidth
- The width of the labels in pixels, defaults to40
className
- The class name to set on theDatGui
divstyle
- The style object to set on theDatGui
div
All of the react-dat-gui
components should be rendered as children of your DatGui
parent component.
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.
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.
path: string
- the path to the value within thedata
object which the component will control, eg., considering your object was{ foo: 'bar' }
:<DatString path='foo' />
- Note, this prop is not required for the following components
DatButton
DatFolder
DatPresets
label: string
- the label for the controller eg.,<DatString path='message' label='Message' />
Used for controlling boolean values. Renders a checkbox input element.
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.
onClick :func
- the function to perform with the rendered element is clicked
Uses react-color
to render a color picker component that will control color values.
Component which wraps other components to render them within an expandable/collapsable nested folder.
title: string
- The folder title eg.,<DatFolder title='MyAwesomeFolder' />
children: array
- The child components to render
A number component for updating numeric values. Will render a slider if min
, max
and step
props are supplied.
min: number
- The minimum range for the numbermax: number
- The maximum range for the numberstep: number
- The amount the number should increment each tick
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.
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.
options: array
- An array of objects, each in the format{ 'presetName': ...data, ...preset }
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.
options: array
- A simple array of options to select from eg.,<DatSelect path='fruits' options={['apple', 'orange', 'pear']} />
A simple text input component that can be used to mutate strings.
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.
Builds the package for publishing.
Runs the app in ./dev
.
Migrates the code in ./src
to ./dev/src/react-dat-gui
. Handy for making sure you're developing with the latest source code.
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.
Runs the app in ./example
.
Deploys the production build of the app in ./example
to the gh-pages
branch of this repo.
Runs unit tests.
Runs unit tests and watches for changes.
Runs eslint
on the supplied path.
Runs eslint --fix
on the supplied path.
Prints the README.md
table of contents into the console.
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.
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.
Local storage however is in the roadmap and will probably be done very soon.
- Loading and storing both default and preset data via
localStorage
- Time travel with undo/redo buttons (edited)
- Better support for floating point
DatNumber
s (rounding etc.)