UNPKG

js.foresight

Version:

Predicts mouse trajectory to trigger actions as users approach elements, enabling anticipatory UI updates or pre-loading. Made with vanilla javascript and usable in every framework.

27 lines 1.41 kB
/** * Finds the index of a focused element within a cache of tabbable elements. * It uses a predictive search for O(1) performance in the common case of * sequential tabbing, and falls back to a linear search O(n) if the * prediction fails. * * @param isReversed - True if the user is tabbing backward (Shift+Tab). * @param lastFocusedIndex - The index of the previously focused element, or null if none. * @param tabbableElementsCache - The array of all tabbable elements. * @param targetElement - The new HTML element that has received focus. * @returns The index of the targetElement in the cache, or -1 if not found. */ export function getFocusedElementIndex(isReversed, lastFocusedIndex, tabbableElementsCache, targetElement) { // First, try to predict the next index based on the last known position. if (lastFocusedIndex !== null) { var predictedIndex = isReversed ? lastFocusedIndex - 1 : lastFocusedIndex + 1; // Check if the prediction is valid and correct. if (predictedIndex >= 0 && predictedIndex < tabbableElementsCache.length && tabbableElementsCache[predictedIndex] === targetElement) { return predictedIndex; } } // Slow way if we dont find it return tabbableElementsCache.findIndex(function (element) { return element === targetElement; }); } //# sourceMappingURL=getFocusedElementIndex.js.map