Skip to content

Commit a9f7574

Browse files
committed
refactor: simplify navlink logic
1 parent ffaca73 commit a9f7574

File tree

6 files changed

+34
-93
lines changed

6 files changed

+34
-93
lines changed

src/client/theme-default/components/HomeHero.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const action = computed(() => ({
116116
}
117117
}
118118
119-
.action ::v-deep(.item) {
119+
.action :deep(.item) {
120120
display: inline-block;
121121
border-radius: 4px;
122122
padding: 0 20px;
@@ -128,14 +128,14 @@ const action = computed(() => ({
128128
transition: background-color .1s ease;
129129
}
130130
131-
.action ::v-deep(.item:hover) {
131+
.action :deep(.item:hover) {
132132
text-decoration: none;
133133
color: #ffffff;
134134
background-color: var(--c-brand-light);
135135
}
136136
137137
@media (min-width: 420px) {
138-
.action ::v-deep(.item) {
138+
.action :deep(.item) {
139139
padding: 0 24px;
140140
line-height: 56px;
141141
font-size: 1.2rem;

src/client/theme-default/components/NavDropdownLinkItem.vue

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
<template>
22
<div class="nav-dropdown-link-item">
3-
<a
4-
class="item"
5-
:class="classes"
6-
:href="href"
7-
:target="target"
8-
:rel="rel"
9-
:aria-label="ariaLabel"
10-
>
3+
<a class="item" v-bind="linkProps">
114
<span class="arrow" />
12-
<span class="text">{{ text }}</span>
5+
<span class="text">{{ item.text }}</span>
136
<span class="icon"><OutboundLink v-if="isExternal" /></span>
147
</a>
158
</div>
@@ -22,27 +15,18 @@ import { useNavLink } from '../composables/navLink'
2215
import OutboundLink from './icons/OutboundLink.vue'
2316
2417
const { item } = defineProps<{
25-
item: DefaultTheme.NavItem
18+
item: DefaultTheme.NavItemWithLink
2619
}>()
2720
28-
const {
29-
classes,
30-
isActive,
31-
isExternal,
32-
href,
33-
target,
34-
rel,
35-
ariaLabel,
36-
text
37-
} = useNavLink(item)
21+
const { props: linkProps, isExternal } = useNavLink(item)
3822
</script>
3923

4024
<style scoped>
4125
.item {
4226
display: block;
4327
padding: 0 1.5rem 0 2.5rem;
4428
line-height: 32px;
45-
font-size: .9rem;
29+
font-size: 0.9rem;
4630
font-weight: 500;
4731
color: var(--c-text);
4832
white-space: nowrap;
@@ -52,7 +36,7 @@ const {
5236
.item {
5337
padding: 0 24px 0 12px;
5438
line-height: 32px;
55-
font-size: .85rem;
39+
font-size: 0.85rem;
5640
font-weight: 500;
5741
color: var(--c-text);
5842
white-space: nowrap;

src/client/theme-default/components/NavLink.vue

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
11
<template>
22
<div class="nav-link">
3-
<a
4-
class="item"
5-
:class="classes"
6-
:href="href"
7-
:target="target"
8-
:rel="rel"
9-
:aria-label="ariaLabel"
10-
>
11-
{{ text }} <OutboundLink v-if="isExternal" />
3+
<a class="item" v-bind="linkProps">
4+
{{ item.text }} <OutboundLink v-if="isExternal" />
125
</a>
136
</div>
147
</template>
158

169
<script setup lang="ts">
1710
import { defineProps } from 'vue'
11+
import type { DefaultTheme } from '../config'
1812
import { useNavLink } from '../composables/navLink'
1913
import OutboundLink from './icons/OutboundLink.vue'
2014
2115
const { item } = defineProps<{
2216
item: DefaultTheme.NavItemWithLink
2317
}>()
2418
25-
const {
26-
classes,
27-
isActive,
28-
isExternal,
29-
href,
30-
target,
31-
rel,
32-
ariaLabel,
33-
text
34-
} = useNavLink(item)
19+
const { props: linkProps, isExternal } = useNavLink(item)
3520
</script>
3621

3722
<style scoped>
38-
.nav-link {
39-
40-
}
41-
4223
.item {
4324
display: block;
4425
padding: 0 1.5rem;
@@ -65,7 +46,7 @@ const {
6546
border-bottom: 2px solid transparent;
6647
padding: 0;
6748
line-height: 24px;
68-
font-size: .9rem;
49+
font-size: 0.9rem;
6950
font-weight: 500;
7051
}
7152

src/client/theme-default/composables/nav.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export function useLocaleLinks() {
5353
? locales[currentLangKey].selectText
5454
: 'Languages'
5555

56-
return { text: selectText, items: candidates }
56+
return {
57+
text: selectText,
58+
items: candidates
59+
} as DefaultTheme.NavItemWithChildren
5760
})
5861
}

src/client/theme-default/composables/navLink.ts

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,25 @@ export function useNavLink(item: DefaultTheme.NavItemWithLink) {
88
const route = useRoute()
99
const { withBase } = useUrl()
1010

11-
const classes = computed(() => ({
12-
active: isActive.value,
13-
external: isExternal.value
14-
}))
15-
16-
const isActive = computed(() => {
17-
return normalizePath(withBase(item.link)) === normalizePath(route.path)
18-
})
19-
20-
const isExternal = computed(() => {
21-
return isExternalCheck(item.link)
22-
})
23-
24-
const href = computed(() => {
25-
return isExternal.value ? item.link : withBase(item.link)
26-
})
27-
28-
const target = computed(() => {
29-
if (item.target) {
30-
return item.target
11+
const isExternal = isExternalCheck(item.link)
12+
13+
const props = computed(() => {
14+
return {
15+
class: {
16+
active:
17+
normalizePath(withBase(item.link)) === normalizePath(route.path),
18+
isExternal
19+
},
20+
href: isExternal ? item.link : withBase(item.link),
21+
target: item.target || isExternal ? `_blank` : null,
22+
rel: item.rel || isExternal ? `noopener noreferrer` : null,
23+
'aria-label': item.ariaLabel
3124
}
32-
33-
return isExternal.value ? '_blank' : ''
3425
})
3526

36-
const rel = computed(() => {
37-
if (item.rel) {
38-
return item.rel
39-
}
40-
41-
return isExternal.value ? 'noopener noreferrer' : ''
42-
})
43-
44-
const ariaLabel = computed(() => item.ariaLabel)
45-
46-
const text = computed(() => item.text)
47-
4827
return {
49-
classes,
50-
isActive,
51-
isExternal,
52-
href,
53-
target,
54-
rel,
55-
ariaLabel,
56-
text
28+
props,
29+
isExternal
5730
}
5831
}
5932

src/client/theme-default/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export namespace DefaultTheme {
8787
}
8888

8989
export interface NavItemWithChildren extends NavItemBase {
90-
items: NavItem[]
90+
items: NavItemWithLink[]
9191
}
9292

9393
// sidebar -------------------------------------------------------------------

0 commit comments

Comments
 (0)