Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 1b9746e

Browse files
authored
Avoid usage of __experimentalUseFocusOutside (#10017)
* Avoid usage of __experimentalUseFocusOutside * Remove unnecessary fix in QuantitySelector * Add explanatory comment
1 parent 22e96e3 commit 1b9746e

File tree

2 files changed

+7
-44
lines changed

2 files changed

+7
-44
lines changed

assets/js/base/components/drawer/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import { close } from '@wordpress/icons';
1919
import {
2020
useFocusReturn,
2121
useFocusOnMount,
22-
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
23-
__experimentalUseFocusOutside as useFocusOutside,
2422
useConstrainedTabbing,
2523
useMergeRefs,
2624
} from '@wordpress/compose';
@@ -93,7 +91,6 @@ const UnforwardedDrawer = (
9391
const focusOnMountRef = useFocusOnMount();
9492
const constrainedTabbingRef = useConstrainedTabbing();
9593
const focusReturnRef = useFocusReturn();
96-
const focusOutsideProps = useFocusOutside( onRequestClose );
9794
const contentRef = useRef< HTMLDivElement >( null );
9895

9996
useEffect( () => {
@@ -148,6 +145,13 @@ const UnforwardedDrawer = (
148145
}
149146
) }
150147
onKeyDown={ handleEscapeKeyDown }
148+
onClick={ ( e ) => {
149+
// If click was done directly in the overlay element and not one
150+
// of its descendants, close the drawer.
151+
if ( e.target === ref.current ) {
152+
onRequestClose();
153+
}
154+
} }
151155
>
152156
<div
153157
className={ classNames(
@@ -157,7 +161,6 @@ const UnforwardedDrawer = (
157161
ref={ drawerRef }
158162
role="dialog"
159163
tabIndex={ -1 }
160-
{ ...focusOutsideProps }
161164
>
162165
<div
163166
className="wc-block-components-drawer__content"

assets/js/base/components/quantity-selector/index.tsx

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { speak } from '@wordpress/a11y';
66
import classNames from 'classnames';
77
import { useCallback, useLayoutEffect, useRef } from '@wordpress/element';
88
import { DOWN, UP } from '@wordpress/keycodes';
9-
import { usePrevious } from '@woocommerce/base-hooks';
109
import { useDebouncedCallback } from 'use-debounce';
1110

1211
/**
@@ -75,45 +74,6 @@ const QuantitySelector = ( {
7574
const canDecrease = ! disabled && quantity - step >= minimum;
7675
const canIncrease =
7776
! disabled && ( ! hasMaximum || quantity + step <= maximum );
78-
const previousCanDecrease = usePrevious( canDecrease );
79-
const previousCanIncrease = usePrevious( canIncrease );
80-
81-
// When the increase or decrease buttons get disabled, the focus
82-
// gets moved to the `<body>` element. This was causing weird
83-
// issues in the Mini-Cart block, as the drawer expects focus to be
84-
// inside.
85-
// To fix this, we move the focus to the text input after the
86-
// increase or decrease buttons get disabled. We only do that if
87-
// the focus is on the button or the body element.
88-
// See https://github.com/woocommerce/woocommerce-blocks/pull/9345
89-
useLayoutEffect( () => {
90-
// Refs are not available yet, so abort.
91-
if (
92-
! inputRef.current ||
93-
! decreaseButtonRef.current ||
94-
! increaseButtonRef.current
95-
) {
96-
return;
97-
}
98-
99-
const currentDocument = inputRef.current.ownerDocument;
100-
if (
101-
previousCanDecrease &&
102-
! canDecrease &&
103-
( currentDocument.activeElement === decreaseButtonRef.current ||
104-
currentDocument.activeElement === currentDocument.body )
105-
) {
106-
inputRef.current.focus();
107-
}
108-
if (
109-
previousCanIncrease &&
110-
! canIncrease &&
111-
( currentDocument.activeElement === increaseButtonRef.current ||
112-
currentDocument.activeElement === currentDocument.body )
113-
) {
114-
inputRef.current.focus();
115-
}
116-
}, [ previousCanDecrease, previousCanIncrease, canDecrease, canIncrease ] );
11777

11878
/**
11979
* The goal of this function is to normalize what was inserted,

0 commit comments

Comments
 (0)