springload / react-accessible-accordion

Accessible Accordion component for React
https://springload.github.io/react-accessible-accordion
MIT License
785 stars 113 forks source link

How to render text as html tags? Is there an option to do that? #337

Closed mynsteria closed 2 years ago

mynsteria commented 2 years ago

Tried \<Fragment> but that didn't work. Is there a way to render string passed in as HTML markup?

EarthlingDavey commented 2 years ago

Is this what you're looking for? Warning.. read up on dangerouslySetInnerHTML in case you're not familiar with it. It can cause security vulnerabilities on un sanitised html.

<AccordionItemPanel
  dangerouslySetInnerHTML={{ __html: yourHtml }}
/>
nithincvpoyyil commented 2 years ago

@mynsteria : You can do it as follows and sanitise the HTML string to avoid XSS.

<AccordionItemPanel>
                    <span  dangerouslySetInnerHTML={{ __html: sanitizedHTMLString }}>
                        Exercitation in fugiat est ut ad ea cupidatat ut in
                        cupidatat occaecat ut occaecat consequat est minim minim
                        esse tempor laborum consequat esse adipisicing eu
                        reprehenderit enim.
                    </span>
</AccordionItemPanel>
EarthlingDavey commented 2 years ago

@nithincvpoyyil that will work ok, but you don't really need to intorduce a span tag, see my answer above.

Where the inner html contains block elements like div, wrapping in span might lead to unexpected results, as by default span is an inline element.

nithincvpoyyil commented 2 years ago

@nithincvpoyyil that will work ok, but you don't really need to intorduce a span tag, see my answer above.

Where the inner html contains block elements like div, wrapping in span might lead to unexpected results, as by default span is an inline element.

Yes, you are correct, I just put a demo code. My point is to keep the panel & custom components separate for better decoupling.

EarthlingDavey commented 2 years ago

I see, that you do not want to assume that dangerouslySetInnerHTML will always work directly on AccordionItemPanel. That's a fair point. I thnk it's safe to close this issue now right? @mynsteria