Compare commits
12 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
813c277445 | ||
![]() |
979c1d4954 | ||
![]() |
d9ea372b7a | ||
![]() |
d2c0297a95 | ||
![]() |
c51879740a | ||
![]() |
2be5a3b81a | ||
![]() |
caa386590d | ||
![]() |
a9b9ada5e3 | ||
![]() |
aa30c99487 | ||
![]() |
91a245ee2f | ||
![]() |
9ba0337223 | ||
![]() |
d2a96b4fa4 |
19 changed files with 177 additions and 38 deletions
25
attach-effect/Counter.mjs
Normal file
25
attach-effect/Counter.mjs
Normal file
|
@ -0,0 +1,25 @@
|
|||
// @ts-check
|
||||
import WebComponent from "../src/WebComponent.js";
|
||||
import { attachEffect } from "../src/attach-effect.js";
|
||||
|
||||
export class Counter extends WebComponent {
|
||||
static properties = ["count"];
|
||||
onInit() {
|
||||
this.props.count = 0;
|
||||
this.onclick = () => ++this.props.count;
|
||||
attachEffect(
|
||||
this.props.count,
|
||||
(count) => console.log(count)
|
||||
);
|
||||
}
|
||||
|
||||
afterViewInit(){
|
||||
attachEffect(this.props.count, (count) => console.log(count + 100));
|
||||
}
|
||||
|
||||
get template() {
|
||||
return `<button id="btn">${this.props.count}</button>`;
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("my-counter", Counter);
|
13
attach-effect/index.html
Normal file
13
attach-effect/index.html
Normal file
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WC demo</title>
|
||||
<script type="module" src="Counter.mjs"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Attach Effect Test</h1>
|
||||
<my-counter></my-counter>
|
||||
</body>
|
||||
</html>
|
|
@ -1,4 +1,4 @@
|
|||
import WebComponent from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
export class BooleanPropTest extends WebComponent {
|
||||
static properties = ["is-inline", "anotherone"];
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// @ts-check
|
||||
import WebComponent from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
export class Counter extends WebComponent {
|
||||
static properties = ["count"];
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// @ts-check
|
||||
import WebComponent from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
export class HelloWorld extends WebComponent {
|
||||
static properties = ["count", "emotion"];
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// @ts-check
|
||||
|
||||
import { WebComponent } from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
class SimpleText extends WebComponent {
|
||||
clickCallback() {
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WC demo</title>
|
||||
<script type="module" src="HelloWorld.mjs"></script>
|
||||
<script type="module" src="SimpleText.mjs"></script>
|
||||
<script type="module" src="BooleanPropTest.mjs"></script>
|
||||
<script type="module" src="Counter.mjs"></script>
|
||||
<script type="module" src="./HelloWorld.mjs"></script>
|
||||
<script type="module" src="./SimpleText.mjs"></script>
|
||||
<script type="module" src="./BooleanPropTest.mjs"></script>
|
||||
<script type="module" src="./Counter.mjs"></script>
|
||||
</head>
|
||||
<body>
|
||||
<hello-world emotion="sad"></hello-world>
|
||||
|
|
58
examples/pens/counter-toggle.html
Normal file
58
examples/pens/counter-toggle.html
Normal file
|
@ -0,0 +1,58 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WC demo</title>
|
||||
<style>
|
||||
* {
|
||||
font-size: larger;
|
||||
}
|
||||
</style>
|
||||
<script type="module">
|
||||
import WebComponent from "https://unpkg.com/web-component-base@1.13.3/WebComponent.min.js";
|
||||
|
||||
/**
|
||||
* @see https://ayco.io/n/web-component-base
|
||||
*/
|
||||
class Counter extends WebComponent {
|
||||
static properties = ["count"];
|
||||
onInit() {
|
||||
this.props.count = 0;
|
||||
this.onclick = () => ++this.props.count;
|
||||
}
|
||||
onChanges(changes) {
|
||||
console.log(changes);
|
||||
// now click the button & check your devtools console
|
||||
}
|
||||
get template() {
|
||||
return `<button>${this.props.count}</button>`;
|
||||
}
|
||||
}
|
||||
|
||||
class Toggle extends WebComponent {
|
||||
static properties = ["toggle"];
|
||||
onInit() {
|
||||
this.props.toggle = false;
|
||||
this.onclick = () => (this.props.toggle = !this.props.toggle);
|
||||
}
|
||||
get template() {
|
||||
return `<button>${this.props.toggle ? "On" : "Off"}</button>`;
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("my-counter", Counter);
|
||||
customElements.define("my-toggle", Toggle);
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
Counter:
|
||||
<my-counter />
|
||||
</div>
|
||||
<div>
|
||||
Toggle:
|
||||
<my-toggle />
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,5 +1,5 @@
|
|||
// @ts-check
|
||||
import WebComponent from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
export class Counter extends WebComponent {
|
||||
static properties = ["count"];
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import WebComponent from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
export class HelloWorld extends WebComponent {
|
||||
static properties = ["name"];
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import WebComponent from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
export class ObjectText extends WebComponent {
|
||||
static properties = ["object"];
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// @ts-check
|
||||
import WebComponent from "../src/WebComponent.js";
|
||||
import WebComponent from "../../src/WebComponent.js";
|
||||
|
||||
export class Toggle extends WebComponent {
|
||||
static properties = ["toggle"];
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WC demo</title>
|
||||
<script type="module" src="Counter.mjs"></script>
|
||||
<script type="module" src="Toggle.mjs"></script>
|
||||
<script type="module" src="HelloWorld.mjs"></script>
|
||||
<script type="module" src="Object.mjs"></script>
|
||||
<script type="module" src="./Counter.mjs"></script>
|
||||
<script type="module" src="./Toggle.mjs"></script>
|
||||
<script type="module" src="./HelloWorld.mjs"></script>
|
||||
<script type="module" src="./Object.mjs"></script>
|
||||
<style>
|
||||
* {
|
||||
font-size: larger
|
||||
|
|
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "web-component-base",
|
||||
"version": "1.13.3",
|
||||
"version": "1.14.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "web-component-base",
|
||||
"version": "1.13.3",
|
||||
"version": "1.14.3",
|
||||
"license": "MIT",
|
||||
"workspaces": [
|
||||
"site"
|
||||
|
|
22
package.json
22
package.json
|
@ -1,9 +1,22 @@
|
|||
{
|
||||
"name": "web-component-base",
|
||||
"version": "1.13.3",
|
||||
"version": "1.14.3",
|
||||
"description": "A zero-dependency, ~600 Bytes (minified & gzipped), JS base class for creating reactive custom elements easily",
|
||||
"main": "WebComponent.js",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./WebComponent.d.ts",
|
||||
"import": "./WebComponent.js"
|
||||
},
|
||||
"./WebComponent.min.js": {
|
||||
"types": "./WebComponent.d.ts",
|
||||
"import": "./WebComponent.min.js"
|
||||
},
|
||||
"./attach-effect": {
|
||||
"types": "./attach-effect.d.ts",
|
||||
"import": "./attach-effect.js"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"start": "npx simple-server .",
|
||||
"demo": "npx simple-server .",
|
||||
|
@ -14,9 +27,12 @@
|
|||
"generate:types": "tsc --allowJs src/* --outDir dist --declaration --emitDeclarationOnly",
|
||||
"copy:meta": "node prepare.js && cp README.md ./dist && cp LICENSE ./dist",
|
||||
"copy:source": "cp ./src/* ./dist",
|
||||
"pub": "npm run clean && npm run build && cd ./dist && npm publish --access public",
|
||||
"pub": "npm run clean && npm run build && cd ./dist && npm publish",
|
||||
"pub:beta": "npm run clean && npm run build && cd ./dist && npm publish --tag beta",
|
||||
"publish:patch": "npm version patch && npm run pub",
|
||||
"publish:patch:beta": "npm version patch && npm run pub:beta",
|
||||
"publish:minor": "npm version minor && npm run pub",
|
||||
"publish:minor:beta": "npm version minor && npm run pub:beta",
|
||||
"check:size": "npm run build && size-limit ./dist/WebComponent.js"
|
||||
},
|
||||
"repository": "https://github.com/ayoayco/web-component-base",
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
shamefully-hoist=true
|
||||
strict-peer-dependencies=false
|
|
@ -1,2 +1 @@
|
|||
import McFly from "@mcflyjs/config";
|
||||
export default defineNitroConfig({ ...McFly() });
|
||||
export default defineNitroConfig({ extends: '@mcflyjs/config' });
|
||||
|
|
|
@ -24,7 +24,6 @@ export class WebComponent extends HTMLElement {
|
|||
* Read-only property containing camelCase counterparts of observed attributes.
|
||||
* @see https://www.npmjs.com/package/web-component-base#prop-access
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
|
||||
* @typedef {{[name: string]: any}} PropStringMap
|
||||
* @type {PropStringMap}
|
||||
*/
|
||||
get props() {
|
||||
|
@ -53,11 +52,7 @@ export class WebComponent extends HTMLElement {
|
|||
|
||||
/**
|
||||
* Triggered when an attribute value changes
|
||||
* @param {{
|
||||
* property: string,
|
||||
* previousValue: any,
|
||||
* currentValue: any
|
||||
* }} changes
|
||||
* @param {Changes} changes
|
||||
*/
|
||||
onChanges(changes) {}
|
||||
|
||||
|
@ -125,7 +120,11 @@ export class WebComponent extends HTMLElement {
|
|||
}
|
||||
};
|
||||
|
||||
#handler(setter, typeMap) {
|
||||
#effectsMap = {};
|
||||
|
||||
#handler(setter, meta) {
|
||||
const effectsMap = meta.#effectsMap;
|
||||
const typeMap = meta.#typeMap;
|
||||
const getKebab = (str) => {
|
||||
return str.replace(
|
||||
/[A-Z]+(?![a-z])|[A-Z]/g,
|
||||
|
@ -141,14 +140,25 @@ export class WebComponent extends HTMLElement {
|
|||
typeMap[prop] = typeof value;
|
||||
}
|
||||
|
||||
if (oldValue !== value) {
|
||||
if (value.attach === "effect") {
|
||||
if (!effectsMap[prop]) {
|
||||
effectsMap[prop] = [];
|
||||
}
|
||||
effectsMap[prop].push(value.callback);
|
||||
} else if (oldValue !== value) {
|
||||
obj[prop] = value;
|
||||
effectsMap[prop]?.forEach((f) => f(value));
|
||||
const kebab = getKebab(prop);
|
||||
setter(kebab, value);
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
get(obj, prop) {
|
||||
Object.getPrototypeOf(obj[prop]).proxy = meta.#props;
|
||||
Object.getPrototypeOf(obj[prop]).prop = prop;
|
||||
return obj[prop];
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -156,13 +166,19 @@ export class WebComponent extends HTMLElement {
|
|||
if (!this.#props) {
|
||||
this.#props = new Proxy(
|
||||
{},
|
||||
this.#handler(
|
||||
(key, value) => this.setAttribute(key, value),
|
||||
this.#typeMap
|
||||
)
|
||||
this.#handler((key, value) => this.setAttribute(key, value), this)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default WebComponent;
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* property: string,
|
||||
* previousValue: any,
|
||||
* currentValue: any
|
||||
* }} Changes
|
||||
* @typedef {{[name: string]: any}} PropStringMap
|
||||
*/
|
||||
|
|
15
src/attach-effect.js
Normal file
15
src/attach-effect.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
/**
|
||||
*
|
||||
* @typedef { import('./WebComponent.js').Changes} Changes
|
||||
* @typedef { import('./WebComponent.js').PropStringMap} PropStringMap
|
||||
* @param {Object} obj
|
||||
* @param {(newValue: any) => void} callback
|
||||
*/
|
||||
export function attachEffect(obj, callback) {
|
||||
const { proxy, prop } = Object.getPrototypeOf(obj);
|
||||
|
||||
proxy[prop] = {
|
||||
attach: "effect",
|
||||
callback,
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue