
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
react-history-modals
Advanced tools
A React hook and context for managing modals with browser history integration.
A lightweight and type-safe React hook and context for managing modal state using the browser’s history API. It allows users to navigate through modal states using the browser’s back and forward buttons — just like navigating pages.
npm install react-history-modals
# or
yarn add react-history-modals
import { HistoryModalProvider } from 'react-history-modals';
function App() {
return (
<HistoryModalProvider>
{/* Your routes/components */}
</HistoryModalProvider>
);
}
import { useHistoryModals } from 'react-history-modals';
function CartButton() {
const {
openModal,
closeModal,
openModalChecker
} = useHistoryModals();
const openCart = () => {
openModal('CartItemsBottomSheet', { cartId: 101 });
};
const closeCart = () => {
closeModal('CartItemsBottomSheet');
};
const openCartIfInHistory = openModalChecker('CartItemsBottomSheet');
// Example usage: openCartIfInHistory({ cartId: 123 });
return (
<>
<button onClick={openCart}>Open Cart</button>
<button onClick={closeCart}>Close Cart</button>
</>
);
}
import { useHistoryModals } from 'react-history-modals';
function ModalRenderer() {
const { modals, closeModal } = useHistoryModals();
return (
<>
{modals.map(({ id, data }) => {
if (id === 'CartItemsBottomSheet') {
return (
<CartItemsModal
key={id}
open={true}
onClose={() => closeModal(id)}
cartId={data?.cartId}
/>
);
}
return null;
})}
</>
);
}
<HistoryModalProvider>
<HistoryModalProvider>
{children}
</HistoryModalProvider>
Wrap your application's root or layout component with this provider.
useHistoryModals()
This hook returns an object with the following properties:
Function / Value | Description |
---|---|
openModal(id, data?) | Opens a modal by its id and pushes a new entry to the browser history. |
closeModal(id?) | Closes a modal. If id is provided, it closes that specific modal. |
openModalChecker(id) | Returns a function that opens the modal only if its state exists in history. |
modals | Array of active modals: { id: string, data?: any }[] . |
openModalChecker(modalId: string): (data?: any) => void
const checker = openModalChecker('SomeModal');
checker({ foo: 'bar' }); // Opens only if in history
openModal()
pushes modal data into browser history.closeModal()
triggers history.back()
.popstate
update modal state accordingly.import { useEffect } from 'react';
import { useHistoryModals } from 'react-history-modals';
function ExamplePage() {
const { openModalChecker } = useHistoryModals();
useEffect(() => {
const openCart = openModalChecker('CartItemsBottomSheet');
openCart({ cartId: 123 });
}, []);
return <p>Example Page Content</p>;
}
window
/history
references on the server// pages/_app.tsx
import { HistoryModalProvider } from 'react-history-modals';
function MyApp({ Component, pageProps }) {
return (
<HistoryModalProvider>
<Component {...pageProps} />
{/* Don't forget your central ModalRenderer component here! */}
</HistoryModalProvider>
);
}
export default MyApp;
react
and react-dom
as peer dependenciesWant to improve the library or report an issue? Contributions are welcome! Please feel free to open a pull request or an issue on the GitHub repository.
MIT
FAQs
A React hook and context for managing modals with browser history integration.
The npm package react-history-modals receives a total of 1 weekly downloads. As such, react-history-modals popularity was classified as not popular.
We found that react-history-modals demonstrated a healthy version release cadence and project activity because the last version was released less than 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.