|
| 1 | +# Toggle |
| 2 | + |
| 3 | +> `v-b-toggle` is a lighweight directive for toggling the visibility of collapses and sidebars, |
| 4 | +> and includes automated accessibility handling. |
| 5 | +
|
| 6 | +## Overview |
| 7 | + |
| 8 | +The `v-b-toggle` directive can be used on interative elements, such as buttons, to toggle the |
| 9 | +visibility state of the [`<b-collapse>`](/docs/components/collapse) and |
| 10 | +[`<b-sidebar>`](/docs/components/sidebar) components. |
| 11 | + |
| 12 | +Besides toggling the visibility of the target component, the directive automatically updates ARIA |
| 13 | +accessibility attributes on the element it is applied to so that they reflect the visibility state |
| 14 | +of the target component. Refer to the [Accessibility section](#accessibility) below for additional |
| 15 | +details. |
| 16 | + |
| 17 | +## Directive syntax and usage |
| 18 | + |
| 19 | +The directive is applied to the element or component that triggers the visibility of hte target. The |
| 20 | +target component can be sepcified (via ID) as either a directive modifier(s) or as a string passed |
| 21 | +to as the directive value: |
| 22 | + |
| 23 | +- `v-b-toggle.my-collapse` - the directive modifier (multiple targets allowed) |
| 24 | +- `v-b-toggle="'my-collapse'"` - the directive value (as a String, single target only) |
| 25 | + |
| 26 | +Modifiers and the value can be used at the same time. |
| 27 | + |
| 28 | +### Example usage |
| 29 | + |
| 30 | +```html |
| 31 | +<template> |
| 32 | + <div> |
| 33 | + <b-button v-b-toggle.my-collaspe>Toggle Collapse</b-button> |
| 34 | + <b-button v-b-toggle.my-sidebar>Toggle Sidebar</b-button> |
| 35 | + <b-collapse id="my-collapse"> |
| 36 | + <b-card title="Collapsable card"> |
| 37 | + Hello world! |
| 38 | + </b-card> |
| 39 | + </b-collapse> |
| 40 | + <b-sidebar id="my-sidebar" title="Sidebar" shadow> |
| 41 | + <div class="px-3 py-2"> |
| 42 | + Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis |
| 43 | + in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. |
| 44 | + </div> |
| 45 | + </b-sidebar> |
| 46 | + </div> |
| 47 | +</template> |
| 48 | + |
| 49 | +<!-- v-b-toggle-directive.vue --> |
| 50 | +``` |
| 51 | + |
| 52 | +## Accessibility |
| 53 | + |
| 54 | +The directive, for accessibility reasons, should be placed on an clickable interactive element such |
| 55 | +as a `<button>` or `<b-button>`, which can easily be accessed by keyboard-only users and screen |
| 56 | +reader users. Elements that do not natively have an accessibility role of `button` will have the |
| 57 | +attributes `role="button"` and `tabindex="0"` applied, and will have the apropriate click and |
| 58 | +keyboard handlers instantiated. Therefore it is _highly reccommended_ to _not_ place the directive |
| 59 | +on form controls other than buttons. |
| 60 | + |
| 61 | +The directive applies, and dynamically updates, the following ARIA attributes on the trigger |
| 62 | +element: |
| 63 | + |
| 64 | +- `aria-controls` - the ID of the collaspe or sidebar component(s) being toggled |
| 65 | +- `aria-expanded` - the visibility state of the collapse or sidebar |
| 66 | + |
| 67 | +When the target component is _not_ expanded, the trigger element will have the class `collapsed` |
| 68 | +applied. When the target component is expanded, the `collapsed` class will be removed from the |
| 69 | +trigger element. |
| 70 | + |
| 71 | +## See also |
| 72 | + |
| 73 | +- [`<b-collapse>`](/docs/components/collapse) Collapsable content with accordion support |
| 74 | +- [`<b-sidebar>`](/docs/components/sidebar) Off-canvas sidebar |
0 commit comments