Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two div elements wrap svg icon #24

Closed
bezenson opened this issue Aug 16, 2017 · 12 comments
Closed

Two div elements wrap svg icon #24

bezenson opened this issue Aug 16, 2017 · 12 comments

Comments

@bezenson
Copy link

Is it necessary to wrap SVG by 2 div elements in DOM? Maybe render only svg without div's or use only one wrapper element?

@chuck-durst
Copy link

+1

@tanem
Copy link
Owner

tanem commented Sep 8, 2017

Is it necessary to wrap SVG by 2 div elements in DOM? Maybe render only svg without div's or use only one wrapper element?

That was actually how this module was originally created, however:

@tanem tanem closed this as completed Sep 8, 2017
@DavidSvetlik
Copy link

Hi,
couldn't you please at least provide a way to target those divs (eg custom class)? Or how are we supposed to target those divs in CSS?

Thank you,
D.

@tanem
Copy link
Owner

tanem commented Sep 11, 2017

@DavidSvetlik: without seeing your use-case I'm guessing a bit, but one way you can do this is wrap the output of this module in your own element which does have a class or id etc, then work from that to target the divs perhaps via a descendant selector.

If you want to provide a use-case, can you please open a new issue as well and we can work from that? (What you're asking sounds different to this original report).

@DavidSvetlik
Copy link

@tanem You are right, I can wrap it in another element. Thanks.

@iporollo iporollo mentioned this issue Oct 21, 2017
@iporollo
Copy link

Would love an example of the solution

@tanem
Copy link
Owner

tanem commented Oct 21, 2017

@iporollo if we use the example contained in this repo, what I'm getting at is doing something like this:

<div className="wrapper">
  <ReactSVG
    path="atomic.svg"
    callback={svg => console.log(svg)}
    className="example"
  />
</div>

Then, you can use CSS however you like to target the internal elements, e.g.:

.wrapper > div { ... }
.wrapper > div > div { ... }
.wrapper svg { ... }

@austincondiff
Copy link

Can we wrap the svg in <></> instead? I really don't want every single icon I user wrapped in two div's. Seems needless and clutters the DOM.

@tanem
Copy link
Owner

tanem commented Jan 25, 2019

Hey @austincondiff the reasons for the wrapping were posted earlier in this thread.

Long story short:

  • The underlying module this lib is based on needs a parent element wrapping the one it's replacing. This means these two elements are not managed by this lib/React.
  • This library itself then needs a wrapper element in React-land in order to manage things properly from it's side.

@JasonMichaelSmith
Copy link

Just use https://www.npmjs.com/package/react-inlinesvg

No unnecessary div/span wrappers.

@ckreeftmeijer
Copy link

ckreeftmeijer commented Mar 9, 2023

This works for me. Replacing the most outer div with the svg after injecting

afterInjection={svg => {
        let parent = svg.parentNode.parentNode
        parent.parentNode.insertBefore(svg, parent)
        parent.parentNode.removeChild(parent)
    }}

@trinhcanhphuc
Copy link

This works for me. Replacing the most outer div with the svg after injecting

afterInjection={svg => {
        let parent = svg.parentNode.parentNode
        parent.parentNode.insertBefore(svg, parent)
        parent.parentNode.removeChild(parent)
    }}

It worked for me with change. Thanks a lot

afterInjection={(svg) => { let parent = svg?.parentNode parent?.parentNode?.insertBefore(svg, parent) parent?.parentNode?.removeChild(parent) }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants