-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20-tabs.js
More file actions
60 lines (52 loc) · 2.06 KB
/
20-tabs.js
File metadata and controls
60 lines (52 loc) · 2.06 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
// ---- Tab navigation ------------------------------------
function initTabs() {
const tabBtns = document.querySelectorAll('.tab-btn[data-tab]');
const VALID_TABS = new Set(
Array.from(tabBtns).map((btn) => btn.dataset.tab)
);
// Build sectionId → tabName map so direct section deep-links work.
const sectionToTab = {};
document.querySelectorAll('[role="tabpanel"]').forEach((panel) => {
const tabName = panel.id.replace(/^tab-/, '');
panel.querySelectorAll('[id]').forEach((el) => {
sectionToTab[el.id] = tabName;
});
});
function switchTab(targetTab, updateHash = true) {
if (!VALID_TABS.has(targetTab)) return;
tabBtns.forEach((btn) => {
const isActive = btn.dataset.tab === targetTab;
btn.classList.toggle('tab-btn--active', isActive);
btn.setAttribute('aria-selected', String(isActive));
});
document.querySelectorAll('[role="tabpanel"]').forEach((panel) => {
panel.hidden = panel.id !== 'tab-' + targetTab;
});
if (updateHash) {
history.pushState(null, '', '#' + targetTab);
}
}
function applyHash(smooth) {
const hash = location.hash.slice(1);
if (!hash) return;
if (VALID_TABS.has(hash)) {
switchTab(hash, false);
} else if (sectionToTab[hash]) {
switchTab(sectionToTab[hash], false);
requestAnimationFrame(() => {
const el = document.getElementById(hash);
if (el) el.scrollIntoView({ behavior: smooth ? 'smooth' : 'auto', block: 'start' });
});
}
}
tabBtns.forEach((btn) => {
btn.addEventListener('click', () => switchTab(btn.dataset.tab));
});
// Inline "Dashboard" link inside the About tab
document.querySelectorAll('.about-inline-tab-link[data-switch-tab]').forEach((link) => {
link.addEventListener('click', () => switchTab(link.dataset.switchTab));
});
window.addEventListener('hashchange', () => applyHash(true));
// Apply initial hash on page load without smooth-scroll
applyHash(false);
}