# Reaction clock

Wait for the cue and try to beat your fastest reaction time.

[View Reaction clock on CyberSpec](https://www.cyberspec.dev/sink/reaction-clock)

## Installation

Use a React 19 project with shadcn configured. Render the installed components inside the Cyber root, and import your global stylesheet in the app entry point.

```sh
bunx --bun shadcn@latest add https://www.cyberspec.dev/r/reaction-clock.json
```

## Usage

```tsx
import { Cyber } from "@/components/ui/cyber"
import Recipe from "@/components/recipes/reaction-clock"

export default function App() {
  return (
    <Cyber>
      <Recipe />
    </Cyber>
  )
}
```

## Source

```tsx
"use client"

import { useEffect, useRef, useState } from "react"
import { Timer } from "lucide-react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Readout } from "@/components/ui/readout"
import "./recipe-layout.css"

type Phase = "idle" | "waiting" | "ready" | "early" | "scored"

export default function ReactionClock() {
  const [phase, setPhase] = useState<Phase>("idle")
  const [last, setLast] = useState(0)
  const [best, setBest] = useState<number>()
  const start = useRef(0)
  useEffect(() => {
    if (phase !== "waiting") return
    const timeout = window.setTimeout(() => {
      start.current = performance.now()
      setPhase("ready")
    }, 1500 + Math.random() * 2000)
    return () => clearTimeout(timeout)
  }, [phase])
  function press() {
    if (phase === "waiting") return setPhase("early")
    if (phase !== "ready") return setPhase("waiting")
    const time = Math.round(performance.now() - start.current)
    setLast(time)
    setBest(best === undefined ? time : Math.min(best, time))
    setPhase("scored")
  }
  const cues = { idle: "Ready to start", waiting: "Wait for the cue", ready: "Tap now!", early: "Too soon. Try again.", scored: `Reaction time · ${last} ms` }
  return (
    <div className="sink-stack">
      <div className="sink-between"><span className="sink-label">Reflex test</span><Badge tone="mute">Best · {best === undefined ? "—" : `${best} ms`}</Badge></div>
      <Readout label="Last time" value={last} unit="ms" size="lg" />
      <p role="status" className="sink-note">{cues[phase]}</p>
      <Button size="lg" tone={phase === "ready" ? "flag" : "secondary"} onClick={press}><Timer aria-hidden />{phase === "ready" ? "Tap now" : phase === "waiting" ? "Wait…" : phase === "idle" ? "Start round" : "Play again"}</Button>
    </div>
  )
}
```

## Components in this recipe

- [Readout](https://www.cyberspec.dev/components/sidebar/readout.md) — A large numeral with a unit and a label that counts to its new value when it changes.
- [Badge](https://www.cyberspec.dev/components/sidebar/badge.md) — A small cut label in seven tones, with count and status variants.
- [Button](https://www.cyberspec.dev/components/sidebar/button.md) — A cut-corner button with a raised tab, five tones, and a press flash. Enable the cipher prop to scramble the label and briefly tear the frame on hover.

## Resources

- [All kitchen sink recipes](https://www.cyberspec.dev/sink.md)
- [Recipe source on GitHub](https://github.com/agnelnieves/cyberspec/blob/main/registry/cyber/recipes/reaction-clock.tsx)
- [Recipe layout stylesheet](https://github.com/agnelnieves/cyberspec/blob/main/registry/cyber/recipes/recipe-layout.css)
- [Shadcn registry item](https://www.cyberspec.dev/r/reaction-clock.json)
