Skip to content

Commit

Permalink
Backwards compatible fix for --camelCase css vars in style attrib…
Browse files Browse the repository at this point in the history
…ute (withastro#6268)

* fix(withastro#6264): backward-compat fix for camelCase css vars in style

* chore: add changeset
  • Loading branch information
natemoo-re committed Feb 17, 2023
1 parent bb18010 commit 933c651
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/tall-countries-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'astro': patch
---

Do not transform `--camelCase` custom properties to `--camel-case` when they're in a `style` attribute.

This bug fix is backwards-compatible because we will emit both `--camelCase` and `--camel-case` temporarily. This behavior will be removed in a future version of Astro.
9 changes: 8 additions & 1 deletion packages/astro/src/runtime/server/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ const kebab = (k: string) =>
k.toLowerCase() === k ? k : k.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
const toStyleString = (obj: Record<string, any>) =>
Object.entries(obj)
.map(([k, v]) => `${kebab(k)}:${v}`)
.map(([k, v]) => {
if (k[0] !== '-' && k[1] !== '-') return `${kebab(k)}:${v}`
// TODO: Remove in v3! See #6264
// We need to emit --kebab-case AND --camelCase for backwards-compat in v2,
// but we should be able to remove this workaround in v3.
if (kebab(k) !== k) return `${kebab(k)}:var(${k});${k}:${v}`;
return `${k}:${v}`;
})
.join(';');

// Adds variables to an inline script.
Expand Down

0 comments on commit 933c651

Please sign in to comment.