@@ -21,49 +21,19 @@ static template(html, { map }) {
2121}
2222```
2323
24- The following binding types are supported:
25-
26- | Type | Example |
27- | :------------------ | :----------------------------------------- |
28- | attribute | ` <span id="target" foo="${bar}"></span> ` |
29- | attribute (boolean) | ` <span id="target" ?foo="${bar}"></span> ` |
30- | attribute (defined) | ` <span id="target" ??foo="${bar}"></span> ` |
31- | property | ` <span id="target" .foo="${bar}"></span> ` |
32- | content | ` <span id="target">${foo}</span> ` |
33-
34- Emulates:
35-
36- ``` javascript
37- const el = document .createElement (' div' );
38- el .attachShadow ({ mode: ' open' });
39- el .innerHTML = ' <span id="target"></span>' ;
40- const target = el .shadowRoot .getElementById (' target' );
41-
42- // attribute value bindings set the attribute value
43- target .setAttribute (' foo' , bar);
44-
45- // attribute boolean bindings set the attribute to an empty string or remove
46- target .setAttribute (' foo' , ' ' ); // when bar is truthy
47- target .removeAttribute (' foo' ); // when bar is falsy
48-
49- // attribute defined bindings set the attribute if the value is non-nullish
50- target .setAttribute (' foo' , bar); // when bar is non-nullish
51- target .removeAttribute (' foo' ); // when bar is nullish
52-
53- // property bindings assign the value to the property of the node
54- target .foo = bar;
55-
56- // content bindings create text nodes for basic content
57- const text = document .createTextNode (' ' );
58- text .textContent = foo;
59- target .append (text);
60-
61- // content bindings append a child for singular, nested content
62- target .append (foo);
63-
64- // content binding maps and appends children for arrays of nested content
65- target .append (... foo);
66- ```
24+ The following bindings are supported:
25+
26+ | Binding | Template | Emulates |
27+ | :------------------ | :--------------------------- | :------------------------------------------------------------ |
28+ | -- | -- | ` const el = document.createElement('div'); ` |
29+ | attribute | ` <div foo="${bar}"></div> ` | ` el.setAttribute('foo', bar); ` |
30+ | attribute (boolean) | ` <div ?foo="${bar}"></div> ` | ` el.setAttribute('foo', ''); // if “bar” is truthy ` |
31+ | -- | -- | ` el.removeAttribute('foo'); // if “bar” is falsy ` |
32+ | attribute (defined) | ` <div ??foo="${bar}"></div> ` | ` el.setAttribute('foo', bar); // if “bar” is non-nullish ` |
33+ | -- | -- | ` el.removeAttribute('foo'); // if “bar” is nullish ` |
34+ | property | ` <div .foo="${bar}"></div> ` | ` el.foo = bar; ` |
35+ | content | ` <div>${foo}</div> ` | ` el.append(document.createTextNode(foo)) // if “bar” is text ` |
36+ | -- | -- | (see [ content binding] ( #content-binding ) for composition) |
6737
6838** Important note on serialization during data binding:**
6939
@@ -81,8 +51,6 @@ The following template languages are supported:
8151The following value updaters are supported:
8252
8353* ` map ` (can be used with content bindings)
84- * ` unsafe ` (can be used with content bindings)
85- * ` live ` (can be used with property bindings)
8654
8755** A note on non-primitive data:**
8856
@@ -216,23 +184,6 @@ html`<div .foo="${bar}"></div>`;
216184// el.foo = bar;
217185```
218186
219- #### The ` live ` property binding
220-
221- You can wrap the property being bound in the ` live ` updater to ensure that each
222- ` render ` call will sync the template‘s value into the DOM. This is primarily
223- used to control form inputs.
224-
225- ``` js
226- const bar = ' something' ;
227- html ` <input .value =" ${live(bar)}" >` ;
228- // <input>
229- // el.value = bar;
230- ```
231-
232- The key difference to note is that the basic property binding will not attempt
233- to perform an update if ` value === lastValue ` . The ` live ` binding will instead
234- check if ` value === el.value ` whenever a ` render ` is kicked off.
235-
236187### Content binding
237188
238189The content binding does different things based on the value type passed in.
@@ -324,35 +275,6 @@ html`<div>${bar}</div>`;
324275// <div><span>♥1</span>…<span>♣A</span></div>
325276```
326277
327- #### The ` unsafe ` content binding
328-
329- The ` unsafe ` content binding allows you to parse / instantiate text from a
330- trusted source. This should _ only_ be used to inject trusted content — never
331- user content.
332-
333- ``` js
334- const bar = ' <script>console.prompt("can you hear me now?")</script>' ;
335- html ` <div >${ unsafe (bar, ' html' )} </div >` ;
336- // <div><script>console.prompt("can you hear me now?")</script></div>
337- // console.prompt('can you hear me now?');
338-
339- const bar = ' <circle cx="50" cy="50" r="50"></circle>' ;
340- html `
341- <svg
342- xmlns =" http://www.w3.org/2000/svg"
343- viewBox =" 0 0 100 100" >
344- ${ unsafe (bar, ' svg' )}
345- </svg >
346- ` ;
347- //
348- // <svg
349- // xmlns="http://www.w3.org/2000/svg"
350- // viewBox="0 0 100 100">
351- // <circle cx="50" cy="50" r="50"></circle>
352- // </svg>
353- //
354- ```
355-
356278## Customizing your base class
357279
358280Following is a working example using [ lit-html] ( https://lit.dev ) :
0 commit comments