Skip to content

Commit

Permalink
trapfocus logic update (#4100)
Browse files Browse the repository at this point in the history
  • Loading branch information
dleroux committed Apr 1, 2021
1 parent ef0b0d4 commit c9fe82a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f
- Removed the `isMounted` check from `Portal` to only rely on the useEffect for calling `onPortalCreated` ([#4066](https://github.com/Shopify/polaris-react/pull/4066))
- Removed transition from `BulkActions` to eliminate flicker ([#4080](https://github.com/Shopify/polaris-react/pulls/4080))
- update error background color in `Select` ([#4089](https://github.com/Shopify/polaris-react/pull/4089))
- Fixed `Trapfocus` issue that was preventing tabbing with react forms ([#4100](https://github.com/Shopify/polaris-react/pull/4100))

### Documentation

Expand Down
26 changes: 14 additions & 12 deletions src/components/TrapFocus/TrapFocus.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useRef} from 'react';
import React, {useRef, useEffect, useState} from 'react';

import {Key} from '../../types';
import {EventListener} from '../EventListener';
Expand All @@ -13,7 +13,6 @@ import {
} from '../../utilities/focus';
import {useFocusManager} from '../../utilities/focus-manager';
import {portal} from '../shared';
import {useIsAfterInitialMount} from '../../utilities/use-is-after-initial-mount';

export interface TrapFocusProps {
trapping?: boolean;
Expand All @@ -23,17 +22,20 @@ export interface TrapFocusProps {
export function TrapFocus({trapping = true, children}: TrapFocusProps) {
const {canSafelyFocus} = useFocusManager({trapping});
const focusTrapWrapper = useRef<HTMLDivElement>(null);
const isMounted = useIsAfterInitialMount();
const [disableFocus, setDisableFocus] = useState(true);

const disableFocus =
isMounted &&
canSafelyFocus &&
!(
focusTrapWrapper.current &&
focusTrapWrapper.current.contains(document.activeElement)
)
? !trapping
: true;
useEffect(() => {
const disable =
canSafelyFocus &&
!(
focusTrapWrapper.current &&
focusTrapWrapper.current.contains(document.activeElement)
)
? !trapping
: true;

setDisableFocus(disable);
}, [canSafelyFocus, trapping]);

const handleFocusIn = (event: FocusEvent) => {
const containerContentsHaveFocus =
Expand Down

0 comments on commit c9fe82a

Please sign in to comment.