Skip to content

Commit

Permalink
[fix]MobileNavbar
Browse files Browse the repository at this point in the history
  • Loading branch information
wb-lyb830048 committed Jan 11, 2024
1 parent fe91fac commit e5e60f9
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 37 deletions.
13 changes: 13 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ export default defineConfig({
image: {
domain: ["img.alicdn"]
},
vite: {
optimizeDeps: {
esbuildOptions: {
target:'es2020' // 指定ES版本以更好地进行代码转换
}
},
esbuild: {
// 确保Babel能够处理JSX和其他特性
jsxInject: `import React from 'react'`,
jsxFactory: 'React.createElement',
jsxFragment: 'React.Fragment',
},
},
integrations: [
starlight({
title: 'Nacos',
Expand Down
4 changes: 2 additions & 2 deletions src/components/common/Header/CommunityMenu.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HEADER_ACTIVITY_CARD } from "../../../consts";
import LinkText from '@components/common/LinkText.astro';
const t = useTranslations(Astro);
export const comtItems = [
export const communityItems = [
{
label: "社区",
translations: {
Expand Down Expand Up @@ -96,7 +96,7 @@ export const comtItems = [
]
}
];
const [comt, events, resources] = comtItems;
const [comt, events, resources] = communityItems;
const posts = await getEntries(HEADER_ACTIVITY_CARD as any);
const blogImgs = [
"https://img.alicdn.com/imgextra/i4/O1CN01bGnAVW1pFeLFnlaQL_!!6000000005331-2-tps-508-370.png",
Expand Down
44 changes: 34 additions & 10 deletions src/components/common/Header/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import _ from 'lodash';
import Navbar from '@components/common/Navbar.astro';
import LanguageToggle from "@components/common/LanguageToggle.astro";
import MobileNavbar from '@components/common/MobileNavbar.astro';
interface Props {
align?: "home" | "docs";
Expand All @@ -24,17 +25,22 @@ const xpadding = align === "home" ? "0" : "1.5rem";
<!-- 方便控制header宽度 -->
<div class="header-content flex items-center justify-between">

<a class="no-underline" href="/">
<img
class="logo"
src="https://img.alicdn.com/imgextra/i4/O1CN01rW3vAB1FDWKSOiFf0_!!6000000000453-2-tps-204-40.png"
alt="nacos-log"
/>
</a>
<div class="left-content flex">
<a class="no-underline" href="/">
<img
class="logo"
src="https://img.alicdn.com/imgextra/i4/O1CN01rW3vAB1FDWKSOiFf0_!!6000000000453-2-tps-204-40.png"
alt="nacos-log"
/>
</a>
<MobileNavbar class="mobile-navbar ml-4" />
</div>

<Navbar class='h-full'/>
<div class="center-content h-full">
<Navbar class='h-full'/>
</div>

<div>
<div class="right-content">
<LanguageToggle id="pc-toggle" size="small" lang={Astro.props.lang} />
</div>

Expand Down Expand Up @@ -70,10 +76,28 @@ const xpadding = align === "home" ? "0" : "1.5rem";
padding-right: var(--header-xl-padding);
}

.logo {
.left-content .logo {
vertical-align: sub;
width: var(--logo-width);
}

.left-content .mobile-navbar {
display: none;
}

@media (max-width: 40rem) {
.header-content {
width: 100%;
padding-left: 1.5rem;
padding-right: 1.5rem;
}
.left-content .mobile-navbar {
display: block;
}
.center-content {
display: none;
}
}

</style>

153 changes: 153 additions & 0 deletions src/components/common/MobileNavbar.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
import MobileNavbarSublist from "./MobileNavbarSublist.astro";
import dataSource from '@components/common/Header/navbar.ts';
import { docsItems } from '@components/common/Header/DocsMenu.astro';
import { communityItems } from '@components/common/Header/CommunityMenu.astro';
const sublist = [];
for (let i = 0; i < dataSource.length; i++) {
const item = dataSource[i];
let _item = {};
if (item.slot) {
_item = { type: 'group', label: item.label, entries: [], collapsed: true };
} else {
_item = { type: 'link', label: item.label, href: item.route, isCurrent: false }
}
sublist.push(_item);
}
(() => {
const [docs, , community, ] = sublist;
const docsEntries = [];
const communityEntries = [];
for (let i = 0; i < docsItems.length; i++) {
const item = docsItems[i];
docsEntries.push({ type: 'link', label: item.label, href: item.link, isCurrent: false })
}
for (let i = 0; i < communityItems.length; i++) {
const item = communityItems[i];
if (item.children) {
const _item = { type: 'group', label: item.label, entries: [], collapsed: false, class: 'large-weight text-13px' };
for (let j = 0; j < item.children.length; j++) {
const child = item.children[j];
_item.entries.push({ type: 'link', label: child.label, href: child.link, isCurrent: false });
}
communityEntries.push(_item);
} else {
communityEntries.push({ type: 'link', label: item.label, href: item.link, isCurrent: false });
}
}
docs.entries = docsEntries;
community.entries = communityEntries;
})()
---


<div class={`mobile-navbar ${Astro.props.class}`}>
<mobile-menu-button>
<button
aria-expanded="false"
aria-label='menu-button'
aria-controls="starlight__sidebar"
class="sl-flex"
>
<svg
viewBox="0 0 1024 1024"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
p-id="6304"
width="24"
height="24"
>
<path
d="M192.037 287.953h640.124c17.673 0 32-14.327 32-32s-14.327-32-32-32H192.037c-17.673 0-32 14.327-32 32s14.327 32 32 32zM832.161 479.169H438.553c-17.673 0-32 14.327-32 32s14.327 32 32 32h393.608c17.673 0 32-14.327 32-32s-14.327-32-32-32zM832.161 735.802H192.037c-17.673 0-32 14.327-32 32s14.327 32 32 32h640.124c17.673 0 32-14.327 32-32s-14.327-32-32-32zM319.028 351.594l-160 160 160 160z"
fill="#f4f4f6"
p-id="6305"
>
</path>
</svg>
</button>
</mobile-menu-button>

<div class="navbar-pane bg-gray-14">
<div class="navbar-content flex">
<MobileNavbarSublist sublist={sublist} />
</div>
</div>
</div>


<script>
class MobileMenuButton extends HTMLElement {
menuButton = this.querySelector('button')!;

constructor() {
super();
this.menuButton.addEventListener('click', () => this.toggleExpanded());

const parentNav = this.closest('nav');
if (parentNav) {
parentNav.addEventListener('keyup', (e) => this.closeOnEscape(e));
}
}

setExpanded(expanded: boolean) {
this.setAttribute('aria-expanded', String(expanded));
document.body.toggleAttribute('data-mobile-menu-expanded', expanded);
}

toggleExpanded() {
document.body.classList.toggle('overflow-hidden');
this.setExpanded(this.getAttribute('aria-expanded') !== 'true');
}

closeOnEscape(e: KeyboardEvent) {
if (e.code === 'Escape') {
this.setExpanded(false);
this.menuButton.focus();
}
}
}

customElements.define('mobile-menu-button', MobileMenuButton);
</script>


<style>
.mobile-navbar button {
border: 0;
display: flex;
align-items: center;
width: 2rem;
height: 2rem;
background-color: theme('colors.gray.14');
cursor: pointer;
}

.mobile-navbar .navbar-pane {
visibility: var(--sl-sidebar-visibility, hidden);
position: fixed;
z-index: 5;
inset-block: 0;
inset-inline-start: 0;
/* padding-top: 4rem; */
margin-top: 4rem;
width: 100%;
overflow-y: auto;
}

:global([aria-expanded='true']) ~ .navbar-pane {
--sl-sidebar-visibility: visible;
}

.mobile-navbar .navbar-content {
height: 100%;
min-height: max-content;
padding: 1rem 1rem 0;
flex-direction: column;
}


</style>
Loading

0 comments on commit e5e60f9

Please sign in to comment.