-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Conversation
🦋 Changeset detectedLatest commit: b4bcaf4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
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 |
On server side : $$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>`;
|
There was a problem hiding this 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
packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js
Outdated
Show resolved
Hide resolved
packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js
Outdated
Show resolved
Hide resolved
packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js
Outdated
Show resolved
Hide resolved
packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js
Outdated
Show resolved
Hide resolved
…s/RegularElement.js unused Co-authored-by: Rich Harris <[email protected]>
…s/RegularElement.js unused for now Co-authored-by: Rich Harris <[email protected]>
…s/shared/element.js unused for now Co-authored-by: Rich Harris <[email protected]>
…s/shared/element.js nit Co-authored-by: Rich Harris <[email protected]>
nit Co-authored-by: Rich Harris <[email protected]>
rename clazz to value :D Co-authored-by: Rich Harris <[email protected]>
Initially, I added the style_directives because I thought I would do |
thank you! |
Nice ! I will prepare a similar PR for But, sorry for my ignorance: why did |
We tend to use |
Currently the
class:xxx
directives are managed separately from theclass
attribute.This is a PoC for rewriting
set_class()
in order to includesclass:
directives.The SSR don't remove the class based on the falsy
class:xxx
directives.The
class
attributes is set globally, based on attribute, hash and class: directives.Note that I edited some tests, just for removing leading whitespace.
Basically :
[edit] I think that the same thing may be applied to
style
andstyle:directive
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint