lion/packages/ui/components/overlays/utils/simulate-tab.js
2022-10-31 16:55:07 +01:00

14 lines
402 B
JavaScript

import { getFocusableElements } from './get-focusable-elements.js';
export function simulateTab(node = document.body) {
const current = /** @type {HTMLElement} */ (document.activeElement);
const all = getFocusableElements(node);
const currentIndex = all.indexOf(current);
let nextIndex = currentIndex + 1;
if (nextIndex === all.length) {
nextIndex = 0;
}
all[nextIndex].focus();
}