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

chore: rewrite set_class() to handle directives #15352

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

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

⚠️ No Changeset found

Latest commit: bf9c305

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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 and others added 6 commits February 22, 2025 07:16
…s/RegularElement.js


unused

Co-authored-by: Rich Harris <hello@rich-harris.dev>
…s/RegularElement.js


unused for now

Co-authored-by: Rich Harris <hello@rich-harris.dev>
…s/shared/element.js


unused for now

Co-authored-by: Rich Harris <hello@rich-harris.dev>
…s/shared/element.js


nit

Co-authored-by: Rich Harris <hello@rich-harris.dev>
nit

Co-authored-by: Rich Harris <hello@rich-harris.dev>
rename clazz to value :D

Co-authored-by: Rich Harris <hello@rich-harris.dev>
@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...

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