Skip to content

fix: suppress focusable warning for disabled Pressable #8261

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

Merged
merged 2 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/@react-aria/interactions/src/Pressable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export const Pressable = React.forwardRef(({children, ...props}: PressableProps,
if (process.env.NODE_ENV === 'production') {
return;
}

let el = ref.current;
if (!el || !(el instanceof getOwnerWindow(el).Element)) {
console.error('<Pressable> child must forward its ref to a DOM element.');
return;
}

if (!isFocusable(el)) {
if (!props.isDisabled && !isFocusable(el)) {
console.warn('<Pressable> child must be focusable. Please ensure the tabIndex prop is passed through.');
return;
}
Expand Down Expand Up @@ -79,7 +79,7 @@ export const Pressable = React.forwardRef(({children, ...props}: PressableProps,
console.warn(`<Pressable> child must have an interactive ARIA role. Got "${role}".`);
}
}
}, [ref]);
}, [ref, props.isDisabled]);

// @ts-ignore
let childRef = parseInt(React.version, 10) < 19 ? child.ref : child.props.ref;
Expand Down
13 changes: 13 additions & 0 deletions packages/@react-aria/interactions/test/Pressable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ describe('Pressable', function () {
expect(spy).toHaveBeenCalledWith('<Pressable> child must be focusable. Please ensure the tabIndex prop is passed through.');
});

it('supports isDisabled', async function () {
let spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
let {getByRole} = render(
<Pressable isDisabled>
<span role="button">Button</span>
</Pressable>
);

let button = getByRole('button');
expect(button).not.toHaveAttribute('tabindex');
expect(spy).not.toHaveBeenCalled();
});

it('should warn if child does not have a role', async function () {
let spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
render(
Expand Down