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