Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Side Sheet: Dismissible should set focus after opening #3940

Closed
2 tasks
acdvorak opened this issue Oct 18, 2018 · 1 comment
Closed
2 tasks

Side Sheet: Dismissible should set focus after opening #3940

acdvorak opened this issue Oct 18, 2018 · 1 comment
Milestone

Comments

@acdvorak
Copy link
Contributor

From #3924 (comment):

this.adapter_.focusActiveNavigationItem();

We probably do still want to focus something within the dismissible side sheet when it opens...but we'd probably have to just look for the first focusable thing within it. I forget if we've already implemented or used something like that somewhere else in our repo? Otherwise we could probably use tabbable (focus-trap's dependency) for it, but might want to delegate that to a util function as well.

Dialog briefly had a util function for this in #3413, but it was removed because focus-trap already provides the same functionality (via its dependency on tabbable):

function getFirstFocusableElement(surfaceEl) {
const includeSelectors = [
'a',
'button',
'input',
'select',
'summary',
'textarea',
'audio[controls]',
'video[controls]',
'[contenteditable]',
'[tabIndex]',
];
const excludeSelectors = [
'input[type="radio"]',
'input[type="checkbox"]',
'input[type="hidden"]',
'[disabled]',
'[tabIndex="-1"]',
];
const isExcluded = (el) => {
const style = getComputedStyle(el);
if (style.display === 'none' || style.visibility === 'hidden' || parseFloat(style.opacity) < 0.01) {
return true;
}
const rect = el.getBoundingClientRect();
if (!rect.width || !rect.height) {
return true;
}
if (excludeSelectors.some((excludeSelector) => matches(el, excludeSelector))) {
return true;
}
return false;
};
/** @type {!Array<!HTMLElement>} */
const includedEls = [].slice.call(surfaceEl.querySelectorAll(includeSelectors.join(', ')));
const focusableEls = includedEls.filter((el) => {
while (el) {
if (isExcluded(el)) {
return false;
}
el = el.parentElement;
}
return true;
});
const autoFocusEls = focusableEls.filter((el) => {
return el.autofocus;
});
return autoFocusEls[0] || focusableEls[0];
}

Tasks:

  • Rename focusActiveNavigationItem() to something more generic (e.g., focusContent())
  • Create a util function to find the first focusable element (probably using tabbable)
@bonniezhou
Copy link
Contributor

Closing this as obsolete.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants