@visa/nova-react
Version:
Visa Product Design System Nova React library
2 lines (1 loc) • 793 B
JavaScript
import{useRef as e}from"react";const t={querySelector:"a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled])"},n=n=>{const o=e(null),r={document:"undefined"!=typeof window?document:void 0,...t,...n},l=r.ref||o;return{onKeyNavigation:(e,t=!1)=>{if(!t||!l.current?.querySelectorAll)return;const n=l.current?.querySelectorAll(r.querySelector||""),o=n[0],a=n[n.length-1];t&&"Tab"===e.key&&(e.shiftKey&&r.document?.activeElement===o?(e.preventDefault(),a.focus()):e.shiftKey||r.document?.activeElement!==a||(e.preventDefault(),o.focus()))},ref:l}};n.displayName="useFocusTrap",n.defaultProps={querySelector:"a[href], button:not(disabled), textarea:not(disabled), input:not(disabled), select:not(disabled)"};export{n as default,n as useFocusTrap};