| --- | |
| // The gallery, the site's showcase: `examples/gallery` published with | |
| // `nsh package --target web` (`cargo xtask web` writes it to | |
| // `public/play/gallery/`), shown full size in a frame that switches between | |
| // a desktop and a phone width, and linked to open on its own. | |
| import { existsSync } from 'node:fs'; | |
| import { join } from 'node:path'; | |
| import { BASE, REPO_URL, url, type Lang } from '../lib/site'; | |
| export interface Props { | |
| lang?: Lang; | |
| } | |
| const { lang = 'en' } = Astro.props; | |
| const zh = lang === 'zh-CN'; | |
| // `index.html` named: the site has `trailingSlash: 'never'`, so `astro dev` | |
| // does not serve a directory's page for its path. | |
| const src = `${BASE}play/gallery/index.html`; | |
| const built = existsSync(join(process.cwd(), 'public', 'play', 'gallery', 'index.html')); | |
| const link = (path: string) => url(zh ? `zh-CN/${path}` : path); | |
| const t = zh | |
| ? { | |
| title: '示例应用', | |
| intro: 'NSH 的示例应用 Gallery:用 TSX 编写的完整应用,涵盖列表、导航、表单、手势、动画、画布、文本和图片。这里运行的是用 nsh package --target web 发布的版本,与它在 iOS、Android 和桌面上运行的代码完全相同。', | |
| view: '宽度', | |
| desktop: '桌面', | |
| phone: '手机', | |
| open: '在新标签页打开', | |
| frame: 'NSH 示例应用 Gallery', | |
| pending: '示例应用尚未构建:运行 bun run dev(或 cargo xtask web)后刷新。', | |
| how: '如何发布到 Web', | |
| source: '源码', | |
| } | |
| : { | |
| title: 'Gallery', | |
| intro: 'The NSH gallery: a whole app written in TSX, with lists, navigation, forms, gestures, animations, canvas, text and images. This is the build nsh package --target web publishes, the same code that runs on iOS, Android and the desktop.', | |
| view: 'Width', | |
| desktop: 'Desktop', | |
| phone: 'Phone', | |
| open: 'Open in a new tab', | |
| frame: 'The NSH gallery', | |
| pending: 'The gallery is not built: run bun run dev (or cargo xtask web) and reload.', | |
| how: 'Publishing for the web', | |
| source: 'Source', | |
| }; | |
| --- | |
| <section class="gallery" data-gallery> | |
| <header class="gallery__head"> | |
| <div> | |
| <h1>{t.title}</h1> | |
| <p>{t.intro}</p> | |
| </div> | |
| <div class="gallery__actions"> | |
| <div class="gallery__seg" role="group" aria-label={t.view}> | |
| <button type="button" data-width="desktop" aria-pressed="true">{t.desktop}</button> | |
| <button type="button" data-width="phone" aria-pressed="false">{t.phone}</button> | |
| </div> | |
| <a class="gallery__btn gallery__btn--primary" href={src} target="_blank" rel="noopener">{t.open}</a> | |
| </div> | |
| </header> | |
| <div class="gallery__stage" data-stage data-mode="desktop"> | |
| {built | |
| ? <iframe class="gallery__frame" src={src} title={t.frame} allow="clipboard-write" data-frame></iframe> | |
| : <p class="gallery__pending">{t.pending}</p>} | |
| </div> | |
| <p class="gallery__links"> | |
| <a href={link(zh ? 'docs/web#发布应用' : 'docs/web#publish-an-app')}>{t.how}</a> | |
| <span aria-hidden="true">·</span> | |
| <a href={`${REPO_URL}/tree/main/examples/gallery`} target="_blank" rel="noopener noreferrer">{t.source}</a> | |
| </p> | |
| </section> | |
| <script> | |
| for (const root of document.querySelectorAll<HTMLElement>('[data-gallery]')) { | |
| const stage = root.querySelector<HTMLElement>('[data-stage]')!; | |
| const frame = root.querySelector<HTMLIFrameElement>('[data-frame]'); | |
| const buttons = [...root.querySelectorAll<HTMLButtonElement>('[data-width]')]; | |
| const KEY = 'nsh-gallery-width'; | |
| const set = (mode: string) => { | |
| stage.dataset.mode = mode; | |
| for (const b of buttons) b.setAttribute('aria-pressed', String(b.dataset.width === mode)); | |
| try { | |
| localStorage.setItem(KEY, mode); | |
| } catch {} | |
| }; | |
| let saved: string | null = null; | |
| try { | |
| saved = localStorage.getItem(KEY); | |
| } catch {} | |
| // Phones see the phone layout whatever was chosen. | |
| set(saved === 'phone' || matchMedia('(max-width: 640px)').matches ? 'phone' : 'desktop'); | |
| for (const b of buttons) b.addEventListener('click', () => set(b.dataset.width!)); | |
| // The app follows its frame's color scheme: the site's theme. | |
| if (frame) { | |
| const sync = () => (frame.style.colorScheme = document.documentElement.classList.contains('light') ? 'light' : 'dark'); | |
| sync(); | |
| new MutationObserver(sync).observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }); | |
| } | |
| } | |
| </script> | |
| <style is:global> | |
| .gallery { | |
| max-width: 1440px; | |
| margin: 0 auto; | |
| padding: 2.25rem clamp(1rem, 3vw, 2.5rem) 2.5rem; | |
| } | |
| .gallery__head { | |
| display: flex; | |
| flex-wrap: wrap; | |
| align-items: flex-end; | |
| justify-content: space-between; | |
| gap: 1rem 2rem; | |
| } | |
| .gallery__head h1 { margin: 0; font-size: 1.75rem; letter-spacing: -0.02em; } | |
| .gallery__head p { margin: 0.35rem 0 0; color: var(--muted-foreground); max-width: 52rem; } | |
| .gallery__actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem; } | |
| .gallery__seg { | |
| display: inline-flex; | |
| padding: 3px; | |
| border: 1px solid var(--border); | |
| border-radius: var(--radius-control); | |
| background: var(--secondary); | |
| } | |
| .gallery__seg button { | |
| height: 1.75rem; | |
| padding: 0 0.85rem; | |
| border: 0; | |
| border-radius: calc(var(--radius-control) - 3px); | |
| background: transparent; | |
| color: var(--muted-foreground); | |
| font: inherit; | |
| font-size: 0.85rem; | |
| cursor: pointer; | |
| } | |
| .gallery__seg button[aria-pressed="true"] { background: var(--finish-raised-bg); box-shadow: var(--finish-raised); color: var(--foreground); } | |
| .gallery__btn { | |
| display: inline-flex; | |
| align-items: center; | |
| height: 2.25rem; | |
| padding: 0 0.95rem; | |
| border: 1px solid var(--border); | |
| border-radius: var(--radius-control); | |
| background: var(--finish-raised-bg); | |
| box-shadow: var(--finish-raised); | |
| color: var(--foreground); | |
| font-size: 0.85rem; | |
| text-decoration: none; | |
| } | |
| .gallery__btn--primary { border-color: transparent; background: var(--finish-primary-bg); box-shadow: var(--finish-primary); color: var(--brand-contrast); font-weight: 600; } | |
| .gallery__btn--primary:hover { background: var(--finish-primary-hover-bg); } | |
| .gallery__stage { | |
| margin-top: 1.25rem; | |
| display: flex; | |
| justify-content: center; | |
| } | |
| .gallery__frame { | |
| display: block; | |
| width: 100%; | |
| height: max(560px, calc(100dvh - 15rem)); | |
| border: 1px solid var(--border); | |
| border-radius: 14px; | |
| background: #000; | |
| box-shadow: var(--shadow-panel); | |
| transition: width 220ms ease; | |
| } | |
| .gallery__stage[data-mode="phone"] .gallery__frame { | |
| width: 390px; | |
| max-width: 100%; | |
| height: min(844px, max(560px, calc(100dvh - 15rem))); | |
| border-radius: 28px; | |
| } | |
| .gallery__pending { padding: 4rem 1rem; color: var(--muted-foreground); text-align: center; } | |
| .gallery__links { margin: 1rem 0 0; display: flex; gap: 0.5rem; justify-content: center; font-size: 0.85rem; color: var(--muted-foreground); } | |
| .gallery__links a { color: inherit; } | |
| .gallery__links a:hover { color: var(--foreground); } | |
| </style> |