Skip to content

fix: stable attachments #15961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ export function build_set_attributes(
);

if (is_dynamic) {
context.state.init.push(b.let(attributes_id));
const update = b.stmt(b.assignment('=', attributes_id, call));
context.state.update.push(update);
context.state.init.push(
b.let(attributes_id),
b.stmt(b.call('$.set_attribute_effect', element_id, b.thunk(b.object(values))))
);
} else {
context.state.init.push(b.stmt(call));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { effect } from '../../reactivity/effects.js';
* @param {() => (node: Element) => void} get_fn
*/
export function attach(node, get_fn) {
effect(() => {
return effect(() => {
const fn = get_fn();

// we use `&&` rather than `?.` so that things like
Expand Down
44 changes: 44 additions & 0 deletions packages/svelte/src/internal/client/dom/elements/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { clsx } from '../../../shared/attributes.js';
import { set_class } from './class.js';
import { set_style } from './style.js';
import { ATTACHMENT_KEY, NAMESPACE_HTML } from '../../../../constants.js';
import { block, branch, destroy_effect } from '../../reactivity/effects.js';

export const CLASS = Symbol('class');
export const STYLE = Symbol('style');
Expand Down Expand Up @@ -456,6 +457,49 @@ export function set_attributes(element, prev, next, css_hash, skip_warning = fal
return current;
}

/**
* @param {Element & ElementCSSInlineStyle} element
* @param {() => Record<string | symbol, any>} fn
* @param {string} [css_hash]
* @param {boolean} [skip_warning]
*/
export function set_attribute_effect(element, fn, css_hash, skip_warning = false) {
/** @type {Record<string | symbol, any>} */
var prev = {};

/** @type {Record<symbol, import('#client').Effect>} */
var effects = {};

block(() => {
var next = fn();

for (const key in prev) {
if (!next[key]) {
element.removeAttribute(key);
}
}

for (let symbol of Object.getOwnPropertySymbols(prev)) {
if (!next[symbol]) {
destroy_effect(effects[symbol]);
delete effects[symbol];
}
}

for (const key in next) {
set_attribute(element, key, next[key], skip_warning);
}

for (let symbol of Object.getOwnPropertySymbols(next)) {
if (symbol.description === ATTACHMENT_KEY && next[symbol] !== prev[symbol]) {
effects[symbol] = branch(() => attach(element, () => next[symbol]));
}
}

prev = next;
});
}

/**
*
* @param {Element} element
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
remove_input_defaults,
set_attribute,
set_attributes,
set_attribute_effect,
set_custom_element_data,
set_xlink_attribute,
set_value,
Expand Down
Loading