# Retracting Bar

A bar that slides out of the way as users scroll down, and comes back when they scroll up.

Source: [retracting-bar.tsx](https://github.com/jakejarvis/jarvis-ui/blob/main/src/components/ui/retracting-bar.tsx)

```tsx
import { InboxIcon, SquarePenIcon, SearchIcon, SettingsIcon } from "lucide-react";

import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { RetractingBar } from "@/components/ui/retracting-bar";
import { ScrollArea } from "@/components/ui/scroll-area";

const messages = [
  { from: "Maya Chen", time: "9:41", subject: "Deploy to production is green" },
  { from: "Stripe", time: "9:12", subject: "Invoice INV-2041 paid" },
  { from: "Leo Park", time: "8:57", subject: "Re: SSO rollout checklist" },
  { from: "Priya Raman", time: "8:30", subject: "Updated Q3 forecast" },
  { from: "GitHub", time: "Yesterday", subject: "#1182 Retry webhooks with backoff" },
  { from: "Tom Weiss", time: "Yesterday", subject: "Office hours moved to Thursday" },
  { from: "Linear", time: "Mon", subject: "INC-88 resolved" },
  { from: "Ana Souza", time: "Mon", subject: "Two quotes for invoice translation" },
  { from: "Jordan Lee", time: "Sun", subject: "Joined the Platform team" },
  { from: "Maya Chen", time: "Sat", subject: "Storage migration dry run" },
  { from: "Vercel", time: "Fri", subject: "Usage alert: 80% of bandwidth" },
  { from: "Leo Park", time: "Fri", subject: "Audit log export design" },
];

const tabs = [
  { label: "Inbox", icon: InboxIcon },
  { label: "Search", icon: SearchIcon },
  { label: "Settings", icon: SettingsIcon },
];

export default function RetractingBarInbox() {
  return (
    <Card className="h-[28rem] w-full max-w-xs gap-0 py-0">
      <ScrollArea scrollFade={false} className="min-h-0 flex-1 rounded-[inherit]">
        <RetractingBar render={<header />} className="flex h-12 items-center justify-between px-4">
          <span className="font-medium">Inbox</span>
          <Button variant="ghost" size="icon" aria-label="New message">
            <SquarePenIcon />
          </Button>
        </RetractingBar>
        <ul className="divide-y-[0.5px] divide-border-muted">
          {messages.map((message) => (
            <li key={`${message.from}-${message.subject}`} className="px-4 py-3">
              <div className="flex items-baseline justify-between gap-3">
                <span className="truncate font-medium">{message.from}</span>
                <span className="shrink-0 text-2xs text-faint">{message.time}</span>
              </div>
              <p className="truncate text-muted-foreground">{message.subject}</p>
            </li>
          ))}
        </ul>
        <RetractingBar
          render={<nav aria-label="Main" />}
          edge="bottom"
          className="grid grid-cols-3 p-1"
        >
          {tabs.map(({ label, icon: Icon }) => (
            <Button key={label} variant="ghost" className="h-auto flex-col gap-0.5 py-1.5 text-2xs">
              <Icon />
              {label}
            </Button>
          ))}
        </RetractingBar>
      </ScrollArea>
    </Card>
  );
}
```

## Installation

Add the registry, once per project:

```bash
npx shadcn@latest registry add @jarvis=https://ui.jarv.is/r/{name}.json
```

Then add the component:

```bash
npx shadcn@latest add @jarvis/retracting-bar
```

## Usage

```tsx
import { RetractingBar } from "@/components/ui/retracting-bar";

export function Toolbar() {
  return (
    <RetractingBar render={<header />} className="flex h-12 items-center px-4">
      Inbox
    </RetractingBar>
  );
}
```

`RetractingBar` sticks to an edge of the nearest scrolling container, or of the page. It slides away
once someone scrolls down `hideAfter` pixels without stopping. Bringing it back takes a longer
scroll up, `revealAfter` pixels, or a quick flick, so the small corrections users make while
reading don't bring it back over the text. It always shows near the top, and it comes back at the
end of the content too, where there's nothing left to read.

A bar at the top is transparent until content scrolls beneath it, and has `data-at-top` until then.
Set `edge="bottom"` for a tab bar, and render it last in the scrolling region. The bar has
`data-hidden` while it's hidden. Like [Scroll Header](https://ui.jarv.is/components/scroll-header.md), it takes the
corners of the edge it sticks to from its parent.

Tabbing into a hidden bar brings it back without the page jumping, and keeps it shown while keyboard
focus is inside. Clicking a button in it doesn't keep it shown, so a tab bar still hides once users
scroll on. Set `pinned` to keep it shown yourself, such as while a menu in it is open. Since it
doesn't hide while pinned, it stays shown after you unpin it until users scroll on.

When reduced motion is on, the bar still hides, but without sliding. `onHiddenChange` fires each
time the bar hides or comes back, which you can use to move a floating button out of its way.

## Props

| Part | Extends |
| --- | --- |
| `RetractingBar` | `<div>` |

Base UI's [styling](https://base-ui.com/react/handbook/styling) and [composition](https://base-ui.com/react/handbook/composition) guides say more about `className`, `style` and `render`.

### RetractingBar

| Prop              | Type                                                     | Default | Description                                                                                                                                                                               |
| ----------------- | -------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `edge?`           | `"bottom" \| "top"`                                      | `"top"` | The edge the bar sticks to. A bar at the top slides up out of view, and a bar at the bottom slides down.                                                                                  |
| `root?`           | `ScrollTarget`                                           | —       | The scroll container. Defaults to the nearest ancestor that scrolls, or the page.                                                                                                         |
| `hideAfter?`      | `number`                                                 | `16`    | How far to scroll down without stopping before the bar hides, in pixels.                                                                                                                  |
| `revealAfter?`    | `number`                                                 | `56`    | How far to scroll up without stopping before the bar comes back, in pixels.                                                                                                               |
| `revealVelocity?` | `number`                                                 | `0.9`   | The speed, in pixels per millisecond, above which a flick up brings the bar back at once.                                                                                                 |
| `topZone?`        | `number`                                                 | —       | The distance from the top, in pixels, within which the bar stays shown. Defaults to the bar's height.                                                                                     |
| `revealAtEnd?`    | `boolean`                                                | `true`  | Brings the bar back at the end of the content, where there's nothing left to read.                                                                                                        |
| `pinned?`         | `boolean`                                                | `false` | Keeps the bar shown, such as while a menu inside it is open.                                                                                                                              |
| `onHiddenChange?` | `((hidden: boolean) => void)`                            | —       | Called when scrolling or focus hides the bar or brings it back.                                                                                                                           |
| `className?`      | `string`                                                 | —       | CSS class applied to the element.                                                                                                                                                         |
| `style?`          | `React.CSSProperties`                                    | —       | Style applied to the element.                                                                                                                                                             |
| `render?`         | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —       | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |
