A simple React hook to synchronize the modal state with URL query parameters. This hook helps to manage modals via query params and provides seamless history support without reloading the page. It is designed to work with React 18+ and Next.js.
- Synchronize modal state with query parameters
- Open and close modals using the
openandclosefunctions - Supports history management with URL updates using shallow routing
- Clean and maintainable code structure
- Easily extendable for future use cases
To install the package, use the following command:
npm install use-modal-with-query
or
pnpm install use-modal-with-query
To use the use-modal-with-query hook, follow these steps:
-
Install the package: Make sure the package is installed in your project by running the following command:
npm install use-modal-with-query
or
pnpm install use-modal-with-query
-
Import the hook: Import
useModalWithQueryin the component where you want to use it.import { useModalWithQuery } from "use-modal-with-query";
-
Use the hook: Call
useModalWithQuery()inside your component. By default, it tracks thecreate-workspacequery parameter. You can also pass a custom query parameter name.const { isOpen, open, close } = useModalWithQuery("create-workspace");
-
Control the modal: Use the
openandclosefunctions to show or hide the modal, respectively. TheisOpenstate will automatically update based on the query parameter in the URL.return ( <div> <button onClick={open}>Open Modal</button> <button onClick={close}>Close Modal</button> {isOpen && ( <div className="modal"> <h2>Workspace Modal</h2> <p>This modal is synchronized with the URL query param.</p> </div> )} </div> );
queryParam(optional): The name of the query parameter to track (default iscreate-workspace).
isOpen: Boolean indicating if the modal is open (based on the query parameter).open: Function to open the modal and set the query parameter totrue.close: Function to close the modal and remove the query parameter.
The use-modal-with-query hook synchronizes the modal state with a query parameter in the URL. Here's how it works:
-
Open the Modal: When you call the
openfunction, it updates the URL by adding the query parameter (default iscreate-workspace=true). This ensures that the modal's visibility is stored in the URL, making it possible to share links with the modal open. -
Close the Modal: When you call the
closefunction, it removes the query parameter from the URL, which effectively closes the modal. -
Shallow Routing: The URL changes (adding or removing query parameters) are done using shallow routing. This means that the page does not reload when the modal state changes, ensuring a smooth user experience.
-
Modal State Sync: The
useEffecthook listens for changes in the query parameter. When the query parameter value is set to"true", the modal is opened. If the query parameter is removed or changed, the modal is closed. -
History Management: Since the query parameter is reflected in the URL, the modal state is also persistent across page reloads. If the page is refreshed or if the URL is shared, the modal will automatically be in the correct state (open or closed) based on the query parameter.
By using this approach, you can easily maintain modal state across page refreshes and make URLs that reflect the modal's open/closed status.