PeakUIPro

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

VariablePurpose
--number-value-suffix-gapSpace between the value and the suffix

API Reference

NumberValue

PropTypeDefaultDescription
valuenumber-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
currencystring-Currency code such as 'USD'; required when style is 'currency'
unitstring-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
minimumFractionDigitsnumber-Fewest fraction digits to show
maximumFractionDigitsnumber-Most fraction digits to show
localestring-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
formatOptionsIntl.NumberFormatOptions-Options handed to Intl.NumberFormat; they override every convenience prop above
childrenReactNode | ((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
classNamestring-Additional classes
renderDOMRenderFunction<'span', undefined>-Replaces the rendered <span> with your own element
...restComponentProps<'span'>-Every other <span> attribute, except style and children

NumberValue.Prefix

PropTypeDefaultDescription
childrenReactNode-Text before the value
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered <span> with your own element; E defaults to 'span'
...restComponentProps<'span'>-Native <span> attributes

NumberValue.Suffix

PropTypeDefaultDescription
childrenReactNode-Text after the value
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered <span> with your own element; E defaults to 'span'
...restComponentProps<'span'>-Native <span> attributes

Render Props

PropTypeDescription
children(formatted: string) => ReactNodeReceives the formatted string; the result fills .number-value__value

On this page