stimulus-toggle-util
An on/off toggle utility for Stimulus.
Table of Contents
Installation
$ yarn add stimulus-toggle-util
Register the Controller
// ./packs/application.js;// import Toggle; ;const application = Applicationstart;const context = require;application; // Manually register `stimulus-toggle-util` as a stimulus controllerapplication;
Usage
- Attach the controller to an element. Recommended to attach to a top-level container, like
<body>
or<main>
so it can be used anywhere.- Example:
... - Attach an
action
and atoggle target
to an element that should perform the toggling.- Example:
Toggledata-action="toggle#toggle"
:toggle
is theToggleController
,#toggle
is the action that is performed when this element is clicked.
- Attach a
toggle name
to an element that should be toggled.- Example:
...
Toggle a single element
Toggle Sidebar 1 Here's "Sidebar 1".
Toggle multiple elements
Toggle Sidebar 1 Toggle Sidebar 1 & 2 Here's "Sidebar 1". Here's "Sidebar 2".
Customize the CSS class
Toggle Sidebar 1 Here's "Sidebar 1".
Options
Option | Type | Required | Default | Description |
---|---|---|---|---|
data-hidden-class |
String |
🚫 | is-hidden |
The CSS class to toggle on/off. It's up to you to apply styles to the to this class to hide/show the element. |
License
This project is licensed under the MIT License.