create-react-library icon indicating copy to clipboard operation
create-react-library copied to clipboard

How to import svg as component

Open rimlin opened this issue 4 years ago • 11 comments

Using CRA, i was able to import svg like component and inline it: import { ReactComponent as skip } from '../../../assets/icons/skip.svg'; But if i try it, i will get an error:

Error: 'ReactComponent' is not exported by src/assets/icons/skip.svg, imported by src/components/shared/Icon/Icon.component.tsx

If i import like this: import skip from '../../../assets/icons/skip.svg'; i will get link to svg, but i need inline svg code.

rimlin avatar Apr 07 '20 08:04 rimlin

I'm having the same problem

rennerwins avatar May 24 '20 05:05 rennerwins

I'm facing the same problem !!

ChamseddineBhd avatar Jun 09 '20 14:06 ChamseddineBhd

@transitive-bullshit seems like the d.ts file has this described but it fails on the build I assume this should work somehow but don't see the options so far

bohdanbirdie avatar Jun 11 '20 07:06 bohdanbirdie

@transitive-bullshit deep apologies for double ping

Just wanted to bring the attention to PR above

Thank you for the great tool!

bohdanbirdie avatar Jun 11 '20 09:06 bohdanbirdie

Thanks a lot @bohdanbirdie you saved me ! Can't wait to test when it's released ~

DDDDDanica avatar Jun 24 '20 23:06 DDDDDanica

@DDDDDanica you can use my fork while the package is not published. (or use original git repo) "microbundle-crl": "https://github.com/bohdanbirdie/microbundle.git",

bohdanbirdie avatar Jun 25 '20 09:06 bohdanbirdie

Hey @bohdanbirdie I wonder if it is babel setting, but when i run storyshots inside the /example src (aka create-react-app), i got this following error:

Screenshot 2020-06-25 at 11 12 59

Any ideas?

DDDDDanica avatar Jun 25 '20 10:06 DDDDDanica

@DDDDDanica yes, this was also my problem You can only use this syntax

import { ReactComponent as Bell } from '../../assets/svg/bell.svg';

bohdanbirdie avatar Jun 25 '20 10:06 bohdanbirdie

@bohdanbirdie Hi, So in my Icon component (inside the library), i imported the svg; and in example app, i imported Icon component along with pre-defined svgs. which means:

import { ReactComponent as Bell } from '../../assets/svg/bell.svg';

It happens in Icon component, and in storybook i direct call

<Icon
        type={type}
        name="bell"
      />

It works to generate the stories, but failed in storyshots (jest).

DDDDDanica avatar Jun 25 '20 10:06 DDDDDanica

It should be like this only. If you want to use a single component (Icon) for that you will need to create it, and map the name prop to appropriate imported SVG component (like { 'bell': Bell }

import { ReactComponent as Bell } from '../../assets/svg/bell.svg';


const MyComp = () => {
  return <Bell />
}

bohdanbirdie avatar Jun 25 '20 10:06 bohdanbirdie

Thanks @bohdanbirdie Eventually i abandoned storyshots and just append snapshots to component itself. Works for me. Thanks for your help !

DDDDDanica avatar Jun 26 '20 09:06 DDDDDanica