diff --git a/src/internal/analytics/components/analytics-funnel.tsx b/src/internal/analytics/components/analytics-funnel.tsx index b8af559d87..158f0f5c97 100644 --- a/src/internal/analytics/components/analytics-funnel.tsx +++ b/src/internal/analytics/components/analytics-funnel.tsx @@ -182,12 +182,16 @@ const InnerAnalyticsFunnel = ({ mounted = true, children, stepConfiguration, ... setFunnelInteractionId(funnelInteractionId); }, 1); - /* - A funnel counts as "successful" if it is unmounted after being "complete". - */ + // A funnel counts as "successful" if it is unmounted after being "complete". /* eslint-disable react-hooks/exhaustive-deps */ return () => { clearTimeout(handle); + + // There is no need in cleanup if the funnel was not started. + if (!funnelInteractionId) { + return; + } + if (props.funnelType === 'single-page' && wizardCount.current > 0) { return; } diff --git a/src/internal/components/focus-lock/index.tsx b/src/internal/components/focus-lock/index.tsx index 06ab0a6b7a..39f958baf9 100644 --- a/src/internal/components/focus-lock/index.tsx +++ b/src/internal/components/focus-lock/index.tsx @@ -26,7 +26,7 @@ function FocusLock( { className, disabled, autoFocus, restoreFocus, children }: FocusLockProps, ref: React.Ref ) { - const returnFocusToRef = useRef(null); + const restoreFocusTargetRef = useRef(null); const containerRef = useRef(null); const focusFirst = () => { @@ -44,21 +44,25 @@ function FocusLock( // Captures focus when `autoFocus` is set, and the component is mounted or // `disabled` changes from true to false. useEffect(() => { + const assignRestoreFocusTarget = () => { + if (document.activeElement && !containerRef.current?.contains(document.activeElement as Node)) { + restoreFocusTargetRef.current = document.activeElement as unknown as HTMLOrSVGElement; + } + }; if (autoFocus && !disabled) { - returnFocusToRef.current = document.activeElement as HTMLOrSVGElement | null; + assignRestoreFocusTarget(); focusFirst(); } }, [autoFocus, disabled]); - // Restore focus if `restoreFocus` is set, and `disabled` changes from false - // to true. + // Restore focus if `restoreFocus` is set, and `disabled` changes from false to true. const [previouslyDisabled, setPreviouslyDisabled] = useState(!!disabled); useEffect(() => { if (previouslyDisabled !== !!disabled) { setPreviouslyDisabled(!!disabled); if (restoreFocus && disabled) { - returnFocusToRef.current?.focus(); - returnFocusToRef.current = null; + restoreFocusTargetRef.current?.focus(); + restoreFocusTargetRef.current = null; } } }, [previouslyDisabled, disabled, restoreFocus]); @@ -68,8 +72,8 @@ function FocusLock( const restoreFocusHandler = useCallback( (elem: HTMLDivElement | null) => { if (elem === null && restoreFocus) { - returnFocusToRef.current?.focus(); - returnFocusToRef.current = null; + restoreFocusTargetRef.current?.focus(); + restoreFocusTargetRef.current = null; } }, [restoreFocus]