2022-03-14 13:36:32 +00:00
|
|
|
import { defineComponent, h, resolveComponent, PropType, computed, DefineComponent } from 'vue'
|
|
|
|
import { RouteLocationRaw, Router } from 'vue-router'
|
2022-03-16 09:44:05 +00:00
|
|
|
import { hasProtocol } from 'ufo'
|
2022-03-14 13:36:32 +00:00
|
|
|
|
2022-07-07 17:28:23 +00:00
|
|
|
import { navigateTo, useRouter } from '#app'
|
2022-03-14 13:36:32 +00:00
|
|
|
|
|
|
|
const firstNonUndefined = <T>(...args: T[]): T => args.find(arg => arg !== undefined)
|
|
|
|
|
|
|
|
const DEFAULT_EXTERNAL_REL_ATTRIBUTE = 'noopener noreferrer'
|
|
|
|
|
|
|
|
export type NuxtLinkOptions = {
|
2022-07-07 17:28:23 +00:00
|
|
|
componentName?: string
|
|
|
|
externalRelAttribute?: string | null
|
|
|
|
activeClass?: string
|
|
|
|
exactActiveClass?: string
|
2022-03-14 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type NuxtLinkProps = {
|
|
|
|
// Routing
|
2022-07-07 17:28:23 +00:00
|
|
|
to?: string | RouteLocationRaw
|
|
|
|
href?: string | RouteLocationRaw
|
|
|
|
external?: boolean
|
|
|
|
replace?: boolean
|
|
|
|
custom?: boolean
|
2022-03-14 13:36:32 +00:00
|
|
|
|
|
|
|
// Attributes
|
2022-07-07 17:28:23 +00:00
|
|
|
target?: string
|
|
|
|
rel?: string
|
|
|
|
noRel?: boolean
|
2022-03-14 13:36:32 +00:00
|
|
|
|
|
|
|
// Styling
|
2022-07-07 17:28:23 +00:00
|
|
|
activeClass?: string
|
|
|
|
exactActiveClass?: string
|
2022-03-14 13:36:32 +00:00
|
|
|
|
|
|
|
// Vue Router's `<RouterLink>` additional props
|
2022-07-07 17:28:23 +00:00
|
|
|
ariaCurrentValue?: string
|
2022-03-14 13:36:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function defineNuxtLink (options: NuxtLinkOptions) {
|
|
|
|
const componentName = options.componentName || 'NuxtLink'
|
|
|
|
|
|
|
|
const checkPropConflicts = (props: NuxtLinkProps, main: string, sub: string): void => {
|
|
|
|
if (process.dev && props[main] !== undefined && props[sub] !== undefined) {
|
|
|
|
console.warn(`[${componentName}] \`${main}\` and \`${sub}\` cannot be used together. \`${sub}\` will be ignored.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defineComponent({
|
|
|
|
name: componentName,
|
|
|
|
props: {
|
|
|
|
// Routing
|
|
|
|
to: {
|
|
|
|
type: [String, Object] as PropType<string | RouteLocationRaw>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
href: {
|
|
|
|
type: [String, Object] as PropType<string | RouteLocationRaw>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
|
|
|
|
// Attributes
|
|
|
|
target: {
|
|
|
|
type: String as PropType<string>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
rel: {
|
|
|
|
type: String as PropType<string>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
noRel: {
|
|
|
|
type: Boolean as PropType<boolean>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
|
|
|
|
// Styling
|
|
|
|
activeClass: {
|
|
|
|
type: String as PropType<string>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
exactActiveClass: {
|
|
|
|
type: String as PropType<string>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
|
|
|
|
// Vue Router's `<RouterLink>` additional props
|
|
|
|
replace: {
|
|
|
|
type: Boolean as PropType<boolean>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
ariaCurrentValue: {
|
|
|
|
type: String as PropType<string>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
|
|
|
|
// Edge cases handling
|
|
|
|
external: {
|
|
|
|
type: Boolean as PropType<boolean>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
|
|
|
|
// Slot API
|
|
|
|
custom: {
|
|
|
|
type: Boolean as PropType<boolean>,
|
|
|
|
default: undefined,
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setup (props, { slots }) {
|
|
|
|
const router = useRouter() as Router | undefined
|
|
|
|
|
|
|
|
// Resolving `to` value from `to` and `href` props
|
|
|
|
const to = computed<string | RouteLocationRaw>(() => {
|
|
|
|
checkPropConflicts(props, 'to', 'href')
|
|
|
|
|
|
|
|
return props.to || props.href || '' // Defaults to empty string (won't render any `href` attribute)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Resolving link type
|
|
|
|
const isExternal = computed<boolean>(() => {
|
|
|
|
// External prop is explictly set
|
|
|
|
if (props.external) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// When `target` prop is set, link is external
|
|
|
|
if (props.target && props.target !== '_self') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// When `to` is a route object then it's an internal link
|
|
|
|
if (typeof to.value === 'object') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-16 10:10:32 +00:00
|
|
|
return to.value === '' || hasProtocol(to.value, true)
|
2022-03-14 13:36:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (!isExternal.value) {
|
|
|
|
// Internal link
|
|
|
|
return h(
|
|
|
|
resolveComponent('RouterLink'),
|
|
|
|
{
|
|
|
|
to: to.value,
|
|
|
|
activeClass: props.activeClass || options.activeClass,
|
|
|
|
exactActiveClass: props.exactActiveClass || options.exactActiveClass,
|
|
|
|
replace: props.replace,
|
2022-07-07 17:28:23 +00:00
|
|
|
ariaCurrentValue: props.ariaCurrentValue,
|
|
|
|
custom: props.custom
|
2022-03-14 13:36:32 +00:00
|
|
|
},
|
|
|
|
slots.default
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves `to` value if it's a route location object
|
|
|
|
// converts `'''` to `null` to prevent the attribute from being added as empty (`href=""`)
|
|
|
|
const href = typeof to.value === 'object' ? router.resolve(to.value)?.href ?? null : to.value || null
|
|
|
|
|
|
|
|
// Resolves `target` value
|
|
|
|
const target = props.target || null
|
|
|
|
|
|
|
|
// Resolves `rel`
|
|
|
|
checkPropConflicts(props, 'noRel', 'rel')
|
2022-03-16 10:10:32 +00:00
|
|
|
const rel = (props.noRel)
|
2022-03-14 13:36:32 +00:00
|
|
|
? null
|
|
|
|
// converts `""` to `null` to prevent the attribute from being added as empty (`rel=""`)
|
2022-03-16 10:10:32 +00:00
|
|
|
: firstNonUndefined<string | null>(props.rel, options.externalRelAttribute, href ? DEFAULT_EXTERNAL_REL_ATTRIBUTE : '') || null
|
2022-03-14 13:36:32 +00:00
|
|
|
|
2022-07-07 17:28:23 +00:00
|
|
|
const navigate = () => navigateTo(href, { replace: props.replace })
|
|
|
|
|
|
|
|
// https://router.vuejs.org/api/#custom
|
|
|
|
if (props.custom) {
|
|
|
|
if (!slots.default) { return null }
|
|
|
|
return slots.default({
|
|
|
|
href,
|
|
|
|
navigate,
|
|
|
|
route: router.resolve(href),
|
|
|
|
rel,
|
|
|
|
target,
|
|
|
|
isActive: false,
|
|
|
|
isExactActive: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-06 10:52:08 +00:00
|
|
|
return h('a', { href, rel, target }, slots.default?.())
|
2022-03-14 13:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}) as unknown as DefineComponent<NuxtLinkProps>
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineNuxtLink({ componentName: 'NuxtLink' })
|