docs icon indicating copy to clipboard operation
docs copied to clipboard

Gotcha: component style custom properties are transformed from camel case to kebab case

Open Jutanium opened this issue 2 years ago • 3 comments

See here.

Jutanium avatar Sep 12 '22 19:09 Jutanium

(Copied text so it's easier to keep an eye on from a distance)

toonddd — 09/12/2022 Astro automatically transforms component style object custom properties from camel case to kebab case.

What is the original reason/benefit for doing so?

From my current perspective it looks unnecessary and even confusing, added complexity.

nate (he/him) — 09/12/2022 Ah this is a good point! Nobody has ever brought this up as a pain point before, so I think we're stuck with it for v1.0, but it could certainly be a breaking change in the future! CSS custom properties are usually --kebab-case but there's no technical reason they can't be --camelCase

sarah11918 avatar Sep 15 '22 14:09 sarah11918

Would love to make this my first Astro contribution! Are we wanting to add this as a 'Gotcha'/good-to-know part of the docs?

If so, I can put something together for it. Please assign to me! Thanks!

WintrCodes avatar Sep 30 '22 11:09 WintrCodes

On a roll today with the contributions, @WintrCodes! 🚀

Sure, let's start by thinking of this as one of those "gotchas" for the Troubleshooting page! Depending on what you come up with, there may end up being a different fit, but I think thinking of it as troubleshooting advice makes a lot of sense to start.

sarah11918 avatar Sep 30 '22 12:09 sarah11918

See Stackblitz: Camel case gets turned into kabab case

Jutanium avatar Feb 14 '23 23:02 Jutanium

Here's a first draft!

Gotcha: --camelCase transformed to --kebab-case

If you attempt to override CSS variables by including them in your HTML using style object custom properties, do note that anything written in --camelCase will be transformed to --kebab-case. See the example below.

<body  style={{ '--cssVariable': 'brown' }} >
	<div  style="--anotherVar: blue">
		<h1>Astro</h1>
	</div>
</body>
<style>
h1 {
	color: var(--css-variable);
	background-color: var(--another-var);
}
</style>

In this example, style={{ '--cssVariable': 'brown' }} will be transformed to style="--css-variable:brown", however style="--anotherVar: blue" does not get transformed.

WintrCodes avatar Feb 15 '23 17:02 WintrCodes