Skip to content

Commit

Permalink
Support custom elements without assign function
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Sep 20, 2024
1 parent 640beef commit e02cc49
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-lobsters-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/plasma': patch
---

Support custom elements without assign function.
11 changes: 7 additions & 4 deletions src/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,22 @@ export function generateSvelteComponent(entry: Entry, options: SvelteTransformOp
function __sync(node, props) {
const state = {};
const assign = (node, props) => {
node.assign(Object.entries(props).reduce((acc, [key, value]) => {
const assign = typeof node.assign === 'function' ?
node.assign.bind(node) :
(props) => Object.assign(node, props);
const sync = (node, props) => {
assign(Object.entries(props).reduce((acc, [key, value]) => {
if (props[key] !== undefined || state[key] !== undefined) {
acc[key] = state[key] = props[key];
}
return acc;
}, {}));
};
assign(node, props);
sync(node, props);
return {
update(newProps) {
assign(node, newProps);
sync(node, newProps);
},
};
}
Expand Down
11 changes: 7 additions & 4 deletions test/src/svelte/TestElement.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
function __sync(node, props) {
const state = {};
const assign = (node, props) => {
node.assign(Object.entries(props).reduce((acc, [key, value]) => {
const assign = typeof node.assign === 'function' ?
node.assign.bind(node) :
(props) => Object.assign(node, props);
const sync = (node, props) => {
assign(Object.entries(props).reduce((acc, [key, value]) => {
if (props[key] !== undefined || state[key] !== undefined) {
acc[key] = state[key] = props[key];
}
return acc;
}, {}));
};
assign(node, props);
sync(node, props);
return {
update(newProps) {
assign(node, newProps);
sync(node, newProps);
},
};
}
Expand Down
11 changes: 7 additions & 4 deletions test/src/svelte/TestLink.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
function __sync(node, props) {
const state = {};
const assign = (node, props) => {
node.assign(Object.entries(props).reduce((acc, [key, value]) => {
const assign = typeof node.assign === 'function' ?
node.assign.bind(node) :
(props) => Object.assign(node, props);
const sync = (node, props) => {
assign(Object.entries(props).reduce((acc, [key, value]) => {
if (props[key] !== undefined || state[key] !== undefined) {
acc[key] = state[key] = props[key];
}
return acc;
}, {}));
};
assign(node, props);
sync(node, props);
return {
update(newProps) {
assign(node, newProps);
sync(node, newProps);
},
};
}
Expand Down

0 comments on commit e02cc49

Please sign in to comment.