forked from zbdpay/zbd-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget-tab.js
More file actions
63 lines (53 loc) · 1.75 KB
/
Copy pathwidget-tab.js
File metadata and controls
63 lines (53 loc) · 1.75 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
// Show the "Widgets" tab only when on /widget routes.
(function () {
function isWidgetRoute() {
var pathname = window.location.pathname.replace(/\/+$/, "");
return pathname === "/widget" || pathname.startsWith("/widget/");
}
function updateRouteClass(onWidget) {
document.documentElement.classList.toggle("zbd-widget-route", onWidget);
}
function toggle() {
var onWidget = isWidgetRoute();
updateRouteClass(onWidget);
// Find the Widgets tab link in the top nav by its text content.
var links = document.querySelectorAll("nav a, header a, [role='tablist'] a, [class*='tab'] a");
for (var i = 0; i < links.length; i++) {
var text = (links[i].textContent || "").trim();
if (text === "Widgets") {
if (onWidget) {
links[i].style.removeProperty("display");
} else {
links[i].style.display = "none";
}
}
}
}
updateRouteClass(isWidgetRoute());
// Run on load and periodically (Mintlify hydrates async).
toggle();
setTimeout(toggle, 500);
setTimeout(toggle, 1500);
setTimeout(toggle, 3000);
// Re-check on client-side navigation.
["pushState", "replaceState"].forEach(function (method) {
var original = history[method];
history[method] = function () {
original.apply(this, arguments);
setTimeout(toggle, 100);
};
});
window.addEventListener("popstate", function () {
setTimeout(toggle, 100);
});
// MutationObserver to catch Mintlify's dynamic rendering.
function observeBody() {
if (!document.body) {
setTimeout(observeBody, 0);
return;
}
var observer = new MutationObserver(function () { toggle(); });
observer.observe(document.body, { childList: true, subtree: true });
}
observeBody();
})();