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-rc.2
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.

<Provider locale="ar-AE">
  <Form necessityIndicator="label">
    <div>
      <TextField label="البريد الإلكتروني" />
    </div>
    <div>
      <Button variant="primary">حفظ</Button>
    </div>
  </Form>
</Provider>
<Provider locale="ar-AE">
  <Form necessityIndicator="label">
    <div>
      <TextField label="البريد الإلكتروني" />
    </div>
    <div>
      <Button variant="primary">حفظ</Button>
    </div>
  </Form>
</Provider>
<Provider locale="ar-AE">
  <Form necessityIndicator="label">
    <div>
      <TextField label="البريد الإلكتروني" />
    </div>
    <div>
      <Button variant="primary">
        حفظ
      </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
childrenReactNodeThe children components to receive Provider props and context.
themeThemeTheme scoped to this provider and its children components. Sets the CSS variables for scale and color scheme values.
colorSchemeColorSchemeColor 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.
scaleScaleSpectrum 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.
isQuietbooleanWhether children components should be displayed with the quiet style.
isEmphasizedbooleanWhether children components should be displayed with the emphasized style.
isDisabledbooleanWhether children components should be disabled.
isRequiredbooleanWhether children components should be displayed with the required style.
isReadOnlybooleanWhether children components should be read only.
validationStateValidationStateWhether children components should be displayed with the validation state style.
Layout
NameTypeDefaultDescription
flexstringnumberbooleanWhen used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrownumberWhen used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinknumberWhen used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisnumberstringWhen 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.
ordernumberThe layout order for the element within a flex or grid container. See MDN.
gridAreastringWhen used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnstringWhen used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowstringWhen used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartstringWhen used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndstringWhen used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartstringWhen used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndstringWhen used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginDimensionValueThe margin for all four sides of the element. See MDN.
marginTopDimensionValueThe margin for the top side of the element. See MDN.
marginBottomDimensionValueThe margin for the bottom side of the element. See MDN.
marginStartDimensionValueThe margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndDimensionValueThe margin for the logical end side of an element, depending on layout direction. See MDN.
marginXDimensionValueThe margin for both the left and right sides of the element. See MDN.
marginYDimensionValueThe margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthDimensionValueThe width of the element. See MDN.
minWidthDimensionValueThe minimum width of the element. See MDN.
maxWidthDimensionValueThe maximum width of the element. See MDN.
heightDimensionValueThe height of the element. See MDN.
minHeightDimensionValueThe minimum height of the element. See MDN.
maxHeightDimensionValueThe maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
position'static' | 'relative' | 'absolute' | 'fixed' | 'sticky'Specifies how the element is positioned. See MDN.
topDimensionValueThe top position for the element. See MDN.
bottomDimensionValueThe bottom position for the element. See MDN.
leftDimensionValueThe left position for the element. See MDN. Consider using start instead for RTL support.
rightDimensionValueThe right position for the element. See MDN. Consider using start instead for RTL support.
startDimensionValueThe logical start position for the element, depending on layout direction. See MDN.
endDimensionValueThe logical end position for the element, depending on layout direction. See MDN.
zIndexnumberThe stacking order for the element. See MDN.
isHiddenbooleanHides the element.
Accessibility
NameTypeDefaultDescription
idstring
Advanced
NameTypeDefaultDescription
UNSAFE_classNamestringSets the CSS className for the element. Only use as a last resort. Use style props instead.
UNSAFE_styleCSSPropertiesSets inline style for the element. Only use as a last resort. Use style props instead.