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

[Breadcrumbs] Keep focus in the component after expanding #20489

Merged
merged 2 commits into from
Apr 10, 2020
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
9 changes: 8 additions & 1 deletion packages/material-ui/src/Breadcrumbs/Breadcrumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ const Breadcrumbs = React.forwardRef(function Breadcrumbs(props, ref) {
const [expanded, setExpanded] = React.useState(false);

const renderItemsBeforeAndAfter = (allItems) => {
const handleClickExpand = () => {
const handleClickExpand = (event) => {
setExpanded(true);

// The clicked element received the focus but gets removed from the DOM.
// Let's keep the focus in the component after expanding.
const focusable = event.currentTarget.parentNode.querySelector('a[href],button,[tabindex]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (focusable) {
focusable.focus();
}
};

// This defends against someone passing weird input, to ensure that if all
Expand Down
6 changes: 3 additions & 3 deletions packages/material-ui/src/Breadcrumbs/Breadcrumbs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ describe('<Breadcrumbs />', () => {
});

it('should expand when `BreadcrumbCollapsed` is clicked', () => {
const { getAllByRole, getByRole } = render(
const { getAllByRole, getByRole, getByText } = render(
<Breadcrumbs>
<span>first</span>
<span tabIndex="-1">first</span>
<span>second</span>
<span>third</span>
<span>fourth</span>
Expand All @@ -79,7 +79,7 @@ describe('<Breadcrumbs />', () => {
);

getByRole('button').click();

expect(document.activeElement).to.equal(getByText('first'));
expect(getAllByRole('listitem', { hidden: false })).to.have.length(9);
});

Expand Down