@vue-a11y/focus-loop

Vue component that helps you to to trap focus in an element.

Showing:

Popularity

Downloads/wk

2.3K

GitHub Stars

20

Maintenance

Last Commit

5mos ago

Contributors

0

Package

Dependencies

0

Size (min+gzip)

1.5KB

License

MIT

Type Definitions

Tree-Shakeable

Yes?

Categories

Readme

@vue-a11y/focus-loop


🔥 HEADS UP! You are in the Vue 2 compatible branch, check the "next" branch for Vue3 support.


Introduction

Vue component that helps you to to trap focus in an element.

When developing accessible components, in certain behaviors it is important to trap focus on the element.

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.

  • Focus is trapped: Tab and Shift+Tab will cycle through the focusable nodes within <FocusLoop> without returning to the main document below;
  • Automatic focus on the first focusable element, with the option to disable;
  • Restoring focus to the last activeElement;
  • Hides the document from screen readers when <focusLoop> is visible and enabled.

Installation

Add @vue-a11y/focus-loop in your Vue project.

npm install -S @vue-a11y/focus-loop
# or
yarn add @vue-a11y/focus-loop

Or via CDN

<script src="http://unpkg.com/@vue-a11y/focus-loop"></script>

Usage

You can use it globally in your main.js.

import Vue from 'vue'
import VueFocusLoop from '@vue-a11y/focus-loop'

Vue.use(VueFocusLoop)

Or you can import into your component.

import { FocusLoop } from '@vue-a11y/focus-loop'

export default {
  components: {
    FocusLoop
  }
}

Example of use on your single file component.

<template>
  <div>
    <b-button v-b-modal.modal-1>Launch demo modal</b-button>

    <b-modal id="modal-1" title="BootstrapVue">
      <FocusLoop :is-visible="isOpen">
        <b-form @submit="onSubmit" @reset="onReset" v-if="show">
          <b-form-group id="input-group-2" label="Your Name:" label-for="input-1">
            <b-form-input
              id="input-1"
              v-model="form.name"
              required
              placeholder="Enter name"
            ></b-form-input>
          </b-form-group>

          <b-form-group id="input-group-2" label="Your Email:" label-for="input-2">
            <b-form-input
              id="input-2"
              type="email"
              v-model="form.email"
              required
              placeholder="Enter email" 
            ></b-form-input>
          </b-form-group>

          <b-button type="submit" variant="primary">Submit</b-button>
          <b-button type="reset" variant="danger">Reset</b-button>
        </b-form>
      </FocusLoop>
    </b-modal>
  </div>
</template>

Make the focus-loop container visible and rendered

proptypedefault
isVisibleBooleanfalse
<FocusLoop :is-visible="isOpen">
  <!-- your elements here -->
</FocusLoop>

Disable loop

You can disable the focus trap and activate it only when you really need it.

proptypedefault
disabledBooleanfalse

For example:

<FocusLoop :is-visible="isOpen" disabled>
  <!-- your elements here -->
</FocusLoop>

Disable autofocus on the first element

When activating the <FocusLoop>, the first element receives the focus automatically, however, if you want to disable this behavior, just disable it through the autoFocus prop.

proptypedefault
autoFocusBooleantrue

For example:

<FocusLoop :is-visible="isOpen" :auto-focus="false">
  <!-- your elements here -->
</FocusLoop>

Keyboard support

Keyboard users will use Tab and Shift + Tab to navigate tabbable elements.

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.

KeyFunction
Tab▸ Moves focus to next focusable element inside the dialog.
▸ When focus is on the last focusable element in the dialog, moves focus to the first focusable element in the dialog.
Shift + Tab▸ Moves focus to previous focusable element inside the dialog.
▸ When focus is on the first focusable element in the dialog, moves focus to the last focusable element in the dialog.

Articles that served as inspiration:

Other options

Contributing

  • From typos in documentation to coding new features;
  • Check the open issues or open a new issue to start a discussion around your feature idea or the bug you found;
  • Fork repository, make changes and send a pull request;

Follow us on Twitter @vue_a11y

Thank you

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial