segmentio / consent-manager

Drop-in consent management plugin for analytics.js
https://segmentio.github.io/consent-manager/
MIT License
341 stars 142 forks source link

Replace container div with Fragment #223

Closed ajrussellaudio closed 2 years ago

ajrussellaudio commented 2 years ago

A small change, but let me explain why we need it.

We have a div surrounding our ConsentManager component. That div applies styles, including a banner:

      <div className="border border-red">
        <ConsentManager
          writeKey={writeKey}

...which renders to the DOM as:

<div class="border border-mint">
  <div>
    <div class="css-h3yqvb">
      <div class="css-ow1vry">
        <p class="css-1u2wgn3">
          <!-- banner content -->
        </p>
// ...

If the user accepts cookies and the banner disappears, then the DOM is now:

<div class="border border-mint">
  <div></div>
</div>

i.e. our wrapper div, containing an empty div. The wrapper div is thus 0px * 0px, but the border is still visible, seen as a 2px square on the page.

This PR removes the empty div, replacing it with a React.Fragment. If none of the conditions are met in the container.tsx render, then only an empty Fragment is returned, which results in nothing being added to the DOM, and we can therefore hide the empty wrapper divat our end with some CSS:

div.myClass:empty {
  display: none;
}
ajrussellaudio commented 2 years ago

Awesome, thanks @edsonjab @nd4p90x and @pooyaj 👍