-
-
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
Dynamic conditional CSS classes #7294
Comments
|
@windyclory Gives |
What about using a reactive-variable ? <script>
export let dynamicClass = ''
let cond = false
$: theDynamicClass = cond ? dynamicClass : '';
</script>
<button class="my-class-name {theDynamicClass}" on:click={() => cond = !cond}>click me</button> |
@adiguba Sure that's what I'm using now and it works but I think the syntax could be improved. That's what I meant with "looks cleaner here than in the real world where [...] you need to manually combine classes". |
I'm interested in this as well. I haven't touched svelte source before but I tried to make an initial assessment. Maybe someone else could chime in on an idea on how to implement it. But by playing around with the parser I got the impression we would need a new node type to solve this problem. If someone with deeper knowledge about the parser could give some insight into how the AST could be structured it would probably be easier for someone to pick this one up. |
Something more nuanced would be great.. even using the |
I think I have a similar issue, Wanted to know if something like this is doable. export const classes = {
'btn-primary': color === 'primary',
'btn-secondary': color === 'secondary',
'btn-accent': color === 'accent',
'btn-ghost': color === 'ghost',
'btn-link': color === 'link',
'btn-square': shape === 'square',
'btn-circle': shape === 'circle',
'btn-active': active,
'btn-outline': outline,
'btn-disabled': disabled,
'btn-loading': loading,
} and then set These are some of the props here, but the actual list is quite long. Current work-around is building a string by iterating through classes object and set Wonder if this could be integrated into svelte itself. |
Ran into a use-case for this as well. I think syntactically, it would be nice to have it similar to how SolidJS uses computed values in classList:
where bracket notation denotes a computed value inside an object like |
Describe the problem
In components that want to support use with CSS frameworks like Tailwind, it would be nice to allow users to pass in custom class names as props which are applied depending on an internal condition in the component:
Describe the proposed solution
MyComponent.svelte
:Alternatives considered
This already works
but it looks cleaner here than in the real world where you might already have class attr in which case you need to manually combine classes before applying.
Importance
nice to have
The text was updated successfully, but these errors were encountered: