|
| 1 | +--- |
| 2 | +title: "AppShell" |
| 3 | +description: "Main application shell with sidebar and header layout" |
| 4 | +--- |
| 5 | + |
| 6 | +The AppShell component provides the main application layout structure with an integrated sidebar, header, and content area. It's the foundation for building enterprise applications with ObjectUI. |
| 7 | + |
| 8 | +## Basic Usage |
| 9 | + |
| 10 | +```tsx |
| 11 | +import { AppShell, SidebarNav, type NavItem } from '@object-ui/layout'; |
| 12 | +import { Home, Settings, Users } from 'lucide-react'; |
| 13 | + |
| 14 | +const navItems: NavItem[] = [ |
| 15 | + { title: 'Dashboard', href: '/', icon: Home }, |
| 16 | + { title: 'Users', href: '/users', icon: Users }, |
| 17 | + { title: 'Settings', href: '/settings', icon: Settings }, |
| 18 | +]; |
| 19 | + |
| 20 | +<AppShell |
| 21 | + sidebar={<SidebarNav items={navItems} />} |
| 22 | + navbar={ |
| 23 | + <div className="flex items-center gap-4"> |
| 24 | + <span className="font-semibold">My App</span> |
| 25 | + </div> |
| 26 | + } |
| 27 | +> |
| 28 | + {/* Your page content */} |
| 29 | + <div>Main content area</div> |
| 30 | +</AppShell> |
| 31 | +``` |
| 32 | + |
| 33 | +## Component Props |
| 34 | + |
| 35 | +```ts |
| 36 | +import type { AppShellBranding } from '@object-ui/layout'; |
| 37 | + |
| 38 | +interface AppShellProps { |
| 39 | + sidebar?: React.ReactNode; // Sidebar content (usually SidebarNav) |
| 40 | + navbar?: React.ReactNode; // Top navbar content (logo, search, user menu) |
| 41 | + children: React.ReactNode; // Main content area |
| 42 | + className?: string; // Additional CSS classes for content area |
| 43 | + defaultOpen?: boolean; // Sidebar default state (default: true) |
| 44 | + branding?: AppShellBranding; // App branding — four theme fields, see Branding |
| 45 | + rightRail?: React.ReactNode; // Optional right rail beside the content, not |
| 46 | + // over it (ADR-0057 P3a) |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Features |
| 51 | + |
| 52 | +### Responsive Sidebar |
| 53 | + |
| 54 | +The AppShell does not build a sidebar of its own: it renders the node you pass in |
| 55 | +`sidebar`, wrapped in a `SidebarProvider`. Pass a `Sidebar`-based node (`SidebarNav`, or |
| 56 | +your own) and you get a sidebar that: |
| 57 | +- Collapses on mobile devices |
| 58 | +- Can be toggled with the `Cmd/Ctrl + B` keyboard shortcut, or by a toggle button you |
| 59 | + render yourself — see Collapsible Sidebar below |
| 60 | +- Persists state across page navigations |
| 61 | +- Supports smooth animations |
| 62 | + |
| 63 | +### Header Bar |
| 64 | + |
| 65 | +The header renders your `navbar` content and nothing else — the AppShell adds no controls |
| 66 | +of its own to it, in particular **no sidebar toggle**. If you want one, render it inside |
| 67 | +`navbar` (see Collapsible Sidebar below). The header itself provides: |
| 68 | +- Custom navbar content area |
| 69 | +- Fixed height: `h-12` (3rem / 48px) at every breakpoint — there is no responsive height variant |
| 70 | +- Border bottom separator |
| 71 | +- Background matching app theme |
| 72 | + |
| 73 | +### Content Area |
| 74 | + |
| 75 | +The main content area features: |
| 76 | +- Responsive padding in three steps: `p-3` on mobile, `sm:p-4` from the `sm` breakpoint, `md:p-6` from `md` up |
| 77 | +- A taller bottom padding on mobile only: `pb-20` (5rem), which keeps content clear of the mobile bottom bar (that bar is itself `sm:hidden`); from `sm` up the bottom edge follows `sm:pb-4` / `md:pb-6` like the other sides |
| 78 | +- Automatic scrolling |
| 79 | +- Flexible height (fills viewport) |
| 80 | +- Custom className support |
| 81 | + |
| 82 | +### Branding |
| 83 | + |
| 84 | +`branding` is theming, not chrome — the shell hands it to `useAppShellBranding`, and all |
| 85 | +four of the fields it declares are read: |
| 86 | + |
| 87 | +- `primaryColor` / `accentColor` — a six-digit hex (`#3B82F6`; the leading `#` is |
| 88 | + optional, three-digit shorthand is not accepted), translated into the Shadcn theme |
| 89 | + tokens listed under Styling below. A value that does not parse that way is ignored |
| 90 | + silently. |
| 91 | +- `favicon` — overwrites the `href` of the icon link the page already has (`#favicon`, |
| 92 | + otherwise `link[rel="icon"]`). It never creates one, so a document with no icon link is |
| 93 | + left unchanged. |
| 94 | +- `title` — assigned to `document.title` as given. It is the whole title, not a suffix: |
| 95 | + the caller composes the string it wants (the console passes |
| 96 | + `"App label — Product name"`). |
| 97 | + |
| 98 | +All of it acts on the document rather than on the shell's subtree, and the colour |
| 99 | +properties are removed again when the shell unmounts. |
| 100 | + |
| 101 | +### Right Rail |
| 102 | + |
| 103 | +`rightRail` renders your node as a flex sibling of the content area, so the rail sits |
| 104 | +**beside** the content and reflows it rather than overlaying it (ADR-0057 P3a). The shell |
| 105 | +adds no wrapper and no width around it — the node you pass owns its width, border and |
| 106 | +scrolling; omit it and the layout is the unchanged single pane. |
| 107 | + |
| 108 | +## Layout Structure |
| 109 | + |
| 110 | +``` |
| 111 | +┌─────────────────────────────────────┐ |
| 112 | +│ Header (your navbar content) │ |
| 113 | +├──────────┬──────────────────────────┤ |
| 114 | +│ │ │ |
| 115 | +│ Sidebar │ Main Content Area │ |
| 116 | +│ │ │ |
| 117 | +│ │ │ |
| 118 | +│ │ │ |
| 119 | +└──────────┴──────────────────────────┘ |
| 120 | +``` |
| 121 | + |
| 122 | +## Sidebar Configuration |
| 123 | + |
| 124 | +### Default Open State |
| 125 | + |
| 126 | +Control whether sidebar is open by default: |
| 127 | + |
| 128 | +```plaintext |
| 129 | +<AppShell defaultOpen={false}> |
| 130 | + {/* Sidebar starts collapsed */} |
| 131 | +</AppShell> |
| 132 | +``` |
| 133 | + |
| 134 | +### Collapsible Sidebar |
| 135 | + |
| 136 | +The AppShell renders no toggle button of its own. What it does give you is the |
| 137 | +`SidebarProvider` wrapped around both slots, so the sidebar you supply can be toggled via: |
| 138 | +- The `Cmd/Ctrl + B` keyboard shortcut, provided by `SidebarProvider` |
| 139 | +- Programmatic control — `useSidebar().toggleSidebar()` from any component inside the shell |
| 140 | +- A toggle button you render yourself; it belongs in the `navbar` slot: |
| 141 | + |
| 142 | +```tsx |
| 143 | +import { SidebarTrigger } from '@object-ui/components'; |
| 144 | +import { AppShell, SidebarNav, type NavItem } from '@object-ui/layout'; |
| 145 | +import { Home, Settings } from 'lucide-react'; |
| 146 | + |
| 147 | +const navItems: NavItem[] = [ |
| 148 | + { title: 'Dashboard', href: '/', icon: Home }, |
| 149 | + { title: 'Settings', href: '/settings', icon: Settings }, |
| 150 | +]; |
| 151 | + |
| 152 | +<AppShell |
| 153 | + navbar={<SidebarTrigger />} |
| 154 | + sidebar={<SidebarNav items={navItems} />} |
| 155 | +> |
| 156 | + <div>Your page content</div> |
| 157 | +</AppShell> |
| 158 | +``` |
| 159 | + |
| 160 | +## Navbar Content |
| 161 | + |
| 162 | +The navbar area is flexible and can contain: |
| 163 | + |
| 164 | +### Logo and Title |
| 165 | + |
| 166 | +```plaintext |
| 167 | +navbar={ |
| 168 | + <div className="flex items-center gap-2"> |
| 169 | + <img src="/logo.svg" alt="Logo" className="h-8 w-8" /> |
| 170 | + <span className="font-semibold text-lg">My Application</span> |
| 171 | + </div> |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +### Search Bar |
| 176 | + |
| 177 | +```plaintext |
| 178 | +navbar={ |
| 179 | + <div className="flex-1 max-w-md"> |
| 180 | + <Input placeholder="Search..." /> |
| 181 | + </div> |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +### User Menu |
| 186 | + |
| 187 | +```plaintext |
| 188 | +navbar={ |
| 189 | + <div className="flex items-center gap-4 ml-auto"> |
| 190 | + <NotificationsButton /> |
| 191 | + <UserDropdown /> |
| 192 | + </div> |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +## Complete Example |
| 197 | + |
| 198 | +```tsx |
| 199 | +import { AppShell, SidebarNav, type NavItem } from '@object-ui/layout'; |
| 200 | +import { Avatar, AvatarFallback, Button, Input } from '@object-ui/components'; |
| 201 | +import { Bell, Folder, LayoutDashboard, Settings, Users } from 'lucide-react'; |
| 202 | + |
| 203 | +const navItems: NavItem[] = [ |
| 204 | + { title: 'Dashboard', href: '/', icon: LayoutDashboard, badge: '12' }, |
| 205 | + { |
| 206 | + title: 'Projects', |
| 207 | + href: '/projects', |
| 208 | + icon: Folder, |
| 209 | + children: [ |
| 210 | + { title: 'Active', href: '/projects/active' }, |
| 211 | + { title: 'Archived', href: '/projects/archived' }, |
| 212 | + ], |
| 213 | + }, |
| 214 | + { title: 'Team', href: '/team', icon: Users }, |
| 215 | + { title: 'Settings', href: '/settings', icon: Settings }, |
| 216 | +]; |
| 217 | + |
| 218 | +function App() { |
| 219 | + return ( |
| 220 | + <AppShell |
| 221 | + defaultOpen={true} |
| 222 | + sidebar={<SidebarNav title="ObjectUI" items={navItems} />} |
| 223 | + navbar={ |
| 224 | + <div className="flex items-center justify-between w-full"> |
| 225 | + {/* Search */} |
| 226 | + <div className="flex-1 max-w-md"> |
| 227 | + <Input |
| 228 | + placeholder="Search..." |
| 229 | + className="w-full" |
| 230 | + /> |
| 231 | + </div> |
| 232 | + |
| 233 | + {/* Right side items */} |
| 234 | + <div className="flex items-center gap-4"> |
| 235 | + <Button variant="ghost" size="icon"> |
| 236 | + <Bell className="h-5 w-5" /> |
| 237 | + </Button> |
| 238 | + <Avatar> |
| 239 | + <AvatarFallback>JD</AvatarFallback> |
| 240 | + </Avatar> |
| 241 | + </div> |
| 242 | + </div> |
| 243 | + } |
| 244 | + className="bg-gray-50" |
| 245 | + > |
| 246 | + {/* Main content */} |
| 247 | + <div className="max-w-7xl"> |
| 248 | + <h1 className="text-2xl font-bold mb-4">Dashboard</h1> |
| 249 | + {/* Page content */} |
| 250 | + </div> |
| 251 | + </AppShell> |
| 252 | + ); |
| 253 | +} |
| 254 | +``` |
| 255 | + |
| 256 | +`SidebarNav` has no logo slot and no footer slot — `title` is its only branding prop, the |
| 257 | +section label printed above a flat `NavItem[]` (it is ignored when `items` is a |
| 258 | +`NavGroup[]`, since each group prints its own `label`). Put a logo in the `navbar` slot |
| 259 | +instead, as the Logo and Title example under Navbar Content above does. |
| 260 | + |
| 261 | +## With Page Component |
| 262 | + |
| 263 | +Combine AppShell with the Page component for complete layouts: |
| 264 | + |
| 265 | +```plaintext |
| 266 | +<AppShell sidebar={<SidebarNav items={navItems} />}> |
| 267 | + <Page |
| 268 | + title="Dashboard" |
| 269 | + description="Welcome to your dashboard" |
| 270 | + body={[ |
| 271 | + { type: 'text', content: 'Dashboard content' } |
| 272 | + ]} |
| 273 | + /> |
| 274 | +</AppShell> |
| 275 | +``` |
| 276 | + |
| 277 | +## Styling |
| 278 | + |
| 279 | +The AppShell uses Tailwind CSS and Shadcn UI components: |
| 280 | + |
| 281 | +- `SidebarProvider`: Manages sidebar state |
| 282 | +- `SidebarInset`: Content area wrapper |
| 283 | + |
| 284 | +Brand colours arrive as Shadcn theme tokens rather than as per-component styles: each hex |
| 285 | +from `branding` is converted to the `H S% L%` triple those tokens carry and set as a CSS |
| 286 | +custom property on `document.documentElement`. `primaryColor` writes `--primary`, |
| 287 | +`--primary-foreground`, `--ring`, `--sidebar-primary` and `--sidebar-ring`; `accentColor` |
| 288 | +writes `--accent` and `--accent-foreground`. Tailwind's utilities resolve through the same |
| 289 | +tokens (`--color-primary: hsl(var(--primary))`), so every `bg-primary` / `text-primary` / |
| 290 | +`ring-primary` / `bg-accent` consumer picks the brand colour up with no per-component |
| 291 | +wiring. In dark mode a lighter variant of the same hue is used instead, and the shell |
| 292 | +re-applies the values whenever the `dark` class on the `html` element changes. |
| 293 | + |
| 294 | +## Accessibility |
| 295 | + |
| 296 | +The AppShell includes: |
| 297 | +- A keyboard shortcut for toggling the sidebar (`Cmd/Ctrl + B`), from `SidebarProvider` |
| 298 | +- Keyboard navigation support |
| 299 | +- Focus management |
| 300 | +- Screen reader announcements |
| 301 | + |
| 302 | +## Related |
| 303 | + |
| 304 | +- [SidebarNav](/docs/frontend/sidebar-nav) - Sidebar navigation component |
| 305 | +- [PageHeader](/docs/ui/pages) - Page header component |
| 306 | +- [Page](/docs/frontend/renderers) - Page layout component |
0 commit comments