@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
100 lines (95 loc) • 3 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
;
const MAX_TIMEOUT_DURATION = 5000;
function waitForThemed(host) {
return new Promise((resolve, reject) => {
let timeoutId;
const observer = new MutationObserver(() => {
if (host.hasAttribute('data-themed')) {
if (timeoutId) {
clearTimeout(timeoutId);
}
observer.disconnect();
resolve();
}
});
observer.observe(host, {
attributes: true,
attributeFilter: ['data-themed'],
});
if (host.hasAttribute('data-themed')) {
observer.disconnect();
resolve();
return;
}
timeoutId = setTimeout(() => {
observer.disconnect();
reject(new Error('Timeout waiting for data-themed attribute'));
}, MAX_TIMEOUT_DURATION);
});
}
const MAX_CLICK_ATTEMPTS = 3;
async function delegateClick(host, callback) {
try {
if (!host.hasAttribute('data-themed')) {
await waitForThemed(host);
}
await callback();
}
catch (_a) {
throw new Error(`The interactive element inside the KoliBri web component could not be clicked. Try calling the click method on the web component after a short delay again.`);
}
}
function isElementVisible(element) {
if (!element)
return false;
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
}
async function setClick(element) {
let attempts = 0;
do {
if (element) {
element.click();
}
await new Promise((r) => requestAnimationFrame(r));
attempts++;
} while (!isElementVisible(element) && attempts < MAX_CLICK_ATTEMPTS);
}
const MAX_FOCUS_ATTEMPTS = 10;
async function delegateFocus(host, callback) {
try {
if (!host.hasAttribute('data-themed')) {
await waitForThemed(host);
}
await callback();
}
catch (_a) {
throw new Error(`The interactive element inside the KoliBri web component could not be focused. Try calling the focus method on the web component after a short delay again.`);
}
}
function isActiveElement(element) {
const root = element.getRootNode();
if (root instanceof ShadowRoot) {
return root.activeElement === element;
}
return document.activeElement === element;
}
async function setFocus(element) {
let attempts = 0;
do {
if (element) {
element.focus();
}
await new Promise((r) => requestAnimationFrame(r));
attempts++;
} while (!isActiveElement(element) && attempts < MAX_FOCUS_ATTEMPTS);
}
exports.delegateClick = delegateClick;
exports.delegateFocus = delegateFocus;
exports.setClick = setClick;
exports.setFocus = setFocus;
//# sourceMappingURL=element-focus-DeVevBcF.js.map
//# sourceMappingURL=element-focus-DeVevBcF.js.map