How do I toggle a CSS class based on a boolean, with Dart?

dart

Solution

Dart's HTML library has two ways to toggle CSS classes on an element. (Well, four ways if you count toggleAll :)

To add a class if it's missing, or remove a class if it already exists, use `toggle(String className)`:

element.classes.toggle('important');

To toggle a CSS class based on a boolean value, use `toggle(String className, [bool shouldAdd])`:

element.classes.toggle('important', condition);

Here's the original feature request, which links to the commit that adds this feature: https://code.google.com/p/dart/issues/detail?id=11741

Problem

I want to add or remove a CSS class from an element, based on a boolean. I want a nicer version of this: ``` if (condition) { element.classes.add('important'); } else { element.classes.remove('important'); } ```

Original source