
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
react-useportal
Advanced tools
🌀 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).
yarn add react-useportal or npm i -S react-useportal
import usePortal from 'react-useportal'
const App = () => {
const { Portal } = usePortal()
return (
<Portal>
This text is portaled at the end of document.body!
</Portal>
)
}
const App = () => {
const { Portal } = usePortal()
return (
<Portal bindTo={document && document.getElementById('san-francisco')}>
This text is portaled into San Francisco!
</Portal>
)
}
import usePortal from 'react-useportal'
const App = () => {
var { openPortal, closePortal, isOpen, Portal } = usePortal()
// want to use array destructuring? You can do that too
var [openPortal, closePortal, isOpen, Portal] = usePortal()
return (
<>
<button onClick={openPortal}>
Open Portal
</button>
{isOpen && (
<Portal>
<p>
This Portal handles its own state.{' '}
<button onClick={closePortal}>Close me!</button>, hit ESC or
click outside of me.
</p>
</Portal>
)}
</>
)
}
import usePortal from 'react-useportal'
const App = () => {
const { openPortal, closePortal, isOpen, Portal } = usePortal()
return (
<>
<button onClick={openPortal}>
Open Portal
</button>
<Portal>
<p className={isOpen ? 'animateIn' : 'animateOut'}>
This Portal handles its own state.{' '}
<button onClick={closePortal}>Close me!</button>, hit ESC or
click outside of me.
</p>
</Portal>
</>
)
}
By using onOpen
, onClose
or any other event handler, you can modify the portal
and return it. See useDropdown for a working example. It's important that you pass the event
object to openPortal
.
const App = () => {
const { openPortal, isOpen } = usePortal({
onOpen({ portal }) {
portal.current.style.cssText = `
position: absolute;
/* add your custom styles here! */
`
return portal
}
})
return <button onClick={e => openPortal(e)}>Click Me<button>
}
Make sure you are passing the html synthetic event to the openPortal
. i.e. onClick={e => openPortal(e)}
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 |
renderBelowClickedElement | This will put the portal right under the element that you click on. Great for dropdowns. Required to pass event to openPortal onClick={event => openPortal(event)} |
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 |
html event handlers (i.e. onClick ) | These can be used instead of onOpen to modify the css of the portal directly |
const {
openPortal,
closePortal,
togglePortal,
isOpen,
Portal
} = usePortal({
closeOnOutsideClick: true,
closeOnEsc: true,
renderBelowClickedElement, // appear directly under the clicked element/node in the DOM
bindTo, // attach the portal to this node in the DOM
isOpen: false,
onOpen: ({ event, portal, targetEl }) => {},
onClose({ event, portal, targetEl }) {},
// in addition, any event handler such as onClick, onMouseOver, etc will be handled like
onClick({ event, portal, targetEl }) {}
})
const { openPortal, closePortal, isOpen, Portal } = usePortal({
popup: ['', '', 'width=600,height=400,left=200,top=200']
})
// window.open('', '', 'width=600,height=400,left=200,top=200')
<Provider order={['Portal', 'openPortal']} />
then you can change the order of the array destructuring syntaxstateful
option, just make usePortal
stateful, and allow import { Portal } from 'react-useportal'
FAQs
🌀 React hook for Portals
The npm package react-useportal receives a total of 25,527 weekly downloads. As such, react-useportal popularity was classified as popular.
We found that react-useportal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.