App shell
Layout and shell
Sidebar, sticky top bar and content area as one reusable frame; the sidebar becomes a drawer on small screens and the brand is a prop.
실제로 돌아가는 부품입니다. 눌러 보고 입력해 보세요.
내 앱에 넣기
아직 npm 패키지가 없어서 Taster 리포를 받아 그 안의 명령을 씁니다. 내 앱 폴더가 들어 있는 폴더에서 한 번만 받습니다.
git clone https://github.com/makim-414/taster.git && npm ci --prefix taster/packages/engine내 앱 폴더에서 이 부품을 넣습니다. 부품 파일과 토큰 파일이
src/components/taster에 들어갑니다.node ../taster/packages/engine/cli.mjs add blocks/app-shell --from https://taster-six.vercel.app --dir src/components/taster부품은 Tailwind 클래스로 그리고, Taster 프리셋이 그 클래스를 토큰에 연결합니다. Tailwind CSS 를 설치하고,
tokens.css를 앱 루트에서 한 번 불러온 뒤, 앱 루트에 이 설정을 둡니다. 프리셋이 없으면 빌드는 되어도 부품이 스타일 없이 나옵니다.// tailwind.config.cjs module.exports = { presets: [require('./src/components/taster/tailwind-preset.cjs')], content: ['./index.html', './src/**/*.{ts,tsx}'], };Tailwind v3 는 이 파일을 그대로 읽습니다. v4 는 Tailwind 를 불러오는
src/의 스타일시트 맨 위에 이 두 줄을 넣습니다.@import "tailwindcss"; @config "../tailwind.config.cjs";tokens.css는:root에--bg,--text,--border,--accent를 정의합니다. 내 앱의 스타일시트가 같은 이름을 뒤에서 다시 정의하면 그쪽 색이 이깁니다. 새 Vite 앱의 스타터 CSS 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.
언제 쓰나요
- App shellSidebar, sticky top bar and content area as one reusable frame; the sidebar becomes a drawer on small screens and the brand is a prop.
- App barsA thin sticky top bar with title, sidebar toggle and page actions, plus a bottom tab bar of three to five destinations on phones.
- Collapsible sidebarGrouped menu with a collapsed icon rail and a drawer on mobile, a workspace switcher on top, one indicator that slides between links. The active item uses a surface tone, not a colored bar or glowing dot.
대신 없애 주는 AI 티
- Glass panels everywhere (CMP-05)
- Thick colored accent bar on one edge of a card (AC-10)
- Rainbow of tinted boxes (COL-06)
정보
코드 보기
'use client'
import * as React from 'react'
import { cn } from './_cn'
import { safeHref } from './_url'
// AppShell. Sidebar plus sticky top bar plus content column, tokens only, no Radix.
// The brand is a prop (never hardcoded). Mobile gets a drawer, desktop a fixed sidebar.
export interface NavItem {
label: string
/** http, https or mailto, or a relative path. Any other scheme draws a button instead (blocks/_url.ts). */
href?: string
onClick?: () => void
icon?: React.ReactNode
active?: boolean
count?: number
group?: string
}
export interface AppShellProps {
brand: React.ReactNode
nav: NavItem[]
title?: string
subtitle?: string
rightSlot?: React.ReactNode
footer?: React.ReactNode
children: React.ReactNode
contentClassName?: string
}
function NavLink({ item }: { item: NavItem }) {
const cls = cn(
'group flex min-h-11 w-full items-center gap-2 rounded-md px-2.5 py-2 text-left text-sm transition-colors outline-none desk:min-h-0',
'focus-visible:ring-2 focus-visible:ring-ring',
item.active ? 'bg-bg-elev font-medium text-text' : 'text-text-muted hover:text-text',
)
const inner = (
<>
{item.icon && <span className="shrink-0">{item.icon}</span>}
<span className="min-w-0 flex-1 truncate">{item.label}</span>
{typeof item.count === 'number' && (
<span className="shrink-0 rounded-full border border-border px-1.5 text-xs tabular-nums text-text-subtle">
{item.count}
</span>
)}
</>
)
const href = safeHref(item.href)
if (href) {
return (
<a href={href} aria-current={item.active ? 'page' : undefined} className={cls}>
{inner}
</a>
)
}
return (
<button type="button" onClick={item.onClick} aria-current={item.active ? 'page' : undefined} className={cls}>
{inner}
</button>
)
}
function SidebarBody({ brand, nav, footer }: Pick<AppShellProps, 'brand' | 'nav' | 'footer'>) {
const groups: { name: string | null; items: NavItem[] }[] = []
for (const item of nav) {
const name = item.group ?? null
const last = groups[groups.length - 1]
if (last && last.name === name) last.items.push(item)
else groups.push({ name, items: [item] })
}
return (
<div className="flex h-full flex-col">
<div className="flex h-12 shrink-0 items-center px-3 font-semibold">{brand}</div>
<nav className="flex-1 space-y-4 overflow-y-auto px-2 py-2">
{groups.map((g, gi) => (
<div key={gi} className="space-y-0.5">
{g.name && (
<div className="px-2.5 pb-1 text-xs font-medium text-text-subtle">{g.name}</div>
)}
{g.items.map((item, ii) => (
<NavLink key={ii} item={item} />
))}
</div>
))}
</nav>
{footer && <div className="shrink-0 border-t border-border px-3 py-3">{footer}</div>}
</div>
)
}
export function AppShell({
brand,
nav,
title,
subtitle,
rightSlot,
footer,
children,
contentClassName,
}: AppShellProps) {
const [drawerOpen, setDrawerOpen] = React.useState(false)
const showHeader = Boolean(title || subtitle || rightSlot)
React.useEffect(() => {
if (!drawerOpen) return
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') setDrawerOpen(false)
}
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
}, [drawerOpen])
return (
<div className="flex min-h-screen bg-bg text-text">
<aside className="sticky top-0 hidden h-screen w-60 shrink-0 border-r border-border bg-sidebar md:block">
<SidebarBody brand={brand} nav={nav} footer={footer} />
</aside>
{drawerOpen && (
<>
<div
className="fixed inset-0 z-40 md:hidden"
style={{ background: 'var(--scrim)' }}
onClick={() => setDrawerOpen(false)}
aria-hidden="true"
/>
<aside className="fixed inset-y-0 left-0 z-50 w-[min(280px,86vw)] border-r border-border bg-sidebar md:hidden">
<SidebarBody brand={brand} nav={nav} footer={footer} />
</aside>
</>
)}
<div className="flex min-w-0 flex-1 flex-col">
{showHeader && (
<header className="sticky top-0 z-30 flex h-12 shrink-0 items-center gap-3 border-b border-border bg-bg-elev px-4">
<button
type="button"
onClick={() => setDrawerOpen(true)}
aria-label="Open menu"
className="-ml-2.5 inline-flex size-11 items-center justify-center rounded-md text-text-muted transition-colors hover:text-text md:hidden"
>
<MenuIcon />
</button>
<div className="min-w-0">
{title && <div className="truncate text-base font-semibold leading-tight">{title}</div>}
{subtitle && <div className="truncate text-xs text-text-muted">{subtitle}</div>}
</div>
{rightSlot && <div className="ml-auto flex items-center gap-2">{rightSlot}</div>}
</header>
)}
<main className={cn('mx-auto w-full max-w-6xl flex-1 px-5 py-6', contentClassName)}>
{children}
</main>
</div>
</div>
)
}
function MenuIcon() {
return (
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.75} strokeLinecap="round" aria-hidden="true">
<path d="M4 6h16M4 12h16M4 18h16" />
</svg>
)
}