Repository navigation
Expand file tree
/
Copy pathsidebar.js
More file actions
136 lines (115 loc) · 4.18 KB
/
Copy pathsidebar.js
File metadata and controls
136 lines (115 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Usage: https://github.com/Grsmto/simplebar
import { Offcanvas } from "bootstrap";
import SimpleBar from "simplebar";
import { mount } from "./mount";
// From the lg breakpoint the sidebar is part of the layout and the navbar toggle hides and
// shows it. Below it the sidebar is a Bootstrap offcanvas (.offcanvas-lg) that the toggle
// opens as a sheet, which brings the backdrop, focus trap, Escape and scroll lock. The
// handlers listen on document, so they also work for a sidebar or toggle added later.
const sidebarSelector = ".js-sidebar";
const toggleSelector = ".js-sidebar-toggle";
let desktop;
// Read once the stylesheet applies
const isDesktop = () => {
if (!desktop) {
const breakpoint = getComputedStyle(
document.documentElement
).getPropertyValue("--bs-breakpoint-lg");
desktop = matchMedia(`(min-width: ${breakpoint})`);
// Bootstrap closes the sheet when the window grows past the breakpoint
desktop.addEventListener("change", updateToggles);
}
return desktop.matches;
};
// aria-expanded: the toggle expands and collapses the sidebar it controls; its name says
// what a click does
const updateToggles = () => {
const sidebar = document.querySelector(sidebarSelector);
if (!sidebar) {
return;
}
const expanded = isDesktop()
? !sidebar.classList.contains("collapsed")
: sidebar.classList.contains("show");
for (const toggle of document.querySelectorAll(toggleSelector)) {
toggle.setAttribute("aria-expanded", String(expanded));
toggle.setAttribute(
"aria-label",
expanded ? "Collapse sidebar" : "Expand sidebar"
);
}
};
// Charts resize once the sidebar has moved; without a transition (reduced motion) there is
// no transitionend, so they resize right away
const resizeAfterTransition = (sidebar) => {
const resize = () => window.dispatchEvent(new Event("resize"));
if (!parseFloat(getComputedStyle(sidebar).transitionDuration)) {
resize();
return;
}
const => {
if (event.target === sidebar) {
sidebar.removeEventListener("transitionend", onTransitionEnd);
resize();
}
};
sidebar.addEventListener("transitionend", onTransitionEnd);
};
document.addEventListener("click", (event) => {
const sidebar = document.querySelector(sidebarSelector);
if (!sidebar) {
return;
}
if (event.target.closest(toggleSelector)) {
if (isDesktop()) {
sidebar.classList.toggle("collapsed");
updateToggles();
resizeAfterTransition(sidebar);
} else {
Offcanvas.getOrCreateInstance(sidebar).toggle();
}
} else if (
// Following a link closes the sheet
!isDesktop() &&
event.target.closest(`${sidebarSelector} a[href]:not([data-bs-toggle])`)
) {
Offcanvas.getInstance(sidebar)?.hide();
}
});
document.addEventListener("shown.bs.offcanvas", ({ target }) => {
if (target.matches(sidebarSelector)) {
updateToggles();
}
});
// Focus returns to the toggle, as with Bootstrap's data-bs-toggle triggers
document.addEventListener("hidden.bs.offcanvas", ({ target }) => {
if (target.matches(sidebarSelector)) {
updateToggles();
document.querySelector(toggleSelector)?.focus();
}
});
mount(toggleSelector, updateToggles);
// Turbo and Astro remove the old sidebar while a link click is still closing its sheet. The
// sheet finishes its transition first; disposing it earlier breaks Bootstrap's callback.
const disposeSheet = (sidebar) => {
const offcanvas = Offcanvas.getInstance(sidebar);
if (sidebar.matches(".showing, .hiding")) {
const dispose = () => offcanvas.dispose();
sidebar.addEventListener("shown.bs.offcanvas", dispose, { once: true });
sidebar.addEventListener("hidden.bs.offcanvas", dispose, { once: true });
} else {
offcanvas?.dispose();
}
};
mount(sidebarSelector, (sidebar) => {
updateToggles();
return () => disposeSheet(sidebar);
});
mount(".js-simplebar", (element) => {
const simplebar = new SimpleBar(element);
const recalculate = () => simplebar.recalculate();
// Recalculate when a sidebar dropdown opens or closes
element.addEventListener("shown.bs.collapse", recalculate);
element.addEventListener("hidden.bs.collapse", recalculate);
return () => simplebar.unMount();
});