- Notifications
You must be signed in to change notification settings - Fork1k
fix: add type-safety for Storybook preview.jsx config file#14671
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,155 @@ | ||
// @ts-check | ||
/** | ||
* @file Defines the main configuration file for all of our Storybook tests. | ||
* This file must be a JSX/JS file, but we can at least add some type safety via | ||
* the ts-check directive. | ||
* @see {@link https://storybook.js.org/docs/configure#configure-story-rendering} | ||
* | ||
* @typedef {import("react").ReactElement} ReactElement | ||
* @typedef {import("react").PropsWithChildren} PropsWithChildren | ||
* @typedef {import("react").FC<PropsWithChildren>} FC | ||
* | ||
* @typedef {import("@storybook/react").StoryContext} StoryContext | ||
* @typedef {import("@storybook/react").Preview} Preview | ||
* | ||
* @typedef {(Story: FC, Context: StoryContext) => React.JSX.Element} Decorator A | ||
* Storybook decorator function used to inject baseline data dependencies into | ||
* our React components during testing. | ||
*/ | ||
import { ThemeProvider as EmotionThemeProvider } from "@emotion/react"; | ||
import CssBaseline from "@mui/material/CssBaseline"; | ||
import { | ||
ThemeProvider as MuiThemeProvider, | ||
StyledEngineProvider, | ||
} from "@mui/material/styles"; | ||
import { DecoratorHelpers } from "@storybook/addon-themes"; | ||
import isChromatic from "chromatic/isChromatic"; | ||
import React, { StrictMode } from "react"; | ||
import { HelmetProvider } from "react-helmet-async"; | ||
import { parseQueryArgs, QueryClient, QueryClientProvider } from "react-query"; | ||
import { withRouter } from "storybook-addon-remix-react-router"; | ||
import "theme/globalFonts"; | ||
importthemes from "../src/theme"; | ||
DecoratorHelpers.initializeThemeState(Object.keys(themes), "dark"); | ||
/** @type {readonly Decorator[]} */ | ||
export const decorators = [withRouter, withQuery, withHelmet, withTheme]; | ||
/** @type {Preview["parameters"]} */ | ||
export const parameters = { | ||
options: { | ||
storySort: { | ||
method: "alphabetical", | ||
order: ["design", "pages", "modules", "components"], | ||
locales: "en-US", | ||
}, | ||
}, | ||
controls: { | ||
expanded: true, | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/, | ||
}, | ||
}, | ||
viewport: { | ||
viewports: { | ||
ipad: { | ||
name: "iPad Mini", | ||
styles: { | ||
height: "1024px", | ||
width: "768px", | ||
}, | ||
type: "tablet", | ||
}, | ||
terminal: { | ||
name: "Terminal", | ||
styles: { | ||
height: "400", | ||
width: "400", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
/** | ||
* There's a mismatch on the React Helmet return type that causes issues when | ||
* mounting the component in JS files only. Have to do type assertion, which is | ||
* especially ugly in JSDoc | ||
*/ | ||
const SafeHelmetProvider = /** @type {FC} */ ( | ||
/** @type {unknown} */ (HelmetProvider) | ||
); | ||
/** @type {Decorator} */ | ||
function withHelmet(Story) { | ||
return ( | ||
<SafeHelmetProvider> | ||
<Story /> | ||
</SafeHelmetProvider> | ||
); | ||
} | ||
/** @type {Decorator} */ | ||
function withQuery(Story, { parameters }) { | ||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
staleTime: Number.POSITIVE_INFINITY, | ||
retry: false, | ||
}, | ||
}, | ||
}); | ||
if (parameters.queries) { | ||
for (const query of parameters.queries) { | ||
if (query.data instanceof Error) { | ||
// This is copied from setQueryData() but sets the error. | ||
const cache = queryClient.getQueryCache(); | ||
const parsedOptions = parseQueryArgs(query.key); | ||
const defaultedOptions = queryClient.defaultQueryOptions(parsedOptions); | ||
const cachedQuery = cache.build(queryClient, defaultedOptions); | ||
// Set manual data so react-query will not try to refetch. | ||
cachedQuery.setData(undefined, { manual: true }); | ||
cachedQuery.setState({ error: query.data }); | ||
} else { | ||
queryClient.setQueryData(query.key, query.data); | ||
} | ||
} | ||
} | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<Story /> | ||
</QueryClientProvider> | ||
); | ||
} | ||
/** @type {Decorator} */ | ||
function withTheme(Story, context) { | ||
const selectedTheme = DecoratorHelpers.pluckThemeFromContext(context); | ||
const { themeOverride } = DecoratorHelpers.useThemeParameters(); | ||
const selected = themeOverride || selectedTheme || "dark"; | ||
return ( | ||
<StrictMode> | ||
<StyledEngineProvider injectFirst> | ||
<MuiThemeProvider theme={themes[selected]}> | ||
<EmotionThemeProvider theme={themes[selected]}> | ||
<CssBaseline /> | ||
<Story /> | ||
</EmotionThemeProvider> | ||
</MuiThemeProvider> | ||
</StyledEngineProvider> | ||
</StrictMode> | ||
); | ||
} | ||
// Try to fix storybook rendering fonts inconsistently | ||
// https://www.chromatic.com/docs/font-loading/#solution-c-check-fonts-have-loaded-in-a-loader | ||
const fontLoader = async () => ({ | ||
fonts: await document.fonts.ready, | ||
}); | ||
export const loaders = isChromatic() && document.fonts ? [fontLoader] : []; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.