summaryrefslogtreecommitdiff
path: root/docs/tabsync.js
blob: 935e42a236c5e4cf3ed5a06e392e30d44551b997 (plain)
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
document.addEventListener("DOMContentLoaded", function () {
  const tabSync = () => {
    const tabs = document.querySelectorAll(".tabbed-set > input, .tabbed-alternate input");

    for (const tab of tabs) {
      tab.addEventListener("click", () => {
        if (tab.dataset.syncing === "true") return;
        
        const currentLabel = document.querySelector(`label[for="${tab.id}"]`);
        if (!currentLabel) return;

        const pos = currentLabel.getBoundingClientRect().top;
        const labelText = currentLabel.textContent.trim();

        const allLabels = document.querySelectorAll(
          ".tabbed-set > label, .tabbed-alternate > .tabbed-labels > label"
        );

        for (const label of allLabels) {
          if (label.textContent.trim() === labelText) {
            const inputId = label.getAttribute("for");
            const input = document.getElementById(inputId);
            if (input && input !== tab) {
              input.dataset.syncing = "true";
              input.click();
              input.dataset.syncing = "false";
            }
          }
        }

        const delta = currentLabel.getBoundingClientRect().top - pos;
        window.scrollBy(0, delta);
      });
    }
  };

  tabSync();
});