Skip to content

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

TypeScript

fix(core): restrict focusableSelector href matching to real links (#3714)

Baraa Hamodi48e442626a8c
diff --git a/.changeset/focusable-selector-href.md b/.changeset/focusable-selector-href.md
new file mode 100644
index 0000000000..fecc9582a4
--- /dev/null
+++ b/.changeset/focusable-selector-href.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] focusableSelector: match only real links (`a[href]`, `area[href]`) instead of any element with an href attribute. Previously non-focusable elements carrying href could be treated as tab stops by useFocusTrap. (#3714)
+@bhamodi
diff --git a/packages/core/src/hooks/focusableSelector.ts b/packages/core/src/hooks/focusableSelector.ts
index eb2395fb39..99e1481e01 100644
--- a/packages/core/src/hooks/focusableSelector.ts
+++ b/packages/core/src/hooks/focusableSelector.ts
@@ -22,4 +22,4 @@
* re-declaring the string so behavior stays consistent across hooks.
*/
export const FOCUSABLE_SELECTOR =
- 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled]), [contenteditable]:not([contenteditable="false"]), audio[controls], video[controls], iframe, details > summary:first-child';
+ 'button:not([disabled]), a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled]), [contenteditable]:not([contenteditable="false"]), audio[controls], video[controls], iframe, details > summary:first-child';
diff --git a/packages/core/src/hooks/useFocusTrap.test.tsx b/packages/core/src/hooks/useFocusTrap.test.tsx
index 3042dff994..bb64bf0184 100644
--- a/packages/core/src/hooks/useFocusTrap.test.tsx
+++ b/packages/core/src/hooks/useFocusTrap.test.tsx
@@ -11,6 +11,7 @@
import {describe, it, expect, vi} from 'vitest';
import {render, screen, fireEvent} from '@testing-library/react';
+import {FOCUSABLE_SELECTOR} from './focusableSelector';
import {useFocusTrap} from './useFocusTrap';
function Trap({children}: {children: React.ReactNode}) {
@@ -84,6 +85,48 @@ describe('useFocusTrap tabbable model (infra-8)', () => {
});
});
+describe('FOCUSABLE_SELECTOR href matching', () => {
+ // Only real links (<a href>/<area href>) are focusable via href. A bare
+ // [href] term also matched non-focusable elements carrying href (e.g. a
+ // <link> in the head, or a custom element), which useFocusTrap would then
+ // treat as tab stops when computing trap boundaries.
+ it('matches real links but not other elements carrying href', () => {
+ const container = document.createElement('div');
+ container.innerHTML =
+ '<a href="#a" data-testid="anchor">Anchor</a>' +
+ '<map name="m">' +
+ '<area href="#area" shape="rect" coords="0,0,1,1" data-testid="area" />' +
+ '</map>' +
+ '<span href="#span" data-testid="span">Span</span>' +
+ '<link href="#link" data-testid="link" />';
+
+ const matches = Array.from(
+ container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR),
+ );
+ const byTestId = (id: string) =>
+ container.querySelector(`[data-testid="${id}"]`);
+
+ // Real links are focusable via href.
+ expect(matches).toContain(byTestId('anchor'));
+ expect(matches).toContain(byTestId('area'));
+ // Non-link elements carrying href are not focusable and must be excluded.
+ expect(matches).not.toContain(byTestId('span'));
+ expect(matches).not.toContain(byTestId('link'));
+ });
+
+ it('treats an <a href> inside a trap as focusable (focusFirst lands on it)', () => {
+ render(
+ <Trap>
+ <a href="#link" data-testid="anchor">
+ Link
+ </a>
+ </Trap>,
+ );
+ fireEvent.click(screen.getByTestId('focus-first'));
+ expect(screen.getByTestId('anchor')).toHaveFocus();
+ });
+});
+
describe('useFocusTrap Escape coordination', () => {
it('calls onEscape for a single active trap', () => {
const onEscape = vi.fn();
@@ -125,7 +168,9 @@ describe('useFocusTrap Escape coordination', () => {
const {rerender} = render(
<EscapeTrap isActive onEscape={onEscape} label="toggle" />,
);
- rerender(<EscapeTrap isActive={false} onEscape={onEscape} label="toggle" />);
+ rerender(
+ <EscapeTrap isActive={false} onEscape={onEscape} label="toggle" />,
+ );
fireEvent.keyDown(document, {key: 'Escape'});
expect(onEscape).not.toHaveBeenCalled();
});