Toggle CSS Class Of An Element When Clicking On Another – Class Toggler

Category: Javascript | September 23, 2020
Author:vyvrhel
Views Total:31 views
Official Page:Go to website
Last Update:September 23, 2020
License:MIT

Preview:

Toggle CSS Class Of An Element When Clicking On Another – Class Toggler

Description:

Class Toggler is a JavaScript library for toggling CSS classes of an element when you click a toggle element.

Ideal for accordions, modals, popovers, read more/less buttons, tabbed content, dropdown menus, and much more.

Basic Usage:

1. Link to the Class Toggler library.

<script defer src="js/class-toggler.min.js"></script>

2. Initialize the Class Toggler and we’re ready to go.

document.addEventListener('DOMContentLoaded', () => {
  classToggler.init();
});

3. Add the ‘-hidden-toggle’ class to the element to be hidden.

<div class="-hidden-toggle" data-toggle-name="toggle-name">
  Element to be toggled
</div>

4. Create a trigger element to toggle the element. Make sure the data-toggle-target and data-toggle-name attributes have the same value.

<button data-toggle-target="toggle-name">
  Toggle The Element
</button>

5. Override the default CSS classes to be toggled.

classToggler.init({
  classActive: '-active-toggle',
  classHidden: '-hidden-toggle'
});

6. Or via HTML data attributes:

<div data-toggle-name="toggle-name" data-toggle-class="text--hidden">
  Element to be toggled
</div>
<button data-toggle-target="toggle-name" data-toggle-class="button--active">
  Toggle The Element
</button>

Advanced Usage:

1. Create an accordion component using the data-toggle-group attribute:

<ul class="Accordion">
  <!-- Accordion #1 -->
  <li class="Accordion__li">
    <!-- Button -->
    <a
      class="Accordion__button"
      data-toggle-target="accordion-1"
      data-toggle-class="Accordion__button--active"
      data-toggle-group="accordion"
      href="#accordion-1"
      >Title of the first accordion</a
    >
    <!-- Content -->
    <div
      class="Accordion__content Accordion__content--hidden"
      data-toggle-name="accordion-1"
      data-toggle-class="Accordion__content--hidden"
      id="accordion-1"
    >
      <div class="Accordion__content-inner">
        <p>
          Content of the first accordion. Integer porttitor odio nibh. In
          non dolor sed est molestie feugiat at eu risus. Duis orci dui,
          viverra id suscipit sed, posuere non ante. Curabitur ac tellus non
          velit dictum aliquet in eu orci.
        </p>
      </div>
    </div>
  </li>
  <!-- Accordion #2 -->
  <li class="Accordion__li">
    <!-- Button -->
    <a
      class="Accordion__button"
      data-toggle-target="accordion-2"
      data-toggle-class="Accordion__button--active"
      data-toggle-group="accordion"
      href="#accordion-2"
      >Title of the second accordion</a
    >
    <!-- Content -->
    <div
      class="Accordion__content Accordion__content--hidden"
      data-toggle-name="accordion-2"
      data-toggle-class="Accordion__content--hidden"
      id="accordion-2"
    >
      <div class="Accordion__content-inner">
        <p>
          Content of the second accordion. Integer porttitor odio nibh. In
          non dolor sed est molestie feugiat at eu risus. Duis orci dui,
          viverra id suscipit sed, posuere non ante. Curabitur ac tellus non
          velit dictum aliquet in eu orci.
        </p>
      </div>
    </div>
  </li>
</ul>
.Accordion__content--hidden {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  transition: max-height 0.25s ease-out;
}

2. Create a tabs component using the data-toggle-tabs attribute:

<div class="Tabs">
  <!-- Tabs buttons -->
  <ul class="Tabs__buttons">
    <li class="Tabs__buttons-li">
      <a
        class="Tabs__buttons-link Tabs__buttons-link--active"
        data-toggle-target="tab-1"
        data-toggle-class="Tabs__buttons-link--active"
        data-toggle-tabs="tabs"
        href="#tab-1"
        >Tab one</a
      >
    </li>
    <li class="Tabs__buttons-li">
      <a
        class="Tabs__buttons-link"
        data-toggle-target="tab-2"
        data-toggle-class="Tabs__buttons-link--active"
        data-toggle-tabs="tabs"
        href="#tab-2"
        >Tab two</a
      >
    </li>
  </ul>
  <!-- Tabs contents -->
  <div
    class="Tabs__content"
    data-toggle-name="tab-1"
    data-toggle-class="Tabs__content--hidden"
    id="tab-1"
  >
    <p>
      Content of the first tab. Integer porttitor odio nibh. In non dolor
      sed est molestie feugiat at eu risus. Duis orci dui, viverra id
      suscipit sed, posuere non ante. Curabitur ac tellus non velit dictum
      aliquet in eu orci.
    </p>
  </div>
  <div
    class="Tabs__content Tabs__content--hidden"
    data-toggle-name="tab-2"
    data-toggle-class="Tabs__content--hidden"
    id="tab-2"
  >
    <p>
      Content of the second tab. Lorem ipsum dolor sit amet, consectetur
      adipiscing elit. Fusce malesuada magna nec justo placerat egestas.
      Integer porttitor odio nibh. In non dolor sed est molestie feugiat at
      eu risus. Duis orci dui, viverra id suscipit sed, posuere non ante.
      Curabitur ac tellus non velit dictum aliquet in eu orci.
    </p>
  </div>
</div>
.Tabs__content--hidden {
  display: none;
}

3. Hide the element when given events are triggered using the data-toggle-abort attribute. Ideal for modals.

<div
  class="Modal Modal--hidden"
  data-toggle-name="modal"
  data-toggle-class="Modal--hidden"
>
  <!-- Overlayer -->
  <div
    class="Modal__overlayer"
    data-toggle-target="modal"
    data-toggle-class="Modal__overlayer--active"
  ></div>
  <!-- Window -->
  <div class="Modal__dialog">
    <!-- Close button -->
    <button
      class="Modal__close"
      data-toggle-target="modal"
      data-toggle-class="Modal__close--active"
    >
      &times;
    </button>
    <!-- Content -->
    <h3 class="Modal__title">Lorem ipsum dolor sit amet</h3>
    <div class="Modal__content">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce
        malesuada magna nec justo placerat egestas. Integer porttitor odio
        nibh. In non dolor sed est molestie feugiat at eu risus. Duis orci
        dui, viverra id suscipit sed, posuere non ante. Curabitur ac tellus
        non velit dictum aliquet in eu orci.
      </p>
      <!-- Confirm button -->
      <p class="Modal__button-row">
        <button
          class="Modal__button"
          data-toggle-target="modal"
          data-toggle-class="Modal__button--active"
        >
          Confirm
        </button>
      </p>
    </div>
  </div>
</div>
<!-- Modal toggler -->
<p class="Modal-toggler">
  <button
    class="Modal-toggler__button"
    data-toggle-target="modal"
    data-toggle-class="Modal-toggler__button--active"
    data-toggle-abort="escape"
  >
    Show modal
  </button>
</p>

4. Move the focus on the toggled element using the data-toggle-focus attribute.

<div class="Focus">
  <!-- Button -->
  <a
    class="Focus__button"
    data-toggle-target="search"
    data-toggle-class="Focus__button--active"
    >Search</a
  >
  <!-- Content -->
  <p
    class="Focus__content Focus__content--hidden"
    data-toggle-name="search"
    data-toggle-class="Focus__content--hidden"
    data-toggle-focus="#search-input"
  >
    <input
      type="text"
      id="search-input"
      class="Focus__field"
      placeholder="Search field"
    />
    <button class="Focus__submit">Go!</button>
  </p>
</div>

5. Determine whether to toggle the element based on the mediq queries.

<div
  class="Modal Modal--hidden"
  data-toggle-name="modal-lg"
  data-toggle-class="Modal--hidden"
>
  <!-- Overlayer -->
  <div
    class="Modal__overlayer"
    data-toggle-target="modal-lg"
    data-toggle-class="Modal__overlayer--active"
  ></div>
  <!-- Window -->
  <div class="Modal__dialog">
    <!-- Close button -->
    <button
      class="Modal__close"
      data-toggle-target="modal-lg"
      data-toggle-class="Modal__close--active"
    >
      &times;
    </button>
    <!-- Content -->
    <h3 class="Modal__title">Lorem ipsum dolor sit amet</h3>
    <div class="Modal__content">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce
        malesuada magna nec justo placerat egestas. Integer porttitor odio
        nibh. In non dolor sed est molestie feugiat at eu risus. Duis orci
        dui, viverra id suscipit sed, posuere non ante. Curabitur ac tellus
        non velit dictum aliquet in eu orci.
      </p>
      <!-- Confirm button -->
      <p class="Modal__button-row">
        <button
          class="Modal__button"
          data-toggle-target="modal-lg"
          data-toggle-class="Modal__button--active"
        >
          Confirm
        </button>
      </p>
    </div>
  </div>
</div>
<!-- Modal toggler - LG -->
<p class="Modal-toggler">
  <button
    class="Modal-toggler__button"
    data-toggle-target="modal-lg"
    data-toggle-class="Modal-toggler__button--active"
    data-toggle-abort="escape"
    data-toggle-match="(min-width: 800px)"
  >
    Show modal only for screens wider then 799px
  </button>
</p>
<!-- Modal toggler - SM -->
<p class="Modal-toggler">
  <button
    class="Modal-toggler__button"
    data-toggle-target="modal-lg"
    data-toggle-class="Modal-toggler__button--active"
    data-toggle-abort="escape"
    data-toggle-match="(max-width: 799px)"
  >
    Show modal only for screens narrower then 800px
  </button>
</p>

6. All default options.

classToggler.init({
  classActive: '-active-toggle',
  classHidden: '-hidden-toggle',
  attrTarget: 'data-toggle-target',
  attrName: 'data-toggle-name',
  attrClass: 'data-toggle-class',
  attrFocus: 'data-toggle-focus',
  attrAbort: 'data-toggle-abort',
  attrMatch: 'data-toggle-match',
  attrGroup: 'data-toggle-group',
  attrTabs: 'data-toggle-tabs',
  eventNamespace: 'ct',
});

7. Event handlers.

document.querySelector('#trigger').addEventListener('ct.button.on', () => {
  // Trigger Button turned on
});
document.querySelector('#trigger').addEventListener('ct.button.off', () => {
  // Trigger Button turned off
});
document.querySelector('#content').addEventListener('ct.content.shown', () => {
  // Content shown
});
document.querySelector('#content').addEventListener('ct.content.hidden', () => {
  // Content shown
});
document.addEventListener('ct.toggled', () => {
  // Clicked trigger button
});

Changelog:

v1.1.0 (09/23/2020)

  • es6/esm versions of dist files

You Might Be Interested In:


Leave a Reply