svelte-html
Simon Holthausen 9 months ago
parent c30cf7121b
commit c7232220ad

@ -19,7 +19,7 @@ export function svelte_html(get_attributes) {
/** @type {Record<string, any>} Does _not_ contain event listeners, those are handled separately */ /** @type {Record<string, any>} Does _not_ contain event listeners, those are handled separately */
let attributes; let attributes;
const set_html_attributes = () => { render_effect(() => {
attributes = get_attributes(); attributes = get_attributes();
for (const name in attributes) { for (const name in attributes) {
@ -30,29 +30,30 @@ export function svelte_html(get_attributes) {
let value = attributes[name]; let value = attributes[name];
current.push({ owner: self, value }); current.push({ owner: self, value });
const set = () => {
if (name === 'class') {
// Avoid unrelated attribute changes from triggering class changes
if (old !== value) {
set_class(node, current_setters[name].map((e) => e.value).join(' '));
}
} else {
set_attribute(node, name, value);
}
};
// Defer hydration on initial render during hydration: If there are attribute duplicates, the last value // Defer hydration on initial render during hydration: If there are attribute duplicates, the last value
// wins, so we wait until all values have been set to see if we're actually the last one that sets the value. // wins, so we wait until all values have been set to see if we're actually the last one that sets the value.
if (hydrating) { if (hydrating) {
effect(() => { effect(() => {
if (current[current.length - 1].owner === self) { if (current[current.length - 1].owner === self) {
untrack(set_html_attributes); set();
} }
}); });
return;
}
if (name === 'class') {
// Avoid unrelated attribute changes from triggering class changes
if (old !== value) {
set_class(node, current_setters[name].map((e) => e.value).join(' '));
}
} else { } else {
set_attribute(node, name, value); set();
} }
} }
}; });
render_effect(set_html_attributes);
teardown(() => { teardown(() => {
for (const name in attributes) { for (const name in attributes) {

Loading…
Cancel
Save