Skip to content

chore: rewrite set_class() to handle directives #15352

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 24 commits into from
Feb 24, 2025

Conversation

adiguba
Copy link
Contributor

@adiguba adiguba commented Feb 21, 2025

Currently the class:xxx directives are managed separately from the class attribute.

This is a PoC for rewriting set_class() in order to includes class: directives.

Note that I edited some tests, just for removing leading whitespace.
Basically :

-<p class=" svelte-xyz">Foo</p>
+<p class="svelte-xyz">Foo</p>

[edit] I think that the same thing may be applied to style and style:directive

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Feb 21, 2025

🦋 Changeset detected

Latest commit: b4bcaf4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15352

@adiguba
Copy link
Contributor Author

adiguba commented Feb 21, 2025

Example (client side) :

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

Currently it's compiled into something like this :

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

	$.template_effect(() => {
		classes = $.set_class(div, 1, $.clsx($$props.className), null, classes,
			{ bar: $$props.bar, baz: $$props.baz}
		);
	});

The version of set_class() will set the full className on hydration or when the base value is modified.
Otherwise it will handle the class:directive one by one, based on previous value.

@adiguba
Copy link
Contributor Author

adiguba commented Feb 21, 2025

On server side :
Currently :

$$payload.out += `<div${$.attr('class', `${$.stringify($.clsx(className))} ${$.stringify([bar ? 'bar' : '', baz ? 'baz' : ''].filter(Boolean).join(' '))}`)}>...</div>`;

With this PR :

$$payload.out += `<div${$.attr('class', $.to_class($.clsx(className), null, { 'bar': bar, 'baz': baz }))}>...</div>`;

$.to_class() is a shared function, used internally by set_class() on client-side to generate the class value.

Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, this is looking good! left a few small notes

@adiguba
Copy link
Contributor Author

adiguba commented Feb 22, 2025

Initially, I added the style_directives because I thought I would do set_style() the same way at the same time...
But I will do it in a separate PR if this one is accepted...

@Rich-Harris Rich-Harris merged commit d4360af into sveltejs:main Feb 24, 2025
9 checks passed
@Rich-Harris
Copy link
Member

thank you!

@github-actions github-actions bot mentioned this pull request Feb 24, 2025
@adiguba
Copy link
Contributor Author

adiguba commented Feb 24, 2025

Nice !

I will prepare a similar PR for style and style: !

But, sorry for my ignorance: why did let/const are replaced by var ?
I always thought that the latter was no longer really recommended...

@Rich-Harris
Copy link
Member

We tend to use var in runtime code because it's faster — engines don't need to do TDZ violation checks. Sometimes it's necessary to use let/const for scoping reasons or because TypeScript gets confused with var, but other than those cases we default to var

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 this pull request may close these issues.

Useless mutations with class: directive bug: The class: directive don't remove existing className on SSR
2 participants