-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: allow objects for style attribute #14832
Comments
Would this support camelCase as well as the default text formatting? |
I do not see much utility in this. <div style={{
'font-size': size,
border: "1px solid",
color
}}>
vs
<div style="
font-size: {size};
border: 1px solid;
color: {color};
"> The object syntax is noisy and, as alluded to, the properties could potentially use either the CSS or DOM API format. |
It's a bit the same with classes : <div class={{ festive }}>
vs
<div class="{festive?'festive':''}"> Especially with array syntax : <div class={['flex', 'festive']}>
vs
<div class="flex festive">
This can be managed quite easily to accept both (ie : uppersace will be replaced with |
Accepting both would be the worst case scenario |
The "camelCase" format has the advantage to be compatible with shorthand properties : <!-- using text : -->
<div style="font-weight: {fontWeight}; font-size: {fontSize} background-color: {backgroundColor}"> ... </div>
<!-- using style: directive : -->
<div style:font-weight={fontWeight} style:font-size={fontSize} style:background-color={backgroundColor}> ... </div>
<!-- using JS object : -->
<div style={{fontWeight, fontSize, backgroundColor}}> ... </div> |
I'm in with this idea, it would allow passing dynamic CSS to children, like the new improvements in svelte 5. I would go with |
👍 Definitely a great idea! Even though it can be done in user-land, I agree that it would be really convenient to have svelte supporting this out of the box like it does now with classes, especially given how little code the I also agree that it should support both css prop syntax and camel case to avoid dealing with accessing object props with a string notation Depending on a use case, would be much more convenient than using the <script>
let style = $state({
fontSize: '16px',
borderStyle: 'solid',
borderWidth: '1px',
color: 'black',
});
</script>
<input {style}> vs <script>
let style = $state({
fontSize: '16px',
borderStyle: 'solid',
borderWidth: '1px',
color: 'black',
});
</script>
<input
style:font-size={style.fontSize}
style:border-style={style.borderStyle}
style:border-width={style.borderWidth}
style:color={style.color}
> While at it, would be great if the |
Describe the problem
Since #14714, Svelte allow objects/arrays for
class
attribute.I think there should be an equivalent for the
style
attribute, at least for objects (I don't see any usage for array).Describe the proposed solution
I expect to have something like this :
This could be easily done by wrapping the object into a function, but I think this could be handheld internaly.
Example :
https://svelte.dev/playground/hello-world?version=5.16.0#H4sIAAAAAAAACq1S24rbMBD9lakI2IbUbkpZimMH-tS-9QPWC-vLuOtEkYQ0TrM1_vdKirOJqfetGIN0zszozMwZmCiPyFL2AzmX8Ftq3kCITUfYRGzN2o6jYenjwOhVuTgHWHzK-qZUbE7IyWFVaXAJr6UgFGTLsMzUulO0K0RB3VFJTTBAbcwZRmi1PEIQJ-4a702wdUHu50hQSy415LAyVBKGQcXL-hBE2ytvuj94R28e1NmzWXJ7UWQGOdYEVSea9FTyHvPBFx69oEwq6qTY-dpZMt3mTI-LxC-NKBYZjc0dbuV4CbtlMa6LuZbNJ3VerOs6XCQ-f3mP-Doj7qT4MYvsZQOGXrkbil1BODgiaO3yPjpdQeqHvHZoJXWDOoWCbdQZjORdUzDP-HEWYozGnT-mME0YEp9u777JLHnZXHaiNNrDdxSo7eqaiwRIr8v_L1qy5PKK-6whCc_EUtI9jut3jD2ZcO7qG3hn6cJW80Zue1G72Xo_hyYCL9r6vIXQQJ6D6Dl_QwvSSL0WHr1YncZbghMlW3B5OQSy2ttNBf8m__REbKXoDo199MoXFNuGCHUYPq5PTxHkOzjBh0nEXdSxVDbkcI15Xg2HMV0Np_H5LQog3stOhME2iGZCJxHGyx_ng30a_wKyD94cWwQAAA==
Importance
nice to have
The text was updated successfully, but these errors were encountered: