@vue-layout/link
Version:
A package containing a link component.
139 lines (135 loc) • 4.29 kB
JavaScript
import { defineComponent, resolveDynamicComponent, computed, h } from 'vue';
const VCLink = defineComponent({
props: {
active: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
href: {
type: String,
default: undefined
},
prefetch: {
type: Boolean,
default: true
},
target: {
type: String,
default: '_self'
},
to: {
type: String,
default: undefined
}
},
emits: [
'click',
'clicked'
],
setup (props, { emit, slots }) {
const routerLink = resolveDynamicComponent('RouterLink');
const nuxtLink = resolveDynamicComponent('NuxtLink');
const computedTag = computed(()=>{
const hasRouter = typeof routerLink !== 'string';
if (!hasRouter || props.disabled || !props.to) {
return 'a';
}
if (typeof nuxtLink !== 'string') {
return 'nuxt-link';
}
return 'router-link';
});
const isRouterLink = computed(()=>computedTag.value !== 'a');
const computedHref = computed(()=>{
if (props.href) {
return props.href;
}
if (isRouterLink.value) {
return null;
}
return '#';
});
const computedProps = computed(()=>{
if (!isRouterLink.value) {
return {};
}
return {
...props.to ? {
to: props.to
} : {},
...typeof props.prefetch !== 'undefined' ? {
prefetch: props.prefetch
} : {}
};
});
const computedAttrs = computed(()=>({
...props.href ? {
href: props.href
} : {},
...isRouterLink.value ? {} : {
target: props.target
}
}));
const buildVNodeProps = ()=>{
const onClick = (event)=>{
const isEvent = typeof event.preventDefault !== 'undefined' && typeof event.stopPropagation !== 'undefined' && typeof event.stopImmediatePropagation !== 'undefined';
if (isEvent && props.disabled) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
} else {
if (isRouterLink.value) {
emit('click', event);
}
emit('clicked', event);
}
if (isEvent && !isRouterLink.value && computedHref.value === '#') {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
}
};
const vNodeProps = {
class: {
active: props.active,
disabled: props.disabled
},
...computedAttrs.value,
...computedProps.value,
onClick
};
return vNodeProps;
};
let component;
switch(computedTag.value){
case 'router-link':
component = routerLink;
break;
case 'nuxt-link':
component = nuxtLink;
break;
default:
component = 'a';
}
if (typeof component === 'string') {
return ()=>h(component, buildVNodeProps(), [
typeof slots.default === 'function' ? slots.default() : []
]);
}
return ()=>h(component, buildVNodeProps(), {
default: ()=>typeof slots.default === 'function' ? slots.default() : []
});
}
});
function install(instance, options) {
instance.component('VCLink', VCLink);
}
var index = {
install
};
export { VCLink, index as default, install };
//# sourceMappingURL=index.mjs.map