-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
base: main
Are you sure you want to change the base?
Conversation
|
|
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 <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>
Initially, I added the style_directives because I thought I would do |
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