Skip to content

Commit afa39a7

Browse files
authored
Merge pull request #13570 from ethereum/fix-svg-storybook
Fix svg imports in SB
2 parents 6b398f1 + 8986fb1 commit afa39a7

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

.storybook/main.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const config: StorybookConfig = {
4747
},
4848
},
4949
webpackFinal: async (config) => {
50+
config.module = config.module || {}
51+
config.module.rules = config.module.rules || []
52+
5053
if (config.resolve) {
5154
config.resolve.plugins = [
5255
...(config.resolve.plugins || []),
@@ -55,6 +58,22 @@ const config: StorybookConfig = {
5558
}),
5659
]
5760
}
61+
62+
// This modifies the existing image rule to exclude .svg files
63+
// since you want to handle those files with @svgr/webpack
64+
const imageRule = config.module.rules.find((rule) =>
65+
rule?.["test"]?.test(".svg")
66+
)
67+
if (imageRule) {
68+
imageRule["exclude"] = /\.svg$/
69+
}
70+
71+
// Configure .svg files to be loaded with @svgr/webpack
72+
config.module.rules.push({
73+
test: /\.svg$/,
74+
use: ["@svgr/webpack"],
75+
})
76+
5877
return config
5978
},
6079
typescript: {

next.config.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,31 @@ module.exports = (phase, { defaultConfig }) => {
2727
test: /\.ya?ml$/,
2828
use: "yaml-loader",
2929
})
30-
config.module.rules.push({
31-
test: /\.svg$/,
32-
use: "@svgr/webpack",
33-
})
30+
31+
// SVG loader
32+
// Grab the existing rule that handles SVG imports
33+
const fileLoaderRule = config.module.rules.find((rule) =>
34+
rule.test?.test?.(".svg")
35+
)
36+
37+
config.module.rules.push(
38+
// Reapply the existing rule, but only for svg imports ending in ?url
39+
{
40+
...fileLoaderRule,
41+
test: /\.svg$/i,
42+
resourceQuery: /url/, // *.svg?url
43+
},
44+
// Convert all other *.svg imports to React components
45+
{
46+
test: /\.svg$/i,
47+
issuer: fileLoaderRule.issuer,
48+
resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] }, // exclude if *.svg?url
49+
use: ["@svgr/webpack"],
50+
}
51+
)
52+
53+
// Modify the file loader rule to ignore *.svg, since we have it handled now.
54+
fileLoaderRule.exclude = /\.svg$/i
3455

3556
return config
3657
},

src/assets.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
declare module "*.svg" {
2-
import { ReactElement, SVGProps } from "react"
3-
const content: (props: SVGProps<SVGElement>) => ReactElement
2+
import { FC, SVGProps } from "react"
3+
const content: FC<SVGProps<SVGElement>>
4+
export default content
5+
}
6+
7+
declare module "*.svg?url" {
8+
const content: unknown
49
export default content
510
}

src/components/FeedbackWidget/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ const FeedbackWidget = () => {
4040
/>
4141
</PopoverTrigger>
4242

43-
<PopoverContent className="mx-2 w-80 max-w-[calc(100vw_-_1rem)] rounded bg-background p-4 sm:p-8">
43+
<PopoverContent
44+
className="mx-2 w-80 max-w-[calc(100vw_-_1rem)] rounded bg-background p-4 sm:p-8"
45+
data-testid="feedback-widget-modal"
46+
>
4447
<div className="flex items-start gap-2">
4548
<header className="me-0 flex-1 p-0 text-xl font-bold">
4649
{feedbackSubmitted

0 commit comments

Comments
 (0)