@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
51 lines (50 loc) • 1.56 kB
JavaScript
import { clsx } from 'clsx';
import { twMerge } from '../twMerge';
export function cn(...inputs) {
return twMerge(clsx(inputs));
}
export const POPUP_Z_INDEX = 'twa:z-[9999999]';
const FOCUSABLE_SELECTOR = [
'a[href]',
'button:not([disabled])',
'input:not([disabled])',
'select:not([disabled])',
'textarea:not([disabled])',
'[tabindex]:not([tabindex="-1"])',
].join(',');
export const getFocusableElements = (root) => {
return Array.from(root.querySelectorAll(FOCUSABLE_SELECTOR)).filter((el) => {
if (el.hasAttribute('disabled') || el.getAttribute('aria-hidden') === 'true') {
return false;
}
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
});
};
export function captureTab(node, event) {
if (event === undefined) {
return (deferredEvent) => captureTab(node, deferredEvent);
}
if (event.key !== 'Tab')
return;
const root = node instanceof HTMLElement ? node : (node?.current ?? null);
if (!root)
return;
const active = document.activeElement;
if (!active || !root.contains(active))
return;
const focusable = getFocusableElements(root);
if (focusable.length === 0)
return;
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (event.shiftKey) {
if (active === first) {
event.preventDefault();
last.focus();
}
}
else if (active === last) {
event.preventDefault();
first.focus();
}
}