Import SVG as a component in Gatsby
Asked Answered
I

4

15

I've seen the following solution:

import { ReactComponent as Img } from 'path/to/file.svg'

But in Gatsby, this doesn't work. I know exist plugins for this, but maybe it can be done more easily.

Impious answered 11/4, 2020 at 15:6 Comment(0)
P
18

As you said, there are plugins to achieve this, which means a cleaner code (not the full SVG tag inlined in the component) with the same final result. Using gatsby-plugin-react-svg plugin you just need to import your SVG like this:

import Icon from "./path/assets/icon.svg";

To install, you only need to add the dependency using npm or yarn and in your gatsby-config.js use this snippet:

{
  resolve: 'gatsby-plugin-react-svg',
  options: {
    rule: {
      include: /assets/
    }
  }
}

Note that /assets/ is an including rule based on a regular expression so the value must be your SVG folder (i.e: /svg/) and must only contain .svg files. In other words, if your path is /images/svg/ your including rule can only contain /svg/ (you can also add the full path but you'll need to escape slashes).

Afterward, you will need to style the SVG if their inline styles don't apply.


If you want to follow the non-plugin approach you can simply use a React-based approach, just creating a component that returns the SVG:

export const YourSvgComponent = () => (
  <svg
    version="1.1"
    baseProfile="full"
    width="300"
    height="200"
    xmlns="http://www.w3.org/2000/svg"
  >
    <rect width="100%" height="100%" fill="red" />
    <circle cx="150" cy="100" r="80" fill="green" />
    <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">
      SVG
    </text>
  </svg>
);

Now you just need to import it and use it.

Phyllys answered 11/4, 2020 at 17:56 Comment(6)
gatsby-node.js or gatsby-config.js?Bleach
Do you have any idea why the svg might just not be appearing? The div that it is inside is emptyBleach
These are the files I'm using github.com/samgermain/somefiles . I'll try to make an MRE and post a new questionsBleach
The SVG is not loading? The SVG folder must only contain SVG filesPhyllys
Well I made one, but it gets a different errorBleach
Non plugin approach doesn’t work in Gatsby.Depoliti
D
7
  1. Add new package:

npm install gatsby-plugin-react-svg

or

yarn add gatsby-plugin-react-svg

  1. Configure at gatsby-config.js:
    {
      resolve: 'gatsby-plugin-react-svg',
      options: {
        rule: {
          include: /\.inline\.svg$/,
        },
      },
    },

Here's an example of what this file might look like when completed

module.exports = {
  siteMetadata: {
    title: `Gatsby`,
  },
  plugins: [
    {
      resolve: 'gatsby-plugin-react-svg',
      options: {
        rule: {
          include: /assets/,
        },
      },
    },
  ],
};
  1. Rename your files to something like example.inline.svg

  2. Import:

import Illustration from './illustration.inline.svg'
  1. Usage:
<Illustration className="example" />

All the information from official Gatsby guide

Din answered 18/8, 2020 at 8:14 Comment(0)
F
0

This is because you are using a setup that has SVGR installed by default.

You can easily get this to work by installing this SVGR plugin for Gatsby.

npm install @svgr/webpack gatsby-plugin-svgr

Then add it to your Gatsby config:

// gatsby-config.js
module.exports = {
  plugins: [
    'gatsby-plugin-svgr',
  ],
}

And use it as you mentioned in your question:

import { ReactComponent as Img } from 'path/to/file.svg'

I have just tested it and it works great!

Fastigiate answered 17/8, 2023 at 15:51 Comment(0)
S
-1

Just do like this...

import YourDesiredName from 'path/to/file.svg'
Subbasement answered 11/4, 2020 at 15:13 Comment(1)
Not exactly. This you can use as src of img tag. And this method: import { ReactComponent as Img } from 'path/to/file.svg' allows you to import inline svg.Impious

© 2022 - 2024 — McMap. All rights reserved.