Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-storybook-addon): transform decorator to function in withAriaLive() #32011

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion apps/vr-tests-react-components/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -69,4 +69,10 @@ setAddon({
});

/** @type {import("@fluentui/react-storybook-addon").FluentParameters} */
export const parameters = { layout: 'none', mode: 'vr-test' };
export const parameters = {
layout: 'none',
mode: 'vr-test',
reactStorybookAddon: {
disabledDecorators: ['AriaLive'],
},
};
Original file line number Diff line number Diff line change
@@ -31,6 +31,10 @@ export interface FluentParameters extends Parameters_2 {
fluentTheme?: ThemeIds;
// (undocumented)
mode?: 'default' | 'vr-test';
// (undocumented)
reactStorybookAddon?: {
disabledDecorators: ['AriaLive' | 'FluentProvider' | 'ReactStrictMode'];
};
}

// @public (undocumented)
@@ -46,6 +50,9 @@ export function parameters(options?: FluentParameters): {
dir: string;
fluentTheme: string;
mode: string;
reactStorybookAddon?: {
disabledDecorators: ["AriaLive" | "FluentProvider" | "ReactStrictMode"];
} | undefined;
fileName?: string | undefined;
options?: OptionsParameter | undefined;
layout?: "centered" | "fullscreen" | "padded" | "none" | undefined;
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { AriaLiveAnnouncer } from '@fluentui/react-aria';
import * as React from 'react';

import { AriaLiveAnnouncer } from '@fluentui/react-aria';
import { FluentStoryContext } from '../hooks';
import { isDecoratorDisabled } from '../utils/isDecoratorDisabled';

export const withAriaLive = (Story: () => JSX.Element, context: FluentStoryContext) => {
if (isDecoratorDisabled(context, 'AriaLive')) {
return Story();
}

export const withAriaLive = (StoryFn: () => JSX.Element) => {
return <AriaLiveWrapper>{StoryFn()}</AriaLiveWrapper>;
return (
<AriaLiveWrapper>
<Story />
</AriaLiveWrapper>
);
};

const AriaLiveWrapper: React.FC<{ children: React.ReactNode }> = props => {
const [mounted, setMounted] = React.useState(false);

React.useEffect(() => {
// The AriaLiveAnnouncer appends an element to DOM in an effect
// Trigger an extra renderer to make sure that doc examples that need to announce on mount can do so
setMounted(true);
}, []);

return <AriaLiveAnnouncer>{mounted && props.children}</AriaLiveAnnouncer>;
};
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import { Theme } from '@fluentui/react-theme';
import { themes, defaultTheme, ThemeIds } from '../theme';
import { DIR_ID, THEME_ID } from '../constants';
import { FluentGlobals, FluentStoryContext } from '../hooks';
import { isDecoratorDisabled } from '../utils/isDecoratorDisabled';

const findTheme = (themeId?: ThemeIds) => {
if (!themeId) {
@@ -23,8 +24,12 @@ const getActiveFluentTheme = (globals: FluentGlobals) => {
export const withFluentProvider = (StoryFn: () => JSX.Element, context: FluentStoryContext) => {
const { globals, parameters } = context;
const { mode } = parameters;
const isVrTest = mode === 'vr-test';

if (isDecoratorDisabled(context, 'FluentProvider')) {
return StoryFn();
}

const isVrTest = mode === 'vr-test';
const dir = parameters.dir ?? globals[DIR_ID] ?? 'ltr';
const globalTheme = getActiveFluentTheme(globals);
const paramTheme = findTheme(parameters.fluentTheme);
Original file line number Diff line number Diff line change
@@ -2,8 +2,13 @@ import * as React from 'react';

import { STRICT_MODE_ID } from '../constants';
import { FluentStoryContext } from '../hooks';
import { isDecoratorDisabled } from '../utils/isDecoratorDisabled';

export const withReactStrictMode = (StoryFn: () => JSX.Element, context: FluentStoryContext) => {
if (isDecoratorDisabled(context, 'ReactStrictMode')) {
return StoryFn();
}

const isActive = context.globals[STRICT_MODE_ID] ?? false;

return <StrictModeWrapper strictMode={isActive}>{StoryFn()}</StrictModeWrapper>;
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ export interface FluentParameters extends Parameters {
dir?: 'ltr' | 'rtl';
fluentTheme?: ThemeIds;
mode?: 'default' | 'vr-test';
reactStorybookAddon?: { disabledDecorators: ['AriaLive' | 'FluentProvider' | 'ReactStrictMode'] };
}

export function useGlobals(): [FluentGlobals, (newGlobals: FluentGlobals) => void] {
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { FluentParameters, FluentStoryContext } from '../hooks';

type DecoratorName = NonNullable<FluentParameters['reactStorybookAddon']>['disabledDecorators'][number];

export function isDecoratorDisabled(context: FluentStoryContext, decoratorName: DecoratorName): boolean {
return context.parameters.reactStorybookAddon?.disabledDecorators?.includes(decoratorName) ?? false;
}