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

The element in dialog lost focus state. #2436

Open
taoliujun opened this issue Oct 9, 2023 · 3 comments
Open

The element in dialog lost focus state. #2436

taoliujun opened this issue Oct 9, 2023 · 3 comments

Comments

@taoliujun
Copy link

taoliujun commented Oct 9, 2023

Bug report

Current Behavior

There is an input element A comes with a custom clear icon in a dialog component, and in its focusout event, the focus behavior of another input element B in the dialog component is triggered. It is found that the trigger failed and the cursor does not enter the B element.

Expected behavior

The cursor enters the B element.

Reproducible example

Suggested solution

I looked at the context source code of the dialog component and read the following in the focus-scope file:

// When the focused element gets removed from the DOM, browsers move focus
// back to the document.body. In this case, we move focus to the container
// to keep focus trapped correctly.
function handleMutations(mutations: MutationRecord[]) {
  const focusedElement = document.activeElement as HTMLElement | null;
  if (focusedElement !== document.body) return;
  for (const mutation of mutations) {
    if (mutation.removedNodes.length > 0) focus(container);
  }
}

With the MutationObserver feature, if an element is removed (the input A lost focus, the clear icon is removed), then the focus will be placed on the container (react-dialog root) node, cause the focus of the element B to be lost immediately.

Additional context

Your environment

Software Name(s) Version
Radix Package(s)
React n/a
Browser
Assistive tech
Node n/a
npm/yarn
Operating System
@enkot
Copy link

enkot commented Nov 15, 2023

RadixVue also has this issue, it uses the same logic with MutationObserver - radix-vue/radix-vue#518

@benoitgrelard
Copy link
Collaborator

Hi @taoliujun could you provide a reproduction in a sandbox?

@BenLorantfy
Copy link

BenLorantfy commented Sep 11, 2024

Hey @benoitgrelard, I fixed this issue and created a PR: #3115

The PR contains a reproduction as a storybook story / cypress test

Also please note the vue fork fixed this already here with a similar fix: radix-vue/radix-vue#519

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants