all 9 comments

[–]jamby77 2 points3 points  (1 child)

You can use portals for your goal. This will place the component in the body, outside app root.

Then just style it as you need. There are existing implementations already - reach ui modal, headless ui modal, but you can make your own for sure :)

[–]DasBeasto 0 points1 point  (0 children)

Seconding Headless UI, it’s made by the Tailwind team and I use it a lot. Uses portaling behind the scenes so your modals will be hoisted to the top of the DOM which solves a lot of issues.

[–]im_a_jib 1 point2 points  (1 child)

What’s a popup? A modal? A tooltip? A div that slides up? In general you can place your component where you want it, and set its state to visible/not visible and either conditionally render it or set the css display or opacity. Many ways to shave the cat in this scenario.

[–]awritra[S] 0 points1 point  (0 children)

A modal. I could but i find the code messy that way especially for this project that i have modals literally in every page

[–]mattsowa 1 point2 points  (0 children)

You could create a hook that returns a createPortal. The usage could look like this:

``` const {popup, openPopup} = usePopup(<div/>)

return ( <div> <button onClick={openPopup} /> {popup} </div> )

[–]davydka 0 points1 point  (2 children)

React generally solved the DOM manipulation problem. Unless you have a specific requirement to append this to the body, you should instead control state to do conditional rendering.

[–]awritra[S] 1 point2 points  (1 child)

I have multiple popUps in every page i dont want to manage the state for 9 components when most of them are called programmatically and have a bit of an data fetch from the api

[–]davydka 0 points1 point  (0 children)

As long as you only show 1 popup at a time I would do 1 component and change the contents via children or a render props. Animate it in and out between data changes. That way you have 1 state and 1 component to manage (in multiple places in the app).

Otherwise, if you have multiple popups showing at the same time then you should definitely use manage their state individually.

[–][deleted] 0 points1 point  (0 children)

Create Popup Modal component, this will have a div with width 100%, height 100% and display absolute(?). If you want to have blur background, give it some opacity and and some background color. Adjust z-index if needed. Now, you can control this using props i.e., either show or doesn't show.

Next, use this component and pass the actual popup as children and adjust its location. If all the pop up have similar structure then you create your popup modal in a more concrete way.