@limetech/lime-elements
Version:
33 lines (32 loc) • 1.12 kB
JavaScript
const FOCUSABLE_SELECTOR = 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
const isDisabled = (element) => {
return (element.disabled === true ||
element.hasAttribute('disabled') ||
element.getAttribute('aria-disabled') === 'true');
};
/**
* Focuses the first focusable element inside a trigger element.
* Supports custom elements by searching both the element's shadow root
* and its light DOM.
*
* @param trigger - The trigger element to focus.
* @returns `true` if focus was moved, otherwise `false`.
*/
export const focusTriggerElement = (trigger) => {
var _a;
if (!trigger || isDisabled(trigger)) {
return false;
}
const shadowFocusable = (_a = trigger.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector(FOCUSABLE_SELECTOR);
if (shadowFocusable) {
shadowFocusable.focus();
return true;
}
const lightDomFocusable = trigger.querySelector(FOCUSABLE_SELECTOR);
if (lightDomFocusable) {
lightDomFocusable.focus();
return true;
}
trigger.focus();
return true;
};