From 054cf4f119a769615a1c8ab8534a03447f914a01 Mon Sep 17 00:00:00 2001 From: Ular Kimsanov Date: Wed, 4 Nov 2026 21:33:35 +0000 Subject: [PATCH] docs(nav): the docs menu button names the section you are in (#7133) --- docs/docs-menu-label.js | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 docs/docs-menu-label.js diff --git a/docs/docs-menu-label.js b/docs/docs-menu-label.js new file mode 100644 index 0000000000..40d8357a8e --- /dev/null +++ b/docs/docs-menu-label.js @@ -0,0 +1,61 @@ +// The top bar's "Docs" menu holds three sections with their own sidebars (docs.json, navigation.tabs[0].menu). Once a +// visitor is inside one, the bar still says "Docs" and two of the sidebars both start with "Start here", so nothing on +// the page says which section this is. This names the open section in the menu's own button ("Studio app ▾"), the way +// a picker shows what is picked. Mintlify runs every .js file in docs/ on every page +// (mintlify.com/docs/customize/custom-scripts). A section is known by its first page, which only its sidebar links to; +// keep these in step with docs.json. Anywhere else (Catalog, a page in no section) the button keeps "Docs". +(function () { + var MENU = "Docs"; + var SECTIONS = [ + { name: "Studio app", firstPage: "/studio-app" }, + { name: "Developers", firstPage: "/developers/overview" }, + { name: "Make a video", firstPage: "/introduction" }, + ]; + + var NAMES = [MENU].concat( + SECTIONS.map(function (section) { + return section.name; + }), + ); + + // The button's own text node, renamed in place: React keeps its node, so its next update of the bar still works. + function menuLabel() { + var buttons = document.querySelectorAll("#navbar button.nav-tabs-item[aria-haspopup=menu]"); + for (var i = 0; i < buttons.length; i++) { + var walker = document.createTreeWalker(buttons[i], NodeFilter.SHOW_TEXT); + for (var node = walker.nextNode(); node; node = walker.nextNode()) { + if (NAMES.indexOf(node.nodeValue.trim()) !== -1) return node; + } + } + return null; + } + + function openSection() { + var sidebar = document.getElementById("sidebar-content"); + if (!sidebar) return null; + for (var i = 0; i < SECTIONS.length; i++) { + if (sidebar.querySelector('a[href="' + SECTIONS[i].firstPage + '"]')) return SECTIONS[i].name; + } + return null; + } + + function update() { + var label = menuLabel(); + if (!label) return; + var text = openSection() || MENU; + if (label.nodeValue !== text) label.nodeValue = text; + } + + // Mintlify swaps pages without a reload and redraws the bar, so the label is set again after the page changes, at + // most once a frame. + var queued = false; + new MutationObserver(function () { + if (queued) return; + queued = true; + requestAnimationFrame(function () { + queued = false; + update(); + }); + }).observe(document.documentElement, { childList: true, subtree: true }); + update(); +})();