Stele - Currency
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 } = propsreturn (// 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 <Currencycurrency="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 <Currencyvalue={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 hookfunction ConsumingComponent(props) {const currency = useCurrency()return (<div>currency from context hook: {currency} {/* USD */}</div>)}// or access it via a higher order componentconst WrappedComponent = withCurrency(function InnerComponent(props) {const { currency } = propsreturn (<div>currency from prop: {currency} {/* USD */}</div>)})