Gathering detailed insights and metrics for react-useportal
Gathering detailed insights and metrics for react-useportal
Gathering detailed insights and metrics for react-useportal
Gathering detailed insights and metrics for react-useportal
react-useportal-test
🌀 React hook for Portals
@kintaba/react-useportal
🌀 React hook for Portals
@custom-react-hooks/use-portal
The `usePortal` hook facilitates the creation and management of portal components in React applications. Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. This hook is parti
@useportal/reagent-react
React UI for Portal reagent
npm install react-useportal
Typescript
Module System
TypeScript (90.62%)
JavaScript (9.38%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
888 Stars
216 Commits
34 Forks
6 Watchers
125 Branches
8 Contributors
Updated on Jul 05, 2025
Latest Version
1.0.19
Package Id
react-useportal@1.0.19
Unpacked Size
38.78 kB
Size
10.70 kB
File Count
8
Published on
Nov 03, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
19
🌀 React hook for using Portals
Need to make dropdowns, lightboxes/modals/dialogs, global message notifications, or tooltips in React? React Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component (react docs).
This hook is also isomorphic, meaning it works with SSR (server side rendering).
1yarn add react-useportal or npm i -S react-useportal
1import usePortal from 'react-useportal' 2 3const App = () => { 4 const { Portal } = usePortal() 5 6 return ( 7 <Portal> 8 This text is portaled at the end of document.body! 9 </Portal> 10 ) 11} 12 13const App = () => { 14 const { Portal } = usePortal({ 15 bindTo: document && document.getElementById('san-francisco') 16 }) 17 18 return ( 19 <Portal> 20 This text is portaled into San Francisco! 21 </Portal> 22 ) 23}
1import usePortal from 'react-useportal' 2 3const App = () => { 4 var { openPortal, closePortal, isOpen, Portal } = usePortal() 5 6 // want to use array destructuring? You can do that too 7 var [openPortal, closePortal, isOpen, Portal] = usePortal() 8 9 return ( 10 <> 11 <button onClick={openPortal}> 12 Open Portal 13 </button> 14 {isOpen && ( 15 <Portal> 16 <p> 17 This Portal handles its own state.{' '} 18 <button onClick={closePortal}>Close me!</button>, hit ESC or 19 click outside of me. 20 </p> 21 </Portal> 22 )} 23 </> 24 ) 25}
1import usePortal from 'react-useportal' 2 3const App = () => { 4 const { openPortal, closePortal, isOpen, Portal } = usePortal() 5 return ( 6 <> 7 <button onClick={openPortal}> 8 Open Portal 9 </button> 10 <Portal> 11 <p className={isOpen ? 'animateIn' : 'animateOut'}> 12 This Portal handles its own state.{' '} 13 <button onClick={closePortal}>Close me!</button>, hit ESC or 14 click outside of me. 15 </p> 16 </Portal> 17 </> 18 ) 19}
By using onOpen
, onClose
or any other event handler, you can modify the Portal
and return it. See useDropdown for a working example. If opening the portal from a click event it's important that you pass the event
object to openPortal
and togglePortal
otherwise you will need to attach a ref
to the clicked element (if you want to be able to open the portal without passing an event you will need to set programmaticallyOpen
to true
).
1const useModal = () => { 2 const { isOpen, openPortal, togglePortal, closePortal, Portal } = usePortal({ 3 onOpen({ portal }) { 4 portal.current.style.cssText = ` 5 /* add your css here for the Portal */ 6 position: fixed; 7 left: 50%; 8 top: 50%; 9 transform: translate(-50%,-50%); 10 z-index: 1000; 11 ` 12 } 13 }) 14 15 return { 16 Modal: Portal, 17 openModal: openPortal, 18 toggleModal: togglePortal, 19 closeModal: closePortal, 20 isOpen 21 } 22} 23 24const App = () => { 25 const { openModal, closeModal, isOpen, Modal } = useModal() 26 27 return <> 28 <button onClick={e => openModal(e)}>Open Modal<button> 29 {isOpen && ( 30 <Modal> 31 This will dynamically center to the middle of the screen regardless of the size of what you put in here 32 </Modal> 33 )} 34 </> 35}
Make sure you are passing the html synthetic event to the openPortal
and togglePortal
. i.e. onClick={e => openPortal(e)}
ref
If for some reason, you don't want to pass around the event
to openPortal
or togglePortal
and you're not using programmaticallyOpen
, you can use a ref
like this.
1import usePortal from 'react-useportal' 2 3const App = () => { 4 var { ref, openPortal, closePortal, isOpen, Portal } = usePortal() 5 6 return ( 7 <> 8 {/* see below how I don't have to pass the event if I use the ref */} 9 <button ref={ref} onClick={() => openPortal()}> 10 Open Portal 11 </button> 12 {isOpen && ( 13 <Portal> 14 <p> 15 This Portal handles its own state.{' '} 16 <button onClick={closePortal}>Close me!</button>, hit ESC or 17 click outside of me. 18 </p> 19 </Portal> 20 )} 21 </> 22 ) 23}
Option | Description |
---|---|
closeOnOutsideClick | This will close the portal when not clicking within the portal. Default is true |
closeOnEsc | This will allow you to hit ESC and it will close the modal. Default is true |
bindTo | This is the DOM node you want to attach the portal to. By default it attaches to document.body |
isOpen | This will be the default for the portal. Default is false |
onOpen | This is used to call something when the portal is opened and to modify the css of the portal directly |
onClose | This is used to call something when the portal is closed and to modify the css of the portal directly |
onPortalClick | This is fired whenever clicking on the Portal |
html event handlers (i.e. onClick ) | These can be used instead of onOpen to modify the css of the portal directly. onMouseEnter and onMouseLeave example |
programmaticallyOpen | This option allows you to open or toggle the portal without passing in an event. Default is false |
1const {
2 openPortal,
3 closePortal,
4 togglePortal,
5 isOpen,
6 Portal,
7 // if you don't pass an event to openPortal, closePortal, or togglePortal and you're not using programmaticallyOpen, you will need
8 // to put this on the element you want to interact with/click
9 ref,
10 // if for some reason you want to interact directly with the portal, you can with this ref
11 portalRef,
12} = usePortal({
13 closeOnOutsideClick: true,
14 closeOnEsc: true,
15 bindTo, // attach the portal to this node in the DOM
16 isOpen: false,
17 // `event` has all the fields that a normal `event` would have such as `event.target.value`, etc.
18 // with the additional `portal` and `targetEl` added to it as seen in the examples below
19 onOpen: (event) => {
20 // can access: event.portal, event.targetEl, event.event, event.target, etc.
21 },
22 // `onClose` will not have an `event` unless you pass an `event` to `closePortal`
23 onClose({ portal, targetEl, event }) {},
24 // `targetEl` is the element that you either are attaching a `ref` to
25 // or that you are putting `openPortal` or `togglePortal` or `closePortal` on
26 onPortalClick({ portal, targetEl, event }) {},
27 // in addition, any event handler such as onClick, onMouseOver, etc will be handled the same
28 onClick({ portal, targetEl, event }) {}
29})
Provider
...1 const { openPortal, closePortal, isOpen, Portal } = usePortal({ 2 popup: ['', '', 'width=600,height=400,left=200,top=200'] 3 }) 4 // window.open('', '', 'width=600,height=400,left=200,top=200')
<Provider order={['Portal', 'openPortal']} />
then you can change the order of the array destructuring syntaxNo vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/13 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
85 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More