Skip to content

Commit

Permalink
fix(nav-link): fix is non http link condition
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Dec 22, 2022
1 parent ee16780 commit 765a61a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/nav-link.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const hasAnchor = computed(() => {
const isNonHttpLink = computed(() => {
const containsProtocol = props.href.includes('://')
if (containsProtocol) {
return props.href.startsWith('http')
return props.href.startsWith('http') === false
} else {
return false
}
Expand All @@ -128,7 +128,16 @@ const target = computed(() => {
})
function click(event) {
if (isNonHttpLink.value || isExternalLink.value || hasAnchor.value) {
if (hasAnchor.value) {
const anchor = props.href.substring(props.href.indexOf('#'))
const targetElement = window.document.querySelector(anchor)
if (!targetElement) {
console.error(`Could not find an element with selector ${anchor}. Does it exist?`)
} else {
targetElement.scrollTo({ behavior: 'smooth' })
}
} else if (isNonHttpLink.value || isExternalLink.value) {
// let the browser do the work
} else {
event.preventDefault()
Expand Down

0 comments on commit 765a61a

Please sign in to comment.