Mirror: React hooks for accessible, common web interactions. UI super powers without the UI.
1import { clickableSelectors, focus } from './focus';
2import { contains } from './element';
3
4export const click = (node: Element) => {
5 const activeElement = document.activeElement;
6 if (!activeElement || contains(node, activeElement)) {
7 let target: Element | null = node;
8 if (node.tagName === 'LABEL') {
9 const forId = node.getAttribute('for');
10 target = forId ? document.getElementById(forId) : null;
11 }
12
13 if (!target || !node.matches(clickableSelectors)) {
14 target = node.querySelector(clickableSelectors);
15 }
16
17 ((target || node) as HTMLElement).click();
18 focus(activeElement);
19 }
20};