Skip to content

Commit

Permalink
Merge pull request #1505 from PrefectHQ/fix-p-link-for-absolute-urls
Browse files Browse the repository at this point in the history
Fix absolute urls when using `PLink`
  • Loading branch information
pleek91 authored Nov 8, 2024
2 parents 0c1034e + 1128e3f commit 0a166bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
10 changes: 10 additions & 0 deletions demo/sections/components/Links.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:demos="[
{ title: 'Local' },
{ title: 'External' },
{ title: 'Absolute' },
]"
>
<template #local>
Expand All @@ -17,9 +18,18 @@
Link to prefect.io
</p-link>
</template>

<template #absolute>
<p-link :to="absolute">
{{ absolute }}
</p-link>
</template>
</ComponentPage>
</template>

<script lang="ts" setup>
import { computed } from 'vue'
import ComponentPage from '@/demo/components/ComponentPage.vue'
const absolute = computed(() => `${window.location.protocol}//${window.location.host}/components/links`)
</script>
7 changes: 4 additions & 3 deletions src/components/Link/PLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@
import { computed } from 'vue'
import { RouteLocationRaw } from 'vue-router'
import PIcon from '@/components/Icon/PIcon.vue'
import { isRouteExternal } from '@/utilities/router'
import { isRouteExternal, isRouteRelative } from '@/utilities/router'
const props = defineProps<{
to?: RouteLocationRaw,
}>()
const isExternal = computed(() => !!props.to && isRouteExternal(props.to))
const component = computed(() => !props.to || isExternal.value ? 'a' : 'router-link')
const isAbsolute = computed(() => !!props.to && !isRouteRelative(props.to))
const component = computed(() => !props.to || isExternal.value || isAbsolute.value ? 'a' : 'router-link')
const componentProps = computed(() => {
if (!props.to) {
return {
target: '_blank',
}
}
if (isExternal.value) {
if (isExternal.value || isAbsolute.value) {
return {
href: props.to,
target: '_blank',
Expand Down
6 changes: 5 additions & 1 deletion src/utilities/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export function isRouteExternal(route: RouteLocationRaw): boolean {
}
}

export function isRouteRelative(route: string): boolean {
export function isRouteRelative(route: RouteLocationRaw): boolean {
if (typeof route !== 'string') {
return false
}

return route.startsWith('/')
}

Expand Down

0 comments on commit 0a166bb

Please sign in to comment.