feat(templating): support more standard attrs
- data- <-> dataset. - for <-> htmlFor - class <-> className
This commit is contained in:
parent
27a5fb3024
commit
6844ffe9f2
3 changed files with 25 additions and 5 deletions
|
@ -6,9 +6,19 @@ export class Counter extends WebComponent {
|
||||||
};
|
};
|
||||||
get template() {
|
get template() {
|
||||||
return html`
|
return html`
|
||||||
<button class="hey" id="btn" onclick=${() => ++this.props.count} style="background-color: black; color: white;">
|
<button
|
||||||
|
class="hey"
|
||||||
|
ID="btn"
|
||||||
|
onClick=${() => ++this.props.count}
|
||||||
|
style="background-color: black; color: white;"
|
||||||
|
about="Elephant"
|
||||||
|
>
|
||||||
<span>${this.props.count}</span>
|
<span>${this.props.count}</span>
|
||||||
</button>
|
</button>
|
||||||
|
<form style="margin: 1em 0;">
|
||||||
|
<label data-my-name="Ayo" for="the-input">Name</label>
|
||||||
|
<input id="the-input" type="foo" value="Name:" />
|
||||||
|
</form>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "web-component-base",
|
"name": "web-component-base",
|
||||||
"version": "2.0.0-beta.15",
|
"version": "2.0.0-beta.16",
|
||||||
"description": "A zero-dependency, ~600 Bytes (minified & gzipped), JS base class for creating reactive custom elements easily",
|
"description": "A zero-dependency, ~600 Bytes (minified & gzipped), JS base class for creating reactive custom elements easily",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { getCamelCase } from "./get-camel-case.js";
|
||||||
|
|
||||||
export function createElement(tree) {
|
export function createElement(tree) {
|
||||||
if (!tree.type) {
|
if (!tree.type) {
|
||||||
const leaves = typeof tree === "object" ? Object.keys(tree) : [];
|
const leaves = typeof tree === "object" ? Object.keys(tree) : [];
|
||||||
|
@ -9,9 +11,17 @@ export function createElement(tree) {
|
||||||
const el = document.createElement(tree.type);
|
const el = document.createElement(tree.type);
|
||||||
if (tree.props) {
|
if (tree.props) {
|
||||||
Object.keys(tree.props).forEach(prop => {
|
Object.keys(tree.props).forEach(prop => {
|
||||||
let domProp = prop.startsWith('on') ? prop.toLowerCase() : prop;
|
let domProp = prop.toLowerCase();
|
||||||
if (domProp === 'class') domProp = 'className';
|
if (domProp.startsWith('data-')) {
|
||||||
|
const dataProp = domProp.replace('data-', '');
|
||||||
|
el.dataset[getCamelCase(dataProp)] = tree.props[prop];
|
||||||
|
} else {
|
||||||
|
switch(domProp) {
|
||||||
|
case 'class': domProp = 'className'; break;
|
||||||
|
case 'for': domProp = 'htmlFor'; break;
|
||||||
|
}
|
||||||
el[domProp] = tree.props[prop]
|
el[domProp] = tree.props[prop]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
tree.children?.forEach((child) => {
|
tree.children?.forEach((child) => {
|
||||||
|
|
Loading…
Reference in a new issue