Stele - Currency

Components
CurrencyBasic UsageExample messagePropsUsing Currency contextPlaygroundDates and TimesNumbersPluralSelectSelect Ordinal
HomeHow Stele Works
How To
LicenseTechdebt

Currency

Similar to numbers, how currency is formatted depends on the locale. Stele uses the browser's Intl.NumberFormat API to ensure formatting matches what users expect to see. The <Currency> component accepts as props any options you would otherwise pass to Intl.Numberformat as well as some more convenient formatting props (see below).

Basic Usage

import * as React from 'react'
import { CurrencyProvider, Currency } from '@patreon/stele'
function MyComponent(props) {
const { myPrice } = props
return (
// wrap a top level component with a <CurrencyProvider> to provide
// that currency via context to descendent components
<CurrencyProvider value="USD">
<Text intl>
<p>
The price in the US is <Currency value={myPrice} />
</p>
<p>
{/* override currency directly via a prop */}
In the UK it will cost you <Currency
currency="GBP"
value={myPrice}
/>
</p>
<p>
{/* set a new currency context with another provider */}
<CurrencyProvider value="EUR">
{/* override format for a specific locale */}
The price in the EU is <Currency
value={myPrice}
locale="de-DE"
/>
</CurrencyProvider>
</p>
</Text>
</CurrencyProvider>
)
}

Example message

The price in the US is { myPrice, number, currency }

Props

value
Number
required
currency
Currencycode | undefined
numSubunits
Number | undefined
alwaysShowDollarName
Boolean | undefined
locale
String | undefined
truncateDecimal
Boolean | undefined
postFormatHook
Postcurrencyformatfunction | undefined
notation
Notation | undefined
compactDisplay
Compactdisplay | undefined
localeMatcher
String | undefined
style
String | undefined
currencyDisplay
String | undefined
useGrouping
Boolean | undefined
minimumIntegerDigits
Number | undefined
minimumFractionDigits
Number | undefined
maximumFractionDigits
Number | undefined
minimumSignificantDigits
Number | undefined
maximumSignificantDigits
Number | undefined

Using Currency context

import * as React from 'react'
import { CurrencyProvider, useCurrency } from '@patreon/stele'
function ParentComponent(props) {
return (
<CurrencyProvider value="USD">
<ConsumingComponent />
<WrappedComponent />
</CurrencyProvider>
)
}
// access the currently set currency from context with a hook
function ConsumingComponent(props) {
const currency = useCurrency()
return (
<div>
currency from context hook: {currency} {/* USD */}
</div>
)
}
// or access it via a higher order component
const WrappedComponent = withCurrency(function InnerComponent(props) {
const { currency } = props
return (
<div>
currency from prop: {currency} {/* USD */}
</div>
)
})

Playground

$0.05

© Patreon