Skip to content

Commit 20b8795

Browse files
committed
add proper hashnav handler
1 parent e8ae474 commit 20b8795

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/useActive.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,31 @@ export function useActive(
182182
setTargets();
183183
}
184184

185+
function getHashId() {
186+
return targets.elements.find(({ id }) => id === location.hash.slice(1))?.id;
187+
}
188+
189+
function onHashChange(event: HashChangeEvent) {
190+
if (matchMedia.value) {
191+
if (!event.newURL.includes('#') && activeId.value) {
192+
const prevY = getSentinel();
193+
194+
requestAnimationFrame(() => {
195+
// If scrolled on its own reset activeId, if not keep current
196+
const newY = getSentinel();
197+
if (prevY !== newY) {
198+
return (activeId.value = jumpToFirst ? ids.value[0] : '');
199+
}
200+
});
201+
}
202+
203+
const hashId = getHashId();
204+
if (hashId) {
205+
activeId.value = hashId;
206+
}
207+
}
208+
}
209+
185210
// Lifecycle
186211

187212
onMounted(async () => {
@@ -195,9 +220,10 @@ export function useActive(
195220
setTargets();
196221

197222
window.addEventListener('resize', onResize, { passive: true });
223+
window.addEventListener('hashchange', onHashChange);
198224

199225
if (matchMedia.value) {
200-
const hashId = targets.elements.find(({ id }) => id === location.hash.slice(1))?.id;
226+
const hashId = getHashId();
201227
if (hashId) {
202228
return (activeId.value = hashId);
203229
}
@@ -210,6 +236,7 @@ export function useActive(
210236

211237
onBeforeUnmount(() => {
212238
window.removeEventListener('resize', onResize);
239+
window.removeEventListener('hashchange', onHashChange);
213240
});
214241

215242
// Watchers
@@ -219,7 +246,7 @@ export function useActive(
219246
});
220247

221248
watch(activeId, (newId) => {
222-
if (replaceHash) {
249+
if (!replaceHash) {
223250
const start = jumpToFirst ? 0 : -1;
224251
const newHash = `${location.pathname}${activeIndex.value > start ? `#${newId}` : ''}`;
225252
history.replaceState(history.state, '', newHash);

src/useScroll.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ export function useScroll({
8484
isClick.value = false;
8585
}
8686

87-
function onPointerDown() {
88-
if (isFirefox()) {
87+
function onPointerDown(event: PointerEvent) {
88+
const isAnchor = (event.target as HTMLElement).tagName === 'A';
89+
90+
if (isFirefox() && !isAnchor) {
8991
reScroll();
9092
// ...and force set if canceling scroll
9193
setActive({ prevY: clickY.value, isCancel: true });
@@ -138,7 +140,7 @@ export function useScroll({
138140
rootEl.addEventListener('touchmove', reScroll, ONCE);
139141
rootEl.addEventListener('scroll', setIdle as unknown as EventListener, ONCE);
140142
rootEl.addEventListener('keydown', onSpaceBar as EventListener, ONCE);
141-
rootEl.addEventListener('pointerdown', onPointerDown, ONCE);
143+
rootEl.addEventListener('pointerdown', onPointerDown as EventListener, ONCE);
142144
}
143145

144146
onCleanup(() => {
@@ -147,7 +149,7 @@ export function useScroll({
147149
rootEl.removeEventListener('touchmove', reScroll);
148150
rootEl.removeEventListener('scroll', setIdle as unknown as EventListener);
149151
rootEl.removeEventListener('keydown', onSpaceBar as EventListener);
150-
rootEl.removeEventListener('pointerdown', onPointerDown);
152+
rootEl.removeEventListener('pointerdown', onPointerDown as EventListener);
151153
}
152154
});
153155
},

0 commit comments

Comments
 (0)