Skip to content

fix: improve element class attribute behaviour #10856

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 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/selfish-spies-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve element class attribute behaviour
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ function serialize_dynamic_element_attributes(attributes, context, element_id) {
function serialize_element_attribute_update_assignment(element, node_id, attribute, context) {
const state = context.state;
const name = get_attribute_name(element, attribute, context);
const is_svg = context.state.metadata.namespace === 'svg';
let [contains_call_expression, value] = serialize_attribute_value(attribute.value, context);

// The foreign namespace doesn't have any special handling, everything goes through the attr function
Expand Down Expand Up @@ -507,13 +508,19 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
if (name === 'class') {
if (singular) {
return {
singular: b.stmt(b.call('$.class_name_effect', node_id, b.thunk(singular))),
grouped: b.stmt(b.call('$.class_name', node_id, singular)),
singular: b.stmt(
b.call(
is_svg ? '$.svg_class_name_effect' : '$.class_name_effect',
node_id,
b.thunk(singular)
)
),
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, singular)),
skip_condition: true
};
}
return {
grouped: b.stmt(b.call('$.class_name', node_id, value)),
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, value)),
skip_condition: true
};
} else if (!DOMProperties.includes(name)) {
Expand Down
49 changes: 46 additions & 3 deletions packages/svelte/src/internal/client/dom/elements/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { set_class_name } from '../operations.js';
import { render_effect } from '../../reactivity/effects.js';

/**
* @param {Element} dom
* @param {HTMLElement} dom
* @param {() => string} value
* @returns {void}
*/
Expand All @@ -14,7 +14,47 @@ export function class_name_effect(dom, value) {
}

/**
* @param {Element} dom
* @param {SVGElement} dom
* @param {() => string} value
* @returns {void}
*/
export function svg_class_name_effect(dom, value) {
render_effect(() => {
svg_class_name(dom, value());
});
}

/**
* @param {SVGElement} dom
* @param {string} value
* @returns {void}
*/
export function svg_class_name(dom, value) {
Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to code-golf this: add a boolean which is true by default, and when true it uses the regular dom element variant, else the "always attribute" variant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I follow? We can't use the class_name variant at all here as it does set_class_name.

// @ts-expect-error need to add __className to patched prototype
var prev_class_name = dom.__className;
var next_class_name = to_class(value);

if (hydrating && dom.getAttribute('class') === next_class_name) {
// In case of hydration don't reset the class as it's already correct.
// @ts-expect-error need to add __className to patched prototype
dom.__className = next_class_name;
} else if (
prev_class_name !== next_class_name ||
(hydrating && dom.getAttribute('class') !== next_class_name)
) {
if (next_class_name === '') {
dom.removeAttribute('class');
} else {
dom.setAttribute('class', next_class_name);
}

// @ts-expect-error need to add __className to patched prototype
dom.__className = next_class_name;
}
}

/**
* @param {HTMLElement} dom
* @param {string} value
* @returns {void}
*/
Expand All @@ -31,7 +71,10 @@ export function class_name(dom, value) {
prev_class_name !== next_class_name ||
(hydrating && dom.className !== next_class_name)
) {
if (next_class_name === '') {
// Removing the attribute when the value is only an empty string causes
// peformance issues vs simply making the className an empty string. So
// we should only remove the class if the the value is nullish.
if (value == null) {
dom.removeAttribute('class');
} else {
set_class_name(dom, next_class_name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!--ssr:0--><div class="foo"></div><!--ssr:0-->
<!--ssr:0--><div id="foo"></div><!--ssr:0-->
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
export let className;
export let id;
</script>

<div class={className}></div>
<div id={id}></div>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<span
on:click="{toggle}"
class="{isCurrentlySelected ? 'selected' : ''}"
class="{isCurrentlySelected ? 'selected' : null}"
>
<slot></slot>
</span>
</span>