/** * Checks whether an element is visible * * @param {Element} element - The element to test visibility for * @returns {boolean} - Whether the element is visible or not */ export const isVisible = (element: HTMLElement) => { if (!(element instanceof Element)) { throw Error('You must provide a DOM element.'); } return ( !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length) && window.getComputedStyle(element).visibility !== 'hidden' && window.getComputedStyle(element).display !== 'none' && element.style?.transform.includes('translateY(-9999px)') === false ); }; /** * Return the previous/next element of a list. * * @param {array} list The list of elements * @param activeElement The active element * @param shouldGetNext Choose to get next or previous element * @param isCycleAllowed * @returns {Element|elem} The proper element */ export const getNextActiveElement = ( list: HTMLElement[], activeElement: HTMLElement | null | undefined, shouldGetNext: boolean, isCycleAllowed: boolean ) => { const listLength = list.length; let index = activeElement ? list.indexOf(activeElement) : -1; // if the element does not exist in the list return an element // depending on the direction and if cycle is allowed if (index === -1) { return !shouldGetNext && isCycleAllowed ? list[listLength - 1] : list[0]; } index += shouldGetNext ? 1 : -1; if (isCycleAllowed) { index = (index + listLength) % listLength; } return list[Math.max(0, Math.min(index, listLength - 1))]; }; /** * * @param el - The element to test * @returns boolean - Whether the element is an HTMLElement or not */ export const isElement = (el: unknown): el is HTMLElement => { return typeof el === 'object' && el !== null && 'tagName' in el && el instanceof HTMLElement; };