|
1 | 1 | # [@vue-a11y/focus-loop](https://focus-loop.vue-a11y.com) |
2 | 2 |
|
| 3 | +Vue component that helps you to to trap focus in an element. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +When developing accessible components, in certain behaviors it is important to trap focus on the element. |
| 8 | + |
| 9 | +For example, when opening a modal, it is recommended that the focus is only on the tabbable elements of that modal and only release the focus, when the modal is closed. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Add `@vue-a11y/focus-loop` in your Vue project. |
| 14 | + |
| 15 | +```sh |
| 16 | +npm install -S @vue-a11y/focus-loop |
| 17 | +# or |
| 18 | +yarn add @vue-a11y/focus-loop |
| 19 | +``` |
| 20 | + |
| 21 | +Or via CDN |
| 22 | + |
| 23 | +```html |
| 24 | +<scriptsrc="http://unpkg.com/@vue-a11y/focus-loop"></script> |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +You can use it globally in your main.js. |
| 30 | + |
| 31 | +```js |
| 32 | +importVuefrom'vue' |
| 33 | +importVueFocusLoopfrom'@vue-a11y/focus-loop' |
| 34 | + |
| 35 | +Vue.use(VueFocusLoop) |
| 36 | +``` |
| 37 | + |
| 38 | +Or you can import into your component. |
| 39 | + |
| 40 | +```js |
| 41 | +import{FocusLoop } from'@vue-a11y/focus-loop' |
| 42 | + |
| 43 | +exportdefault{ |
| 44 | + components:{ |
| 45 | + FocusLoop |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +**Example** of use on your single file component. |
| 51 | + |
| 52 | +```vue |
| 53 | +<template> |
| 54 | + <div> |
| 55 | + <b-button v-b-modal.modal-1>Launch demo modal</b-button> |
| 56 | +
|
| 57 | + <b-modal id="modal-1" title="BootstrapVue"> |
| 58 | + <FocusLoop> |
| 59 | + <b-form @submit="onSubmit" @reset="onReset" v-if="show"> |
| 60 | + <b-form-group id="input-group-2" label="Your Name:" label-for="input-1"> |
| 61 | + <b-form-input |
| 62 | + id="input-1" |
| 63 | + v-model="form.name" |
| 64 | + required |
| 65 | + placeholder="Enter name" |
| 66 | + ></b-form-input> |
| 67 | + </b-form-group> |
| 68 | +
|
| 69 | + <b-form-group id="input-group-2" label="Your Email:" label-for="input-2"> |
| 70 | + <b-form-input |
| 71 | + id="input-2" |
| 72 | + type="email" |
| 73 | + v-model="form.email" |
| 74 | + required |
| 75 | + placeholder="Enter email" |
| 76 | + ></b-form-input> |
| 77 | + </b-form-group> |
| 78 | +
|
| 79 | + <b-button type="submit" variant="primary">Submit</b-button> |
| 80 | + <b-button type="reset" variant="danger">Reset</b-button> |
| 81 | + </b-form> |
| 82 | + </FocusLoop> |
| 83 | + </b-modal> |
| 84 | + </div> |
| 85 | +</template> |
| 86 | +``` |
| 87 | + |
| 88 | +## Disable loop |
| 89 | + |
| 90 | +You can disable the focus trap and activate it only when you really need it. |
| 91 | + |
| 92 | +prop | type | default |
| 93 | +---------- | --------- | ------------ |
| 94 | +disabled | `Boolean` | `false` |
| 95 | + |
| 96 | +For example: |
| 97 | + |
| 98 | +```html |
| 99 | + |
| 100 | +<FocusLoopdisabled> |
| 101 | +<!-- your elements here --> |
| 102 | +</FocusLoop> |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +## Keyboard support |
| 107 | + |
| 108 | +Keyboard users will use `Tab` and `Shift + Tab` to navigate tabbable elements. |
| 109 | + |
| 110 | +According to the Modal Dialog Example in WAI-ARIA Authoring Practices specification, when the focus is on the last focusable element, you must move the focus to the first element and vice versa. |
| 111 | + |
| 112 | +Key | Function |
| 113 | +------------ | ------------ |
| 114 | +Tab | ▸ Moves focus to next focusable element inside the dialog. <br> ▸ When focus is on the last focusable element in the dialog, moves focus to the first focusable element in the dialog. |
| 115 | +Shift + Tab | ▸ Moves focus to previous focusable element inside the dialog. <br> ▸ When focus is on the first focusable element in the dialog, moves focus to the last focusable element in the dialog. |
| 116 | + |
| 117 | +## Links |
| 118 | +-[Demo](https://vue-focus-loop.surge.sh) |
| 119 | + |
| 120 | +## Articles that served as inspiration: |
| 121 | +-[Using JavaScript to trap focus in an element](https://hiddedevries.nl/en/blog/2017-01-29-using-javascript-to-trap-focus-in-an-element) |
| 122 | + |
| 123 | +## Other options |
| 124 | +-[focus-trap](https://github.com/davidtheclark/focus-trap) |
| 125 | +-[vue-focus-lock](https://github.com/theKashey/vue-focus-lock) |
| 126 | + |
| 127 | +## Contributing |
| 128 | +- From typos in documentation to coding new features; |
| 129 | +- Check the open issues or open a new issue to start a discussion around your feature idea or the bug you found; |
| 130 | +- Fork repository, make changes and send a pull request; |
| 131 | + |
| 132 | +Follow us on Twitter [@vue_a11y](https://twitter.com/vue_a11y) |
| 133 | + |
| 134 | +**Thank you** |
0 commit comments