Skip to content

Latest commit

 

History

History
120 lines (93 loc) · 4.92 KB

README.md

File metadata and controls

120 lines (93 loc) · 4.92 KB

Toggle

v-b-toggle is a light-weight directive for toggling the visibility of collapses and sidebars, and includes automated WAI-ARIA accessibility attribute handling.

Overview

The v-b-toggle directive can be used on interactive elements, such as buttons, to toggle the visibility state of the <b-collapse> and <b-sidebar> components.

Besides toggling the visibility of the target component, the directive automatically updates ARIA accessibility attributes on the element it is applied to so that they reflect the visibility state of the target component. Refer to the Accessibility section below for additional details and caveats.

Directive syntax and usage

The directive is applied to the element or component that triggers the visibility of hte target. The target component can be specified (via its ID) as either a directive modifier(s), the directive argument, or as a string/array passed to as the directive value:

  • v-b-toggle.my-collapse - the directive modifier (multiple targets allowed, each modifier is a target ID)
  • v-b-toggle:my-collapse - the directive argument (Vue dynamic argument is supported) v2.14.0+
  • v-b-toggle="'my-collapse'" - the directive value as a string ID
  • v-b-toggle="'my-collapse1 my-collapse2'" - the directive value as a space separated string of IDs v2.14.0+
  • v-b-toggle="['my-collapse1', 'my-collapse2']" - the directive value as an array of string IDs v2.14.0+

Modifiers, argument, and the value can be used at the same time when targeting multiple components.

Example usage

<template>
  <div>
    <div class="mb-3">
      <b-button v-b-toggle.my-collapse>Toggle Collapse</b-button>
      <b-button v-b-toggle.my-sidebar>Toggle Sidebar</b-button>
    </div>

    <b-collapse id="my-collapse">
      <b-card title="Collapsible card">
        Hello world!
      </b-card>
    </b-collapse>

    <b-sidebar id="my-sidebar" title="Sidebar" shadow>
      <div class="px-3 py-2">
        Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis
        in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
      </div>
    </b-sidebar>
  </div>
</template>

<!-- v-b-toggle-directive.vue -->

Hiding and showing content in the toggle trigger element

When using the v-b-toggle directive, the class collapsed will automatically be placed on the trigger element when the target component is closed, and removed when open. As of BootstrapVue 2.14.0, the class not-collapsed will be applied when the target is not closed.

Example HTML markup:

<div>
  <b-button v-b-toggle:my-collapse>
    <span class="when-open">Close</span><span class="when-closed">Open</span> My Collapse
  </b-button>
  <b-collapse id="my-collapse">
    <!-- Content here -->
  </b-collapse>
</div>

Example Custom CSS:

.collapsed > .when-open,
.not-collapsed > .when-closed {
  display: none;
}

Accessibility

The directive, for accessibility reasons, should be placed on an clickable interactive element such as a <button> or <b-button>, which can easily be accessed by keyboard-only users and screen reader users. Elements that do not natively have an accessibility role of button (or link) will have the attributes role="button" and tabindex="0" applied, and will have the appropriate click handler instantiated. Therefore it is highly recommended to not place the directive on form controls other than buttons.

The directive applies, and dynamically updates, the following ARIA attributes on the trigger element:

  • aria-controls - the ID(s) of the collapse or sidebar component(s) being toggled
  • aria-expanded - the visibility state of the collapse or sidebar (see the caveats section below)

Caveats with multiple targets

When multiple targets are specified, the value of the aria-expanded attribute may not be correct if the individual target components can have their collapsed state controlled independently (either via v-model, other controls with v-b-toggle directive, or CSS visibility).

See also