UNPKG

6.68 kBMarkdownView Raw
1# settings-panel [![unstable](http://badges.github.io/stability-badges/dist/unstable.svg)](http://github.com/badges/stability-badges)
2
3Simple settings panel for your app, demo or tests.
4
5[![settings-panel](https://raw.githubusercontent.com/dfcreative/settings-panel/gh-pages/images/preview.png "settings-panel")](http://dfcreative.github.io/settings-panel/)
6
7In the preview there is a _typer_ theme, for other themes or customizations see [demo](http://dfcreative.github.io/settings-panel/).
8
9## Usage
10
11[![npm install settings-panel](https://nodei.co/npm/settings-panel.png?mini=true)](https://npmjs.org/package/settings-panel/)
12
13```javascript
14var createPanel = require('settings-panel')
15
16var panel = createPanel([
17 {type: 'range', label: 'my range', min: 0, max: 100, value: 20},
18 {type: 'range', label: 'log range', min: 0.1, max: 100, value: 20, scale: 'log'},
19 {type: 'text', label: 'my text', value: 'my cool setting', help: 'why this is cool'},
20 {type: 'checkbox', label: 'my checkbox', value: true},
21 {type: 'color', label: 'my color', format: 'rgb', value: 'rgb(10,200,0)', change: value => console.log(value)},
22 {type: 'button', label: 'gimme an alert', change: () => alert('hello!')},
23 {type: 'select', label: 'select one', options: ['option 1', 'option 2'], value: 'option 1'}
24],
25 {
26 title: 'Settings',
27 style: 'position: absolute; right: 0; z-index: 1'
28 }
29);
30```
31
32[**Run this in requirebin**](http://requirebin.com/?gist=21fc39f7f206ca50a4d5cd7298f8b9f8)
33
34## API
35
36`const Panel = require('settings-panel')`
37<details><summary>**`let panel = new Panel(fields, options?)`**</summary>
38
39The first argument is a list of fields or object with id/field pairs. Each field may have following properties:
40
41* `type` one of `range``interval``checkbox``color``select``switch``raw``textarea``text` or any `<input>` type. If undefined, type will be detected from the value.
42* `id` used as key to identify the field. If undefined, the label will be used instead.
43* `label` label for the input. If label is false, it will be hidden.
44* `value` current value of the field.
45* `default` explicitly defines default value, if differs from the initial value.
46* `orientation` defines position of a label relative to the input, one of `top`, `left`, `right`, `bottom`. Redefines `options.orientation`.
47* `style` appends additinal style to the field, can be a css object or css string.
48* `hidden` defines whether field should be visually hidden, but present as a value.
49* `disabled` just disables the input, making it inactive.
50* `input` callback, invoked if value changed.
51* `init` invoked once component is set up.
52* `change` invoked each time the field value changed, whether through `input` or API.
53* `before` and `after` define an html to display before or after the element, can be a string, an element or a function returning one of the two. That may come handy in displaying help, info or validation messages, separators, additional buttons, range limits etc - anything related to the element.
54* `title` will display text in tooltip.
55
56For example,
57
58```javascript
59{type: 'checkbox', label: 'My Checkbox', value: true, input: value => {}}
60```
61
62Some types have additional properties:
63
64- `range` can specify a `min`, `max`, and `step` (or integer `steps`). Scale can be either `'linear'` (default) or `'log'`. If a log scale, the sign of `min`, `max`, and `value` must be the same and only `steps` is permitted (since the step size is not constant on a log scale). It also takes `precision` optional parameter for the displayed value.
65- `interval` obeys the same semantics as `range` inputs, except the input and ouput is a two-element array corresponding to the low/high bounds, e.g. `value: [1, 7.5]`.
66- `color` can specify a `format` as either `rgb``hex``array`
67- `select`, `switch` and `checkbox` can specify `options`, either as an `Array` (in which case the value is the same as the option text) or as an object containing key/value pairs (in which case the key/value pair maps to value value/label pairs).
68- `text` and `textarea` can specify `placeholder`.
69- `raw` can define `content` method, returning HTML string, element or documentFragment.
70
71#### options
72
73```js
74// element to which to append the panel
75container: document.body,
76
77// a title to add to the top of the panel
78title: 'Settings',
79
80// specifies label position relative to the input: `top` • `left` • `bottom` • `right`
81orientation: 'left',
82
83// collapse by clicking on title
84collapsible: false,
85
86// use a theme, see `theme` folder.
87// available themes: typer, flat, control, dragon
88theme: require('settings-panel/theme/none'),
89
90//theme customization, can redefine theme defaults
91palette: ['black', 'white'],
92labelWidth: '9em',
93inputHeight: '1.6em',
94fontFamily: 'sans-serif',
95fontSize: 13,
96
97//additional css, aside from the theme’s one. Useful for custom styling
98css: '',
99
100//appends additional className to the panel element.
101className: ''
102```
103
104</details>
105<details><summary>**`panel.on(event, callback)`**</summary>
106
107Attach callback to `change`, `input` or `init` event.
108
109The callback will recieve `name`, `data` and `state` arguments:
110
111```javascript
112panel.on('change', (name, value, state) => {
113 // name === 'my checkbox'
114 // value === false
115 // state === {'my checkbox': false, 'my range': 75, ...}
116});
117```
118
119</details>
120<details><summary>**`panel.get(name?)`**</summary>
121
122Get the value of a field defined by `name`. Or get full list of values, if `name` is undefined.
123
124</details>
125<details><summary>**`panel.set(name, value|options)`**</summary>
126
127Update specific field, with value or field options. You can also pass an object or array to update multiple fields:
128
129```js
130panel.set({ 'my range': { min: -100, value: 200}, 'my color': '#fff' });
131```
132
133</details>
134<details><summary>**`panel.update(options?)`**</summary>
135
136Rerender panel with new options. Options may include values for the theme, like `palette`, `fontSize`, `fontFamily`, `labelWidth`, `padding` etc, see specific theme file for possible options.
137
138</details>
139
140## Spotted in the wild
141
142> [plot-grid](https://dfcreative.github.io/plot-grid)<br/>
143> [app-audio](https://dfcreative.github.io/app-audio)<br/>
144> [gl-waveform](https://audio-lab.github.io/gl-waveform)<br/>
145
146## See also
147
148> [control-panel](https://github.com/freeman-lab/control-panel) — original forked settings panel.<br/>
149> [oui](https://github.com/wearekuva/oui) — sci-ish panel.<br/>
150> [dat.gui](https://github.com/dataarts/dat.gui) — other oldschool settings panel.<br/>
\No newline at end of file