Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: ComposedModal autofocus ActionableNotification action button on open #16231

Open
2 tasks done
ferdelamad opened this issue Apr 19, 2024 · 1 comment
Open
2 tasks done

Comments

@ferdelamad
Copy link

ferdelamad commented Apr 19, 2024

Package

@carbon/react

Browser

Chrome

Package version

1.55.0

React version

18.2.0

Description

When an ActionableNotification is present on the page when opening a Modal this will cause the side-effect of auto-focusing on the action button or the exit button from the ActionableNotification causing an undesired scroll to the page.

If you exit the modal by clicking outside, it will scroll back up and autofocus the button that triggered it to be opened.

If you exit the modal by clicking the "X" button, it will not scroll to the original position and will keep the focus on the action button or the exit button, and if you try to click the button that triggers the modal it will just scroll to the ActionableNotification since the focus is still on that element.

This can be fixed by adding the hasFocus prop set to false to the ActionableNotification, however, this should not be the default behavior.
I'm still not sure what is causing this side-effect, therefor I still have concerns that this may be happening to other components.

Demo:
carbon_modal_actionable_notification

Reproduction/example

https://codesandbox.io/p/devbox/carbon-modal-actionable-notification-s8z4gz?file=%2Fsrc%2FExample%2FExample.jsx

Steps to reproduce

Reproduce the scrolling/autofocus behavior

  • Click on the "Launch composed modal" button

To reproduce the issue of the focus remaining in the actionable notification:

  • Click on the "Launch composed modal" button
  • Exit the modal by clicking the "X" button

Suggested Severity

None

Application/PAL

No response

Code of Conduct

@tay1orjones
Copy link
Member

@ferdelamad Since ActionableNotification takes focus on render, if the modal opening triggers a re-render, it could cause this odd behavior.

useIsomorphicEffect(() => {
if (hasFocus) {
const button = document.querySelector(
'button.cds--actionable-notification__action-button'
) as HTMLButtonElement;
button?.focus();
}
});

It might be that the functionality of hasFocus could be improved to be more compatible with the modal.

A few things worth investigating would include:

  1. Does using launcherButtonRef fix this? feat(Modal): add launcherButtonRef prop to handle focus on close #14355
  2. Why is there a behavior difference between closing the modal via the X vs clicking outside the modal
  3. Is it linked with the existing focus wrap behavior - does it still happen with enable-experimental-focus-wrap-without-sentinels on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ⏱ Backlog
Development

No branches or pull requests

3 participants