@locmod/trap-focus
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@locmod/trap-focus

Installation

npm i @locmod/trap-focus --save

Usage

import { useTrapFocus } from '@locmod/trap-focus'

const Component = () => {
  const ref = useTrapFocus()
  
  return (
    <div 
      ref={ref}
      tabIndex={0} // don't forget to add this
    >
      ...
    </div>
  )
}

Or manually

import TrapFocus from '@locmod/trap-focus'

const Component = () => {
  const ref = useRef()
  
  useEffect(() => {
    const trapFocus = new TrapFocus(ref.current)
  
    trapFocus.mount()
  
    return () => {
      trapFocus.unmount()
    }
  }, [ trapFocusId ])
  
  return (
    <div 
      ref={ref}
      tabIndex={0} // don't forget to add this
    >
      ...
    </div>
  )
}

Options

There is only one option withInitialFocus: Boolean. Default is True.

True means that when trapFocus.mount() called first element from the selectors list will be focused.

selectors list:

a[href]:not([disabled]), 
button:not([disabled]), 
input[type="text"]:not([disabled]),
input[type="radio"]:not([disabled]), 
input[type="checkbox"]:not([disabled]), 
textarea:not([disabled]), 
select:not([disabled]),
div[role="button"]:not([disabled])

False means that the lib won't be looking for elements to focus.

useTrapFocus({ withInitialFocus: false })

new TrapFocus(ref.current, { withInitialFocus: false })

Readme

Keywords

Package Sidebar

Install

npm i @locmod/trap-focus

Weekly Downloads

60

Version

1.0.2

License

MIT

Unpacked Size

17 kB

Total Files

19

Last publish

Collaborators

  • on47sky
  • clean_bread
  • irondsd
  • grammka