Skip to content

Signal binding for CSS class name #22670

@mshabarov

Description

@mshabarov

Describe your motivation

A Signal binding for CSS class name that tracks class name toggles automatically with no need to call set(String className, boolean set) manually - less boilerplate for common “toggle class by state” use cases.

Consistent behavior with other bindXyz patterns (e.g., throwing when a binding is active).

Describe the solution you'd like

Add a new method to com.vaadin.flow.dom.ClassList:

void bind(String name, Signal<Boolean> signal)
  • when bound, the presence of the class name mirrors signal.value()
  • while a binding for name is active:
    • manually adding/removing that specific class throws BindingActiveException
    • signal effect is active when the owner element is attached, and deactivated otherwise.
  • bulk operations that indiscriminately replace/clear classes (e.g., element.getClassList().clear() or element.setAttribute("class", "foo")) silently clear all signal bindings.
  • passing null to bind method removes binding the binding.

Example usage:

Signal<Boolean> highlight = new ValueSignal<Boolean>(false);
Span span = new Span("Hello");
span.getElement().getClassList().bind("highlight", highlight);

// Later:
highlight.value(true);  // "highlight" class is added
// span.getElement().getClassList().add("highlight"); // throws BindingActiveException while bound

Additional context

RFC

Metadata

Metadata

Assignees

Labels

Projects

Status

🔎Iteration reviews

Status

Next for Dev. Team

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions