All Projects → posva → focus-trap-vue

posva / focus-trap-vue

Licence: MIT license
Vue component to trap the focus within a DOM element

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to focus-trap-vue

React Focus On
🎯 Solution for WAI ARIA compatible modal dialogs or full-screen tasks, you were looking for
Stars: ✭ 180 (+15.38%)
Mutual labels:  lock, focus
Ally.js
JavaScript library to help modern web applications with accessibility concerns
Stars: ✭ 1,447 (+827.56%)
Mutual labels:  a11y, focus
react-focus-marshal
👮🏻 Traps focus within a DOM node
Stars: ✭ 83 (-46.79%)
Mutual labels:  focus, trap
React Locky
"🔒-y" – Asgardian God of Event Scoping 📦, Scroll Locking 📜, Silence Casting 🙊
Stars: ✭ 141 (-9.62%)
Mutual labels:  a11y, lock
Personal Goals
List of books I've read, projects I've done, videos I've seen, articles I've read or podcasts I've listened to.
Stars: ✭ 75 (-51.92%)
Mutual labels:  ux, a11y
vue-focus-loop
Vue component that helps you to to trap focus in an element.
Stars: ✭ 23 (-85.26%)
Mutual labels:  a11y, focus
React Focus Lock
It is a trap! A lock for a Focus. 🔓
Stars: ✭ 749 (+380.13%)
Mutual labels:  a11y, focus
Focusoverlay
Library for creating animated overlays on focused elements
Stars: ✭ 167 (+7.05%)
Mutual labels:  a11y, focus
Tooltip Sequence
A simple step by step tooltip helper for any site
Stars: ✭ 287 (+83.97%)
Mutual labels:  ux, focus
Accessibility interview questions
A starting point for questions to ask someone that wants you to give them a job
Stars: ✭ 236 (+51.28%)
Mutual labels:  ux, a11y
eufemia
DNB Design System
Stars: ✭ 38 (-75.64%)
Mutual labels:  ux, a11y
dynosign
🎨 Dynosign is the UI/UX tool, which can help you to create interfaces
Stars: ✭ 17 (-89.1%)
Mutual labels:  ux
ux-lab
No description or website provided.
Stars: ✭ 49 (-68.59%)
Mutual labels:  ux
bootstrap-confirm-button
A simple button to confirm tasks, inline and unobtrusive confirm system
Stars: ✭ 24 (-84.62%)
Mutual labels:  ux
react-awesome-toasts
Toast notifications for react.
Stars: ✭ 64 (-58.97%)
Mutual labels:  a11y
accessible-forms
A series of HTML test cases to determine how specific elements are announced in different screen reader / browser combinations.
Stars: ✭ 159 (+1.92%)
Mutual labels:  a11y
sa11y
Sa11y is an accessibility quality assurance tool that visually highlights common accessibility and usability issues. Geared towards content authors, Sa11y straightforwardly identifies errors or warnings at the source with a simple tooltip on how to fix them.
Stars: ✭ 137 (-12.18%)
Mutual labels:  a11y
eslint-config-welly
😎 ⚙️ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (-86.54%)
Mutual labels:  a11y
focus-trap
A lightweight web component that traps focus within a DOM node
Stars: ✭ 44 (-71.79%)
Mutual labels:  focus
snmp notifier
A webhook to relay Prometheus alerts as SNMP traps, because sometimes, you have to deal with legacy
Stars: ✭ 33 (-78.85%)
Mutual labels:  trap

focus-trap-vue Build Status npm package thanks

Vue component to trap the focus within a DOM element

Installation

For Vue 2

npm install focus-trap focus-trap-vue

 For Vue 3

npm install focus-trap focus-trap-vue@next

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:

 Register globally in a Vue 2 app

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

Vue.component('FocusTrap', FocusTrap)

 Register globally in a Vue 3 app

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

createApp(App)
  .component('FocusTrap', FocusTrap)
  .mount('#app)

Note this documentation is for Vue 3 and some props/events might not exist in the Vue 2 version

FocusTrap can be controlled in three different ways:

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

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

<focus-trap v-model:active="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:active="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 | ((e: MouseEvent | TouchEvent) => boolean)
  • clickOutsideDeactivates: boolean | ((e: MouseEvent | TouchEvent) => 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
  • deactivate: Whenever the trap deactivates (note it can also be deactivated by pressing Esc or clicking outside)

Methods

FocusTrap can be used without v-model:active. 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

This project was created using the Vue Library boilerplate by posva
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].