packages/react-components/react-storybook-addon/README.md
Storybook addon for Fluent UI React
Exports types and utilities to set and consume the correct Fluent theme in Storybook globals. Here's an example picker that sets the Fluent theme in Storybook globals.
import * as React from 'react';
import { themes, setGlobalTheme, FluentStoryContext, THEME_ID } from '@fluentui/react-storybook-addon';
// Storybook context which can be accessed, for example, in decorators
// https://storybook.js.org/docs/react/writing-stories/decorators#context-for-mocking
export const ThemePicker: React.FC<{ context: FluentStoryContext }> = ({ context }) => {
const handleChange = e => {
setGlobalTheme(e.target.value);
};
const selectedTheme = themes.find(theme => theme.id === context.globals[THEME_ID]);
return (
<select onChange={handleChange}>
{themes.map(theme => (
<option selected={selectedTheme.id === theme.id} value={theme.id}>
{theme.label}
</option>
))}
</select>
);
};
This presets uses custom docs container and page for unified FluentUI experience including:
Slot api renderingTo install the addon, run:
yarn add -D @fluentui/react-storybook-addon
Add the following content to .storybook/main.js:
module.exports = {
addons: ['@fluentui/react-storybook-addon'],
};
Run the inner loop from the monorepo root with yarn workspace @fluentui/react-storybook-addon storybook.
prestorybook script that compiles the addon implementation with all of its direct dependencies within the monorepo, so it can be consumed by the local Storybook.After making any changes to the implementation, manually run yarn workspace @fluentui/react-storybook-addon build to reflect those changes in your local Storybook.
Three custom optional parameters can be set to alter the behavior of the addon:
dir - Determines whether to render the story in ltr or rtl mode. Default is undefined.fluentTheme - Determines whether to render the story theme in web-light, web-dark, teams-high-contrast, teams-dark, or teams-light. Setting this parameter will disable the ability to dynamically change the theme within the story canvas or doc.mode - When set to vr-test, this removes the injected padding and background theme that's automatically applied from the rendered story. Default is default.import { FluentParameters, parameters } from '@fluentui/react-storybook-addon';
import { Button } from '@fluentui/react-components';
export const Button = () => <Button>Hello World</Button>;
export const ButtonDarkMode = {
render: Button,
parameters: { fluentTheme: 'web-dark' } as FluentParameters, // Story renders in Dark mode.
};
export const ButtonHighContrast = {
render: Button,
parameters: { fluentTheme: 'teams-high-contrast', mode: 'vr-test' } as FluentParameters, // Story renders in High Contrast mode without injected padding and background style.
};
export const ButtonRTL = {
render: Button,
// Parameters identity function will have all TS type annotations built in for intellisense.
parameters: parameters({ fluentTheme: 'web-light', dir: 'rtl', mode: 'vr-test' }), // Story renders in RTL, Web light mode and without injected padding and background style.
};