-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: run tests and storybook in StrictMode (#3352)
Co-authored-by: WK Wong <[email protected]>
- Loading branch information
Showing
8 changed files
with
140 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
29 changes: 29 additions & 0 deletions
29
packages/storybook/.storybook/addons/react-strict-mode/index.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { PropsWithChildren } from "react" | ||
|
||
import { addons, makeDecorator } from "@storybook/preview-api" | ||
import { getQueryParams } from "@storybook/preview-api" | ||
import React, { StrictMode, useEffect, useState } from "react" | ||
|
||
function StrictModeDecorator({ children }: PropsWithChildren<any>) { | ||
const [isStrict, setStrict] = useState(() => getQueryParams()?.strict === "true") | ||
|
||
useEffect(() => { | ||
const channel = addons.getChannel() | ||
|
||
channel.on("strict/updated", setStrict) | ||
|
||
return () => { | ||
channel.removeListener("strict/updated", setStrict) | ||
} | ||
}, []) | ||
|
||
return isStrict ? <StrictMode>{children}</StrictMode> : children | ||
} | ||
|
||
export const withStrictModeSwitcher = makeDecorator({ | ||
name: "withStrictModeSwitcher", | ||
parameterName: "strictModeSwitcher", | ||
wrapper: (getStory, context) => { | ||
return <StrictModeDecorator>{getStory(context)}</StrictModeDecorator> | ||
}, | ||
}) |
55 changes: 55 additions & 0 deletions
55
packages/storybook/.storybook/addons/react-strict-mode/register.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { API } from "@storybook/manager-api"; | ||
|
||
import { addons, types } from "@storybook/manager-api"; | ||
import React, { useEffect, useState } from "react"; | ||
|
||
const ADDON_ID = "StrictModeSwitcher"; | ||
|
||
function StrictModeSwitcher({ api }: { api: API }) { | ||
const [isStrict, setStrict] = useState(() => api.getQueryParam("strict") === "true"); | ||
|
||
const onChange = () => setStrict((val) => !val); | ||
|
||
useEffect(() => { | ||
const channel = api.getChannel(); | ||
|
||
channel?.emit("strict/updated", isStrict); | ||
|
||
api.setQueryParams({ | ||
strict: String(isStrict), | ||
}); | ||
}, [isStrict]); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
alignItems: "center", | ||
display: "flex", | ||
fontSize: "0.75rem", | ||
fontWeight: 600, | ||
lineHeight: "1rem", | ||
}} | ||
title="Enable Strict Mode" | ||
> | ||
<label htmlFor="strictmode">StrictMode:</label> | ||
<input | ||
checked={isStrict} | ||
id="strictmode" | ||
name="strictmode" | ||
onChange={onChange} | ||
type="checkbox" | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
if (process.env.NODE_ENV !== "production") { | ||
addons.register(ADDON_ID, (api) => { | ||
addons.add(ADDON_ID, { | ||
match: ({ viewMode }) => !!viewMode?.match(/^(story|docs)$/), | ||
render: () => <StrictModeSwitcher api={api} />, | ||
title: "Strict Mode Switcher", | ||
type: types.TOOL, | ||
}); | ||
}); | ||
} |
This file contains 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
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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