support internal links with vue-router outside vue context

This commit is contained in:
lightling 2024-05-16 18:09:12 -04:00
parent 10dc4372c0
commit af5ec7a9dc
5 changed files with 30 additions and 3 deletions

View file

@ -1,7 +1,11 @@
import type { RoutedWindow } from '@goldenwere/static-web-templates-types'
import type { AttributeObserver } from './observer'
export type LinkEmbedAttributes = typeof LinkEmbed.observedAttributes[number]
declare const window: RoutedWindow
/**
* A link embed is a link with a title and an optional icon + subtitle
*/
@ -23,11 +27,19 @@ export class LinkEmbed extends HTMLElement {
const _link = ownerDocument.createElement('a')
_link.setAttribute('rel', 'noreferrer noopener')
_link.addEventListener('click', (e) => {
if (_link.dataset?.href?.startsWith('/') && !!window.router) {
e.preventDefault()
const route = window.router.resolve({ path: _link.dataset.href })
window.router.push(route)
}
})
this._observers.href = {
element: _link,
observer: (oldVal, newVal) => {
observer: (oldVal, newVal: string) => {
const { element } = this._observers.href
element!.setAttribute('href', newVal)
element!.dataset.href = newVal
},
}
this._observers.target = {