sinuous-bind
About
sinuous-bind
brings the bind
element directives of Svelte to Sinuous.
Don't do
`<input value= oninput= />`
Do
`<input bind= />`
size | esm | cdn |
---|---|---|
sinuous-bind |
https://unpkg.com/sinuous-bind@latest/dist/all.min.js | |
sinuous-bind/bind |
https://unpkg.com/sinuous-bind@latest/dist/bind.min.js | |
sinuous-bind/bindArea |
https://unpkg.com/sinuous-bind@latest/dist/bindArea.min.js | |
sinuous-bind/bindGroup |
https://unpkg.com/sinuous-bind@latest/dist/bindGroup.min.js | |
sinuous-bind/bindMedia |
https://unpkg.com/sinuous-bind@latest/dist/bindMedia.min.js |
Installation
There are two ways to consume sinuous-bind:
ESM
Run the following inside your project directory to get all the bind packages:
npm install sinuous-bind
Setup
Import all the packages into the root of your project like so:
;
Alternatively, bring in any or all of the packages, depending on what you need:
;;;;
Gotchas
These packages are not tree-shakeable. If you import the submodule, you will be stuck with it in your bundle.
CDN
Put this in your html to get all the packages:
Alternatively, you can bring in just what you need (though you must include the top one):
You must also bring in Sinuous:
Setup
If you have brought in sinuous-bind/dist/all.min.js
, setup is something like what follows:
windowbindAll;
Alternatively, if you brought in only sinuous-bind/dist/bind.min.js
, setup is as follows:
windowbind;
If you have brought in more than one package, setup is something like this:
let w = window;let register enableBind = wbind;;;;;
Gotchas
Unlike the esm format, the cdn format requires the basic bind package (sinuous-bind/dist/bind.min.js
) for any of the others to be enabled.
sinuous-bind/bind
bind
can take two kinds of arguments
bind=${<observable>}
bind=${[<observable> [, <attribute> [, <event> [, <boolean>]]]]}
<attribute>
defaults to"value"
<event>
defaults to"oninput"
<boolean>
- Pass in
true
to set the attribute with a Sinuoussubscribe
. This is necessary for things like acontentEditable
binding totextContent
orinnerHTML
.
- Pass in
Generally, bind=${state}
is sufficient.
bind
will attempt to coerce values to numerical values, but will not attempt to coerce booleans or the empty string.
<input>
(implicit)
{ const state = ; return html` `;}
<input>
(explicit)
{ const state = ; return html` `;}
<input type="range">
{ const state = ; return html` `;}
contentEditable
{ const state = ; return html` `;}
Takes "textContent"
or "innerHTML"
as the second element of the array. Requires true
as the fourth element.
<input type="checkbox">
{ const state = ; return html` `;}
<input type="radio">
{ const state = ; return html` `;}
<select>
{ const state = ; return html` 1 2 3 `;}
Gotchas: It won't work with the multiple
attribute. Also, it won't sync until user action. If you need either of these things, use sinuous-bind/bindGroup
ref
{ const ref = ; return html` `;}
Gotchas: The value will not be passed into the ref
observable until the call to html
is made. The DOM node has to be created before it can be passed to ref
.
sinuous-bind/bindArea
Both bind:area
and bindArea
work.
All bind:area
bindings are read-only.
bind:area
accepts an object of observables, with any of the following keys:
clientWidth
, clientHeight
, offsetWidth
, offsetHeight
, scrollLeft
, scrollTop
All these keys can be updated whenever the element is resized. Additionally, scrollLeft
and scrollTop
are updated whenever the element is scrolled.
Effects: Set a function on the object passed to bind:area
with the key effect
. The effect
will be called on resize and scroll. Set a function with any other key, and it will be called on resize, only.
Gotchas: bind:area
sets position: relative
on the bound element. Do not set the style on the element unless you set position
.
single property
{ const clientWidth = ; return html` `;}
multiple properties
{ const clientWidth = ; const clientHeight = ; const offsetHeight = ; return html` `;}
multiple properties with scrolling
{ const clientWidth = ; const clientHeight = ; const scrollLeft = ; const scrollTop = ; return html` `;}
multiple properties with effects
{ const clientWidth = ; const arbitraryEffect = console; const scrollLeft = ; const effect = console; return html` `;}
sinuous-bind/bindGroup
Both bind:group
and bindGroup
work.
Like bind
, bind:group
will attempt to coerce values to numerical values, but will not attempt to coerce booleans or the empty string.
select
{ const state = ; return html` 1 2 3 `;}
select multiple
{ const state = ; return html` 1 2 3 `;}
radio group
{ const state = ; return html` `;}
checkbox group
{ const state = ; return html` `;}
sinuous-bind/bindMedia
Both bind:media
and bindMedia
work.
Read-only Properties: duration, buffered, seekable, seeking, played, ended
Read/Write Properties: currentTime, playbackRate, paused, volume
Video-only Properties videoWidth, videoHeight
video
{ const currentTime = ; const playbackRate = ; const paused = ; const volume = ; const duration = ; const buffered = ; const seekable = ; const seeking = ; const played = ; const ended = ; const videoWidth = ; const videoHeight = ; const state = currentTime playbackRate paused volume duration buffered seekable seeking played ended videoWidth videoHeight ; return html` `;}
audio
{ const currentTime = ; const playbackRate = ; const paused = ; const volume = ; const duration = ; const buffered = ; const seekable = ; const seeking = ; const played = ; const ended = ; const state = currentTime playbackRate paused volume duration buffered seekable seeking played ended ; return html` `;}
Acknowledgments and Thanks
- Author of Sinuous
Rich Harris and the rest of the Svelte team
- These packages are a port of much of the bind directive functionality present in the Svelte framework