| --- | |
| import DocSearch from './DocSearch.astro'; | |
| import Logo from './Logo.astro'; | |
| import ThemeSwitch from './ThemeSwitch.astro'; | |
| import { BASE, GPUI_KIT_URL, REPO_URL, url, type Lang } from '../lib/site'; | |
| import { SECTIONS, sectionHrefs } from '../lib/sections'; | |
| export interface Props { | |
| lang?: Lang; | |
| /** The page's path, base included. */ | |
| currentPath?: string; | |
| /** The same page in the other locale; defaults to the other locale's home. */ | |
| langHref?: string; | |
| } | |
| const { lang = 'en', currentPath = '', langHref } = Astro.props; | |
| const isZh = lang === 'zh-CN'; | |
| const hrefs = await sectionHrefs(lang); | |
| const home = url(isZh ? 'zh-CN' : ''); | |
| const switchHref = langHref ?? url(isZh ? '' : 'zh-CN'); | |
| // The section is the path segment after the base and the optional locale. | |
| const rest = currentPath.startsWith(BASE) ? currentPath.slice(BASE.length) : currentPath.replace(/^\/+/, ''); | |
| const activeSection = rest.replace(/^zh-CN(\/|$)/, '').split('/')[0]; | |
| --- | |
| <header class="site-nav"> | |
| <div class="site-nav__inner"> | |
| <a href={home} class="site-nav__brand" aria-label="Native Shell"> | |
| <Logo class="site-nav__logo" /> | |
| <span>Native Shell</span> | |
| </a> | |
| <details class="site-nav__projects"> | |
| <summary aria-label={isZh ? '切换项目' : 'Switch project'} title={isZh ? '切换项目' : 'Switch project'}> | |
| <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg> | |
| </summary> | |
| <div class="site-nav__projects-menu"> | |
| <a href={home} class="is-current" aria-current="true"> | |
| <Logo class="site-nav__project-logo" /> | |
| <div class="site-nav__project-copy"> | |
| <strong>Native Shell</strong> | |
| <span>{isZh ? '面向每个平台的 Native UI 框架' : 'A native UI framework for every platform'}</span> | |
| </div> | |
| </a> | |
| <a href={GPUI_KIT_URL} target="_blank" rel="noopener noreferrer"> | |
| <svg class="site-nav__project-logo site-nav__gpui-kit-logo" viewBox="0 0 32 32" aria-hidden="true"> | |
| <path d="M4 4h24v5H10v14h18v5H4V4Z" fill="currentColor" /> | |
| <path d="M16 13h12v10h-5v-5h-7v-5Z" fill="#3B82F6" /> | |
| </svg> | |
| <div class="site-nav__project-copy"> | |
| <strong>GPUI Kit</strong> | |
| <span>{isZh ? 'GPUI 桌面应用的组件与工具集' : 'Components and tools for GPUI desktop apps'}</span> | |
| </div> | |
| </a> | |
| </div> | |
| </details> | |
| <nav class="site-nav__links" id="site-nav-links" aria-label={isZh ? '主导航' : 'Primary'}> | |
| {SECTIONS.map((section) => ( | |
| <a | |
| href={hrefs[section.id]} | |
| class:list={['site-nav__link', { active: activeSection === section.id }]} | |
| aria-current={activeSection === section.id ? 'true' : undefined} | |
| > | |
| {section.label[lang]} | |
| </a> | |
| ))} | |
| <a | |
| href={url(isZh ? 'zh-CN/gallery' : 'gallery')} | |
| class:list={['site-nav__link', { active: activeSection === 'gallery' }]} | |
| aria-current={activeSection === 'gallery' ? 'true' : undefined} | |
| > | |
| {isZh ? '示例应用' : 'Gallery'} | |
| </a> | |
| </nav> | |
| <div class="site-nav__actions"> | |
| <DocSearch lang={lang} /> | |
| <a href={switchHref} class="site-nav__lang-btn site-nav__icon-btn" hreflang={isZh ? 'en' : 'zh-CN'} lang={isZh ? 'en' : 'zh-CN'} title={isZh ? 'English' : '中文'}> | |
| {isZh ? 'EN' : '中文'} | |
| </a> | |
| <ThemeSwitch lang={lang} /> | |
| <a href={REPO_URL} class="site-nav__icon-btn" target="_blank" rel="noopener noreferrer" aria-label="GitHub" title="GitHub"> | |
| <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M12 .5a11.5 11.5 0 0 0-3.64 22.41c.58.1.79-.25.79-.56v-2c-3.2.7-3.88-1.37-3.88-1.37-.53-1.33-1.28-1.69-1.28-1.69-1.05-.72.08-.7.08-.7 1.16.08 1.77 1.19 1.77 1.19 1.03 1.77 2.7 1.26 3.36.96.1-.75.4-1.26.73-1.55-2.56-.29-5.25-1.28-5.25-5.69 0-1.26.45-2.29 1.19-3.09-.12-.29-.52-1.46.11-3.05 0 0 .97-.31 3.17 1.18a11 11 0 0 1 5.78 0c2.2-1.49 3.17-1.18 3.17-1.18.63 1.59.23 2.76.11 3.05.74.8 1.19 1.83 1.19 3.09 0 4.42-2.7 5.39-5.26 5.68.41.36.78 1.06.78 2.14v3.17c0 .31.21.67.8.56A11.5 11.5 0 0 0 12 .5Z"/></svg> | |
| </a> | |
| <button id="nav-burger" class="site-nav__burger site-nav__icon-btn" type="button" aria-label={isZh ? '菜单' : 'Menu'} aria-expanded="false" aria-controls="site-nav-links"> | |
| <svg class="site-nav__burger-open" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M4 7h16M4 12h16M4 17h16"/></svg> | |
| <svg class="site-nav__burger-close" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"/></svg> | |
| </button> | |
| </div> | |
| </div> | |
| </header> | |
| <script> | |
| const projects = document.querySelector<HTMLDetailsElement>('.site-nav__projects'); | |
| document.addEventListener('click', (event) => { | |
| if (projects?.open && !projects.contains(event.target as Node)) projects.open = false; | |
| }); | |
| const burger = document.getElementById('nav-burger'); | |
| const links = document.getElementById('site-nav-links'); | |
| const setOpen = (open: boolean) => { | |
| links?.classList.toggle('is-open', open); | |
| burger?.setAttribute('aria-expanded', String(open)); | |
| }; | |
| burger?.addEventListener('click', () => setOpen(!links?.classList.contains('is-open'))); | |
| links?.addEventListener('click', (event) => { | |
| if ((event.target as HTMLElement).closest('a')) setOpen(false); | |
| }); | |
| document.addEventListener('keydown', (event) => { | |
| if (event.key === 'Escape' && links?.classList.contains('is-open')) { | |
| setOpen(false); | |
| burger?.focus(); | |
| } | |
| }); | |
| </script> |