Skip to content
ProLifetime access for $199, paid once, every future release included
Truffle UI

Calculator

ProNew

A calculator built from fields and formulas: sliders, number inputs, selects, radios, checkboxes and toggles feed a result panel that counts to the new total.

Installation

pnpm dlx shadcn@latest add @viewport/calculator

First time? Add the @viewport registry to components.json. Setup takes one minute.

Usage

The code behind the preview above comes with Truffle Pro.

Props

<Calculator>

PropTypeDefault
titlestring—
descriptionstring—
fields*CalculatorField[]

The inputs, in order.

—
results*CalculatorResult[]

The first result is the headline number; the rest are listed under it.

—
breakdown{ label: string; value: (values: CalculatorValues) => number }[]

Parts of a whole drawn as a stacked bar with a legend, e.g. principal and interest.

—
ctaCalculatorCta

A call to action at the bottom of the result panel.

—
layout"side" | "stacked"

`side` puts the results beside the fields when there is room; `stacked` always puts them below.

"side"
tone"muted" | "tint" | "solid" | "dark"

Look of the result panel: neutral, a tint of the accent, the accent itself, or near black.

"muted"
fieldStyle"filled" | "outlined"

Look of the value boxes and selects.

"filled"
align"start" | "center"

Title alignment.

"start"
accentstring

Accent for slider fills, checks and the button. Any CSS color.

"var(--primary)"
accentForegroundstring

Text color on top of the accent.

"var(--primary-foreground)"
localestring

BCP 47 locale for number formatting.

"en-US"
resettableboolean

Show a reset link once a value differs from its default.

true
resetLabelstring

Text of the reset link.

"Reset"
disabledboolean

Disable every input.

—
onValuesChange(values: CalculatorValues) => void

Called with the resolved values whenever one changes.

—
classNamestring—

<Field>

PropTypeDefault
field*CalculatorField—
raw*Raw—
onChange*(raw: Raw) => void—
disabledboolean—
number*Intl.NumberFormat—
box*string—

Accessibility

  • Sliders are Radix sliders with arrow, Page and Home/End keys, and announce the formatted value such as “$500,000”.
  • Each value box is a labelled input; Enter commits, Escape restores, and out-of-range entries are clamped.
  • Selects, radios and checkboxes are native controls; the toggle is a switch named by its label.
  • Results are announced through a status region once the value settles, not on every step of a drag.
  • Numbers change instantly, without counting, under reduced motion.