# Shell

Shell lays out an app's frame, placing the header, navigation, content and side panels and opening, closing and resizing them.

Shell is the frame of an application. It places a header, navigation, the main content, an inspector and a bottom panel on one grid, and each pane opens, closes and turns into an overlay where the window is too narrow for it. The rows inside a navigation pane are [Sidebar](/docs/sidebar). For a bar at the top of a marketing page with no panes, use a [Navbar](/docs/navbar).


## Usage

```tsx
import { Button, Flex, Shell, Text } from '@kushagradhawan/kookie-ui';
import { HugeiconsIcon } from '@hugeicons/react';
import { SidebarLeft01Icon } from '@hugeicons/core-free-icons';

export function AppFrame({ children }: { children: React.ReactNode }) {
  return (
    <Shell.Root>
      <Shell.Header>
        <Flex align="center" gap="3" px="4" height="100%">
          <Shell.Trigger target="sidebar" asChild>
            <Button iconOnly emphasis="quiet" aria-label="Toggle navigation">
              <HugeiconsIcon icon={SidebarLeft01Icon} strokeWidth={1.75} />
            </Button>
          </Shell.Trigger>
          <Text weight="medium">Projects</Text>
        </Flex>
      </Shell.Header>
      <Shell.Sidebar>{/* Sidebar rows */}</Shell.Sidebar>
      <Shell.Content>{children}</Shell.Content>
    </Shell.Root>
  );
}
```

From 768px up the sidebar sits in the frame, open. Below that it's an overlay, closed until the trigger opens it.

## Anatomy

```tsx
<Shell.Root>
  <Shell.Header />
  <Shell.Rail />
  <Shell.Panel />
  <Shell.Sidebar>
    <Shell.PaneHeader />
    <Shell.Scroll />
    <Shell.PaneFooter />
  </Shell.Sidebar>
  <Shell.Content />
  <Shell.Inspector />
  <Shell.Bottom />
</Shell.Root>
```

- **Root:** The grid. It renders its children in the order you write them.
- **Header:** The top row.
- **Rail:** A narrow column of icons. Sidebar rows in it draw as tiles, one step larger than the Shell. At radius `full` the tiles are circles.
- **Panel:** The wider navigation a Rail opens.
- **Sidebar:** One navigation column, in place of a Rail and Panel.
- **Content:** The work area. The only part every Shell needs.
- **Inspector:** Details at the inline end.
- **Bottom:** A panel along the bottom, such as a console.
- **Trigger:** A button anywhere in the Root that drives a pane by name.
- **PaneHeader, Scroll, PaneFooter:** A pane's top row, scrolling list and bottom row.

## Best practices

**Use a Sidebar or a Rail with a Panel, not both.** They take the same column. A Sidebar suits most apps. A Rail with a Panel suits apps with several sections that each have their own navigation.

**Keep panes as direct children of the Root.** Each pane takes its own place on the grid. Inside any other wrapper the wrapper takes the cell, unless it has `display: contents`.

**Give every pane a Trigger.** An overlay pane is closed until something opens it, and a pane closed by a double-click on its handle needs a way back.

## Presentation

`presentation` sets how a pane sits in the frame. It takes one value or a responsive object such as `{ initial: 'overlay', md: 'fixed' }`.

- `fixed` takes its own column or row, and the content gets narrower. Use it on wide windows.
- `overlay` covers the frame with a scrim. Escape or a press on the scrim closes it. Use it on narrow windows.
- `stacked` sits over the content with no scrim and takes no space from it.

In a contained Shell an overlay covers only the Shell. Otherwise it covers the window.

The breakpoints are `xs` 520px, `sm` 768px, `md` 1024px, `lg` 1280px and `xl` 1640px. Import `breakpoints` from the package for the same widths in your own media queries.

## Scrolling

By default a Shell is the window: it takes the window's height, and the content scrolls inside it. On a phone or any touch screen narrower than 768px, the page scrolls instead, so the browser can hide its toolbars. The header and the content's pinned rows stay put, and drawers stay on screen. While a drawer is open the page holds still.

A Shell that lives inside something else, such as a card or a demo, takes `contained`. It fills its parent and always scrolls inside itself. A `height` of its own, such as `height="600px"`, makes it contained too.

```tsx
<Box height="480px">
  <Shell.Root contained>{/* Panes */}</Shell.Root>
</Box>
```

When the page scrolls, content that can't wrap is cut off at the screen's edge. Give a wide table or a code block its own horizontal scroller.

## State

A pane is in one of three states:

- **Untouched:** No state props. It follows the window: navigation is open in the frame and closed as an overlay, and detail panes are closed. The stylesheet decides, so the first paint is right. A responsive `defaultOpen`, such as `{ initial: false, lg: true }`, sets the answer per breakpoint.
- **Touched:** Once someone opens or closes it, or you pass a single `defaultOpen`, it keeps that state at every width.
- **Controlled:** Pass `open` and `onOpenChange` (`state` and `onStateChange` on Sidebar). Don't pass both a value and its default.

`onOpenChange` runs only when a person acts, never on mount or at a breakpoint. Its `reason` is `'toggle'` for a trigger or the handle, and `'dismiss'` for Escape or the scrim. Panes don't drive each other: to close the Panel with the Rail, do it in the Rail's `onOpenChange`.

## Examples

### Resizable inspector

`resizable` adds a handle on the pane's inner edge. Shell doesn't store sizes, so keep the width in your own state to restore it on the next visit.

```tsx
const [width, setWidth] = React.useState(320);

<Shell.Inspector
  resizable
  size={width}
  onSizeChange={setWidth}
  minSize={240}
  maxSize={480}
>
  {/* Properties */}
</Shell.Inspector>
```

### Navigation with a logo and a profile row

`Shell.PaneHeader` and `Shell.PaneFooter` stay put while `Shell.Scroll` scrolls between them. With `float`, the rows sit over the list and it scrolls behind them. Add `fade` to the Scroll and `backdrop` to a Toolbar in the row so its buttons stay readable.

```tsx
<Shell.Sidebar aria-label="Main">
  <Shell.PaneHeader float>
    <Toolbar backdrop aria-label="Workspace">{/* Logo, search */}</Toolbar>
  </Shell.PaneHeader>
  <Shell.Scroll fade>
    {/* Sidebar rows */}
  </Shell.Scroll>
  <Shell.PaneFooter>{/* Profile */}</Shell.PaneFooter>
</Shell.Sidebar>
```

### Page with a pinned toolbar

The same parts work in `Shell.Content`. With a `Shell.Scroll` in it, the content stops scrolling and the Scroll scrolls the page. A `Shell.PaneHeader float` beside it stays pinned, with the same inset as a sidebar's header, and the page starts below it. Link targets and focused controls scroll into view below it too.

```tsx
<Shell.Content>
  <Shell.PaneHeader float>
    <Toolbar backdrop aria-label="Page">{/* Page actions */}</Toolbar>
  </Shell.PaneHeader>
  <Shell.Scroll fade>{/* Page */}</Shell.Scroll>
</Shell.Content>
```

The floating row's height is published as `--kui-pane-inset-start` inside the Scroll. Use it to place something under the row, such as a sticky table of contents (`top: var(--kui-pane-inset-start)`), or to pull a full-bleed band up to the pane's top edge.

## Props

### Root

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `height` | `'full' \| 'auto' \| string \| number` | `'full'` | The frame's height. `'full'` is the window's. `'auto'` grows with the content. Any other value makes the Shell contained |
| `contained` | `boolean` | `false` | Fills the parent instead of the window and always scrolls inside itself |
| `size` | `'1' \| '2' \| '3'` | Theme size, else `'2'` | The size of the pane rows and the Sidebar rows in them. A Theme size of 4 maps to 3 |

### Header

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `height` | `number` | `64` | The row's height in pixels |

### Sidebar

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `state` | `'expanded' \| 'collapsed'` | | The state, when you control it |
| `defaultState` | `'expanded' \| 'collapsed'`, responsive | | The starting state. Unset, it follows the window |
| `onStateChange` | `(state, { reason }) => void` | | Runs when a person changes the state |
| `presentation` | `'fixed' \| 'overlay' \| 'stacked'`, responsive | `{ initial: 'overlay', sm: 'fixed' }` | How it sits in the frame |
| `expandedSize` | `number` | `288` | The open width in pixels |
| `flush` | `boolean` | `true` | Joins the frame, with a hairline to its neighbour. `false` makes it a card |

Sidebar also takes the resize props, with `minSize` 200 and `maxSize` 400.

### Rail, Panel, Inspector and Bottom

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | | The open state, when you control it |
| `defaultOpen` | `boolean`, responsive | | The starting state. Unset, the pane is untouched |
| `onOpenChange` | `(open, { reason }) => void` | | Runs when a person opens or closes the pane |
| `presentation` | `'fixed' \| 'overlay' \| 'stacked'`, responsive | See below | How it sits in the frame |
| `expandedSize` | `number` | See below | The open width, or height for Bottom, in pixels |
| `flush` | `boolean` | `true` | Joins the frame, with a hairline to its neighbour. `false` makes it a card |
| `size` | `'1' \| '2' \| '3'` | One step above the Shell, up to 3 | Rail only. The size of its rows |

Rows rendered elsewhere and portalled into the Rail need `Shell.RailScope` around their `Shell.Scroll` to draw as rail tiles at the rail's size. It takes `size` too.

| Pane | `presentation` | `expandedSize` | `minSize` / `maxSize` | Untouched |
| --- | --- | --- | --- | --- |
| Rail | `'fixed'` | 64 | Not resizable | Open in the frame, closed as an overlay |
| Panel | The Rail's | 288 | 100 / 800 | Closed |
| Inspector | `{ initial: 'overlay', sm: 'fixed' }` | 320 | 200 / 500 | Closed |
| Bottom | `'fixed'` | 200 | 100 / 400 | Closed |

### Resize props

Sidebar, Panel, Inspector and Bottom take these.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `resizable` | `boolean` | `false` | Shows a handle on the inner edge |
| `size` | `number \| string` | | The size, when you control it |
| `defaultSize` | `number \| string` | | The starting size |
| `onSizeChange` | `(size, { reason }) => void` | | Runs with the new size when a drag or key press ends, and once on mount with `defaultSize` |
| `sizeUpdate` | `'throttle' \| 'debounce'` | | Rate-limits `onSizeChange` across repeated key presses |
| `sizeUpdateMs` | `number` | `50` | The interval for `sizeUpdate` |
| `minSize` | `number` | Per pane | The smallest size in pixels |
| `maxSize` | `number` | Per pane | The largest size in pixels |
| `collapsible` | `boolean` | `true` | A double-click on the handle, or Enter on it, closes and reopens the pane |
| `onResizeStart`, `onResize`, `onResizeEnd` | `(size) => void` | | Run as a drag starts, moves (once per frame at most) and ends |

### Content

Content takes `flush`, like the panes. With `flush={false}` it is a card on the Shell's background, and nothing floats over it. With a `Shell.Scroll` child, the Scroll scrolls the page instead of the content. Where the page scrolls (see [Scrolling](#scrolling)), the Scroll is out of the tab order.

### Trigger

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `target` | `'rail' \| 'panel' \| 'sidebar' \| 'inspector' \| 'bottom'` | | The pane it drives |
| `action` | `'toggle' \| 'expand' \| 'collapse'` | `'toggle'` | What a press does |
| `asChild` | `boolean` | `false` | Renders its one child as the trigger, such as a Button |

Trigger renders a `button` with `type="button"`. With `asChild`, it puts its press and its ARIA on the child instead, so `<Shell.Trigger asChild><Button>` renders one button. Your `onClick` runs first, and `event.preventDefault()` cancels the trigger's action.

### PaneHeader, Scroll and PaneFooter

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `float` | `boolean` | `false` | PaneHeader and PaneFooter only. The row sits over the list |
| `fade` | `boolean` | `false` | Scroll only. Fades the list toward an edge with more past it |

A ref on Scroll is the element that scrolls, so `ref.current.scrollTo()` works, and `onScroll` and ARIA props land there too. `className` and `style` style the content inside it.

Header accepts the attributes of the HTML `header` element, Content those of `main`, Trigger those of `button`, and every other part those of `div`.

Each part's props are exported as a type: `Shell.RootProps`, `Shell.HeaderProps`, `Shell.RailProps`, `Shell.PanelProps`, `Shell.SidebarProps`, `Shell.ContentProps`, `Shell.InspectorProps`, `Shell.BottomProps`, `Shell.TriggerProps`, `Shell.PaneHeaderProps`, `Shell.PaneFooterProps` and `Shell.ScrollProps`.

## Accessibility

| Key | Action |
| --- | --- |
| Escape | Closes open overlays and returns focus |
| Arrow keys | On a resize handle, move it by 8px, or 32px with Shift |
| Home or End | On a resize handle, go to the smallest or largest size |
| Enter | On a resize handle of a collapsible pane, close or reopen the pane |

- **Landmarks:** Header is a `header` and Content is a `main`. Rail, Panel and Sidebar are navigation landmarks, and Inspector is a complementary one. Give each an `aria-label` when there is more than one, and pass your own `role` to change it. `Shell.Scroll` is not a landmark.
- **Forced colors:** A focused resize handle shows its line in the system highlight colour, and an overlay or stacked pane gets an outline in place of its shadow.
- **Trigger:** It sets `aria-controls` to the pane's id and `aria-expanded` to its state. On an untouched pane, `aria-expanded` is left off until the state is known.
- **Overlay:** Opening one moves focus into the pane and makes the rest of the frame `inert`. Closing it returns focus to where it was, or to the pane's trigger.
- **Escape:** A menu or dialog opened from a pane closes first. A handler in the pane that calls `preventDefault()` keeps the pane open.
- **Resize handle:** It's a focusable `role="separator"` named "Resize Sidebar pane" (or the pane's name), with `aria-valuenow`, `aria-valuemin` and `aria-valuemax`.
