@redocly/theme
Version:
Shared UI components lib
40 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendDetailsBehaviour = extendDetailsBehaviour;
function extendDetailsBehaviour() {
enhanceDetails();
openDetailsFromHash();
window.addEventListener('hashchange', openDetailsFromHash);
}
function openDetailsAndScroll(detailsElement) {
detailsElement.setAttribute('open', '');
setTimeout(() => {
detailsElement.scrollIntoView({
block: 'center',
behavior: 'smooth',
});
}, 0);
}
function enhanceDetails() {
const existingIds = [];
Array.from(document.getElementsByTagName('details')).forEach((detailsElement, index) => {
var _a, _b;
if (detailsElement.id) {
return;
}
const generatedId = ((_b = (_a = detailsElement.querySelector('summary')) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.replace(/\s+/g, '-').toLowerCase()) ||
`details-${index}`;
detailsElement.id = existingIds.includes(generatedId) ? `${generatedId}-${index}` : generatedId;
existingIds.push(detailsElement.id);
});
}
function openDetailsFromHash() {
const hash = window.location.hash;
if (!hash)
return;
const detailsElement = document.getElementById(hash.substring(1));
if ((detailsElement === null || detailsElement === void 0 ? void 0 : detailsElement.tagName) === 'DETAILS') {
openDetailsAndScroll(detailsElement);
}
}
//# sourceMappingURL=details.js.map