import * as React from 'react'; import { NavLink } from 'react-router-dom'; import { Header, Provider, createComponent, ComponentSlotStyle, ComponentVariablesInput, ReactChildren, } from '@fluentui/react-northstar'; import DocPage from '../components/DocPage/DocPage'; import ExampleSnippet from '../components/ExampleSnippet/ExampleSnippet'; import GuidesNavigationFooter from '../components/GuidesNavigationFooter'; interface StyledButtonProps { className?: string; styles?: ComponentSlotStyle; variables?: ComponentVariablesInput; children?: ReactChildren; } const StyledButton = createComponent({ displayName: 'StyledButton', render({ config, children }) { const { classes } = config; return ; }, }); export default () => (

You can use your own components as part of the Fluent UI's styling and theming mechanisms. In order for all theming aspects to be available to your custom components, you should use the createComponent{' '} function, provided by the Fluent UI library.

Let's take a look into one simple example of using the createComponent function for adapting your custom component to the Fluent UI's styling and theming mechanisms.

{ const { classes } = config return } }) `} />

Let's go step by step throughout all bits of the createComponent method.

The first argument to the createComponent config's param is the is the displayName, which value might be used as key to define component's styles and variables in theme, exactly the same way how it might be done for any first-class Fluent UI component.

({ backgroundColor: siteVariables.colors.brand[600], color: variables.color, }), }, }, }} > Provider styled button `} />

The second argument of the createComponent config param is the render method. This is the place where where you might link Fluent UI bits with your custom component - e.g. by simply passing them as props. This render method will be invoked with the following parameters:

  • config - the object containing the evaluated theming props (classes/styles, accessibility and rtl).
  • ...props - all user-defined props.

We already saw how the Provider can define some stylings and variables for the custom components. Next, we will take a look into several examples of how the user can further customize styles and variables of these components, the same way they would do with the Fluent UI components.

Example 1. Using styles property } /> ( Inline styled button )} />

The same can be achieved with adding styles in the componentStyles part of the theme in the Provider.

( Inline styled button )} />

For more advanced theming scenarios, please take a look in the Styles section on the{' '} Theming guide.

Example 2. Using variables property } />

Let's consider that the following theme was passed to the Provider.

({ color: variables.color }), }, }, }} > ... `} />

Then we can use the variables prop for changing the color inside the StyledButton.

Inline styled button `} render={() => ( ({ color: (variables as any).color, }), }, }, }} > Inline styled button )} />

The alternative approach with defining componentVariables inside the theme would like like this:

({ color: variables.color }), }, }, }} > ... `} />

For more advanced theming scenarios, please take a look in the Variables section on the{' '} Theming guide.

Example 3. Using accessibility property } />

It is possible to define accessibility behavior of the created component - in order to do that the{' '} accessibility prop should be defined as part of defaultProps.

In the following example buttonBehavior is provided to StyledButton that results in accessibility attributes being evaluated for the component - and those, subsequently, are consumed in the{' '} render function:

{ const { classes, accessibility } = config return } }) `} />

To get more details on Accessibility support in Fluent UI refer to the{' '} Accessibility guide section.

);