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

fix: ensure undefined class still applies scoping class, if necessary #15643

Merged
merged 1 commit into from
Mar 31, 2025
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/weak-knives-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure `undefined` class still applies scoping class, if necessary
6 changes: 5 additions & 1 deletion packages/svelte/src/internal/client/dom/elements/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export function set_class(dom, is_html, value, hash, prev_classes, next_classes)
// @ts-expect-error need to add __className to patched prototype
var prev = dom.__className;

if (hydrating || prev !== value) {
if (
hydrating ||
prev !== value ||
prev === undefined // for edge case of `class={undefined}`
) {
var next_class_name = to_class(value, hash, next_classes);

if (!hydrating || next_class_name !== dom.getAttribute('class')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<p class="svelte-xyz">Foo</p>
<p class="svelte-xyz">Foo</p>
<p class="svelte-xyz">Bar</p>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<style>p { color: red; }</style>

<p class={undefined}>Foo</p>
<p class={undefined}>Foo</p>
<p class="{undefined}">Bar</p>