Skip to main content

ToggleClassController

Purpose

To toggle a class on an element, based on various optional interactions i.e. On Hover, On click-away, on direct-click

Actions

ActionPurpose
toggleIf the class is currently applied, removed it. Otherwise, add it.
onAdd the class if it is currently missing
offRemove the class if it is currently applied

Targets

TargetPurposeDefault
toggleThe target to toggle the class on-

Classes

ClassPurposeDefault
---

Values

ValueTypeDescriptionDefault
classStringThe space separated list of classes to toggle-
mouseEnter (Optional)StringOne of on, off, or toggle, what to do when the user's mouse enters the controller scope-
mouseLeave (Optional)StringOne of on, off, or toggle, what to do when the user's mouse leaves the controller scope-
clickAway (Optional)BooleanWhen the user clicks outside of the controller scope, should any previously toggled classes be toggled back-
initial (Optional)StringEither on: the controller will ensure the class is always present when the controller connects. Or off: it will ensure the class is not present on connect. If the option is not specified then the class will be left as it is in the DOM-

Note: If you use Turbo/Turbolinks, it's best to specify a value for initial, otherwise the Turbo(links) cache can put the class into a strange state and the toggle will be inverted.

Events

EventWhenDispatched onevent.detail
----

Side Effects

None

How to Use

Example - showing a dropdown menu on a click, and hiding it again if the user clicks away.

HTML

<div
data-controller="toggle-class"
data-toggle-class-class-value="hidden"
data-toggle-class-click-away-value="true"
data-toggle-class-initial-value="on"
>
<button id="user-menu" class="navbar-button" data-action="click->toggle-class#toggle">
<i class="far fa-bell"></i>
<span class="hidden dropdown" data-toggle-class-target="toggle">
<a href="/profile" class="dropdown-link">Your Profile</a>
<a href="/settings" class="dropdown-link">Settings</a>
<a href="/sign-out" class="dropdown-link">Sign Out</a>
</span>
</button>
</div>

ezgif-3-697e3a644da7

Example, showing an action button on hover

HTML

<div id="calendar" class="grid grid-cols-7">
<!-- ...-->
<div
class="calendar-cell"
data-controller="toggle-class"
data-toggle-class-class-value="invisible"
data-toggle-class-mouse-enter-value="off"
data-toggle-class-mouse-leave-value="on"
data-toggle-class-initial-value="on"
>
<span class="cell-heading">4th</span>
<div class="cell-body">
<div class="text-center invisible" data-toggle-class-target="toggle">
<a class="btn btn-primary" href="/events/new">+ New event</a>
</div>
</div>
</div>
<!-- ...-->
</div>

ezgif-3-dd9317a73732