NumberValue
Locale-aware number with prefix and suffix
Usage
import {NumberValue} from "@peakui-pro/react";Anatomy
Keep parts as direct children: a part inside a fragment or wrapper is not repositioned.
import {NumberValue} from "@peakui-pro/react";
// Rendered as: prefix, the formatted value, then every other child in the order it was written.
// A Prefix inside a fragment or another component is not recognised and renders after the value.
export default () => (
<NumberValue value={1234.5}>
<NumberValue.Prefix />
<NumberValue.Suffix />
</NumberValue>
);Examples
Currency
currency is required when style is "currency".
Percent
The value is a fraction: 0.033 renders as 3.3%.
Compact
Sign Display
With Prefix Suffix
The prefix precedes the value whatever order the parts are written in.
Tabular Nums
Stack figures in a column: the digits are tabular, so they stay aligned.
Format Options
formatOptions goes straight to Intl.NumberFormat and overrides the props above.
Customization
Tailwind CSS
<NumberValue className="text-3xl font-semibold" value={1234.5} />Global CSS
@layer components {
.number-value {
--number-value-suffix-gap: calc(var(--spacing) * 2);
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The NumberValue component uses these CSS classes (view source styles):
Base Classes
.number-value- The root; an inline-flex row on one baseline, with tabular figures
Element Classes
.number-value__value- The formatted number.number-value__prefix- Text before the value; inherits the figure's colour, weight and size.number-value__suffix- Text after the value; muted, regular weight, set off by a margin
Slots
[data-slot="number-value"]- The root element[data-slot="number-value-value"]- The formatted number[data-slot="number-value-prefix"]- The prefix[data-slot="number-value-suffix"]- The suffix
CSS Variables
| Variable | Purpose |
|---|---|
--number-value-suffix-gap | Space between the value and the suffix |
API Reference
NumberValue
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | - | Required. The number to format |
style | 'decimal' | 'currency' | 'percent' | 'unit' | 'decimal' | The number style — Intl.NumberFormat's option, not inline CSS. The native style attribute is not accepted |
currency | string | - | Currency code such as 'USD'; required when style is 'currency' |
unit | string | - | Unit such as 'kilometer'; required when style is 'unit' |
notation | 'standard' | 'compact' | 'scientific' | 'engineering' | 'standard' | Notation style |
signDisplay | 'auto' | 'always' | 'exceptZero' | 'never' | - | When the sign is shown |
minimumFractionDigits | number | - | Fewest fraction digits to show |
maximumFractionDigits | number | - | Most fraction digits to show |
locale | string | - | Locale to format in; without it, the nearest I18nProvider's locale, and with no provider the environment's — pin one of the two when a server renders the page, or the server and the browser produce two different strings for the same markup |
formatOptions | Intl.NumberFormatOptions | - | Options handed to Intl.NumberFormat; they override every convenience prop above |
children | ReactNode | ((formatted: string) => ReactNode) | - | The parts, and anything else to render beside the figure. As a function it receives the formatted string and its result fills the value element |
className | string | - | Additional classes |
render | DOMRenderFunction<'span', undefined> | - | Replaces the rendered <span> with your own element |
...rest | ComponentProps<'span'> | - | Every other <span> attribute, except style and children |
NumberValue.Prefix
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Text before the value |
className | string | - | Additional classes |
render | DOMRenderFunction<E, undefined> | - | Replaces the rendered <span> with your own element; E defaults to 'span' |
...rest | ComponentProps<'span'> | - | Native <span> attributes |
NumberValue.Suffix
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Text after the value |
className | string | - | Additional classes |
render | DOMRenderFunction<E, undefined> | - | Replaces the rendered <span> with your own element; E defaults to 'span' |
...rest | ComponentProps<'span'> | - | Native <span> attributes |
Render Props
| Prop | Type | Description |
|---|---|---|
children | (formatted: string) => ReactNode | Receives the formatted string; the result fills .number-value__value |