Skip to content

Commit 7c7fd65

Browse files
authored
feat: Little improvement to set_style() (#13836)
* improvement to set_style() * add __styles to Element prototype
1 parent b979c29 commit 7c7fd65

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,7 @@ export function RegularElement(node, context) {
291291

292292
// class/style directives must be applied last since they could override class/style attributes
293293
build_class_directives(class_directives, node_id, context, is_attributes_reactive);
294-
build_style_directives(
295-
style_directives,
296-
node_id,
297-
context,
298-
is_attributes_reactive,
299-
lookup.has('style') || has_spread
300-
);
294+
build_style_directives(style_directives, node_id, context, is_attributes_reactive);
301295

302296
// Apply the src and loading attributes for <img> elements after the element is appended to the document
303297
if (node.name === 'img' && (has_spread || lookup.has('loading'))) {

packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteElement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export function SvelteElement(node, context) {
109109

110110
// class/style directives must be applied last since they could override class/style attributes
111111
build_class_directives(class_directives, element_id, inner_context, is_attributes_reactive);
112-
build_style_directives(style_directives, element_id, inner_context, is_attributes_reactive, true);
112+
build_style_directives(style_directives, element_id, inner_context, is_attributes_reactive);
113113

114114
const get_tag = b.thunk(/** @type {Expression} */ (context.visit(node.tag)));
115115

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,12 @@ export function build_set_attributes(
9494
* @param {Identifier} element_id
9595
* @param {ComponentContext} context
9696
* @param {boolean} is_attributes_reactive
97-
* @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
9897
*/
9998
export function build_style_directives(
10099
style_directives,
101100
element_id,
102101
context,
103-
is_attributes_reactive,
104-
force_check
102+
is_attributes_reactive
105103
) {
106104
const state = context.state;
107105

@@ -126,8 +124,7 @@ export function build_style_directives(
126124
element_id,
127125
b.literal(directive.name),
128126
value,
129-
/** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined),
130-
force_check ? b.true : undefined
127+
/** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined)
131128
)
132129
);
133130

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ export function set_attribute(element, attribute, value, skip_warning) {
105105

106106
if (attributes[attribute] === (attributes[attribute] = value)) return;
107107

108+
if (attribute === 'style' && '__styles' in element) {
109+
// reset styles to force style: directive to update
110+
element.__styles = {};
111+
}
112+
108113
if (attribute === 'loading') {
109114
// @ts-expect-error
110115
element[LOADING_ATTR_SYMBOL] = value;
@@ -289,6 +294,10 @@ export function set_attributes(
289294
}
290295
}
291296
}
297+
if (key === 'style' && '__styles' in element) {
298+
// reset styles to force style: directive to update
299+
element.__styles = {};
300+
}
292301
}
293302

294303
// On the first run, ensure that events are added after bindings so

packages/svelte/src/internal/client/dom/elements/style.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33
* @param {string} key
44
* @param {string} value
55
* @param {boolean} [important]
6-
* @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
76
*/
8-
export function set_style(dom, key, value, important, force_check) {
7+
export function set_style(dom, key, value, important) {
98
// @ts-expect-error
10-
var attributes = (dom.__attributes ??= {});
11-
var style = dom.style;
12-
var style_key = 'style-' + key;
9+
var styles = (dom.__styles ??= {});
1310

14-
if (attributes[style_key] === value && (!force_check || style.getPropertyValue(key) === value)) {
11+
if (styles[key] === value) {
1512
return;
1613
}
1714

18-
attributes[style_key] = value;
15+
styles[key] = value;
1916

2017
if (value == null) {
21-
style.removeProperty(key);
18+
dom.style.removeProperty(key);
2219
} else {
23-
style.setProperty(key, value, important ? 'important' : '');
20+
dom.style.setProperty(key, value, important ? 'important' : '');
2421
}
2522
}

packages/svelte/src/internal/client/dom/operations.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export function init_operations() {
4444
// @ts-expect-error
4545
element_prototype.__attributes = null;
4646
// @ts-expect-error
47+
element_prototype.__styles = null;
48+
// @ts-expect-error
4749
element_prototype.__e = undefined;
4850

4951
// @ts-expect-error

0 commit comments

Comments
 (0)