Skip to content

fix: separate template_effect for dynamic class/style directive with dynamic attributes #13171

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

Merged
merged 6 commits into from
Sep 16, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/nasty-eggs-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: separate `template_effect` for dynamic class/style directive with dynamic attributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** @import { ComponentContext } from '../../types' */
import { normalize_attribute } from '../../../../../../utils.js';
import * as b from '../../../../../utils/builders.js';
import { build_getter } from '../../utils.js';
import { build_getter, create_derived } from '../../utils.js';
import { build_template_literal, build_update } from './utils.js';

/**
Expand All @@ -25,11 +25,20 @@ export function build_style_directives(
const state = context.state;

for (const directive of style_directives) {
const { has_state, has_call } = directive.metadata.expression;

let value =
directive.value === true
? build_getter({ name: directive.name, type: 'Identifier' }, context.state)
: build_attribute_value(directive.value, context).value;

if (has_call) {
const id = b.id(state.scope.generate('style_directive'));

state.init.push(b.const(id, create_derived(state, b.thunk(value))));
value = b.call('$.get', id);
}

const update = b.stmt(
b.call(
'$.set_style',
Expand All @@ -41,8 +50,6 @@ export function build_style_directives(
)
);

const { has_state, has_call } = directive.metadata.expression;

if (!is_attributes_reactive && has_call) {
state.init.push(build_update(update));
} else if (is_attributes_reactive || has_state || has_call) {
Expand All @@ -69,10 +76,17 @@ export function build_class_directives(
) {
const state = context.state;
for (const directive of class_directives) {
const value = /** @type {Expression} */ (context.visit(directive.expression));
const update = b.stmt(b.call('$.toggle_class', element_id, b.literal(directive.name), value));

const { has_state, has_call } = directive.metadata.expression;
let value = /** @type {Expression} */ (context.visit(directive.expression));

if (has_call) {
const id = b.id(state.scope.generate('class_directive'));

state.init.push(b.const(id, create_derived(state, b.thunk(value))));
value = b.call('$.get', id);
}

const update = b.stmt(b.call('$.toggle_class', element_id, b.literal(directive.name), value));

if (!is_attributes_reactive && has_call) {
state.init.push(build_update(update));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
test({ target, logs, assert }) {
const [div, div2] = target.querySelectorAll('div');
const button = target.querySelector('button');

assert.deepEqual(logs, ['called', 'called']);

// this is to assert that the order of the attributes is still not relevant
// and directives take precedence over generic attribute
assert.equal(div.classList.contains('dark'), false);
assert.equal(div2.style.color, 'red');

flushSync(() => button?.click());
assert.deepEqual(logs, ['called', 'called']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script>
let value = $state(0);

function dark(){
console.log('called')
return false;
}

function get_class(){
return 'dark';
}

function color(){
console.log('called')
return 'red';
}

function get_style(){
return 'color: green';
}
</script>

<div class:dark={dark()} class={get_class()}></div>
<div style:color={color()} style={get_style()}></div>
<button onclick={()=> value++}>{value}</button>