reakit-utils
Version:
Reakit utils
20 lines (18 loc) • 634 B
text/typescript
import { getActiveElement } from "./getActiveElement";
/**
* Checks if `element` has focus. Elements that are referenced by
* `aria-activedescendant` are also considered.
*
* @example
* import { hasFocus } from "reakit-utils";
*
* hasFocus(document.getElementById("id"));
*/
export function hasFocus(element: Element): boolean {
const activeElement = getActiveElement(element);
if (!activeElement) return false;
if (activeElement === element) return true;
const activeDescendant = activeElement.getAttribute("aria-activedescendant");
if (!activeDescendant) return false;
return activeDescendant === element.id;
}