Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Create Custom PopUp Component in React
Jeetendra
Jeetendra

Posted on • Edited on • Originally published atw3school.info

     

Create Custom PopUp Component in React

This blog is originally published atMy Blog

Sometimes we fade up with using various modal box provided by Bootstrap or material or suppose we are not using any of these frameworks. then in such case we need to create our own component for Popups and Modal boxes, I created this for one of such requirement.

Before reading it if you want to take a look then try this demonstration

This will be a fully Reusable Component that we can Invoke from any of the component entire our project.

Step 1: Create a file named custom-popup.module.css with following code :

.overlay{visibility:hidden;opacity:0;position:fixed;top:0;bottom:0;left:0;right:0;background:rgba(0,0,0,0.7);transition:opacity500ms;}.popup{margin:70pxauto;padding:20px;background:#fff;border-radius:5px;width:30%;position:relative;transition:all5sease-in-out;}.popuph2{margin-top:0;color:#333;font-family:Tahoma,Arial,sans-serif;}.popup.close{position:absolute;top:20px;right:30px;transition:all200ms;font-size:30px;font-weight:bold;text-decoration:none;color:#333;}.popup.close:hover{cursor:pointer;color:#000;}.popup.content{max-height:30%;overflow:auto;}@mediascreenand(max-width:700px){.popup{width:70%;}}
Enter fullscreen modeExit fullscreen mode

Step 2: Now create Popup Component with name CustomPopup.jsx with following code

import{useEffect,useState}from"react";importpopupStylesfrom"./custom-popup.module.css";importPropTypesfrom"prop-types";constCustomPopup=(props)=>{const[show,setShow]=useState(false);constcloseHandler=(e)=>{setShow(false);props.onClose(false);};useEffect(()=>{setShow(props.show);},[props.show]);return(<divstyle={{visibility:show?"visible":"hidden",opacity:show?"1":"0"}}className={popupStyles.overlay}><divclassName={popupStyles.popup}><h2>{props.title}</h2><spanclassName={popupStyles.close}onClick={closeHandler}>&times;</span><divclassName={popupStyles.content}>{props.children}</div></div></div>);};CustomPopup.propTypes={title:PropTypes.string.isRequired,show:PropTypes.bool.isRequired,onClose:PropTypes.func.isRequired};exportdefaultCustomPopup;
Enter fullscreen modeExit fullscreen mode

This component using PropTypes, if you havent installed PropTypes in your project then do install that first using

npminstallprop-types--save
Enter fullscreen modeExit fullscreen mode

Step 3: Invocation from another component

<CustomPopuponClose={popupCloseHandler}show={visibility}title="Hello Jeetendra"><h1>HelloThisisPopupContentArea</h1><h2>Thisismyloremipsumtexthere!</h2></CustomPopup>
Enter fullscreen modeExit fullscreen mode

It will need 3 props :
1: onClose – need a handler to do some activity after close
click in popup itself
2: show – pass the visibility of popup using boolean
3: title – provide the popup title

and Inside the you may pass any valid JSX that you want to render as content of popup

If you need a complete example how can we do utilise this PopUp Component then you may look into following code

import{useState}from"react";importCustomPopupfrom"./CustomPopup";import"./styles.css";exportdefaultfunctionApp(){const[visibility,setVisibility]=useState(false);constpopupCloseHandler=(e)=>{setVisibility(e);};return(<divclassName="App"><buttononClick={(e)=>setVisibility(!visibility)}>TogglePopup</button><CustomPopuponClose={popupCloseHandler}show={visibility}title="Hello Jeetendra"><h1>HelloThisisPopupContentArea</h1><h2>Thisismyloremipsumtexthere!</h2></CustomPopup></div>);}
Enter fullscreen modeExit fullscreen mode

Thats it for this blog. you may reach out to me in case you have any doubts and suggestions please let me know in comments section.

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
link2twenty profile image
Andrew Bone
A British web developer, that is passionate about web accessibility.
  • Location
    Britain, Europe
  • Pronouns
    He/Him
  • Work
    Senior Web Developer at bloc-digital
  • Joined

Hey, something cool you can do with your posts to help people understand is add a demo. You can use something likecodesandbox to host your code and then embed it straight into your post, like this.

A couple of suggestions for you to think about that, I think, will give a better user experience;

  • Useportals so your popup isn't sitting in the dom even when it's not being used.
  • Look into some way to trap focus so keyboard users don't end up focusing outside the popup.
  • Finally it might be worth reading though thearia examples for a dialog so you can copy the expected behaviours.

All in all a solid start 👍

CollapseExpand
 
g10dra profile image
Jeetendra
Jeetendra Singh is a Full Stack Architect who is a Microsoft certified technology Specialist and AWS Certified Associate Architect.

Thanks Andrew,
I will look into the suggestions

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Jeetendra Singh is a Full Stack Architect who is a Microsoft certified technology Specialist and AWS Certified Associate Architect.
  • Location
    Hyderabad
  • Education
    MCA
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp