Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@chakra-ui/portal
Advanced tools
React component used to render children outside the DOM hierarchy of the parent component
@chakra-ui/portal is a package that provides a way to render components outside the main DOM hierarchy. This is particularly useful for creating modals, tooltips, and other UI elements that need to be rendered in a different part of the DOM tree to avoid clipping or other layout issues.
Basic Portal Usage
This example demonstrates the basic usage of the Portal component. The content inside the Portal is rendered outside the main DOM hierarchy, which can be useful for avoiding layout issues.
import { Portal } from '@chakra-ui/portal';
import { Box, Button } from '@chakra-ui/react';
function App() {
return (
<Box>
<Button>Click me</Button>
<Portal>
<Box position="absolute" top="50px" left="50px" bg="tomato" p={4}>
This is a portal content
</Box>
</Portal>
</Box>
);
}
export default App;
Nested Portals
This example demonstrates the usage of nested portals. The nested portal content is rendered outside the main DOM hierarchy and also outside the parent portal's DOM hierarchy.
import { Portal } from '@chakra-ui/portal';
import { Box, Button } from '@chakra-ui/react';
function App() {
return (
<Box>
<Button>Click me</Button>
<Portal>
<Box position="absolute" top="50px" left="50px" bg="tomato" p={4}>
This is a portal content
<Portal>
<Box position="absolute" top="20px" left="20px" bg="blue" p={4}>
Nested portal content
</Box>
</Portal>
</Box>
</Portal>
</Box>
);
}
export default App;
Conditional Rendering with Portal
This example demonstrates conditional rendering with the Portal component. The portal content is rendered only when the state `isOpen` is true.
import { useState } from 'react';
import { Portal } from '@chakra-ui/portal';
import { Box, Button } from '@chakra-ui/react';
function App() {
const [isOpen, setIsOpen] = useState(false);
return (
<Box>
<Button onClick={() => setIsOpen(!isOpen)}>Toggle Portal</Button>
{isOpen && (
<Portal>
<Box position="absolute" top="50px" left="50px" bg="tomato" p={4}>
This is a portal content
</Box>
</Portal>
)}
</Box>
);
}
export default App;
react-portal is a package that provides a simple way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. It is similar to @chakra-ui/portal in that it allows for rendering components outside the main DOM tree, but it is more lightweight and does not come with the additional UI components that Chakra UI provides.
react-dom is a core package from the React library that includes the createPortal function. This function allows you to render children into a DOM node that exists outside the DOM hierarchy of the parent component. It is a lower-level API compared to @chakra-ui/portal and requires more manual setup.
react-overlays is a library that provides a set of components for creating overlays, including modals, tooltips, and popovers. It includes a Portal component similar to @chakra-ui/portal but also provides additional components and utilities for managing overlays.
A wrapper for rendering components in React Portals, with support for nested portals and stacking.
yarn add @chakra-ui/portal
# or
npm i @chakra-ui/portal
import { Portal, PortalManager } from "@chakra-ui/portal"
Render the PortalManager
once at the root of your application
function App() {
return (
<ThemeProvider>
<CSSReset />
<PortalManager>{/* Your app goes here */}</PortalManager>
</ThemeProvider>
)
}
Portals are render into the portal manager's node by default not
document.body
.
<div>
<p>Welcome</p>
<Portal>This text has been portaled</Portal>
</div>
Nesting portal can be very useful to build complex widgets like nested menu, popovers, modal, etc.
<Portal>
This is a portal.
<Portal>This is a nested portal</Portal>
</Portal>
You can also portal elements into a custom containers. Simply pass a
containerRef
prop that points to the node
of that element.
<>
<div ref={ref} />
<Portal containerRef={ref}>
<h1>Hello world</h1>
</Portal>
</>
FAQs
React component used to render children outside the DOM hierarchy of the parent component
The npm package @chakra-ui/portal receives a total of 400,373 weekly downloads. As such, @chakra-ui/portal popularity was classified as popular.
We found that @chakra-ui/portal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.