File size: 654 Bytes
d867717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Code-sample tab switcher. Progressive enhancement: with JS disabled every
// panel except the first stays hidden via the `hidden` attribute in the markup,
// so the page still shows one working example.
document.querySelectorAll(".code-tabs").forEach((tabs) => {
	const buttons = [...tabs.querySelectorAll("button")];
	const panels = buttons.map((b) => document.getElementById(b.dataset.panel));

	const select = (i) => {
		buttons.forEach((b, j) => b.setAttribute("aria-selected", String(i === j)));
		panels.forEach((p, j) => p && (p.hidden = i !== j));
	};

	buttons.forEach((b, i) => b.addEventListener("click", () => select(i)));
	select(0);
});