useTrapTab.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { getFocusableElements } from '@shared/utils/getFocusableElements'
  3. import { onKeyStroke } from '@vueuse/core'
  4. import type { Ref } from 'vue'
  5. export const useTrapTab = (container: Ref<HTMLElement | undefined>) => {
  6. const trapFocus = (e: KeyboardEvent) => {
  7. const focusableElements = getFocusableElements(container.value)
  8. const firstFocusableElement = focusableElements[0]
  9. const lastFocusableElement = focusableElements[focusableElements.length - 1]
  10. if (e.shiftKey) {
  11. // if shift key pressed for shift + tab combination
  12. if (document.activeElement === firstFocusableElement) {
  13. lastFocusableElement.focus() // add focus for the last focusable element
  14. e.preventDefault()
  15. }
  16. return
  17. }
  18. if (document.activeElement === lastFocusableElement) {
  19. // if focused has reached to last focusable element then focus first focusable element after pressing tab
  20. firstFocusableElement.focus() // add focus for the first focusable element
  21. e.preventDefault()
  22. }
  23. }
  24. onKeyStroke(
  25. (e) => {
  26. const isTab = e.key === 'Tab' || e.keyCode === 9
  27. if (isTab) {
  28. trapFocus(e)
  29. }
  30. },
  31. { target: container as Ref<EventTarget> },
  32. )
  33. }