# Lockout

A lockout-tagout hasp: everyone working on something hangs their own padlock and tag, and it can't start until the last lock comes off. For freezes and holds.

- Category: Coordination
- Docs: https://bevel-ui.vercel.app/components/lockout
- Import from: `@/components/ui/lockout`

## Install

```bash
npx shadcn@latest add @bevel/lockout
```

npm dependencies (installed by the CLI): `@base-ui/react`, `cn`.

## Anatomy

```tsx
<Lockout aria-label="Database migration">
  <LockoutHasp>
    <LockoutLock id="priya" owner="Priya" reason="Restoring a backup" />
  </LockoutHasp>
  <LockoutAction>Run migration</LockoutAction>
</Lockout>
```

## Parts

| Part | Renders | Description |
| --- | --- | --- |
| `Lockout` | div (group) | Counts the locks on it. Give it an aria-label. |
| `LockoutHasp` | ul | The bar the locks hang from. |
| `LockoutLock` | li | id, owner, reason, shown on a danger tag. onRemove adds “Remove my lock”: pass it only for the current user's lock. |
| `LockoutAction` | Button | Disabled while any lock remains, described as “Locked by 2: Priya, Sam”. |

## Example: A migration on hold

Three locks hold the migration. Remove yours; the others come off as their work finishes.

```tsx
"use client"

import * as React from "react"
import { Lockout, LockoutAction, LockoutHasp, LockoutLock } from "@/components/ui/lockout"

const start = [
  { id: "priya", owner: "Priya", reason: "Restoring a backup" },
  { id: "you", owner: "You", reason: "Reviewing the schema" },
  { id: "sam", owner: "Sam", reason: "Load test running" },
]

/** The migration waits for every lock. Remove yours; the others come off as their work finishes. */
export function LockoutMigration() {
  const [locks, setLocks] = React.useState(start)
  const [ran, setRan] = React.useState(false)
  const remove = (id: string) => setLocks((l) => l.filter((x) => x.id !== id))

  React.useEffect(() => {
    if (locks.some((l) => l.id === "you")) return
    const others = locks.filter((l) => l.id !== "you")
    if (!others.length) return
    const timer = setTimeout(() => remove(others[0]!.id), 1600)
    return () => clearTimeout(timer)
  }, [locks])

  return (
    <div className="flex w-full max-w-md flex-col items-center gap-4">
      <Lockout aria-label="Database migration">
        <LockoutHasp>
          {locks.map((l) => (
            <LockoutLock
              key={l.id}
              id={l.id}
              owner={l.owner}
              reason={l.reason}
              onRemove={l.id === "you" ? () => remove("you") : undefined}
            />
          ))}
        </LockoutHasp>
        <LockoutAction onClick={() => setRan(true)}>{ran ? "Migration started" : "Run migration"}</LockoutAction>
      </Lockout>
      <button
        type="button"
        onClick={() => {
          setLocks(start)
          setRan(false)
        }}
        className="h-9 rounded-md border px-3 text-sm font-medium hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none"
      >
        Reset
      </button>
    </div>
  )
}
```
