Skip to content

Commit 72ae5a6

Browse files
author
Kubit
committed
Add new usescape hook to Popover component
1 parent 943bed8 commit 72ae5a6

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/components/popover/popoverControlled.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@/hooks';
1212
import { useTrapFocus } from '@/hooks/useTrapFocus/useTrapFocus';
1313
import { ErrorBoundary, FallbackComponent } from '@/provider/errorBoundary';
14-
import { focusFirstDescendant, isKeyTabPressed } from '@/utils';
14+
import { focusFirstDescendant } from '@/utils';
1515
import { convertDurationToNumber } from '@/utils/stringUtility/string.utility';
1616

1717
import { PopoverStandAlone } from './popoverStandAlone';
@@ -34,6 +34,7 @@ const PopoverControlledComponent = React.forwardRef(
3434
clickOverlayClose = true,
3535
pressEscapeClose = true,
3636
preventScrollOnCloseFocus = false,
37+
trapFocusInsideModal = true,
3738
ctv,
3839
...props
3940
}: IPopoverControlled,
@@ -43,6 +44,7 @@ const PopoverControlledComponent = React.forwardRef(
4344
const currentFocus = React.useRef<HTMLElement | null>(null);
4445
const innerRef = React.useRef<HTMLDivElement | null>(null);
4546
const forwardedRef = React.useRef<HTMLDivElement | null>(null);
47+
const [forwardedElementInitialized, setForwardedElementInitialized] = React.useState(false);
4648
const setForwareddRef = React.useCallback(
4749
node => {
4850
if (node && focusFirstDescendantAutomatically) {
@@ -57,6 +59,7 @@ const PopoverControlledComponent = React.forwardRef(
5759
if (!node && blockBack) {
5860
allowScroll();
5961
}
62+
setForwardedElementInitialized(!!node);
6063
forwardedRef.current = node;
6164
},
6265
[props.forwardedRef, focusFirstDescendantAutomatically]
@@ -120,7 +123,12 @@ const PopoverControlledComponent = React.forwardRef(
120123
};
121124

122125
useClickOutside(forwardedRef, handleClickOutside, props.preventCloseOnClickElements);
123-
useEscPressedV2({ ref: innerRef, onEscPress: handlePressScape });
126+
useEscPressedV2({
127+
ref: innerRef,
128+
onEscPress: handlePressScape,
129+
disablePreventDefault: !pressEscapeClose,
130+
disableStopPropagation: !pressEscapeClose,
131+
});
124132

125133
const beforeModalFocus = () => {
126134
currentFocus.current = document.activeElement as HTMLElement;
@@ -137,17 +145,10 @@ const PopoverControlledComponent = React.forwardRef(
137145
}
138146
};
139147

140-
// to force rerender to update ref in useTrapFocus
141-
const [tabPressed, setTabPressed] = React.useState(false);
142-
143-
const handleKeyDown: React.KeyboardEventHandler<HTMLElement> = event => {
144-
if (isKeyTabPressed(event.key) && forwardedRef.current) {
145-
setTabPressed(true);
146-
props.onKeyDown?.(event);
147-
}
148-
};
149-
150-
useTrapFocus({ element: forwardedRef, hasFocusTrap: props.trapFocusInsideModal, tabPressed });
148+
useTrapFocus({
149+
ref: forwardedRef,
150+
trapFocus: forwardedElementInitialized && trapFocusInsideModal,
151+
});
151152

152153
React.useEffect(() => {
153154
if (!props.open && openAnimationRef.current) {
@@ -207,7 +208,6 @@ const PopoverControlledComponent = React.forwardRef(
207208
forwardedRef={setForwareddRef}
208209
open={openAnimation}
209210
styles={styles}
210-
onKeyDown={handleKeyDown}
211211
/>
212212
);
213213
}

0 commit comments

Comments
 (0)