From 3665672ed4e47cebba1a85042ff45a87c8cd7d94 Mon Sep 17 00:00:00 2001 From: Andrei Zhaleznichenka Date: Tue, 12 May 2026 09:38:57 +0200 Subject: [PATCH] chore: Adds disable dismiss auto-focis for internal chart tooltips --- .../components/chart-popover/index.tsx | 5 +++ src/popover/__tests__/popover-body.test.tsx | 34 +++++++++++++++++++ src/popover/body.tsx | 6 ++-- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/popover/__tests__/popover-body.test.tsx diff --git a/src/internal/components/chart-popover/index.tsx b/src/internal/components/chart-popover/index.tsx index 48aadb67db..5e77c101c5 100644 --- a/src/internal/components/chart-popover/index.tsx +++ b/src/internal/components/chart-popover/index.tsx @@ -56,6 +56,9 @@ export interface ChartPopoverProps extends PopoverProps { /** Popover footer */ footer?: React.ReactNode; + + /** Prevents dismiss button auto-focus */ + disableDismissAutoFocus?: boolean; } export default React.forwardRef(ChartPopover); @@ -67,6 +70,7 @@ function ChartPopover( fixedWidth = false, dismissButton = false, dismissAriaLabel, + disableDismissAutoFocus, children, footer, @@ -157,6 +161,7 @@ function ChartPopover( {title}} onDismiss={() => onDismiss()} overflowVisible="content" diff --git a/src/popover/__tests__/popover-body.test.tsx b/src/popover/__tests__/popover-body.test.tsx new file mode 100644 index 0000000000..9d8e4139d0 --- /dev/null +++ b/src/popover/__tests__/popover-body.test.tsx @@ -0,0 +1,34 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import React from 'react'; +import { render } from '@testing-library/react'; + +import PopoverBody from '../../../lib/components/popover/body'; + +function renderPopoverBody(disableDismissAutoFocus = false) { + return render( + {}} + header={undefined} + > + Content + + ); +} + +describe('disableDismissAutoFocus', () => { + it('focuses the dismiss button by default when dismissButton is true', () => { + const { container } = renderPopoverBody(); + const dismissButton = container.querySelector('button[aria-label="Close"]'); + expect(document.activeElement).toBe(dismissButton); + }); + + it('does not focus the dismiss button when disableDismissAutoFocus is true', () => { + const { container } = renderPopoverBody(true); + const dismissButton = container.querySelector('button[aria-label="Close"]'); + expect(document.activeElement).not.toBe(dismissButton); + }); +}); diff --git a/src/popover/body.tsx b/src/popover/body.tsx index 283a4236b1..43295e5ac1 100644 --- a/src/popover/body.tsx +++ b/src/popover/body.tsx @@ -18,6 +18,7 @@ import styles from './styles.css.js'; export interface PopoverBodyProps { dismissButton: boolean; dismissAriaLabel: string | undefined; + disableDismissAutoFocus?: boolean; onDismiss: ((method?: 'escape' | 'close-button') => void) | undefined; onBlur?: (event: React.FocusEvent) => void; @@ -37,6 +38,7 @@ const PopoverBody = React.forwardRef( { dismissButton: showDismissButton, dismissAriaLabel, + disableDismissAutoFocus = false, header, children, onDismiss, @@ -68,11 +70,11 @@ const PopoverBody = React.forwardRef( // because we also want to focus the dismiss button when it // is added dynamically (e.g. in chart popovers) useEffect(() => { - if (showDismissButton && !dismissButtonFocused.current) { + if (showDismissButton && !disableDismissAutoFocus && !dismissButtonFocused.current) { dismissButtonRef.current?.focus({ preventScroll: true }); } dismissButtonFocused.current = showDismissButton; - }, [showDismissButton]); + }, [showDismissButton, disableDismissAutoFocus]); const dismissButton = (showDismissButton ?? null) && (