@postenbring/hedwig-react
Version:
React components for [Hedwig Design System](https://github.com/bring/hedwig-design-system).
45 lines (44 loc) • 1.31 kB
JavaScript
// src/form/error-summary/focus.ts
function focusWithLegendOrLabelInViewport(id) {
var _a;
const input = document.getElementById(id);
if (!input) {
return false;
}
const legendOrLabel = (_a = maybeLegendForInput(input)) != null ? _a : labelForInput(input);
if (!legendOrLabel) {
return false;
}
legendOrLabel.scrollIntoView();
input.focus({ preventScroll: true });
return true;
}
function maybeLegendForInput(input) {
const fieldset = input.closest("fieldset");
if (!fieldset) {
return null;
}
const legend = fieldset.querySelector("legend");
if (!legend) {
return null;
}
if (input instanceof HTMLInputElement && (input.type === "checkbox" || input.type === "radio")) {
return legend;
}
const legendTop = legend.getBoundingClientRect().top;
const inputRect = input.getBoundingClientRect();
if (inputRect.height && window.innerHeight) {
const inputBottom = inputRect.top + inputRect.height;
if (inputBottom - legendTop < window.innerHeight / 2) {
return legend;
}
}
}
function labelForInput(input) {
var _a;
return (_a = document.querySelector(`label[for='${input.getAttribute("id")}']`)) != null ? _a : input.closest("label");
}
export {
focusWithLegendOrLabelInViewport
};
//# sourceMappingURL=chunk-CKAL5ZJZ.mjs.map