8000 GitHub - sandrina-p/react-url-modal: A React library to help you keep track of your modal state using the URL.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

sandrina-p/react-url-modal

 
 

Repository files navigation

React URL Modal

A React library to help you keep track of your modal state using the URL.

Build Passing

Features

  • ☁️ Have URL's for modals
  • 🔒 Encode all the parameters sent to a modal
  • 🦄 Works on any framework since it uses the history api
  • 📦 Headless and tiny
  • 🚀 Supports React Portals

Documentation

Documentation

To create a new instance of modals you can import the URLModal and pass the modals you have in your application:

import { URLModal } from 'react-url-modal';
import { CreateAccount, EditAccount } from './Modals';

export const App = () => (
  <URLModal
    modals={{
      createAccount: CreateAccount,
      editAccount: EditAccount,
    }}
  />
);

To open this modal from any button in your application you can use the openModal function and pass the name of the modals and any params this modal needs:

import { openModal } from 'react-url-modal';

<button
  onClick={() =>
    openModal({
      name: 'editAccount',
      params: {
        userId: user.id,
      },
    })
  }
>
  Edit your profile
</button>;

Then in your modal you will have access to any param you passed to the modal:

const ModalWithParams = ({
  params,
  onClose,
}: {
  params: { [key: string]: string },
  onClose: () => void,
}) => (
  <>
    {params.userId}
    <button onClick={onClose}>CloseModal</button>
  </>
);

You can also pass a Wrapper to the <URLModal> component and that component and that will wrap all your modals and will have access to the onClose function:

<URLModal
  modals={{
    customWrapper: CustomWrapperModal,
  }}
  Wrapper={({ onClose, children }) => (
    <>
      {children}
      <button onClick={onClose} type="button">
        x
      </button>
    </>
  )}
/>

To see all the available props you can check the API reference below.

API Reference

URLModal

<URLModal
  modals={{
    test: TestModal,
  }}
/>
Parameter Type Description
modals [name: string]: React Component or Promise<Component> Required
Wrapper React Component A component to wrap each modal with
usePortal boolean Should this modal be mounted on a portal
portalElement HTML Element A component to mount the modals in, defaults to body

openModal

Will open any modal you declared in modals by passing its name.

openModal({
    name: 'createAccountForm'
    params: {
        hello: 'world'
    }
})
Parameter Type Description
name string Required. Name of the modal to open
params {[key: string]: string} Any params this modal need

closeModal

Close all open modals.

openModal();

isModalOpen

Checks if a modal passed is open at the moment the function is called

isModalOpen('createAccountForm');
Parameter Type Description
name string Required. Name of the modal to check open

encodeUrlParams

Useful if you want to open a modal by a link instead of a button. It will create the url fro 737F m the params passed.

router.push({
  pathname: '/account',
  query: {
    modal: 'editAccount',
    params: encodeUrlParams({
      id: user.id,
    }),
  },
});
Parameter Type Description
params Object Required. Object you want to encode

Run Locally

Clone the project

  git clone git@github.com:remoteoss/react-url-modal.git

Go to the project directory

  cd react-url-modal

Install dependencies

  yarn

Start the server

  yarn dev

To open the example and test your changes please in another tab and run:

cd example
yarn && yarn dev

Running Tests

To run tests, run the following command

  yarn test

To run coverage you can run:

  yarn test:coverage

License

MIT

About

A React library to help you keep track of your modal state using the URL.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 86.1%
  • CSS 6.3%
  • JavaScript 6.1%
  • HTML 1.5%
0