From 262b517a83844f3e05cdbcff17b33de91bc95721 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Sun, 13 Feb 2022 14:49:07 +1100 Subject: [PATCH] fix: disable positive index guard by default; use hasPositiveIndices to control behavior, fixes #32 --- README.md | 2 +- interfaces.d.ts | 13 ++++++++++--- src/Lock.js | 15 +++++++++------ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 55778bb..5bc4447 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ I've got a good [article about focus management, dialogs and WAI-ARIA](https:// - `whiteList=fn` you could _whitelist_ locations FocusLock should carry about. Everything outside it will ignore. For example - any modals. - `as='div'` if you need to change internal `div` element, to any other. Use ref forwarding to give FocusLock the node to work with. - `lockProps={}` to pass any extra props (except className) to the internal wrapper. - - `usePositiveIndices` to maintain a focus lock when elements exterior to the focus lock have a tabIndex greater than 0. + - `hasPositiveIndices=false` to support a focus lock behavior when any elements tabIndex greater than 0. ### Focusing in OSX (Safari/Firefox) is strange! By default `tabbing` in OSX `sees` only controls, but not links or anything else `tabbable`. This is system settings, and Safari/Firefox obey. diff --git a/interfaces.d.ts b/interfaces.d.ts index 1b3cf04..57c57ac 100644 --- a/interfaces.d.ts +++ b/interfaces.d.ts @@ -1,7 +1,7 @@ import * as React from 'react'; import {Ref} from "react"; -export interface ReactFocusLockProps> { +export interface ReactFocusLockProps> { disabled?: boolean; /** @@ -12,7 +12,7 @@ export interface ReactFocusLockProps boolean | FocusOptions); + returnFocus?: boolean | FocusOptions | ((returnTo: Element) => boolean | FocusOptions); /** * used to control behavior or "returning focus back to the lock" @@ -61,6 +61,13 @@ export interface ReactFocusLockProps, + as?: string | React.ElementType, lockProps?: LockProps, ref?: Ref; diff --git a/src/Lock.js b/src/Lock.js index 2ca94cc..0ad8f39 100644 --- a/src/Lock.js +++ b/src/Lock.js @@ -28,7 +28,7 @@ const FocusLock = React.forwardRef(function FocusLockUI(props, parentRef) { group, className, whiteList, - usePositiveIndices, + hasPositiveIndices, shards = emptyArray, as: Container = 'div', lockProps: containerProps = {}, @@ -41,8 +41,6 @@ const FocusLock = React.forwardRef(function FocusLockUI(props, parentRef) { onDeactivation: onDeactivationCallback, } = props; - const maxTabIndex = usePositiveIndices ? 1 : 0; - const [id] = React.useState({}); // SIDE EFFECT CALLBACKS @@ -65,7 +63,7 @@ const FocusLock = React.forwardRef(function FocusLockUI(props, parentRef) { }, [onDeactivationCallback]); useEffect(() => { - if(!disabled) { + if (!disabled) { // cleanup return focus on trap deactivation // sideEffect/returnFocus should happen by this time originalFocusedElement.current = null; @@ -139,8 +137,13 @@ const FocusLock = React.forwardRef(function FocusLockUI(props, parentRef) { return ( {hasLeadingGuards && [ -
, // nearest focus guard -
, // first tabbed element guard + // nearest focus guard +
, + + // first tabbed element guard + hasPositiveIndices + ?
+ : null, ]} {!disabled && (