/
Link.vue
84 lines (72 loc) 路 1.87 KB
/
Link.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<script>
import { VIcon } from 'vuetify/lib/components/VIcon'
export default {
name: 'AppLink',
inheritAttrs: false,
props: {
href: {
type: String,
default: '',
},
},
computed: {
attrs () {
return this.isExternal
? { href: this.href, target: '_blank', rel: 'noopener' }
: { to: { path: this.href } }
},
icon () {
if (this.isSamePage) return '$mdiPound'
if (this.isExternal) return '$mdiOpenInNew'
if (this.href) return '$mdiPageNext'
return null
},
isExternal () {
return (
this.href.startsWith('http') ||
this.href.startsWith('mailto')
)
},
isSamePage () {
return (
!this.isExternal &&
this.href.startsWith('#')
)
},
},
methods: {
// vue-router scroll-behavior is skipped
// on same page hash changes. Manually
// run $vuetify goTo scroll function
onClick (e) {
if (!this.isSamePage) return
e.preventDefault()
this.$vuetify.goTo(this.href)
},
},
render (h) {
const children = []
if (!this.isSamePage) children.push(this.$slots.default)
if (this.icon) children.push(h(VIcon, {
class: `m${this.isSamePage ? 'r' : 'l'}-1`,
attrs: {
color: 'primary',
size: '.875rem',
},
}, [this.icon]))
if (this.isSamePage) children.push(this.$slots.default)
return h(this.isExternal ? 'a' : 'router-link', {
staticClass: 'app-link text-decoration-none primary--text font-weight-medium d-inline-block',
attrs: this.attrs,
[this.isExternal ? 'on' : 'nativeOn']: {
click: this.onClick,
},
}, children)
},
}
</script>
<style lang="sass">
.app-link
p
margin-bottom: 0
</style>