Skip to content

Commit ce69f00

Browse files
authored
chore: Fixes funnel integ tests with React 18 strict mode (#3852)
1 parent 7c40992 commit ce69f00

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/internal/analytics/components/analytics-funnel.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,16 @@ const InnerAnalyticsFunnel = ({ mounted = true, children, stepConfiguration, ...
182182
setFunnelInteractionId(funnelInteractionId);
183183
}, 1);
184184

185-
/*
186-
A funnel counts as "successful" if it is unmounted after being "complete".
187-
*/
185+
// A funnel counts as "successful" if it is unmounted after being "complete".
188186
/* eslint-disable react-hooks/exhaustive-deps */
189187
return () => {
190188
clearTimeout(handle);
189+
190+
// There is no need in cleanup if the funnel was not started.
191+
if (!funnelInteractionId) {
192+
return;
193+
}
194+
191195
if (props.funnelType === 'single-page' && wizardCount.current > 0) {
192196
return;
193197
}

src/internal/components/focus-lock/index.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function FocusLock(
2626
{ className, disabled, autoFocus, restoreFocus, children }: FocusLockProps,
2727
ref: React.Ref<FocusLockRef>
2828
) {
29-
const returnFocusToRef = useRef<HTMLOrSVGElement | null>(null);
29+
const restoreFocusTargetRef = useRef<HTMLOrSVGElement | null>(null);
3030
const containerRef = useRef<HTMLDivElement | null>(null);
3131

3232
const focusFirst = () => {
@@ -44,21 +44,25 @@ function FocusLock(
4444
// Captures focus when `autoFocus` is set, and the component is mounted or
4545
// `disabled` changes from true to false.
4646
useEffect(() => {
47+
const assignRestoreFocusTarget = () => {
48+
if (document.activeElement && !containerRef.current?.contains(document.activeElement as Node)) {
49+
restoreFocusTargetRef.current = document.activeElement as unknown as HTMLOrSVGElement;
50+
}
51+
};
4752
if (autoFocus && !disabled) {
48-
returnFocusToRef.current = document.activeElement as HTMLOrSVGElement | null;
53+
assignRestoreFocusTarget();
4954
focusFirst();
5055
}
5156
}, [autoFocus, disabled]);
5257

53-
// Restore focus if `restoreFocus` is set, and `disabled` changes from false
54-
// to true.
58+
// Restore focus if `restoreFocus` is set, and `disabled` changes from false to true.
5559
const [previouslyDisabled, setPreviouslyDisabled] = useState(!!disabled);
5660
useEffect(() => {
5761
if (previouslyDisabled !== !!disabled) {
5862
setPreviouslyDisabled(!!disabled);
5963
if (restoreFocus && disabled) {
60-
returnFocusToRef.current?.focus();
61-
returnFocusToRef.current = null;
64+
restoreFocusTargetRef.current?.focus();
65+
restoreFocusTargetRef.current = null;
6266
}
6367
}
6468
}, [previouslyDisabled, disabled, restoreFocus]);
@@ -68,8 +72,8 @@ function FocusLock(
6872
const restoreFocusHandler = useCallback(
6973
(elem: HTMLDivElement | null) => {
7074
if (elem === null && restoreFocus) {
71-
returnFocusToRef.current?.focus();
72-
returnFocusToRef.current = null;
75+
restoreFocusTargetRef.current?.focus();
76+
restoreFocusTargetRef.current = null;
7377
}
7478
},
7579
[restoreFocus]

0 commit comments

Comments
 (0)