primitives icon indicating copy to clipboard operation
primitives copied to clipboard

Focused Toast removal with `Escape` key broken after `1.1.6-rc.1`

Open jaknas opened this issue 1 year ago • 1 comments

Bug report

Current Behavior

https://github.com/radix-ui/primitives/assets/36169811/44f5b28b-01fd-4034-a42f-cce957faf0cf

Currently, when more than 2 toasts are visible, and focus is move to the oldest toast, when clicking Escape, 2 Toasts are getting removed instead of the focused toast.

Expected behavior

Expectation is that only the focused toast is removed.

Reproducible example

CodeSandbox Template

  1. Open 3 toasts
  2. Move focus to the middle toast
  3. Press Escape
  4. 2 toasts are removed instead of a single, currently focused toast.

Suggested solution

According to my investigation (might be wrong though), after making the useEscapeKeyDown handler a capturing event listener, the order in which event handlers responsible for toast focus management are fired is reversed.

Previously:

  1. onKeyDown
  2. onEscapeKeyDown

Now:

  1. onEscapeKeyDown
  2. onKeyDown

This causes the ref flags to not be set in a correct ore

Additional context

The bug is caused by https://github.com/radix-ui/primitives/pull/2761

Your environment

Software Name(s) Version
Radix Package(s) @radix-ui/toast 1.1.6-rc.1
React n/a latest
Browser Chrome 125
Assistive tech
Node n/a
npm/yarn
Operating System MacOS 14.4.1

jaknas avatar May 17 '24 14:05 jaknas

This is also happening under recently released @radix-ui/[email protected]

jaknas avatar Jun 24 '24 12:06 jaknas