UNPKG

@bianic-ui/utils

Version:

Common utilties and types for Bianic UI

77 lines (69 loc) 2.41 kB
// Really great work done by Diego Haz on this one // https://github.com/reakit/reakit/blob/master/packages/reakit-utils/src/tabbable.ts export var hasDisplayNone = element => window.getComputedStyle(element).display === "none"; export var hasTabIndex = element => element.hasAttribute("tabindex"); export var hasNegativeTabIndex = element => hasTabIndex(element) && element.tabIndex === -1; export function isDisabled(element) { return Boolean(element.getAttribute("disabled")) == true || Boolean(element.getAttribute("aria-disabled")) == true; } export function hasFocusWithin(element) { if (!document.activeElement) return false; return element.contains(document.activeElement); } export function isHTMLElement(element) { return element instanceof HTMLElement; } export function isHidden(element) { if (element.parentElement && isHidden(element.parentElement)) return true; return element.hidden; } export function isContentEditable(element) { var value = element.getAttribute("contenteditable"); return value !== "false" && value != null; } export function isFocusable(element) { if (!isHTMLElement(element) || isHidden(element) || isDisabled(element)) { return false; } var { localName } = element; var focusableTags = ["input", "select", "textarea", "button"]; if (focusableTags.indexOf(localName) >= 0) return true; var others = { a: () => element.hasAttribute("href"), audio: () => element.hasAttribute("controls"), video: () => element.hasAttribute("controls") }; if (localName in others) { return others[localName](); } if (isContentEditable(element)) return true; return hasTabIndex(element); } export function isTabbable(element) { return isHTMLElement(element) && isFocusable(element) && !hasNegativeTabIndex(element); } var isActiveElement = element => document.activeElement === element; function isInputElement(element) { return isHTMLElement(element) && element.tagName.toLowerCase() === "input" && "select" in element; } export function focus(element, options) { if (options === void 0) { options = {}; } var { isActive = isActiveElement, preventScroll } = options; if (isActive(element)) return -1; return requestAnimationFrame(() => { element.focus({ preventScroll }); if (isInputElement(element)) { element.select(); } }); } //# sourceMappingURL=tabbable.js.map