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
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -69,4 +69,10 @@ setAddon({
});

/** @type {import("@fluentui/react-storybook-addon").FluentParameters} */
export const parameters = { layout: 'none', mode: 'vr-test' };
export const parameters = {
layershifter marked this conversation as resolved.
Show resolved Hide resolved
layout: 'none',
mode: 'vr-test',
reactStorybookAddon: {
disabledDecorators: ['AriaLive'],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { AriaLiveAnnouncer } from '@fluentui/react-aria';
import * as React from 'react';

layershifter marked this conversation as resolved.
Show resolved Hide resolved
import { AriaLiveAnnouncer } from '@fluentui/react-aria';
import { FluentStoryContext } from '../hooks';

export const withAriaLive = (Story: () => JSX.Element, context: FluentStoryContext) => {
const shouldDisable = context.parameters.reactStorybookAddon?.disabledDecorators?.includes('AriaLive');

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

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
Expand Up @@ -23,8 +23,14 @@ const getActiveFluentTheme = (globals: FluentGlobals) => {
export const withFluentProvider = (StoryFn: () => JSX.Element, context: FluentStoryContext) => {
const { globals, parameters } = context;
const { mode } = parameters;
const isVrTest = mode === 'vr-test';

const shouldDisable = parameters.reactStorybookAddon?.disabledDecorators?.includes('FluentProvider');

if (shouldDisable) {
return StoryFn();
}
layershifter marked this conversation as resolved.
Show resolved Hide resolved

const isVrTest = mode === 'vr-test';
const dir = parameters.dir ?? globals[DIR_ID] ?? 'ltr';
const globalTheme = getActiveFluentTheme(globals);
const paramTheme = findTheme(parameters.fluentTheme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { STRICT_MODE_ID } from '../constants';
import { FluentStoryContext } from '../hooks';

export const withReactStrictMode = (StoryFn: () => JSX.Element, context: FluentStoryContext) => {
const shouldDisable = context.parameters.reactStorybookAddon?.disabledDecorators?.includes('ReactStrictMode');

if (shouldDisable) {
return StoryFn();
}

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

return <StrictModeWrapper strictMode={isActive}>{StoryFn()}</StrictModeWrapper>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
Loading