# Dice tray

Pick a die, roll a handful, and keep the last four scores.

[View Dice tray on CyberSpec](https://www.cyberspec.dev/sink/dice-tray)

## 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/dice-tray.json
```

## Usage

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

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

## Source

```tsx
"use client"

import { useState } from "react"
import { Dice5 } from "lucide-react"
import { Button } from "@/components/ui/button"
import { SegmentedControl } from "@/components/ui/segmented-control"
import { Readout } from "@/components/ui/readout"
import "./recipe-layout.css"

const dice = ["D4", "D6", "D12", "D20"]
type Roll = { faces: string; values: number[] }

export default function DiceTray() {
  const [faces, setFaces] = useState("D6")
  const [rolls, setRolls] = useState<Roll[]>([])
  const latest = rolls[0]
  function roll() {
    const sides = Number(faces.slice(1))
    const values = Array.from({length: 3}, () => 1 + Math.floor(Math.random() * sides))
    setRolls([{faces, values}, ...rolls].slice(0, 4))
  }
  return (
    <div className="sink-stack">
      <div className="sink-between">
        <Readout label="Last total" value={latest?.values.reduce((a, b) => a + b, 0) ?? 0} size="lg" />
        <Dice5 size={52} color="var(--cy-ink)" aria-hidden />
      </div>
      <SegmentedControl label="Die type" fill value={faces} onValueChange={value => { if (value) setFaces(value) }} options={dice} />
      <Button tone="signal" onClick={roll}>Roll 3{faces.toLowerCase()}</Button>
      <div role="status" className="sink-note">{latest ? `3${latest.faces.toLowerCase()} rolled ${latest.values.join(" + ")}` : "Roll three dice to start a score sheet."}</div>
      {rolls.length > 1 && <p className="sink-note">Previous totals · {rolls.slice(1).map(r => r.values.reduce((a, b) => a + b, 0)).join(" / ")}</p>}
    </div>
  )
}
```

## Components in this recipe

- [Segmented control](https://www.cyberspec.dev/components/sidebar/segmented-control.md) — A row of segments with an indicator that slides to the choice.
- [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.
- [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/dice-tray.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/dice-tray.json)
