carbon-react
Version:
A library of reusable React components for easily building user interfaces.
2 lines (1 loc) • 774 B
JavaScript
import{useCallback as e}from"react";import o from"../../../__internal__/utils/helpers/events/events.js";import s from"../useModalManager/useModalManager.js";var n=(n,r,t,l)=>{const a=e((()=>{var e;t(),null===(e=n.current)||void 0===e||e.focus()}),[t,n]),i=e((e=>{o.isEscKey(e)&&a()}),[a]);return s({open:l,closeModal:i,modalRef:n}),e((e=>{o.isEnterKey(e)||o.isSpaceKey(e)||o.isTabKey(e)||e.preventDefault();const s=r(),n=null==s?void 0:s.length;let t=-1;const l=Array.from(s).indexOf(document.activeElement),a=e.ctrlKey||e.metaKey;var i;(o.isEndKey(e)||a&&o.isDownKey(e))&&(t=n-1),(o.isHomeKey(e)||a&&o.isUpKey(e))&&(t=0),!a&&o.isUpKey(e)&&l>0&&(t=l-1),!a&&o.isDownKey(e)&&l<n-1&&(t=l+1),t>-1&&(null==s||null===(i=s[t])||void 0===i||i.focus())}),[r])};export{n as default};