Gathering detailed insights and metrics for @yaireo/knobs
Gathering detailed insights and metrics for @yaireo/knobs
npm install @yaireo/knobs
Typescript
Module System
Node Version
NPM Version
JavaScript (58.42%)
SCSS (29.49%)
HTML (12.09%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
124,385
Last Day
39
Last Week
269
Last Month
1,372
Last Year
23,117
AGPL-3.0 License
319 Stars
202 Commits
6 Forks
6 Watchers
1 Branches
2 Contributors
Updated on Mar 11, 2025
Minified
Minified + Gzipped
Latest Version
1.3.6
Package Id
@yaireo/knobs@1.3.6
Unpacked Size
127.76 kB
Size
39.16 kB
File Count
20
NPM Version
8.12.2
Node Version
14.20.1
Published on
Apr 02, 2023
Cumulative downloads
Total Downloads
Last Day
8.3%
39
Compared to previous day
Last Week
-2.9%
269
Compared to previous week
Last Month
-11%
1,372
Compared to previous month
Last Year
-50.1%
23,117
Compared to previous year
|
What is this:Started as something I needed for my many Codepens - A way to provide viewers, and myself, a set of controllers, for manipulating the DOM instantaneously. Imagine certain aspects of a web page, or a specific *component*, which you would like to add the ability to control on-the-fly. Change the visual looks or certain javascript parameters with a move or a slider. CSS-variables (custom properties) are a great match for this script as they compute in real-time. Javascript is of course a benefitor because every knobs can be attached with a callback that recieves the current value, and additional data, as to what that value should be applied on. It's so easy & quick to use Knobs, about 1 minute! ⚠️ Supported only in modern browsers |
range
input (wheel-supported)color
input with awesome custom color pickercheckbox
inputradio
inputs groupselect
dropdown (native)0
- Starts as hidden1
- Starts as visible2
- Always visibleposition
(relative to window viewport):
top right
(default)bottom right
top left
bottom left
All is needed is to include the knobs script on the page, and set it up.
1new Knobs(settings)
Name | Type | Default | Info |
---|---|---|---|
theme | Object | Knobs theme variables. Since the Knobs are encapsulated within an iframe, they cannot be be styled from outside. | |
live | Boolean | true | Should changes be immediately applied |
persist | Boolean | false | Persist changes using the browser's localstorage. Store key/value per knob, where key is the knob's label. |
visible | Number | 0 | 0 - Starts as hidden1 - Starts as visible2 - Always visible |
CSSVarTarget | Element /NodeList | Global HTML element(s) for which to apply the CSS custom properties. Can also be configured per-knob. | |
knobsToggle | Boolean | false | if true - adds a checkbox next to each knob, which allows temporarily disabling the knob, reverting to default |
knobs | Array | Array of Objects describing the knobs controllers on-screen | |
standalone | Boolean | false | if true - does not create an iframe and appends it to the page, but simply gives the developer the DOM node, as is, to inject manually with knobs.DOM.scope node. Note that CSS in also needed ('./src/styles/styles.scss`) |
1{ 2 styles : ``, // optioanlly add any CSS and it will be injected into the iframe 3 flow : 'horizontal', // use 'compact' to keep things tight 4 position : 'top right', 5 primaryColor: '#0366D6', // mainly for links / range sliders 6 'base-color': "rgba(0,0,0,1)", // mainly for the background color but also for input fields such as text or number 7 textColor : "#CCC", 8 border : 'none' 9}
An array of Objects, where the properties describe a knob.
It is possible to define/update the knobs
Array after instance initialization, like so:
1var myKnobs = new Knobs({ CSSVarTarget:document.body }) // only if working with CSS variables 2 3myKnobs.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 nameString
) - 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:
1div{ 2 --size: 10; 3 /* limits with width to a minimum of 10px by using unitless variable for the "max" function */ 4 width: calc(Max(50, var(--size)) * 1px); 5}
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
.
1{ 2 cssVar: ['main-color'], 3 cssVarsHSLA: true, 4 label: 'Page background', 5 type: 'color', 6 defaultFormat: 'hsla', 7},
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.
1{ 2 cssVar: ['hide'], // CSS variable name "--hide" 3 label: 'Show', 4 type: 'checkbox', 5 // checked: true, // not checked by default 6 value: 'none', // if checked: --hide: none; 7}
Then in your CSS you can write the below, so when --hide
is not defined,
block
is used as the display
property value.
1display: 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:
1{ 2 render: ` 3 <button onclick='alert(1)'>1</button> 4 <button onclick='alert(2)'>2</button> 5 `, 6 knobClass: 'custom-actions' 7}
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.
1{ 2 label: 'Custom HTML with label', 3 render: ` 4 <button type='button' class='specialBtn1'>1</button> 5 <button type='button' class='specialBtn2'>2</button> 6 `, 7 script(knobs, name){ 8 knobs.getKnobElm(name).addEventListener("click", e => { 9 if( e.target.tagName == 'BUTTON' ) 10 alert(e.target.textContent) 11 }) 12 }, 13},
npm i @yaireo/knobs
CDN source:
https://unpkg.com/@yaireo/knobs@latest
Knobs
1import Knobs from '@yaireo/knobs'
format
& CSStoHSLA
are defined on Knobs' instances in color
property, for example:
1const myKnobs = new Knobs({
2 ...,
3 knobs: [
4 {
5 cssVar: ['bg'], // alias for the CSS variable
6 label: 'Color',
7 type: 'color',
8 value: '#45FDA9',
9 onChange(e, knobData, hsla) => {
10 console.log( myKnobs.format(knobData.value, 'rgb') ) // will print a color string in RGBA
11 }
12 }
13 ]
14})
15
16myKnobs.color.format()
1var settings = { 2 theme: { 3 position: 'bottom right', // default is 'top left' 4 }, 5 6 // should update immediately (default true) 7 live: false, 8 9 // 0 - starts as hidden, 1 - starts as visible, 2 - always visible 10 visible: 0, 11 12 CSSVarTarget: document.querySelector('.testSubject'), 13 14 knobs: [ 15 { 16 cssVar: ['width', '-px'], // prefix unit with '-' makes it only a part of the title but not of the variable 17 label: 'Width', 18 labelTitle: 'Changes the width at steps of 50 pixels', 19 type: 'range', 20 value: 200, 21 min: 0, 22 max: 500, 23 step: 50, 24 onChange: console.log // javascript callback on every "input" event 25 }, 26 27 { 28 cssVar: ['width', '-px'], 29 label: 'Width preset', 30 type: 'select', 31 options: [20, 150, [200, '200 nice pixels'], 500], 32 value: 150, // should be one of the options 33 defaultValue: 150 // value for which to reset to (optional) 34 isToggled: false, // this knob will not take affect by default 35 }, 36 37 { 38 cssVar: ['height', 'vh'], 39 label: 'Height', 40 type: 'range', 41 // value: 20, // if a value is not defined, Knobs will try to get it from the CSS ("CSSVarTarget" selector) automatically 42 min: 0, 43 max: 100, 44 onChange: console.log 45 }, 46 47 { 48 cssVar: ['align'], 49 label: 'Align boxes', 50 type: 'radio', 51 name: 'align-radio-group', 52 options: [ 53 { value:'left', hidden:true, label: '<svg ...' }, 54 { value: 'center', label:'Center' }, 55 { value:'right', hidden:true, label:'<svg ...' } 56 ], 57 value: 'center', 58 defaultValue: 'left' 59 }, 60 61 { 62 cssVar: ['radius', '%'], 63 label: 'Radius of the big square here', 64 type: 'range', 65 value: 0, 66 min: 0, 67 max: 50, 68 onChange: console.log 69 }, 70 71 "Label example", 72 73 { 74 cssVar: ['bg'], // alias for the CSS variable 75 label: 'Color', 76 type: 'color', 77 defaultFormat: 'hsla', 78 cssVarsHSLA: true, 79 value: '#45FDA9', 80 swatches: ['red', 'gold'], // swatches which can be selected inside the color picker 81 onChange: (e, knobData, hsla) => console.log(e, knobData, hsla, knobData.value) 82 }, 83 84 { 85 cssVar: ['main-bg', null, document.body], // [alias for the CSS variable, units, applies on element] 86 label: 'Background', 87 type: 'color', 88 value: '#FFFFFF', 89 onChange: (e, knobData, hsla) => console.log(e, knobData, hsla, knobData.value) 90 }, 91 92 ["Label example", false] // group is collapsed by default 93 { 94 cssVar: ['hide'], // alias for the CSS variable 95 label: 'Show', 96 type: 'checkbox', 97 // checked: true, // default 98 value: 'none', 99 onChange: console.log 100 }, 101 102 { 103 label: 'Custom with label', 104 render: ` 105 <button type='button' class='specialBtn1'>1</button> 106 <button type='button' class='specialBtn2'>2</button> 107 `, 108 script(knobs, name){ 109 knobs.getKnobElm(name).addEventListener("click", e => { 110 if( e.target.tagName == 'BUTTON' ) 111 alert(e.target.textContent) 112 }) 113 }, 114 }, 115 116 { 117 render: ` 118 <button type='button' class='specialBtn3'>😎</button> 119 `, 120 script(knobs){ 121 const elm = knobs.DOM.scope.querySelector('.specialBtn3') 122 elm.addEventListener("click", () => alert('😎')) 123 }, 124 knobClass: 'custom-actions' 125 } 126 ] 127} 128 129var penKnobs = new Knobs(settings)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
7 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-03-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More