CustomNavBar.vue 6.3 KB

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