CustomNavBar.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <script lang="ts" setup>
  2. import { useWindowScroll } from "@vueuse/core";
  3. import { ref, watchPostEffect, computed } from "vue";
  4. import { useData } from "vitepress";
  5. import { useSidebar } from "vitepress/theme";
  6. import VPNavBarAppearance from "vitepress/dist/client/theme-default/components/VPNavBarAppearance.vue";
  7. import VPNavBarExtra from "vitepress/dist/client/theme-default/components/VPNavBarExtra.vue";
  8. import VPNavBarHamburger from "vitepress/dist/client/theme-default/components/VPNavBarHamburger.vue";
  9. import VPNavBarMenu from "vitepress/dist/client/theme-default/components/VPNavBarMenu.vue";
  10. import VPNavBarSearch from "vitepress/dist/client/theme-default/components/VPNavBarSearch.vue";
  11. import VPNavBarSocialLinks from "vitepress/dist/client/theme-default/components/VPNavBarSocialLinks.vue";
  12. import VPNavBarTitle from "vitepress/dist/client/theme-default/components/VPNavBarTitle.vue";
  13. import VPNavBarTranslations from "vitepress/dist/client/theme-default/components/VPNavBarTranslations.vue";
  14. defineProps<{
  15. isScreenOpen: boolean;
  16. }>();
  17. defineEmits<{
  18. (e: "toggle-screen"): void;
  19. }>();
  20. const { y } = useWindowScroll();
  21. const { hasSidebar } = useSidebar();
  22. const { frontmatter } = useData();
  23. const classes = ref<Record<string, boolean>>({});
  24. watchPostEffect(() => {
  25. classes.value = {
  26. "has-sidebar": hasSidebar.value,
  27. home: frontmatter.value.layout === "home",
  28. top: y.value === 0,
  29. };
  30. });
  31. const hasSearch = computed(() => {
  32. return frontmatter.value.layout !== 'home'
  33. })
  34. </script>
  35. <template>
  36. <div class="VPNavBar" :class="classes">
  37. <div class="wrapper">
  38. <div class="container">
  39. <div class="title">
  40. <VPNavBarTitle>
  41. <template #nav-bar-title-before
  42. ><slot name="nav-bar-title-before"
  43. /></template>
  44. <template #nav-bar-title-after><slot name="nav-bar-title-after" /></template>
  45. </VPNavBarTitle>
  46. </div>
  47. <div class="content">
  48. <div class="content-body">
  49. <slot name="nav-bar-content-before" />
  50. <VPNavBarSearch class="search" :class="{ 'hide-search': !hasSearch }" />
  51. <VPNavBarMenu class="menu" />
  52. <VPNavBarTranslations class="translations" />
  53. <VPNavBarAppearance class="appearance" />
  54. <VPNavBarSocialLinks class="social-links" />
  55. <VPNavBarExtra class="extra" />
  56. <slot name="nav-bar-content-after" />
  57. <VPNavBarHamburger
  58. class="hamburger"
  59. :active="isScreenOpen"
  60. @click="$emit('toggle-screen')"
  61. />
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. <div class="divider">
  67. <div class="divider-line" />
  68. </div>
  69. </div>
  70. </template>
  71. <style scoped lang="scss">
  72. .search {
  73. justify-content: end;
  74. &.hide-search > :deep(div) {
  75. display: none;
  76. }
  77. }
  78. </style>
  79. <style scoped>
  80. .VPNavBar {
  81. position: relative;
  82. height: var(--vp-nav-height);
  83. pointer-events: none;
  84. white-space: nowrap;
  85. transition: background-color 0.5s;
  86. }
  87. .VPNavBar:not(.home) {
  88. background-color: var(--vp-nav-bg-color);
  89. }
  90. @media (min-width: 960px) {
  91. .VPNavBar:not(.home) {
  92. background-color: transparent;
  93. }
  94. .VPNavBar:not(.has-sidebar):not(.home.top) {
  95. background-color: var(--vp-nav-bg-color);
  96. }
  97. }
  98. .wrapper {
  99. padding: 0 8px 0 24px;
  100. }
  101. @media (min-width: 768px) {
  102. .wrapper {
  103. padding: 0 32px;
  104. }
  105. }
  106. @media (min-width: 960px) {
  107. .VPNavBar.has-sidebar .wrapper {
  108. padding: 0;
  109. }
  110. }
  111. .container {
  112. display: flex;
  113. justify-content: space-between;
  114. margin: 0 auto;
  115. max-width: calc(var(--vp-layout-max-width) - 64px);
  116. height: var(--vp-nav-height);
  117. pointer-events: none;
  118. }
  119. .container > .title,
  120. .container > .content {
  121. pointer-events: none;
  122. }
  123. .container :deep(*) {
  124. pointer-events: auto;
  125. }
  126. @media (min-width: 960px) {
  127. .VPNavBar.has-sidebar .container {
  128. max-width: 100%;
  129. }
  130. }
  131. .title {
  132. flex-shrink: 0;
  133. height: calc(var(--vp-nav-height) - 1px);
  134. transition: background-color 0.5s;
  135. }
  136. @media (min-width: 960px) {
  137. .VPNavBar.has-sidebar .title {
  138. position: absolute;
  139. top: 0;
  140. left: 0;
  141. z-index: 2;
  142. padding: 0 32px;
  143. width: var(--vp-sidebar-width);
  144. height: var(--vp-nav-height);
  145. background-color: transparent;
  146. }
  147. }
  148. @media (min-width: 1440px) {
  149. .VPNavBar.has-sidebar .title {
  150. padding-left: max(32px, calc((100% - (var(--vp-layout-max-width) - 64px)) / 2));
  151. width: calc(
  152. (100% - (var(--vp-layout-max-width) - 64px)) / 2 + var(--vp-sidebar-width) - 32px
  153. );
  154. }
  155. }
  156. .content {
  157. flex-grow: 1;
  158. }
  159. @media (min-width: 960px) {
  160. .VPNavBar.has-sidebar .content {
  161. position: relative;
  162. z-index: 1;
  163. padding-right: 32px;
  164. padding-left: var(--vp-sidebar-width);
  165. }
  166. }
  167. @media (min-width: 1440px) {
  168. .VPNavBar.has-sidebar .content {
  169. padding-right: calc((100vw - var(--vp-layout-max-width)) / 2 + 32px);
  170. padding-left: calc(
  171. (100vw - var(--vp-layout-max-width)) / 2 + var(--vp-sidebar-width)
  172. );
  173. }
  174. }
  175. .content-body {
  176. display: flex;
  177. justify-content: flex-end;
  178. align-items: center;
  179. height: var(--vp-nav-height);
  180. transition: background-color 0.5s;
  181. }
  182. @media (min-width: 960px) {
  183. .VPNavBar:not(.home.top) .content-body {
  184. position: relative;
  185. background-color: var(--vp-nav-bg-color);
  186. }
  187. .VPNavBar:not(.has-sidebar):not(.home.top) .content-body {
  188. background-color: transparent;
  189. }
  190. }
  191. @media (max-width: 767px) {
  192. .content-body {
  193. column-gap: 0.5rem;
  194. }
  195. }
  196. .menu + .translations::before,
  197. .menu + .appearance::before,
  198. .menu + .social-links::before,
  199. .translations + .appearance::before,
  200. .appearance + .social-links::before {
  201. margin-right: 8px;
  202. margin-left: 8px;
  203. width: 1px;
  204. height: 24px;
  205. background-color: var(--vp-c-divider);
  206. content: "";
  207. }
  208. .menu + .appearance::before,
  209. .translations + .appearance::before {
  210. margin-right: 16px;
  211. }
  212. .appearance + .social-links::before {
  213. margin-left: 16px;
  214. }
  215. .social-links {
  216. margin-right: -8px;
  217. }
  218. .divider {
  219. width: 100%;
  220. height: 1px;
  221. }
  222. @media (min-width: 960px) {
  223. .VPNavBar.has-sidebar .divider {
  224. padding-left: var(--vp-sidebar-width);
  225. }
  226. }
  227. @media (min-width: 1440px) {
  228. .VPNavBar.has-sidebar .divider {
  229. padding-left: calc(
  230. (100vw - var(--vp-layout-max-width)) / 2 + var(--vp-sidebar-width)
  231. );
  232. }
  233. }
  234. .divider-line {
  235. width: 100%;
  236. height: 1px;
  237. transition: background-color 0.5s;
  238. }
  239. .VPNavBar:not(.home) .divider-line {
  240. background-color: var(--vp-c-gutter);
  241. }
  242. @media (min-width: 960px) {
  243. .VPNavBar:not(.home.top) .divider-line {
  244. background-color: var(--vp-c-gutter);
  245. }
  246. .VPNavBar:not(.has-sidebar):not(.home.top) .divider {
  247. background-color: var(--vp-c-gutter);
  248. }
  249. }
  250. </style>