# Currency widget

A conversion you can work with: both amounts are fields, swap turns the pair, and the rate says how fresh it is.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Currency widget. Type in either amount, swap the pair, or pick another currency. The rate says how fresh it is. Preview: https://chatcn.dev/preview/currency-widget-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/currency-widget
```

## Usage

```tsx
import { CurrencyWidget } from "@/components/ui/chatcn/pro/currency-widget"

<CurrencyWidget
  rates={{ USD: 1, EUR: 0.862, JPY: 151.2 }}
  defaultFrom="EUR"
  defaultTo="JPY"
  defaultAmount={250}
  updatedAt={fetchedAt}
  history={eurJpyLastMonth}
  fee={0.0175}
/>
```

Pass rates against any common base and the widget works out each pair. Both amounts are fields: type in either and the other follows, with each currency's own decimals (no cents on yen). The menus are native selects, so they work well on phones; picking the currency already on the other side swaps them. Under the amounts the rate shows with its freshness: a green Live while it is younger than `staleAfter` minutes, an amber Delayed after. `history` draws the month as a small line in green or red with its change, and flips when the pair is swapped. `fee` adds what would arrive after a card or transfer fee.

## Examples

### States

Example: States. Delayed rates with a falling month, and a plain pair with no history. Preview: https://chatcn.dev/preview/currency-widget-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `rates` | `Record<string, number>` | Units per one unit of a common base |
| `defaultFrom`, `defaultTo`, `defaultAmount` |  | The starting pair and amount |
| `updatedAt`, `staleAfter` | `Date | string`, `number` | Freshness; default 15 minutes |
| `history` | `number[]` | The starting pair's rate, oldest first |
| `fee` | `number` | A fraction, such as 0.0175 |
| `currencies` | `string[]` | Codes in the menus. Default all in `rates` |
| `title`, `meta`, `locale` |  | Header and formatting |
| `labels` | `Partial<CurrencyWidgetLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Tab | Currency, amount, swap, then the other side |
| Enter | Finish typing an amount |

## Accessibility

- Amounts are labelled with their currency; menus with their side.
- Freshness is said in words as well as color, and the history has its change in its label.

## Motion

The swap arrow turns half a circle per swap over 300 ms; the live dot pulses. Reduced motion stills both.

Source: https://chatcn.dev/docs/components/currency-widget