Git Product home page Git Product logo

focus-trap-vue's Introduction

focus-trap-vue Build Status npm package thanks

Vue component to trap the focus within a DOM element

Installation

npm install focus-trap focus-trap-vue

Usage

This library exports one single named export FocusTrap and requires focus-trap as a peer dependency. So you can locally import the component or declare it globally:

import { FocusTrap } from 'focus-trap-vue'

Vue.component('FocusTrap', FocusTrap)

FocusTrap can be controlled in three different ways:

  • by using the active Boolean prop
  • by using v-model (uses the active prop)
  • by calling the activate/deactivate method on the component

The recommended approach is using v-model and it should contain one single child:

<focus-trap v-model="isActive">
  <modal-dialog tabindex="-1">
    <p>
      Do you accept the cookies?
    </p>
    <button @click="acceptCookies">Yes</button>
    <button @click="isActive = false">No</button>
  </modal-dialog>
</focus-trap>

When isActive becomes true, it activates the focus trap. By default it sets the focus to its child, so make sure the element is a focusable element. If it's not you wil need to give it the tabindex="-1" attribute. You can also customize the initial element focused. This element should be an element that the user can interact with. For example, an input. It's a good practice to always focus an interactable element instead of the modal container:

<focus-trap v-model="isActive" :initial-focus="() => $refs.nameInput">
  <modal-dialog>
    <p>
      What name do you want to use?
    </p>
    <form @submit.prevent="setName">
      <label>
        New Name
        <input ref="nameInput" />
      </label>
      <button>Change name</button>
    </form>
  </modal-dialog>
</focus-trap>

Props

FocusTrap also accepts other props:

  • escapeDeactivates: boolean
  • returnFocusOnDeactivate: boolean
  • allowOutsideClick: boolean
  • initialFocus: string | (() => Element) Selector or function returning an Element
  • fallbackFocus: string | (() => Element) Selector or function returning an Element

Please, refer to focus-trap documentation to know what they do.

Events

FocusTrap emits 2 events. They are in-sync with the prop active

  • activate: Whenever the trap activates
  • deactive: Whenever the trap deactivates (note it can also be deactivated by pressing Esc or clicking outside)

Methods

FocusTrap can be used without v-model. In that case, you will use the methods and probably need to initialize the trap as deactivated, otherwise, the focus will start as active:

<button @click="() => $refs.focusTrap.activate()">Show the modal</button>

<focus-trap :active="false" ref="focusTrap">
  <modal-dialog>
    <p>Hello there!</p>
    <button @click="() => $refs.focusTrap.deactivate()">Okay...</button>
  </modal-dialog>
</focus-trap>

Note the use of arrow functions, this is necessary because we are accessing $refs which are unset on first render.

Related

License

MIT

focus-trap-vue's People

Contributors

dependabot-preview[bot] avatar posva avatar jakobberglund avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.