Skip to content

Commit 5e39149

Browse files
committed
A few small additional perf improvements for large apges with margin content
1 parent 8877297 commit 5e39149

File tree

1 file changed

+97
-93
lines changed

1 file changed

+97
-93
lines changed

src/resources/formats/html/quarto.js

Lines changed: 97 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
259259

260260
const manageSidebarVisiblity = (el, placeholderDescriptor) => {
261261
let isVisible = true;
262+
let elRect;
262263

263264
return (hiddenRegions) => {
264265
if (el === null) {
@@ -276,104 +277,107 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
276277
child.style.overflow = "hidden";
277278
}
278279

279-
const toggleContainer = window.document.createElement("div");
280-
toggleContainer.style.width = "100%";
281-
toggleContainer.classList.add("zindex-over-content");
282-
toggleContainer.classList.add("quarto-sidebar-toggle");
283-
toggleContainer.classList.add("headroom-target"); // Marks this to be managed by headeroom
284-
toggleContainer.id = placeholderDescriptor.id;
285-
toggleContainer.style.position = "fixed";
286-
287-
const toggleIcon = window.document.createElement("i");
288-
toggleIcon.classList.add("quarto-sidebar-toggle-icon");
289-
toggleIcon.classList.add("bi");
290-
toggleIcon.classList.add("bi-caret-down-fill");
291-
292-
const toggleTitle = window.document.createElement("div");
293-
const titleEl = window.document.body.querySelector(
294-
placeholderDescriptor.titleSelector
295-
);
296-
if (titleEl) {
297-
toggleTitle.append(
298-
titleEl.textContent || titleEl.innerText,
299-
toggleIcon
280+
nexttick(() => {
281+
const toggleContainer = window.document.createElement("div");
282+
toggleContainer.style.width = "100%";
283+
toggleContainer.classList.add("zindex-over-content");
284+
toggleContainer.classList.add("quarto-sidebar-toggle");
285+
toggleContainer.classList.add("headroom-target"); // Marks this to be managed by headeroom
286+
toggleContainer.id = placeholderDescriptor.id;
287+
toggleContainer.style.position = "fixed";
288+
289+
const toggleIcon = window.document.createElement("i");
290+
toggleIcon.classList.add("quarto-sidebar-toggle-icon");
291+
toggleIcon.classList.add("bi");
292+
toggleIcon.classList.add("bi-caret-down-fill");
293+
294+
const toggleTitle = window.document.createElement("div");
295+
const titleEl = window.document.body.querySelector(
296+
placeholderDescriptor.titleSelector
300297
);
301-
}
302-
toggleTitle.classList.add("zindex-over-content");
303-
toggleTitle.classList.add("quarto-sidebar-toggle-title");
304-
toggleContainer.append(toggleTitle);
305-
306-
const toggleContents = window.document.createElement("div");
307-
toggleContents.classList = el.classList;
308-
toggleContents.classList.add("zindex-over-content");
309-
toggleContents.classList.add("quarto-sidebar-toggle-contents");
310-
for (const child of el.children) {
311-
if (child.id === "toc-title") {
312-
continue;
298+
if (titleEl) {
299+
toggleTitle.append(
300+
titleEl.textContent || titleEl.innerText,
301+
toggleIcon
302+
);
313303
}
314-
315-
const clone = child.cloneNode(true);
316-
clone.style.opacity = 1;
317-
clone.style.display = null;
318-
toggleContents.append(clone);
319-
}
320-
toggleContents.style.height = "0px";
321-
toggleContainer.append(toggleContents);
322-
323-
el.parentElement.prepend(toggleContainer);
324-
325-
// Process clicks
326-
let tocShowing = false;
327-
// Allow the caller to control whether this is dismissed
328-
// when it is clicked (e.g. sidebar navigation supports
329-
// opening and closing the nav tree, so don't dismiss on click)
330-
const clickEl = placeholderDescriptor.dismissOnClick
331-
? toggleContainer
332-
: toggleTitle;
333-
334-
const closeToggle = () => {
335-
if (tocShowing) {
336-
toggleContainer.classList.remove("expanded");
337-
toggleContents.style.height = "0px";
338-
tocShowing = false;
304+
toggleTitle.classList.add("zindex-over-content");
305+
toggleTitle.classList.add("quarto-sidebar-toggle-title");
306+
toggleContainer.append(toggleTitle);
307+
308+
const toggleContents = window.document.createElement("div");
309+
toggleContents.classList = el.classList;
310+
toggleContents.classList.add("zindex-over-content");
311+
toggleContents.classList.add("quarto-sidebar-toggle-contents");
312+
for (const child of el.children) {
313+
if (child.id === "toc-title") {
314+
continue;
315+
}
316+
317+
const clone = child.cloneNode(true);
318+
clone.style.opacity = 1;
319+
clone.style.display = null;
320+
toggleContents.append(clone);
339321
}
340-
};
341-
342-
const positionToggle = () => {
343-
// position the element (top left of parent, same width as parent)
344-
const elRect = el.getBoundingClientRect();
345-
toggleContainer.style.left = `${elRect.left}px`;
346-
toggleContainer.style.top = `${elRect.top}px`;
347-
toggleContainer.style.width = `${elRect.width}px`;
348-
};
322+
toggleContents.style.height = "0px";
323+
const positionToggle = () => {
324+
// position the element (top left of parent, same width as parent)
325+
if (!elRect) {
326+
elRect = el.getBoundingClientRect();
327+
}
328+
toggleContainer.style.left = `${elRect.left}px`;
329+
toggleContainer.style.top = `${elRect.top}px`;
330+
toggleContainer.style.width = `${elRect.width}px`;
331+
};
332+
positionToggle();
333+
334+
toggleContainer.append(toggleContents);
335+
el.parentElement.prepend(toggleContainer);
336+
337+
// Process clicks
338+
let tocShowing = false;
339+
// Allow the caller to control whether this is dismissed
340+
// when it is clicked (e.g. sidebar navigation supports
341+
// opening and closing the nav tree, so don't dismiss on click)
342+
const clickEl = placeholderDescriptor.dismissOnClick
343+
? toggleContainer
344+
: toggleTitle;
345+
346+
const closeToggle = () => {
347+
if (tocShowing) {
348+
toggleContainer.classList.remove("expanded");
349+
toggleContents.style.height = "0px";
350+
tocShowing = false;
351+
}
352+
};
353+
354+
// Get rid of any expanded toggle if the user scrolls
355+
window.document.addEventListener(
356+
"scroll",
357+
throttle(() => {
358+
closeToggle();
359+
}, 50)
360+
);
349361

350-
// Get rid of any expanded toggle if the user scrolls
351-
window.document.addEventListener(
352-
"scroll",
353-
throttle(() => {
354-
closeToggle();
355-
}, 50)
356-
);
362+
// Handle positioning of the toggle
363+
window.addEventListener(
364+
"resize",
365+
throttle(() => {
366+
positionToggle();
367+
}, 50)
368+
);
357369

358-
// Handle positioning of the toggle
359-
window.addEventListener(
360-
"resize",
361-
throttle(() => {
362-
positionToggle();
363-
}, 50)
364-
);
365-
positionToggle();
366-
367-
// Process the click
368-
clickEl.onclick = () => {
369-
if (!tocShowing) {
370-
toggleContainer.classList.add("expanded");
371-
toggleContents.style.height = null;
372-
tocShowing = true;
373-
} else {
374-
closeToggle();
375-
}
376-
};
370+
// Process the click
371+
clickEl.onclick = () => {
372+
if (!tocShowing) {
373+
toggleContainer.classList.add("expanded");
374+
toggleContents.style.height = null;
375+
tocShowing = true;
376+
} else {
377+
closeToggle();
378+
}
379+
};
380+
});
377381
};
378382

379383
// Converts a sidebar from a menu back to a sidebar

0 commit comments

Comments
 (0)