# Fields Fields are the settings of a widget. Each field in `fields.json` becomes a control in the editor's settings, and its value can be used in the HTML, CSS and JS. The values are kept in `data.json`. ## Fields JSON `fields.json` is an object: each key is a field's name, each value describes the field. ```json { "title": { "type": "text", "label": "Title", "value": "New follower!", "group": "Texts" }, "fontSize": { "type": "slider", "label": "Font size (px)", "value": 32, "min": 16, "max": 72, "step": 1, "group": "Style" } } ``` | Property | For | Meaning | |---|---|---| | `type` | all | The control, see below. Required. | | `label` | all | Its label. The field's name when missing. | | `value` | all | The default value. For a button: the value sent with its event. | | `group` | all | Fields with the same group are shown in one collapsible section. | | `options` | `dropdown` | `{ "value": "Label", … }` | | `min`, `max`, `step` | `number`, `slider` | Limits of the value. | | `multiple` | `image-input`, `video-input`, `sound-input` | Several files: the value becomes a list. | A field name has 1-64 letters, digits, `_` or `-`, and does not start with a digit. `__proto__`, `constructor` and `prototype` are not allowed. ## Types | `type` | Control | Value | |---|---|---| | `text` | One line of text | text | | `textarea` | Several lines of text | text | | `number` | Number box | number (`""` when emptied) | | `slider` | Slider | number | | `checkbox` | Switch | `true` or `false` | | `colorpicker` | Color picker with transparency | text, e.g. `"#9147ff"` or `"rgba(20, 20, 28, 0.85)"` | | `dropdown` | Select box | the key of the chosen option (text) | | `googleFont` | Font picker | a font's name, e.g. `"Poppins"` | | `image-input` | Image from your media | the image's URL (`""` when none) | | `video-input` | Video from your media | the video's URL | | `sound-input` | Sound from your media | the sound's URL | | `button` | Button | none, see below | | `hidden` | Not shown | anything; use it for constants | An unknown type is shown as `text`. **Fonts.** A font chosen in a `googleFont` field is loaded for you from Google Fonts, in all weights. Use it in CSS as `font-family: '{{fontName}}', sans-serif;`. The picker also offers fonts every computer has. **Media.** Media fields store which file of your media gallery was picked; the widget receives its full URL. With `"multiple": true` the value is a list of URLs. ### Button A `button` field shows a button in the settings. Clicking it fires `onEventReceived` with the listener `widget-button` in every open copy of the widget (the preview and OBS): ```js window.addEventListener('onEventReceived', (obj) => { const { listener, event } = obj.detail; if (listener === 'widget-button' && event.field === 'testAlert') { // event.value is the button field's "value" showAlert({ type: 'follower', name: 'TestUser' }); } }); ``` ## Data JSON `data.json` holds the values set in the editor: `{ "fieldName": value, … }`. A value in Data overrides the field's default `value`. You rarely edit it by hand: the settings controls write it. Values are text, numbers, `true`/`false`, `null`, or lists of those. The value a widget gets for a field (its "field data") is: the value in Data, else the field's `value`, else nothing. ## Using values ### In HTML, CSS and JS: placeholders `{{fieldName}}` (or `{fieldName}`) is replaced with the field's value before the widget loads, in all three. Spaces are allowed only inside double braces: `{{ fieldName }}` is replaced, `{ fieldName }` is not. A list becomes its items joined with `,`. A placeholder whose name is not a field stays as it is. ```html
{{title}}
``` ```css .title { font-size: {{fontSize}}px; color: {{textColor}}; } ``` > **Watch out in JS.** Placeholders are replaced as plain text, unescaped, before your code runs: > > - `{name}` counts as a placeholder too. With a field called `name`, the template literal `` `Hi ${name}` `` becomes `` `Hi $PixelPanda` ``. Avoid field names that are also variable names in your code, or read values from `fieldData` instead. > - A text value can contain quotes or line breaks and then breaks your code: `const title = "{{title}}";` fails for `Say "hi"`. In JS, read values from `fieldData` (below). > - In HTML, a value is inserted as HTML. That is fine for your own settings, but never put viewer input there. ### In JS: fieldData `onWidgetLoad` gives the values as `obj.detail.fieldData`, with their real types (numbers stay numbers, lists stay lists): ```js let fields = {}; window.addEventListener('onWidgetLoad', (obj) => { fields = obj.detail.fieldData; document.querySelector('.title').textContent = fields.title; }); ``` ## Reserved field: widgetDuration A field called `widgetDuration` (a number, in seconds; usually a `hidden` field) turns on the event queue: alerts are delivered one at a time, each one waiting until the previous one has played for that long or the widget calls `SE_API.resumeQueue()`. [Lifecycle: the event queue](https://streamed.hu/docs/lifecycle.md#the-event-queue) ```json { "widgetDuration": { "type": "hidden", "value": 30 } } ``` ## Changing a value from code `SE_API.setField(key, value)` changes a value while the widget runs. In the editor's preview the new value also shows in the settings, unsaved, and the preview reloads. On stream it is not saved anywhere. Call it in response to something (a button, an event). In the editor, every call that changes a value reloads the preview, so a call in `onWidgetLoad` that sets a new value each time (a time, a random number) reloads it without end. ## Limits At most 200 fields and 300 values in Data. A text value is at most 64 KB, a list at most 50 items, a dropdown at most 1000 options. Labels are at most 256 characters, group names at most 128.