1.0.5 • Published 8 months ago

react-overlay-modal v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

React Overlay Modal

A minimalistic, super lightweight and accessible React modal library that offers robust type safety. This headless solution gives you full flexibility to integrate your own content and styles without worrying about z-index or complex configurations.

Features

  1. No dependencies other than React.
  2. Fully accessible with proper ARIA attributes.
  3. Lightweight and headless (no pre-applied styles).
  4. Typescript support for type safety.
  5. Minimal configuration with support for custom styling.

Demo

View the Demo

Install

npm i react-overlay-modal

Wrap the root app with ModalProvider

import { StrictMode } from "react"
import { createRoot } from "react-dom/client"
import App from "./App.tsx"
import { ModalProvider } from "react-overlay-modal"

createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <ModalProvider>
      <App />
    </ModalProvider>
  </StrictMode>,
)

Usage

import { useModal } from "react-overlay-modal"

function ModalContent() {
  const { closeModal } = useModal()
  return (
    <div>
      <h2>✨ Welcome to Cool Modal!</h2>
      <button
        onClick={closeModal}
        className="btn secondary"
      >
        ✨ Close Modal
      </button>
    </div>
  )
}

export default function Page() {
  const { showModal } = useModal()
  return (
    <button
      className="btn primary"
      onClick={() => showModal({
        content: <ModalContent />,
        size: "md"
      })}
    >
      ✨ Open Cool Modal
    </button>
  )
}

API

Props

PropertyTypeDefault ValueDescription
sizeModalSize"md"Specifies the size of the modal (see ModalSize).
disableOutsideClickbooleanfalseIf true, disables clicking outside the modal to close it.
contentReactElementThe content to display inside the modal.
classNamestringundefinedPass class to handle the overlay style.
bodyPropsModalBodyPropsundefinedSee body props for more details (see ModalBodyProps)

Modal Size

ValueDescription
smSmall modal size.
mdMedium modal size. (default)
lgLarge modal size.
xlExtra-large modal size.
2xlDouble extra-large modal size.
3xlTriple extra-large modal size.
fullFullscreen modal.

Modal Body Props

PropertyDescription
classNameOptional string for CSS class name
aria-labelledbyOptional string to reference labeling element(s)
aria-describedbyOptional string to reference describing element(s)
aria-hiddenOptional boolean to hide from assistive tech
aria-liveOptional region updates announcement: "off", "polite", or "assertive"
aria-modalOptional boolean indicating if element is modal
aria-roleOptional string defining element's ARIA role
tabIndexOptional number for tab order
roleOptional string defining element's role
onKeyDownOptional keyboard event handler
onFocusOptional focus event handler
idOptional string identifier
styleOptional React CSS properties object
onClickOptional mouse click event handler
1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago

0.0.0

8 months ago