@helpwave/hightide
Version:
helpwave's component and theming library
84 lines (83 loc) • 3.13 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/hooks/useFocusManagement.ts
var useFocusManagement_exports = {};
__export(useFocusManagement_exports, {
useFocusManagement: () => useFocusManagement
});
module.exports = __toCommonJS(useFocusManagement_exports);
var import_react = require("react");
function useFocusManagement() {
const getFocusableElements = (0, import_react.useCallback)(() => {
return Array.from(
document.querySelectorAll(
'input, button, select, textarea, a[href], [tabindex]:not([tabindex="-1"])'
)
).filter(
(el) => el instanceof HTMLElement && !el.hasAttribute("disabled") && !el.hasAttribute("hidden") && el.tabIndex !== -1
);
}, []);
const getNextFocusElement = (0, import_react.useCallback)(() => {
const elements = getFocusableElements();
if (elements.length === 0) {
return void 0;
}
let nextElement = elements[0];
if (document.activeElement instanceof HTMLElement) {
const currentIndex = elements.indexOf(document.activeElement);
nextElement = elements[(currentIndex + 1) % elements.length];
}
return nextElement;
}, [getFocusableElements]);
const focusNext = (0, import_react.useCallback)(() => {
const nextElement = getNextFocusElement();
nextElement?.focus();
}, [getNextFocusElement]);
const getPreviousFocusElement = (0, import_react.useCallback)(() => {
const elements = getFocusableElements();
if (elements.length === 0) {
return void 0;
}
let previousElement = elements[0];
if (document.activeElement instanceof HTMLElement) {
const currentIndex = elements.indexOf(document.activeElement);
if (currentIndex === 0) {
previousElement = elements[elements.length - 1];
} else {
previousElement = elements[currentIndex - 1];
}
}
return previousElement;
}, [getFocusableElements]);
const focusPrevious = (0, import_react.useCallback)(() => {
const previousElement = getPreviousFocusElement();
if (previousElement) previousElement.focus();
}, [getPreviousFocusElement]);
return {
getFocusableElements,
getNextFocusElement,
getPreviousFocusElement,
focusNext,
focusPrevious
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useFocusManagement
});
//# sourceMappingURL=useFocusManagement.js.map