Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
@chakra-ui/portal
Advanced tools
Package description
@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.
Readme
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 523,590 weekly downloads. As such, @chakra-ui/portal popularity was classified as popular.
We found that @chakra-ui/portal demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.