diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js index 86707b1a23..d748fd85e8 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js @@ -291,13 +291,7 @@ export function RegularElement(node, context) { // class/style directives must be applied last since they could override class/style attributes build_class_directives(class_directives, node_id, context, is_attributes_reactive); - build_style_directives( - style_directives, - node_id, - context, - is_attributes_reactive, - lookup.has('style') || has_spread - ); + build_style_directives(style_directives, node_id, context, is_attributes_reactive); // Apply the src and loading attributes for elements after the element is appended to the document if (node.name === 'img' && (has_spread || lookup.has('loading'))) { diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteElement.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteElement.js index 8bae80be37..d95f03d1d4 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteElement.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteElement.js @@ -109,7 +109,7 @@ export function SvelteElement(node, context) { // class/style directives must be applied last since they could override class/style attributes build_class_directives(class_directives, element_id, inner_context, is_attributes_reactive); - build_style_directives(style_directives, element_id, inner_context, is_attributes_reactive, true); + build_style_directives(style_directives, element_id, inner_context, is_attributes_reactive); const get_tag = b.thunk(/** @type {Expression} */ (context.visit(node.tag))); diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js index 1d0705b88d..e0fbe11b73 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js @@ -94,14 +94,12 @@ export function build_set_attributes( * @param {Identifier} element_id * @param {ComponentContext} context * @param {boolean} is_attributes_reactive - * @param {boolean} force_check Should be `true` if we can't rely on our cached value, because for example there's also a `style` attribute */ export function build_style_directives( style_directives, element_id, context, - is_attributes_reactive, - force_check + is_attributes_reactive ) { const state = context.state; @@ -126,8 +124,7 @@ export function build_style_directives( element_id, b.literal(directive.name), value, - /** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined), - force_check ? b.true : undefined + /** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined) ) ); diff --git a/packages/svelte/src/internal/client/dom/elements/attributes.js b/packages/svelte/src/internal/client/dom/elements/attributes.js index 91aa1aee65..b05dd8f9aa 100644 --- a/packages/svelte/src/internal/client/dom/elements/attributes.js +++ b/packages/svelte/src/internal/client/dom/elements/attributes.js @@ -105,6 +105,11 @@ export function set_attribute(element, attribute, value, skip_warning) { if (attributes[attribute] === (attributes[attribute] = value)) return; + if (attribute === 'style' && '__styles' in element) { + // reset styles to force style: directive to update + element.__styles = {}; + } + if (attribute === 'loading') { // @ts-expect-error element[LOADING_ATTR_SYMBOL] = value; @@ -289,6 +294,10 @@ export function set_attributes( } } } + if (key === 'style' && '__styles' in element) { + // reset styles to force style: directive to update + element.__styles = {}; + } } // On the first run, ensure that events are added after bindings so diff --git a/packages/svelte/src/internal/client/dom/elements/style.js b/packages/svelte/src/internal/client/dom/elements/style.js index dd297ff72b..34531029c9 100644 --- a/packages/svelte/src/internal/client/dom/elements/style.js +++ b/packages/svelte/src/internal/client/dom/elements/style.js @@ -3,23 +3,20 @@ * @param {string} key * @param {string} value * @param {boolean} [important] - * @param {boolean} [force_check] Should be `true` if we can't rely on our cached value, because for example there's also a `style` attribute */ -export function set_style(dom, key, value, important, force_check) { +export function set_style(dom, key, value, important) { // @ts-expect-error - var attributes = (dom.__attributes ??= {}); - var style = dom.style; - var style_key = 'style-' + key; + var styles = (dom.__styles ??= {}); - if (attributes[style_key] === value && (!force_check || style.getPropertyValue(key) === value)) { + if (styles[key] === value) { return; } - attributes[style_key] = value; + styles[key] = value; if (value == null) { - style.removeProperty(key); + dom.style.removeProperty(key); } else { - style.setProperty(key, value, important ? 'important' : ''); + dom.style.setProperty(key, value, important ? 'important' : ''); } } diff --git a/packages/svelte/src/internal/client/dom/operations.js b/packages/svelte/src/internal/client/dom/operations.js index 76e5ca3cb2..e179ca0c71 100644 --- a/packages/svelte/src/internal/client/dom/operations.js +++ b/packages/svelte/src/internal/client/dom/operations.js @@ -44,6 +44,8 @@ export function init_operations() { // @ts-expect-error element_prototype.__attributes = null; // @ts-expect-error + element_prototype.__styles = null; + // @ts-expect-error element_prototype.__e = undefined; // @ts-expect-error