Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): only cancel Tab when focus is inside the focus trap (#5397)

Cindy Zhangeb42538efd85
diff --git a/.changeset/focus-trap-tab-outside.md b/.changeset/focus-trap-tab-outside.md
new file mode 100644
index 0000000000..bfa6adcd20
--- /dev/null
+++ b/.changeset/focus-trap-tab-outside.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] useFocusTrap: Tab is only cancelled when focus is actually inside the trapped container. An open layer whose focus legitimately sits outside it — a listbox popup anchored to its own input, as in DateTimeInput, Typeahead, Selector and MultiSelector — no longer swallows Tab for the whole page, so keyboard users move to the next control on the first press. A trapped surface with no tabbable controls and focus on a `tabIndex={-1}` panel still keeps Tab inside it.
+@cixzhang
diff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsx
index 17c3e049db..adb3570d34 100644
--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx
@@ -437,6 +437,7 @@ describe('MultiSelector', () => {
await user.keyboard('{Tab}');
expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(screen.getByRole('button', {name: 'Next'})).toHaveFocus();
});
it('supports keyboard navigation with ArrowDown/ArrowUp', async () => {
diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsx
index 2a488e4464..f281eabf78 100644
--- a/packages/core/src/Selector/Selector.test.tsx
+++ b/packages/core/src/Selector/Selector.test.tsx
@@ -961,7 +961,7 @@ describe('Selector', () => {
expect(onChange).toHaveBeenCalledWith('Banana');
});
- it('closes dropdown on Tab without preventing default focus movement', async () => {
+ it('closes the search dropdown on Tab without preventing default focus movement', async () => {
const user = userEvent.setup();
render(
<>
@@ -1253,6 +1253,29 @@ describe('Selector', () => {
});
describe('keyboard accessibility', () => {
+ it('Tab from the open listbox moves focus to the next control', async () => {
+ const user = userEvent.setup();
+ render(
+ <>
+ <Selector
+ label="Fruit"
+ options={OPTIONS}
+ value="Apple"
+ onChange={() => {}}
+ />
+ <button type="button">Next</button>
+ </>,
+ );
+
+ const trigger = screen.getByRole('combobox');
+ await user.click(trigger);
+ expect(trigger).toHaveAttribute('aria-expanded', 'true');
+
+ await user.keyboard('{Tab}');
+ expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(screen.getByRole('button', {name: 'Next'})).toHaveFocus();
+ });
+
it('trigger is focusable via Tab when enabled', async () => {
const user = userEvent.setup();
render(<Selector label="Fruit" options={OPTIONS} />);
diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsx
index 48e668ea54..0f2ad5a69c 100644
--- a/packages/core/src/Typeahead/Typeahead.test.tsx
+++ b/packages/core/src/Typeahead/Typeahead.test.tsx
@@ -420,6 +420,30 @@ describe('BaseTypeahead focus-out', () => {
});
});
+ it('Tab from the input with the list open moves focus to the next control', async () => {
+ const user = userEvent.setup();
+ render(
+ <>
+ <BaseTypeahead
+ searchSource={fruitSource}
+ value={null}
+ onChange={() => {}}
+ debounceMs={0}
+ />
+ <button type="button">Next</button>
+ </>,
+ );
+ const input = screen.getByRole('combobox');
+ input.focus();
+ fireEvent.change(input, {target: {value: 'App'}});
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ });
+
+ await user.keyboard('{Tab}');
+ expect(screen.getByRole('button', {name: 'Next'})).toHaveFocus();
+ });
+
it('keeps the dropdown open when focus moves into the anchor wrapper', async () => {
const anchor = document.createElement('div');
document.body.appendChild(anchor);
diff --git a/packages/core/src/hooks/useFocusTrap.test.tsx b/packages/core/src/hooks/useFocusTrap.test.tsx
index 4ff4052dca..394da81b40 100644
--- a/packages/core/src/hooks/useFocusTrap.test.tsx
+++ b/packages/core/src/hooks/useFocusTrap.test.tsx
@@ -208,6 +208,20 @@ describe('useFocusTrap tabbable model (infra-8)', () => {
expect(target).toHaveFocus();
expect(screen.getByTestId('outside')).not.toHaveFocus();
});
+
+ it('leaves Tab alone when focus is outside a trap with no tabbable controls', () => {
+ render(
+ <Trap>
+ <div role="option" tabIndex={-1} data-testid="option">
+ Option
+ </div>
+ </Trap>,
+ );
+ const outside = screen.getByTestId('outside');
+ outside.focus();
+
+ expect(fireEvent.keyDown(outside, {key: 'Tab'})).toBe(true);
+ });
});
describe('FOCUSABLE_SELECTOR href matching', () => {
diff --git a/packages/core/src/hooks/useFocusTrap.ts b/packages/core/src/hooks/useFocusTrap.ts
index 9c6fe593d6..c05638a06c 100644
--- a/packages/core/src/hooks/useFocusTrap.ts
+++ b/packages/core/src/hooks/useFocusTrap.ts
@@ -356,14 +356,18 @@ export function useFocusTrap<T extends HTMLElement = HTMLElement>(
const focusable = getFocusableElements(container);
if (focusable.length === 0) {
+ const active = document.activeElement;
+ if (!(active instanceof HTMLElement) || !container.contains(active)) {
+ // A layer can be open while focus legitimately sits outside it —
+ // a listbox popup anchored to its own input. Cancelling there
+ // would take Tab away from the whole page.
+ return;
+ }
// There is nowhere to advance to. Keep focus on the current
// programmatic target (for example a dialog panel with tabIndex=-1)
// rather than allowing the browser to move into background content.
event.preventDefault();
- const active = document.activeElement;
- if (active instanceof HTMLElement && container.contains(active)) {
- lastFocusRef.current = active;
- }
+ lastFocusRef.current = active;
isKeyboardNavigationRef.current = false;
return;
}