Currently, I am in the process of developing my own UI library by utilizing angular and Tailwind. However, I have encountered a challenge while using ngClass :
<button
[ngClass]="{
'px-4 py-2 text-sm': size === 'md',
'px-4 py-2 text-base': size === 'lg',
}"
>
My Button
</button>
My objective is to apply one of the classes listed above when the condition based on the size
variable is met.
The issue arises when the value of size
is 'md'
, triggering the first condition. In this scenario, the resulting class in the DOM only displays text-sm
instead of px-4 py-2 text-sm
.
According to the documentation for ngClass :
keys represent CSS classes that are added if the corresponding expression evaluates as truthy, and removed if falsey
Based on my interpretation:
- The first condition evaluates to true => the class is correctly applied
- Subsequently, the second condition evaluates to false => causing
px-4 py-2 text-base
to be removed from the class list, leaving onlytext-sm
.
While I believe I comprehend the issue at hand, being new to Angular, I would appreciate any guidance on effectively utilizing ngClass. Is there a solution available to address this matter?