All is needed is to include the knobs script on the page, and set it up.
knobs
An array of Objects, where the properties describe a knob.
It is possible to define/update the knobs Array after instance initialization, like so:
var myKnobs = new Knobs({ CSSVarTarget:document.body }) // only if working with CSS variables
myKnobs.knobs = [{...}, ...] // Add/change the knobs. will automatically re-render (see example further below)
All defined knob properties, beside a special few, are attributes that
are applied on the HTML input element that controls the knob, so it is up
to the developer who set up the knobs to use the appropriate attributes, for
each type of of the supported knobs (range, color, checkbox).
The special other properties are:
onChange
Callback which fires on every input event
cssVar
Optional. An array of 3 items:
- (
String) - CSS variable name
- (
String) - Units (optional - Ex. % or px)
- (
HTML NODE) - Reference to an HTML node to apply the knob's CSS variable on (optional)
Sometimes it is wanted for variables to be defined unitless, for calculation-purposes, like so:
div{
--size: 10;
/* limits with width to a minimum of 10px by using unitless variable for the "max" function */
width: calc(Max(50, var(--size)) * 1px);
}
So, when a unitless-variable is desired, but ultimatly it will have a unit, then units (2nd item in the array)
should be written with a dash prefix, Ex.: -px, and it will be displayed in the label correctly but ignored when
applying the variable.
cssVarsHSLA (boolean)
Applies only to color knobs and if set to true will generate 4 CSS variables for the HSLA version of the color.
--main-color-h, --main-color-s, --main-color-l & --main-color-a.
{
cssVar: ['main-color'],
cssVarsHSLA: true,
label: 'Page background',
type: 'color',
defaultFormat: 'hsla',
},
defaultFormat (string)
Applies only to color knobs. Sets the default format displayed to the user and also the value which will
be set to the input. Possible values are: hsla, rgba, hex.
label (string)
A text which is displayed alongside the knob
labelTitle (string)
Optional title attribute for the knob's label
value (string, number)
Acts as the initial value of the knob, except for checkbox knobs, in which case,
if the knob also has cssVar property set, then the checkbox is checked, that CSS variable
value will be the value property of the knob, Ex.
{
cssVar: ['hide'], // CSS variable name "--hide"
label: 'Show',
type: 'checkbox',
// checked: true, // not checked by default
value: 'none', // if checked: --hide: none;
}
Then in your CSS you can write the below, so when --hide is not defined,
block is used as the display property value.
display: var(--hide, block);
It is possible to use an already-declared CSS-varaible (on the target element) by emmiting the value
prop from the knob decleration. The program will try to get the value using getComputedStyle and getPropertyValue.
Variables which has calc or any other computations might result in NaN. In which case, a console.warn will be presented
and a manually typed value property for the knob would be advised.
isToggled (boolean)
If this property is set to false, the knob will be toggled off by default.
Will only take affect if knobsToggle setting is set to true
options (array)
Used for knobs of type select. An Array of options to render.
[20, 150, [200, '200 nice pixels'], 500]
An option can be split to the actual value it represents and its textual value, as the above example shows.
knobClass (string)
Add your own class to the knob (row) element itself (for styling purposes).
Remember that in order to add custom styles, the theme.styles setting should be used, because all knobs
are encapsulated within an iframe so your page styles won't affect anything that's inside.
render (string)
Allows to render anything you want in the knob area.
Should return a string of HTML, for example:
{
render: `
<button onclick='alert(1)'>1</button>
<button onclick='alert(2)'>2</button>
`,
knobClass: 'custom-actions'
}
script (function)
A function to be called which has logic related to the custom HTML in the render property (shown above).
The function recieves 2 arguments: The knobs instance referece and the (auto)generated knob name string.
{
label: 'Custom HTML with label',
render: `
<button type='button' class='specialBtn1'>1</button>
<button type='button' class='specialBtn2'>2</button>
`,
script(knobs, name){
knobs.getKnobElm(name).addEventListener("click", e => {
if( e.target.tagName == 'BUTTON' )
alert(e.target.textContent)
})
},
},