Skip to content

Commit a20bcf3

Browse files
authored
feat: navlinks in sidebar (#80)
* feat: make navBarLinks component * feat: show nav contents in sidebar when mobile * feat: nav links style in mobile
1 parent 5dfddb8 commit a20bcf3

File tree

8 files changed

+112
-32
lines changed

8 files changed

+112
-32
lines changed
+3-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
import { computed } from 'vue'
21
import { withBase } from '../utils'
3-
import { useSiteDataByRoute } from 'vitepress'
4-
import NavBarLink from './NavBarLink.vue'
5-
import NavDropdownLink from './NavDropdownLink.vue'
2+
import NavBarLinks from './NavBarLinks.vue'
63

74
export default {
85
components: {
9-
NavBarLink,
10-
NavDropdownLink
6+
NavBarLinks
117
},
128

139
setup() {
1410
return {
15-
withBase,
16-
navData:
17-
process.env.NODE_ENV === 'production'
18-
? // navbar items do not change in production
19-
useSiteDataByRoute().value.themeConfig.nav
20-
: // use computed in dev for hot reload
21-
computed(() => useSiteDataByRoute().value.themeConfig.nav)
11+
withBase
2212
}
2313
}
2414
}

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

+2-15
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
/>
1313
<span>{{ $site.title }}</span>
1414
</a>
15-
<nav class="nav-links" v-if="navData">
16-
<template v-for="item of navData">
17-
<NavDropdownLink v-if='item.items' :item="item"/>
18-
<NavBarLink v-else :item="item"/>
19-
</template>
20-
</nav>
15+
<NavBarLinks class="hide-mobile"/>
2116
</template>
2217

2318
<script src="./NavBar"></script>
@@ -35,16 +30,8 @@
3530
vertical-align: bottom;
3631
}
3732
38-
.nav-links {
39-
display: flex;
40-
align-items: center;
41-
height: 35px;
42-
list-style-type: none;
43-
transform: translateY(1px);
44-
}
45-
4633
@media screen and (max-width: 719px) {
47-
.nav-links {
34+
.hide-mobile {
4835
display: none;
4936
}
5037
}

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
2727
.nav-link {
28-
display: inline-block;
28+
display: block;
2929
margin-bottom: -2px;
3030
border-bottom: 2px solid transparent;
3131
font-size: 0.9rem;
@@ -43,4 +43,23 @@
4343
.nav-link.external:hover {
4444
border-bottom-color: transparent;
4545
}
46+
47+
@media screen and (max-width: 719px) {
48+
.nav-item {
49+
margin-left: 0;
50+
padding: 0.35rem 1.5rem 0.35rem 1.25rem;
51+
}
52+
53+
.nav-link {
54+
line-height: 1.7;
55+
font-size: 1em;
56+
font-weight: 600;
57+
border-bottom: none;
58+
margin-bottom: 0;
59+
}
60+
.nav-link:hover,
61+
.nav-link.active {
62+
color: var(--accent-color);
63+
}
64+
}
4665
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { computed } from 'vue'
2+
import { useSiteDataByRoute } from 'vitepress'
3+
import NavBarLink from './NavBarLink.vue'
4+
import NavDropdownLink from './NavDropdownLink.vue'
5+
6+
export default {
7+
components: {
8+
NavBarLink,
9+
NavDropdownLink
10+
},
11+
12+
setup() {
13+
return {
14+
navData:
15+
process.env.NODE_ENV === 'production'
16+
? // navbar items do not change in production
17+
useSiteDataByRoute().value.themeConfig.nav
18+
: // use computed in dev for hot reload
19+
computed(() => useSiteDataByRoute().value.themeConfig.nav)
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<nav class="nav-links" v-if="navData">
3+
<template v-for="item of navData">
4+
<NavDropdownLink v-if='item.items' :item="item"/>
5+
<NavBarLink v-else :item="item"/>
6+
</template>
7+
</nav>
8+
</template>
9+
10+
<script src="./NavBarLinks"></script>
11+
12+
<style>
13+
.nav-links {
14+
display: flex;
15+
align-items: center;
16+
height: 35px;
17+
list-style-type: none;
18+
transform: translateY(1px);
19+
}
20+
21+
@media screen and (max-width: 719px) {
22+
.nav-links {
23+
display: block;
24+
height: auto;
25+
padding-bottom: 0.5rem;
26+
border-bottom: 1px solid var(--border-color);
27+
}
28+
}
29+
</style>

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<span class="arrow" :class="open ? 'down' : 'right'" />
1111
</button>
1212

13-
<ul v-show="open" class="nav-dropdown">
13+
<ul class="nav-dropdown">
1414
<li v-for="(subItem, index) in item.items" :key="subItem.link || index" class="dropdown-item">
1515
<h4 v-if="subItem.items">{{ subItem.text }}</h4>
1616
<ul v-if="subItem.items" class="dropdown-subitem-wrapper">
@@ -46,7 +46,7 @@
4646
.dropdown-wrapper {
4747
position: relative;
4848
cursor: pointer;
49-
display: inline-block;
49+
display: block;
5050
margin-left: 1.5rem;
5151
}
5252
.dropdown-wrapper .dropdown-title {
@@ -129,7 +129,7 @@
129129
}
130130
.dropdown-wrapper:hover .nav-dropdown,
131131
.dropdown-wrapper.open .nav-dropdown {
132-
display: block !important;
132+
display: block;
133133
}
134134
.dropdown-wrapper.open:blur {
135135
display: none;
@@ -158,4 +158,25 @@
158158
white-space: nowrap;
159159
margin: 0;
160160
}
161+
162+
@media screen and (max-width: 719px) {
163+
.dropdown-wrapper {
164+
height: auto;
165+
margin-left: 1.25rem;
166+
}
167+
168+
.dropdown-wrapper .nav-dropdown {
169+
position: relative;
170+
top: none;
171+
right: none;
172+
border: none;
173+
background-color: transparent;
174+
}
175+
.dropdown-wrapper:hover .nav-dropdown {
176+
display: none;
177+
}
178+
.dropdown-wrapper.open .nav-dropdown {
179+
display: block;
180+
}
181+
}
161182
</style>

src/client/theme-default/components/SideBar.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Header } from '../../../../types/shared'
99
import { isActive, joinUrl, getPathDirName } from '../utils'
1010
import { DefaultTheme } from '../config'
1111
import { useActiveSidebarLinks } from '../composables/activeSidebarLink'
12+
import NavBarLinks from './NavBarLinks.vue'
1213

1314
const SideBarItem: FunctionalComponent<{
1415
item: ResolvedSidebarItem
@@ -33,6 +34,7 @@ const SideBarItem: FunctionalComponent<{
3334

3435
export default {
3536
components: {
37+
NavBarLinks,
3638
SideBarItem
3739
},
3840

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

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<template>
2+
<NavBarLinks class="show-mobile"/>
23
<ul class="sidebar">
34
<SideBarItem v-for="item of items" :item="item" />
45
</ul>
@@ -7,6 +8,15 @@
78
<script src="./SideBar"></script>
89

910
<style>
11+
.show-mobile {
12+
display: none;
13+
}
14+
@media screen and (max-width: 719px) {
15+
.show-mobile {
16+
display: block;
17+
}
18+
}
19+
1020
.sidebar,
1121
.sidebar-items {
1222
list-style-type: none;

0 commit comments

Comments
 (0)