fix: static props makes state shared across class instances
- uses structuredClone on static props - closes #28
This commit is contained in:
parent
c4a5d7d6e6
commit
9268676797
3 changed files with 26 additions and 9 deletions
16
examples/demo/Toggle.js
Normal file
16
examples/demo/Toggle.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { WebComponent, html } from "../../src/index.js";
|
||||
|
||||
class Toggle extends WebComponent {
|
||||
static props = {
|
||||
toggle: false,
|
||||
};
|
||||
get template() {
|
||||
return html`
|
||||
<button onClick=${() => (this.props.toggle = !this.props.toggle)}>
|
||||
${this.props.toggle}
|
||||
</button>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("my-toggle", Toggle);
|
|
@ -8,8 +8,12 @@
|
|||
<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="./Toggle.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<my-toggle></my-toggle>
|
||||
<my-toggle toggle></my-toggle>
|
||||
<hr />
|
||||
<hello-world emotion="sad"></hello-world>
|
||||
<my-counter></my-counter>
|
||||
<p>
|
||||
|
|
|
@ -168,15 +168,12 @@ export class WebComponent extends HTMLElement {
|
|||
}
|
||||
|
||||
#initializeProps() {
|
||||
let initialProps = {};
|
||||
if (this.constructor.props) {
|
||||
initialProps = this.constructor.props;
|
||||
Object.keys(initialProps).forEach((camelCase) => {
|
||||
const value = initialProps[camelCase];
|
||||
this.#typeMap[camelCase] = typeof value;
|
||||
this.setAttribute(getKebabCase(camelCase), serialize(value));
|
||||
});
|
||||
}
|
||||
let initialProps = structuredClone(this.constructor.props) ?? {};
|
||||
Object.keys(initialProps).forEach((camelCase) => {
|
||||
const value = initialProps[camelCase];
|
||||
this.#typeMap[camelCase] = typeof value;
|
||||
this.setAttribute(getKebabCase(camelCase), serialize(value));
|
||||
});
|
||||
if (!this.#props) {
|
||||
this.#props = new Proxy(
|
||||
initialProps,
|
||||
|
|
Loading…
Reference in a new issue