Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Format for Solid

Show file sizes, currency, percents, compact numbers, and relative times with the Intl API. For Solid, when values should read right in the user's locale.

UI library: Ark UI
Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
Use email insteadNo credit card

Usage

import { FormatByte } from "@/components/ui/format";
<div class="flex flex-col gap-1">
  <span class="text-muted-foreground text-sm">File size</span>
  <span class="font-semibold text-2xl text-foreground tabular-nums tracking-tight">
    <FormatByte value={120_000} />
  </span>
</div>

Demos

Byte

Preview width

Byte Unit Display

Preview width

Byte Unit System

Preview width

Number

Preview width

Number Compact

Preview width

Number Currency

Preview width

Number Percent

Preview width

Relative Time

Preview width

Relative Time Short

Preview width

FAQ

Which formatters does Format give me?

FormatNumber for plain numbers, currency, percents and compact numbers, FormatByte for file sizes, and FormatRelativeTime for "3 hours ago". Each renders a span, so you can style it with a class like any text.

Which locale does Format use?

The default locale of the environment that renders it, through the Intl API. It does not take a locale prop. On a server that is the server's locale, in the browser the user's, so wrap it in your own component if you need to pin one.

How do I show a price or a percent?

FormatNumber takes the options of Intl.NumberFormat as props: style="currency" with currency="USD", style="percent", or notation="compact". The Currency, Percent and Compact demos show each.

Does FormatByte use 1000 or 1024?

1000 by default (unitSystem="decimal"). Set unitSystem="binary" to divide by 1024. The labels stay kilobyte, megabyte and so on, up to terabyte, and unitDisplay picks short, long or narrow.

Does FormatRelativeTime update as time passes?

No. It computes the distance from now each time it renders, in seconds, minutes, hours, days, months or years, and does not tick. Re-render it, for example from a timer, to refresh it. style picks long, short or narrow.

Can FormatRelativeTime cause a hydration warning?

It can. The server and the browser each read their own clock and locale, so the text may differ between the HTML and the first client render. Render it after mount, for example inside ClientOnly, when the value is close to now.

How do I install Format in Solid?

Run the command in the Installation section on this page. One file, format, is added, with no dependency besides the platform's Intl API.

API Reference

FormatByte

data-slot="format-byte"

PropType
valuenumber

The byte size to format.

unit?"bit" | "byte"

The unit granularity to display.

Type
"bit" | "byte"
unitDisplay?"long" | "short" | "narrow"

The unit display.

Type
"long" | "short" | "narrow"
unitSystem?"decimal" | "binary"

The unit system to use for formatting.

Type
"decimal" | "binary"

FormatNumber

data-slot="format-number"

PropType
valuenumber

The number to format.

FormatRelativeTime

data-slot="format-relative-time"

PropType
valueDate

The date to format.