170 lines
4.8 KiB
JavaScript
170 lines
4.8 KiB
JavaScript
"use strict";
|
|
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/focus.ts
|
|
var focus_exports = {};
|
|
__export(focus_exports, {
|
|
focus: () => focus
|
|
});
|
|
module.exports = __toCommonJS(focus_exports);
|
|
|
|
// src/dom.ts
|
|
function isElement(el) {
|
|
return el != null && typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE;
|
|
}
|
|
function isHTMLElement(el) {
|
|
var _a;
|
|
if (!isElement(el)) {
|
|
return false;
|
|
}
|
|
const win = (_a = el.ownerDocument.defaultView) != null ? _a : window;
|
|
return el instanceof win.HTMLElement;
|
|
}
|
|
function getOwnerDocument(node) {
|
|
var _a;
|
|
return isElement(node) ? (_a = node.ownerDocument) != null ? _a : document : document;
|
|
}
|
|
|
|
// src/assertion.ts
|
|
var __DEV__ = process.env.NODE_ENV !== "production";
|
|
var __TEST__ = process.env.NODE_ENV === "test";
|
|
|
|
// src/function.ts
|
|
function once(fn) {
|
|
let result;
|
|
return function func(...args) {
|
|
if (fn) {
|
|
result = fn.apply(this, args);
|
|
fn = null;
|
|
}
|
|
return result;
|
|
};
|
|
}
|
|
var warn = /* @__PURE__ */ once((options) => () => {
|
|
const { condition, message } = options;
|
|
if (condition && __DEV__) {
|
|
console.warn(message);
|
|
}
|
|
});
|
|
|
|
// src/tabbable.ts
|
|
function isInputElement(element) {
|
|
return isHTMLElement(element) && element.localName === "input" && "select" in element;
|
|
}
|
|
function isActiveElement(element) {
|
|
const doc = isHTMLElement(element) ? getOwnerDocument(element) : document;
|
|
return doc.activeElement === element;
|
|
}
|
|
|
|
// src/focus.ts
|
|
function focus(element, options = {}) {
|
|
const {
|
|
isActive = isActiveElement,
|
|
nextTick,
|
|
preventScroll = true,
|
|
selectTextIfInput = true
|
|
} = options;
|
|
if (!element || isActive(element))
|
|
return -1;
|
|
function triggerFocus() {
|
|
if (!element) {
|
|
warn({
|
|
condition: true,
|
|
message: "[chakra-ui]: can't call focus() on `null` or `undefined` element"
|
|
});
|
|
return;
|
|
}
|
|
if (supportsPreventScroll()) {
|
|
element.focus({ preventScroll });
|
|
} else {
|
|
element.focus();
|
|
if (preventScroll) {
|
|
const scrollableElements = getScrollableElements(element);
|
|
restoreScrollPosition(scrollableElements);
|
|
}
|
|
}
|
|
if (selectTextIfInput) {
|
|
if (isInputElement(element)) {
|
|
element.select();
|
|
} else if ("setSelectionRange" in element) {
|
|
const el = element;
|
|
el.setSelectionRange(el.value.length, el.value.length);
|
|
}
|
|
}
|
|
}
|
|
if (nextTick) {
|
|
return requestAnimationFrame(triggerFocus);
|
|
}
|
|
triggerFocus();
|
|
return -1;
|
|
}
|
|
var supportsPreventScrollCached = null;
|
|
function supportsPreventScroll() {
|
|
if (supportsPreventScrollCached == null) {
|
|
supportsPreventScrollCached = false;
|
|
try {
|
|
const div = document.createElement("div");
|
|
div.focus({
|
|
get preventScroll() {
|
|
supportsPreventScrollCached = true;
|
|
return true;
|
|
}
|
|
});
|
|
} catch (e) {
|
|
}
|
|
}
|
|
return supportsPreventScrollCached;
|
|
}
|
|
function getScrollableElements(element) {
|
|
var _a;
|
|
const doc = getOwnerDocument(element);
|
|
const win = (_a = doc.defaultView) != null ? _a : window;
|
|
let parent = element.parentNode;
|
|
const scrollableElements = [];
|
|
const rootScrollingElement = doc.scrollingElement || doc.documentElement;
|
|
while (parent instanceof win.HTMLElement && parent !== rootScrollingElement) {
|
|
if (parent.offsetHeight < parent.scrollHeight || parent.offsetWidth < parent.scrollWidth) {
|
|
scrollableElements.push({
|
|
element: parent,
|
|
scrollTop: parent.scrollTop,
|
|
scrollLeft: parent.scrollLeft
|
|
});
|
|
}
|
|
parent = parent.parentNode;
|
|
}
|
|
if (rootScrollingElement instanceof win.HTMLElement) {
|
|
scrollableElements.push({
|
|
element: rootScrollingElement,
|
|
scrollTop: rootScrollingElement.scrollTop,
|
|
scrollLeft: rootScrollingElement.scrollLeft
|
|
});
|
|
}
|
|
return scrollableElements;
|
|
}
|
|
function restoreScrollPosition(scrollableElements) {
|
|
for (const { element, scrollTop, scrollLeft } of scrollableElements) {
|
|
element.scrollTop = scrollTop;
|
|
element.scrollLeft = scrollLeft;
|
|
}
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
focus
|
|
});
|