From c05143cf2c754e333f41b819f28aa1f5d8b1d6c8 Mon Sep 17 00:00:00 2001 From: Lightling Date: Thu, 30 May 2024 17:27:50 -0400 Subject: [PATCH] guard against same-route links --- libs/embeds/src/link.ts | 10 ++++- .../src/components/shared/header-link.vue | 5 ++- .../components/shared/router-link-guard.vue | 44 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 projects/frontend/src/components/shared/router-link-guard.vue diff --git a/libs/embeds/src/link.ts b/libs/embeds/src/link.ts index 5b28aaf..60bf404 100644 --- a/libs/embeds/src/link.ts +++ b/libs/embeds/src/link.ts @@ -31,7 +31,9 @@ export class LinkEmbed extends HTMLElement { if (_link.dataset?.href?.startsWith('/') && !!window.router) { e.preventDefault() const route = window.router.resolve({ path: _link.dataset.href }) - window.router.push(route) + if (route.path !== window.router.currentRoute.value.path) { + window.router.push(route) + } } }) this._observers.href = { @@ -40,6 +42,12 @@ export class LinkEmbed extends HTMLElement { const { element } = this._observers.href element!.setAttribute('href', newVal) element!.dataset.href = newVal + if (!!window.router) { + const route = window.router.resolve({ path: newVal }) + if (route.path === window.router.currentRoute.value.path) { + element!.classList.add('current-route') + } + } }, } this._observers.target = { diff --git a/projects/frontend/src/components/shared/header-link.vue b/projects/frontend/src/components/shared/header-link.vue index b5f57d7..f9e64df 100644 --- a/projects/frontend/src/components/shared/header-link.vue +++ b/projects/frontend/src/components/shared/header-link.vue @@ -1,8 +1,11 @@ + +