# Currency Input

Enter an amount with its currency, picked from a list in the field, built on Base UI.

```tsx title="components/ui/currency-input.tsx"
"use client";

import { NumberField } from "@base-ui/react/number-field";
import { Select } from "@base-ui/react/select";
import {
  IconCheck,
  IconChevronDown,
  IconCurrencyDollar,
  IconCurrencyDollarAustralian,
  IconCurrencyDollarCanadian,
  IconCurrencyEuro,
  IconCurrencyFrank,
  IconCurrencyKroneSwedish,
  IconCurrencyPeso,
  IconCurrencyPound,
  IconCurrencyReal,
  IconCurrencyRupee,
  IconCurrencyYen,
  IconCurrencyYuan,
} from "@tabler/icons-react";
import { type ComponentType, useId, useState } from "react";
import { merge } from "yummacss/merge";
import {
  FOCUS,
  INPUT_TEXT,
  RADIUS,
  type Shape,
  type Size,
  SIZE,
  useShape,
} from "./style";

type Glyph = ComponentType<{ className?: string }>;

// each known currency's sign; any other code shows its text sign
const SIGNS: Record<string, Glyph> = {
  AUD: IconCurrencyDollarAustralian,
  BRL: IconCurrencyReal,
  CAD: IconCurrencyDollarCanadian,
  CHF: IconCurrencyFrank,
  CNY: IconCurrencyYuan,
  EUR: IconCurrencyEuro,
  GBP: IconCurrencyPound,
  INR: IconCurrencyRupee,
  JPY: IconCurrencyYen,
  MXN: IconCurrencyPeso,
  SEK: IconCurrencyKroneSwedish,
  USD: IconCurrencyDollar,
};

// the box draws the outline only while the amount has focus, not the picker
const TYPING = "os:s ow:3 oo:0 oc:focus/40 bc:focus";

function Sign({ code, locale }: { code: string; locale: string }) {
  const Icon = SIGNS[code];
  if (Icon) return <Icon aria-hidden className="w:4 h:4 fs:0 ml:3 c:hint" />;
  const text = new Intl.NumberFormat(locale, {
    style: "currency",
    currency: code,
    currencyDisplay: "narrowSymbol",
  })
    .formatToParts(0)
    .find((part) => part.type === "currency")?.value;
  return (
    <span aria-hidden className="ml:3 c:hint">
      {text ?? code}
    </span>
  );
}

// the picker sits inset, one step shorter than the field, so its hover keeps rounded corners
const PICKER: Record<Size, string> = {
  sm: "h:6 ml:1",
  md: "h:7 ml:1",
  lg: "h:9 ml:1",
};

export interface CurrencyInputProps {
  /** Text above the field, wired to it as its accessible name. */
  label?: string;
  /** A line under the field. */
  description?: string;
  /**
   * Marks the field invalid and shows this under it in place of `description`.
   */
  error?: string;
  /** The amount, controlled. Pair with `onValueChange`. */
  value?: number | null;
  /** The amount on first render, uncontrolled. */
  defaultValue?: number | null;
  /** Called with the amount, or `null` when the field is cleared. */
  onValueChange?: (value: number | null) => void;
  /** The currency, as an ISO code, controlled. Pair with `onCurrencyChange`. */
  currency?: string;
  /** The currency on first render, uncontrolled. */
  defaultCurrency?: string;
  /** Called with the ISO code when another currency is picked. */
  onCurrencyChange?: (currency: string) => void;
  /** The ISO codes the picker offers. One code hides the picker. */
  currencies?: string[];
  /** Formats the grouping, the decimal mark and the currency names. */
  locale?: string;
  /** Marks the field required and adds an asterisk to the label. */
  required?: boolean;
  /** Blocks the field and the picker and dims both. */
  disabled?: boolean;
  /** Height & width of the field. */
  size?: Size;
  /** Corner radius of the field and its popup. Inherits the nearest `Shape`. */
  shape?: Shape;
  /**
   * The picker list rendering inside this element instead of the page, for a
   * frame or a container that owns its own stacking context.
   */
  container?: HTMLElement | null;
  /**
   * The focus outline. `true` draws it, `false` removes it, and a string of
   * Yumma CSS utilities restyles it. Removing it outright and putting nothing
   * back fails WCAG 2.4.7.
   */
  focus?: boolean | string;
  /**
   * Extra classes on the field. `merge` folds them in last, so one here
   * replaces the component's own class for the same utility.
   */
  className?: string;
}

/**
 * An amount with its currency, picked at the start of the field, its sign
 * beside the value, grouped for the locale when the field is left.
 */
export default function CurrencyInput({
  label,
  description,
  error,
  value,
  defaultValue = null,
  onValueChange,
  currency: controlledCurrency,
  defaultCurrency = "USD",
  onCurrencyChange,
  currencies = ["USD", "EUR", "GBP", "JPY"],
  locale = "en-US",
  required = false,
  disabled = false,
  size = "md",
  shape: shapeProp,
  container,
  focus = true,
  className,
}: CurrencyInputProps) {
  const shape = useShape(shapeProp);
  const id = useId();
  const [internalCurrency, setInternalCurrency] = useState(defaultCurrency);
  const currency = controlledCurrency ?? internalCurrency;
  const pickable = currencies.length > 1;
  const [typing, setTyping] = useState(false);

  const digits =
    new Intl.NumberFormat(locale, { style: "currency", currency }).resolvedOptions()
      .maximumFractionDigits ?? 2;
  const names = new Intl.DisplayNames([locale], { type: "currency" });

  const pick = (next: string | null) => {
    if (!next) return;
    setInternalCurrency(next);
    onCurrencyChange?.(next);
  };

  const outline =
    focus && typing
      ? merge(TYPING, focus === true ? "" : focus.replace(/\bfv:/g, ""))
      : "";
  const itemOutline = focus ? merge(FOCUS, focus === true ? "" : focus) : "";

  return (
    <NumberField.Root
      id={id}
      value={value}
      defaultValue={defaultValue ?? undefined}
      onValueChange={onValueChange}
      format={{ minimumFractionDigits: digits, maximumFractionDigits: digits }}
      locale={locale}
      min={0}
      required={required}
      disabled={disabled}
      className={`d:f fd:c g:2 c:label fs:sm ${disabled ? "o:60 c:na" : ""}`}
    >
      {label && (
        <label htmlFor={id} className="fw:500">
          {label}
          {required && <span className="c:red-5"> *</span>}
        </label>
      )}

      <div
        className={merge(
          "d:f ai:c bg:field bs-o:key bw:1",
          error ? "bc:red-5" : "bc:rim",
          INPUT_TEXT,
          SIZE.height[size],
          SIZE.width[size],
          RADIUS.control[shape],
          outline,
          className,
        )}
      >
        {pickable && (
          <Select.Root value={currency} onValueChange={pick} disabled={disabled}>
            <Select.Trigger
              aria-label={`Currency, ${names.of(currency) ?? currency}`}
              className={merge(
                itemOutline,
                "d:f ai:c g:1 px:2 bw:0 bg:transparent c:label fw:500 fs:sm us:none c:p h:bg:well",
                PICKER[size],
                RADIUS.item[shape],
              )}
            >
              <Select.Value>{currency}</Select.Value>
              <Select.Icon className="d:f c:hint">
                <IconChevronDown className="w:4 h:4" />
              </Select.Icon>
            </Select.Trigger>
            <Select.Portal container={container}>
              <Select.Positioner sideOffset={8} align="start" alignItemWithTrigger={false} className="zi:50">
                <Select.Popup
                  className={`d:f fd:c o:h py:1 w:56 bg:raised bc:line bs-o:pop bw:1 tor:var(--transform-origin) tp:ot tdu:200 ttf:out-quart opening:o:0 wc:s opening:s:97 closing:o:0 closing:s:97 closing:tdu:100 closing:ttf:eo @prm:tp:none ${RADIUS.popup[shape]}`}
                >
                  <Select.List>
                    {currencies.map((code) => (
                      <Select.Item
                        key={code}
                        value={code}
                        className={(state) =>
                          `d:f ai:c g:2 h:8 px:2 mx:1 ${RADIUS.item[shape]} fs:sm us:none c:p c:label ${state.highlighted ? "bg:well" : "bg:transparent"}`
                        }
                      >
                        <span className="d:f ai:c w:4 h:4 fs:0">
                          <Select.ItemIndicator className="d:f ai:c">
                            <IconCheck className="w:4 h:4" />
                          </Select.ItemIndicator>
                        </span>
                        <Select.ItemText className="fw:500">{code}</Select.ItemText>
                        <span className="c:hint to:e o:h ws:nw">{names.of(code)}</span>
                      </Select.Item>
                    ))}
                  </Select.List>
                </Select.Popup>
              </Select.Positioner>
            </Select.Portal>
          </Select.Root>
        )}
        {pickable && (
          <span aria-hidden className="w:px h:5 ml:1 fs:0 bg:rim" />
        )}
        {!pickable && (
          <span className="d:f ai:c g:2 h:100% pl:3 pr:2 brw:1 bc:rim fw:500 fs:sm">
            {currency}
          </span>
        )}
        <Sign code={currency} locale={locale} />
        <NumberField.Group className="d:f f:1 min-w:0 h:100%">
          <NumberField.Input
            aria-invalid={error ? true : undefined}
            aria-label={label ? undefined : `Amount in ${names.of(currency) ?? currency}`}
            placeholder={(0).toFixed(digits)}
            onFocus={() => setTyping(true)}
            onBlur={() => setTyping(false)}
            className={`f:1 min-w:0 h:100% pl:2 pr:3 bw:0 bg:transparent c:label os:none fvn:tn ${INPUT_TEXT}`}
          />
        </NumberField.Group>
      </div>

      {error ? (
        <p role="alert" className="m:0 fs:xs c:red-7">
          {error}
        </p>
      ) : (
        description && <p className="m:0 fs:xs c:hint">{description}</p>
      )}
    </NumberField.Root>
  );
}
```

An amount with its currency, picked at the start of the field, its sign beside the value, grouped for the locale when the field is left.

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `label` | `string` | - | Text above the field, wired to it as its accessible name. |
| `description` | `string` | - | A line under the field. |
| `error` | `string` | - | Marks the field invalid and shows this under it in place of `description`. |
| `value` | `number` | - | The amount, controlled. Pair with `onValueChange`. |
| `defaultValue` | `number` | - | The amount on first render, uncontrolled. |
| `onValueChange` | `(value: number \| null) => void` | - | Called with the amount, or `null` when the field is cleared. |
| `currency` | `string` | - | The currency, as an ISO code, controlled. Pair with `onCurrencyChange`. |
| `defaultCurrency` | `string` | `"USD"` | The currency on first render, uncontrolled. |
| `onCurrencyChange` | `(currency: string) => void` | - | Called with the ISO code when another currency is picked. |
| `currencies` | `string[]` | - | The ISO codes the picker offers. One code hides the picker. |
| `locale` | `string` | `"en-US"` | Formats the grouping, the decimal mark and the currency names. |
| `size` | `"sm"` \| `"md"` \| `"lg"` | `"md"` | Height & width of the field. |
| `shape` | `"none"` \| `"rounded"` \| `"pill"` | - | Corner radius of the field and its popup. Inherits the nearest `Shape`. |
| `disabled` | `boolean` | `false` | Blocks the field and the picker and dims both. |
| `required` | `boolean` | `false` | Marks the field required and adds an asterisk to the label. |
| `className` | `string` | - | Extra classes on the field. `merge` folds them in last, so one here replaces the component's own class for the same utility. |
| `focus` | `boolean \| string` | `true` | The focus outline. `true` draws it, `false` removes it, and a string of Yumma CSS utilities restyles it. Removing it outright and putting nothing back fails WCAG 2.4.7. |
| `container` | `HTMLElement \| null` | - | The picker list rendering inside this element instead of the page, for a frame or a container that owns its own stacking context. |