Provider

Provider is the containing component that all other React Spectrum components are the children of. Used to set locale, theme, scale, toast position and provider, modal provider, and common props for children components. Providers can be nested.

installyarn add @react-spectrum/provider
version3.0.0
usageimport {Provider} from '@react-spectrum/provider'

Example#


<Provider isDisabled scale="large">
  <Button variant="primary">Button</Button>
</Provider>
<Provider isDisabled scale="large">
  <Button variant="primary">Button</Button>
</Provider>
<Provider
  isDisabled
  scale="large">
  <Button variant="primary">
    Button
  </Button>
</Provider>

Global settings#


Props such as colorScheme, locale, scale, and theme are applied to all children components, including nested Providers. The theme allows the definition of custom CSS variables for the colorScheme and scale prop values.

Color scheme and scale#

<Provider colorScheme="dark" scale="large">
  <Form margin="15px">
    <div>
      <TextField label="Email" />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div style={{'marginBottom': '15px'}}>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
<Provider colorScheme="dark" scale="large">
  <Form margin="15px">
    <div>
      <TextField label="Email" />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div style={{'marginBottom': '15px'}}>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
<Provider
  colorScheme="dark"
  scale="large">
  <Form margin="15px">
    <div>
      <TextField label="Email" />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div
      style={{
        marginBottom:
          '15px'
      }}>
      <Button variant="primary">
        Button
      </Button>
    </div>
  </Form>
</Provider>

Theme#

The Provider's theme is the CSS variables for colorScheme and scale values.

import {theme} from '@react-spectrum/theme-default';

<Provider theme={theme} colorScheme="dark" scale="large">
  <Form>
    <div>
      <TextField label="Email" />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
import {theme} from '@react-spectrum/theme-default';

<Provider theme={theme} colorScheme="dark" scale="large">
  <Form>
    <div>
      <TextField label="Email" />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
import {theme} from '@react-spectrum/theme-default';

<Provider
  theme={theme}
  colorScheme="dark"
  scale="large">
  <Form>
    <div>
      <TextField label="Email" />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div>
      <Button variant="primary">
        Button
      </Button>
    </div>
  </Form>
</Provider>

Locale#

The locale prop can be set to specify a locale for children of the Provider. The default for the locale prop is based on the system/browser settings. The locale prop does not support nesting. It must be the same across the page and all lang/dir attributes in the dom must match. See this list of supported locales.

<Provider locale="en-US">
  <Form necessityIndicator="label">
    <div>
      <TextField label="email" />
    </div>
    <div>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
<Provider locale="en-US">
  <Form necessityIndicator="label">
    <div>
      <TextField label="email" />
    </div>
    <div>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
<Provider locale="en-US">
  <Form necessityIndicator="label">
    <div>
      <TextField label="email" />
    </div>
    <div>
      <Button variant="primary">
        Button
      </Button>
    </div>
  </Form>
</Provider>

Provider nesting#


Providers can be nested. The parent Provider passes its props down to the children Providers.

<Provider isDisabled scale="large">
  <Button variant="primary">Button</Button>
  <Provider isQuiet>
    <Button variant="primary">Button</Button>
  </Provider>
</Provider>
<Provider isDisabled scale="large">
  <Button variant="primary">Button</Button>
  <Provider isQuiet>
    <Button variant="primary">Button</Button>
  </Provider>
</Provider>
<Provider
  isDisabled
  scale="large">
  <Button variant="primary">
    Button
  </Button>
  <Provider isQuiet>
    <Button variant="primary">
      Button
    </Button>
  </Provider>
</Provider>

Children props#


As a convenience, Provider supports passing common props to all components within it. For example, an entire group of components could be disabled by setting isDisabled in one place rather than on each individual element. These props are isDisabled, isEmphasized, isQuiet, isReadOnly, isRequired, and validationState.

isEmphasized#

<Provider isEmphasized>
  <Form>
    <div>
      <TextField label="Email" placeholder="info@adobe.com" />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
<Provider isEmphasized>
  <Form>
    <div>
      <TextField
        label="Email"
        placeholder="info@adobe.com"
      />
    </div>
    <Checkbox>Checkbox Label</Checkbox>
    <div>
      <Button variant="primary">Button</Button>
    </div>
  </Form>
</Provider>
<Provider isEmphasized>
  <Form>
    <div>
      <TextField
        label="Email"
        placeholder="info@adobe.com"
      />
    </div>
    <Checkbox>
      Checkbox Label
    </Checkbox>
    <div>
      <Button variant="primary">
        Button
      </Button>
    </div>
  </Form>
</Provider>

Props#


NameTypeDefaultDescription
childrenReactNode—The children components to receive Provider props and context.
themeTheme—Theme scoped to this provider and its children components. Sets the CSS variables for scale and color scheme values.
colorSchemeColorScheme—Color scheme scoped to this provider and its children components. Defaults to the color scheme set by the OS.
defaultColorSchemeColorScheme'light'If there is not an OS/browser color scheme this is the default.
scaleScale—Spectrum scale scoped to this provider and its children components. By default this is selected based on touch or mouse pointer type of the OS.
localestring'en-US'Locale (language specific format) of this provider and its children. Using the format primary-region, ex. en-US, fr-CA, ar-AE.
isQuietboolean—Whether children components should be displayed with the quiet style.
isEmphasizedboolean—Whether children components should be displayed with the emphasized style.
isDisabledboolean—Whether children components should be disabled.
isRequiredboolean—Whether children components should be displayed with the required style.
isReadOnlyboolean—Whether children components should be read only.
validationStateValidationState—Whether children components should be displayed with the validation state style.
Layout
NameTypeDefaultDescription
flexstring | number | boolean—When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrownumber—When used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinknumber—When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisnumber | string—When used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelf'auto' | 'normal' | 'start' | 'end' | 'center' | 'flex-start' | 'flex-end' | 'self-start' | 'self-end' | 'stretch'—Overrides the alignItems property of a flex or grid container. See MDN.
justifySelf'auto' | 'normal' | 'start' | 'end' | 'flex-start' | 'flex-end' | 'self-start' | 'self-end' | 'center' | 'left' | 'right' | 'stretch'—Specifies how the element is justified inside a flex or grid container. See MDN.
ordernumber—The layout order for the element within a flex or grid container. See MDN.
gridAreastring—When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnstring—When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowstring—When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartstring—When used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndstring—When used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartstring—When used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndstring—When used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginDimensionValue—The margin for all four sides of the element. See MDN.
marginTopDimensionValue—The margin for the top side of the element. See MDN.
marginBottomDimensionValue—The margin for the bottom side of the element. See MDN.
marginStartDimensionValue—The margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndDimensionValue—The margin for the logical end side of an element, depending on layout direction. See MDN.
marginXDimensionValue—The margin for both the left and right sides of the element. See MDN.
marginYDimensionValue—The margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthDimensionValue—The width of the element. See MDN.
minWidthDimensionValue—The minimum width of the element. See MDN.
maxWidthDimensionValue—The maximum width of the element. See MDN.
heightDimensionValue—The height of the element. See MDN.
minHeightDimensionValue—The minimum height of the element. See MDN.
maxHeightDimensionValue—The maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
position'static' | 'relative' | 'absolute' | 'fixed' | 'sticky'—Specifies how the element is positioned. See MDN.
topDimensionValue—The top position for the element. See MDN.
bottomDimensionValue—The bottom position for the element. See MDN.
leftDimensionValue—The left position for the element. See MDN. Consider using start instead for RTL support.
rightDimensionValue—The right position for the element. See MDN. Consider using start instead for RTL support.
startDimensionValue—The logical start position for the element, depending on layout direction. See MDN.
endDimensionValue—The logical end position for the element, depending on layout direction. See MDN.
zIndexnumber—The stacking order for the element. See MDN.
isHiddenboolean—Hides the element.
Accessibility
NameTypeDefaultDescription
idstring—The element's unique identifier. See MDN.
Advanced
NameTypeDefaultDescription
UNSAFE_classNamestring—Sets the CSS className for the element. Only use as a last resort. Use style props instead.
UNSAFE_styleCSSProperties—Sets inline style for the element. Only use as a last resort. Use style props instead.