phongdt:header footer

This commit is contained in:
Duong Truong Phong
2024-05-30 22:57:45 +07:00
parent dd09434deb
commit 3b435e22ea
18 changed files with 787 additions and 467 deletions
+53
View File
@@ -0,0 +1,53 @@
import type { ObjectDirective } from 'vue'
type InterpolationElement = HTMLElement & {
$componentUpdated?: () => void
$destroy?: () => void
}
export const vInterpolate: ObjectDirective<InterpolationElement> = {
mounted(el) {
const links = Array.from(el.getElementsByTagName('a')).filter((linkEl) => {
const href = linkEl.getAttribute('href')
if (!href) {
return false
}
return isInternalLink(href)
})
addListeners(links)
// cleanup
el.$componentUpdated = () => {
removeListeners(links)
nextTick(() => addListeners(links))
}
el.$destroy = () => removeListeners(links)
},
updated: (el) => el.$componentUpdated?.(),
beforeUnmount: (el) => el.$destroy?.()
}
function navigate(event: Event) {
const target = event.target as HTMLElement
const href = target.getAttribute('href')
event.preventDefault()
return navigateTo(href)
}
function addListeners(links: HTMLAnchorElement[]) {
links.forEach((link) => {
link.addEventListener('click', navigate, false)
})
}
function removeListeners(links: HTMLAnchorElement[]) {
links.forEach((link) => {
link.removeEventListener('click', navigate, false)
})
}
function isInternalLink(href?: string) {
return href?.startsWith('/')
}