- Type safe
- Small size
- Zero dependencies
- Simple API
npm i use-portal-hook
import { PortalProvider } from "use-portal-hook";
...
root.render(
<PortalProvider>
<App />
</PortalProvider>
)
import { PortalComponent } from "use-portal-hook";
interface DeleteModalProps {
title: string
}
export const DeleteModal: PortalComponent<DeleteModalProps, boolean> = ({ onClose, props }) => {
return (
<div>
{props.title}
<button onClick={() => onClose(false)}>Cancel</button>
<button onClick={() => onClose(true)}>Delete</button>
</div>
)
}
import { usePortal } from "use-portal-hook";
import { DeleteModal } from "./delete-modal";
const App = () => {
const showDeleteModal = usePortal(DeleteModal);
const handleDelete = async () => {
const confirmed = await showDeleteModal({ title: 'Delete' })
console.log('Confirmed: ', confirmed)
}
return (
<button onClick={handleDelete}>Delete Item</button>
);
}
- See code
- CodeSandBox TODO
MIT