Skip to content

Commit c24c54f

Browse files
committed
Fix code example url hash targetting
1 parent b5ff0f5 commit c24c54f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

web/src/components/CodeExamplesSection.astro

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ if (codeExamples.length === 0 && !examplesRequired) {
9696
});
9797
});
9898

99-
const hash = window.location.hash;
100-
const match = hash.match(/^#example(\d+)$/i);
101-
if (match) {
99+
function activateExampleFromHash() {
100+
const hash = window.location.hash;
101+
const match = hash.match(/^#example(\d+)$/i);
102+
if (!match) return;
103+
102104
const tabIndex = String(parseInt(match[1], 10) - 1);
103105
const targetBtn = document.querySelector(`.tab-btn[data-tab="${tabIndex}"]`);
104-
if (targetBtn) {
105-
activateTab(tabIndex);
106+
const targetContent = document.querySelector(`.tab-content[data-tab="${tabIndex}"]`);
106107

107-
const section = document.querySelector('.examples-section');
108-
if (section) {
109-
section.scrollIntoView({ behavior: 'smooth' });
110-
}
108+
if (targetBtn && targetContent) {
109+
activateTab(tabIndex);
110+
targetBtn.scrollIntoView({ behavior: 'smooth' });
111111
}
112112
}
113113

@@ -122,6 +122,9 @@ if (codeExamples.length === 0 && !examplesRequired) {
122122
}
123123
}
124124

125+
activateExampleFromHash();
126+
window.addEventListener('hashchange', activateExampleFromHash);
127+
125128
checkOverflow();
126129
window.addEventListener('resize', checkOverflow);
127130
});

0 commit comments

Comments
 (0)