Skip to content
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

Useless mutations with class: directive #15351

Open
adiguba opened this issue Feb 21, 2025 · 0 comments · May be fixed by #15352
Open

Useless mutations with class: directive #15351

adiguba opened this issue Feb 21, 2025 · 0 comments · May be fixed by #15352

Comments

@adiguba
Copy link
Contributor

adiguba commented Feb 21, 2025

Describe the bug

The class:directive can produce multiple mutations on DOM.

Example with this code :

<script>
	let { className, bar, baz } = $props();
</script>

<div class={className} class:bar class:baz>...</div>

Currently we got the following code :

	$.template_effect(() => {
		$.set_class(div, $.clsx($$props.className));
		$.toggle_class(div, 'bar', $$props.bar);
		$.toggle_class(div, 'baz', $$props.baz);
	});

With the props { className: "foo", bar: true, baz: true } :

  • The SSR will generate <div class="foo bar baz">...</div>
  • But on client site (with hydration or not) :
    • set_class() will set the class to "foo"
    • toggle_class('bar') will add the class bar, so he class will be "foo bar"
    • toggle_class('baz') will add the class baz, so he class will be "foo bar bar"

It would be nice to define the class once (except for the reactivity after that of course)

Reproduction

Need to watch the DOM, or add a MutationObserver on the page...

Logs

-

System Info

-

Severity

annoyance

@adiguba adiguba linked a pull request Feb 21, 2025 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant