Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stimulus-toggle-util

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stimulus-toggle-util

An on/off toggle utility for Stimulus

  • 0.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

stimulus-toggle-util

An on/off toggle utility for Stimulus.

View a demo.

Table of Contents

Installation

$ yarn add stimulus-toggle-util

Register the Controller

// ./packs/application.js
import { Application } from 'stimulus';
// import Toggle
import Toggle from 'stimulus-toggle-util';

import { definitionsFromContext } from 'stimulus/webpack-helpers';
const application = Application.start();
const context = require.context('../controllers', true, /\.js$/);
application.load(definitionsFromContext(context));

// Manually register `stimulus-toggle-util` as a stimulus controller
application.register('toggle', Toggle);

Usage

  1. 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:
    <main data-controller="toggle">...</main>
    
  2. Attach an action and a toggle target to an element that should perform the toggling.
    • Example:
    <button data-action="toggle#toggle" data-toggle-target="sidebar-1">Toggle</button>
    
    • data-action="toggle#toggle": toggle is the ToggleController, #toggle is the action that is performed when this element is clicked.
  3. Attach a toggle name to an element that should be toggled.
    • Example:
    <aside data-toggle-name="sidebar-1">...</aside>
    

Toggle a single element

<main data-controller="toggle">
  <button data-action="toggle#toggle" data-toggle-target="sidebar-1">
    Toggle Sidebar 1
  </button>
      
  <aside class="is-hidden" data-toggle-name="sidebar-1">
    <p>Here's "Sidebar 1".</p>
  </aside>
</main>

1

Toggle multiple elements

<main data-controller="toggle">
  <button data-action="toggle#toggle" data-toggle-target="sidebar-1">
    Toggle Sidebar 1
  </button>
  <button
    data-action="toggle#toggle"
    data-toggle-target="sidebar-1,sidebar-2"
  >
    Toggle Sidebar 1 & 2
  </button>

  <aside class="is-hidden" data-toggle-name="sidebar-1">
    <p>Here's "Sidebar 1".</p>
  </aside>
  <aside class="is-hidden" data-toggle-name="sidebar-2">
    <p>Here's "Sidebar 2".</p>
  </aside>
</main>

2

Customize the CSS class

<main data-controller="toggle" data-hidden-class="custom-hidden-class">
  <button data-action="toggle#toggle" data-toggle-target="sidebar-1">
    Toggle Sidebar 1
  </button>
      
  <aside data-toggle-name="sidebar-1">
    <p>Here's "Sidebar 1".</p>
  </aside>
</main>

Options

OptionTypeRequiredDefaultDescription
data-hidden-classString🚫is-hiddenThe 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.

Keywords

FAQs

Package last updated on 14 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc