Add/Remove Enable/Disable USS class best practice?

Hello,

I frequently want to adjust the styling on ui elements (e.g text styling or toggle display:none)

I have noticed that you can enable/disable classes and also add/remove classes from an element. They both have the same result, however the former means the class is added in the UXML and needs to be disabled in set up (unless it’s possible to add a disabled class to the element?).

I can imagine add/remove alters the UQUERY results, but this is not a factor my editor as I do not use these classes as qualifiers.

What is the best practice for toggling class’ on/off?

Another frequent thing I come up against is I have no way of knowing if the class exists on the element, or if it needs adding to the element.

Thanks in advance!

1 Like

Hi BinaryCats,

The EnableInClassList(string className, bool enable) function is basically a wrapper around AddToClassList() and RemoveFromClassList() based on the enable parameter. That’s why they both have the same result.

Use ToggleInClassList(“className”)

Use ClassListContains(“className”)

2 Likes