Skip to content

Commit a4e6792

Browse files
committed
Support for example IDs in the URL
Support for links with IDs, e.g., https://wiki.preview.multitheftauto.com/isCursorShowing#example2
1 parent 85cc278 commit a4e6792

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

web/src/components/CodeExamplesSection.astro

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,35 @@ const { codeExamples } = Astro.props;
116116
const buttons = document.querySelectorAll('.tab-btn');
117117
const tabs = document.querySelectorAll('.tab-content');
118118

119+
function activateTab(tabIndex) {
120+
buttons.forEach((b) => b.classList.toggle('active', b.dataset.tab === tabIndex));
121+
tabs.forEach((t) => (t.style.display = t.dataset.tab === tabIndex ? 'block' : 'none'));
122+
}
123+
119124
buttons.forEach((btn) => {
120125
btn.addEventListener('click', () => {
121126
const tab = btn.dataset.tab;
127+
activateTab(tab);
122128

123-
buttons.forEach((b) => b.classList.toggle('active', b.dataset.tab === tab));
124-
tabs.forEach((t) => (t.style.display = t.dataset.tab === tab ? 'block' : 'none'));
129+
history.replaceState(null, '', `#example${parseInt(tab) + 1}`);
125130
});
126131
});
127132

133+
const hash = window.location.hash;
134+
const match = hash.match(/^#example(\d+)$/i);
135+
if (match) {
136+
const tabIndex = String(parseInt(match[1], 10) - 1);
137+
const targetBtn = document.querySelector(`.tab-btn[data-tab="${tabIndex}"]`);
138+
if (targetBtn) {
139+
activateTab(tabIndex);
140+
141+
const section = document.querySelector('.examples-section');
142+
if (section) {
143+
section.scrollIntoView({ behavior: 'smooth' });
144+
}
145+
}
146+
}
147+
128148
function checkOverflow() {
129149
const tabButtons = document.querySelector('.tab-buttons');
130150
if (!tabButtons) return;

0 commit comments

Comments
 (0)