|
| 1 | +import {Layout} from '../../src/Layout'; |
| 2 | +import {InstallCommand} from '../../src/InstallCommand'; |
| 3 | +import {BundlerSwitcher, BundlerSwitcherItem} from '../../src/BundlerSwitcher'; |
| 4 | +export default Layout; |
| 5 | +import {SegmentedControl, SegmentedControlItem} from '@react-spectrum/s2'; |
| 6 | + |
| 7 | +export const section = 'Getting started'; |
| 8 | +export const description = 'Getting started with React Spectrum'; |
| 9 | +import docs from 'docs:@react-spectrum/s2'; |
| 10 | + |
| 11 | +# Provider |
| 12 | + |
| 13 | +<PageDescription>{docs.exports.Provider.description}</PageDescription> |
| 14 | + |
| 15 | +## Example |
| 16 | + |
| 17 | +```tsx render |
| 18 | +'use client'; |
| 19 | +import {Provider} from '@react-spectrum/s2'; |
| 20 | +import {Button} from '@react-spectrum/s2'; |
| 21 | + |
| 22 | +function App() { |
| 23 | + return ( |
| 24 | + <Provider background='base'> |
| 25 | + <Button variant="accent"> |
| 26 | + Hello React Spectrum! |
| 27 | + </Button> |
| 28 | + </Provider> |
| 29 | + ); |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Color Schemes |
| 34 | +By default, React Spectrum follows the operating system color scheme setting, supporting both light and dark mode. The colorScheme prop can be set on `<Provider>` to force the app to always render in a certain color scheme. |
| 35 | + |
| 36 | +```tsx render |
| 37 | +'use client'; |
| 38 | +import {Provider} from '@react-spectrum/s2'; |
| 39 | +import {ActionButton} from '@react-spectrum/s2'; |
| 40 | +import {style} from '@react-spectrum/s2/style' with {type: 'macro'}; |
| 41 | + |
| 42 | +<div className={style({display: 'flex', gap: 12})}> |
| 43 | + <Provider colorScheme="light"> |
| 44 | + <div className={style({backgroundColor: 'base', padding: 12})}> |
| 45 | + <ActionButton>I'm a light button</ActionButton> |
| 46 | + </div> |
| 47 | + </Provider> |
| 48 | + <Provider colorScheme="dark"> |
| 49 | + <div className={style({backgroundColor: 'elevated', padding: 12})}> |
| 50 | + <ActionButton>I'm a dark button</ActionButton> |
| 51 | + </div> |
| 52 | + </Provider> |
| 53 | +</div> |
| 54 | +``` |
| 55 | + |
| 56 | +## Locales |
| 57 | +By default, React Spectrum chooses the locale matching the user’s browser/operating system language, but this can be overridden with the locale prop if you have an application specific setting. This prop accepts a [BCP 47](https://www.ietf.org/rfc/bcp/bcp47.txt) language code. A list of supported locales is available [here](Concepts.html#supported-locales) |
| 58 | + |
| 59 | +```tsx |
| 60 | +<Provider locale="en-US"> |
| 61 | + <YourApp /> |
| 62 | +</Provider> |
| 63 | +``` |
| 64 | + |
| 65 | +{/* ### Breakpoints */} |
| 66 | +{/* TODO: Link to style macro docs about breakpoints */} |
| 67 | + |
| 68 | +## Client side routing |
| 69 | + |
| 70 | +The Provider component accepts an optional `router` prop. This enables React Spectrum components that render links to perform client side navigation using your application or framework's client side router. See the client side routing guide for details on how to set this up. |
| 71 | + |
| 72 | +```tsx |
| 73 | +let navigate = useNavigateFromYourRouter(); |
| 74 | + |
| 75 | +<Provider router={{navigate}}> |
| 76 | + <YourApp /> |
| 77 | +</Provider> |
| 78 | +``` |
| 79 | + |
| 80 | +## Server side rendering |
| 81 | + |
| 82 | +When using SSR, the `<Provider>` component can be rendered as the root `<html>` element. The `locale` prop should always be specified to avoid hydration errors. |
| 83 | + |
| 84 | +```tsx |
| 85 | +<Provider elementType="html" locale="en-US"> |
| 86 | + <YourApp /> |
| 87 | +</Provider> |
| 88 | +``` |
| 89 | + |
| 90 | +## Props |
| 91 | + |
| 92 | +<PropTable component={docs.exports.Provider} links={docs.links} /> |
0 commit comments