Skip to content

Commit

Permalink
Add option to focus on any element - including modal root
Browse files Browse the repository at this point in the history
  • Loading branch information
megawebmaster committed May 28, 2021
1 parent 32c976c commit 688f0b7
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 193 deletions.
1 change: 1 addition & 0 deletions react-responsive-modal/package.json
Expand Up @@ -55,6 +55,7 @@
}
],
"dependencies": {
"@bedrock-layout/use-forwarded-ref": "^1.1.4",
"body-scroll-lock": "^3.1.5",
"classnames": "^2.2.6"
},
Expand Down
13 changes: 8 additions & 5 deletions react-responsive-modal/src/FocusTrap.tsx
Expand Up @@ -7,7 +7,7 @@ import {
} from './lib/focusTrapJs';

export interface FocusTrapOptions {
focusOn?: 'firstFocusableElement' | 'modalRoot';
focusOn?: React.RefObject<HTMLElement>;
}

interface FocusTrapProps {
Expand Down Expand Up @@ -44,15 +44,18 @@ export const FocusTrap = ({ container, options }: FocusTrapProps) => {
}
};

if (options?.focusOn === 'firstFocusableElement') {
if (options?.focusOn) {
savePreviousFocus();
// We need to schedule focusing on a next frame - this allows to focus on the modal root
requestAnimationFrame(() => {
options.focusOn?.current?.focus();
});
} else {
const allTabbingElements = getAllTabbingElements(container.current);
if (allTabbingElements[0]) {
savePreviousFocus();
allTabbingElements[0].focus();
}
} else if (options?.focusOn === 'modalRoot') {
savePreviousFocus();
container?.current?.focus();
}
}
return () => {
Expand Down

0 comments on commit 688f0b7

Please sign in to comment.