0

The logic in the js for this.has[LogicalName]Class is always true, as the class is always defined.

For example if you have the html:

<div class="someclass" data-controller=​"example"​ data-example-hidden-class=​"hidden"​>

And then the js magic property:

hasHiddenClass: boolean

The boolean this.hasHiddenClass is always true.

My question is - what is the case where this would ever be false? I cannot see it ever being false.

Here is the docs regarding this property: https://stimulus.hotwired.dev/reference/css-classes

CafeHey
  • 5,699
  • 19
  • 82
  • 145

1 Answers1

0

If the element that that's the controller does not have the class attribute then the value will be false.

div class="someclass" data-controller=​"example"​ data-example-hidden-class=​"hidden"​>
this.hasHiddenClass; // true

If not set at all...

div class="someclass" data-controller=​"example"​>
this.hasHiddenClass; // false

This is useful to default to some other behaviour or other class if not set.

if (this.hasHiddenClass) {
  // do the thing and then add hidden class
  this.element.classList.add(this.hiddenClass);
}
LB Ben Johnston
  • 4,751
  • 13
  • 29