11.0.1 • Published 7 days ago

@dopt/react-modal v11.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 days ago

@dopt/react-modal

Overview

A React modal component for building experiences with Dopt.

You can use the modal component out of the box as a pre-built component or break out and use it headlessly with your own UI component.

Learn more about how to use this component with Dopt →

Installation

ℹ️ If you are using a particular React framework like Next.js, please check out our framework specific docs.

# npm
npm install @dopt/react-modal

# Yarn
yarn add @dopt/react-modal

# pnpm
pnpm add @dopt/react-modal

Usage

The default export from @dopt/react-modal is a collection of components that you can use to structure and compose a modal.

import Modal, { useModal } from '@dopt/react-modal';

function MyModal() {
  const modal = useModal('my-flow.four-pandas-jam');

  return (
    <Modal.Root active={modal.active}>
      <Modal.Overlay />
      <Modal.Content>
        <Modal.Header>
          <Modal.Title>{modal.title}</Modal.Title>
          <Modal.DismissIcon onClick={modal.dismiss} />
        </Modal.Header>
        <Modal.Body>{modal.body}</Modal.Body>
        <Modal.Footer>
          <Modal.DismissButton onClick={modal.dismiss}>
            {modal.dismissLabel}
          </Modal.DismissButton>
          <Modal.CompleteButton onClick={modal.complete}>
            {modal.completeLabel}
          </Modal.CompleteButton>
        </Modal.Footer>
      </Modal.Content>
    </Modal.Root>
  );
}

Check out our modal example and our headless modal example for more in-depth usage.

Props

Root

The root element of the modal. Extends HTMLDivElement.

NameTypeDescription
active?booleanDetermines the visibility of the component (default: false)
children?ReactNodeThe contents of the component
container?HTMLElementAn HTML element to portal the component to (default: document.body)
lockScroll?booleanDetermines if the document body should be scrollable when the component is active (default: true)
theme?ThemeA theme definition to attach to the component

Overlay

The overlay element that renders behind the modal content. Extends HTMLDivElement.

NameTypeDescription
container?HTMLElementAn HTML element to portal the component to (default: document.body)
theme?ThemeA theme definition to attach to the component

Content

The modal content. Extends HTMLDivElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Header

The header of the modal. Extends HTMLElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Title

The title of the modal. Extends HTMLHeadingElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

DismissIcon

The dismiss icon of the modal. Extends HTMLButtonElement.

NameTypeDescription
theme?ThemeA theme definition to attach to the component

Body

The body of the modal. Extends HTMLDivElement.

NameTypeDescription
children?RichTextThe rich text contents of the component
theme?ThemeA theme definition to attach to the component

Footer

The footer of the modal. Extends HTMLElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

DismissButton

The dismiss button of the modal. Extends HTMLButtonElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

CompleteButton

The complete button of the modal. Extends HTMLButtonElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Hooks

If you are planning to only use the modal headlessly, you can import the hooks alone using @dopt/react-modal/hooks.

useModal

  • useModal(id: string): Modal

A React hook for accessing and updating a modal's state.

import { useModal } from '@dopt/react-modal';
import RichText from '@dopt/react-rich-text';

function MyModal() {
  const {
    id,
    title,
    body,
    completeLabel,
    dismissLabel,
    active,
    completed,
    dismissed,
    complete,
    dismiss,
  } = useModal('my-flow.four-pandas-jam');

  return (
    <div>
      <div id="states">
        <div>modal.active: {active}</div>
        <div>modal.completed: {completed}</div>
        <div>modal.dismissed: {dismissed}</div>
      </div>
      <div id="actions">
        <button onClick={complete}>{completeLabel}</button>
        <button onClick={dismiss}>{dismissLabel}</button>
      </div>
      <div id="content">
        <div>modal.title: {title}</div>
        <div>
          modal.body: <RichText.Root>{body}</RichText.Root>
        </div>
        <div>modal.completeLabel: {completeLabel}</div>
        <div>modal.dismissLabel: {dismissLabel}</div>
      </div>
    </div>
  );
}

Styling API

Learn more about styling and theming →

NameSelectorDescription
root.dopt-modalRoot element
overlay.dopt-modal__overlayOverlay shown underneath content
content.dopt-modal__contentContent container
header.dopt-modal__headerHeader containing title and dismiss icon
title.dopt-modal__titleTitle heading
dismissIcon.dopt-modal__dismiss-iconDismiss icon button
body.dopt-modal__bodyBody content
footer.dopt-modal__footerFooter containing dismiss and complete buttons
dismissButton.dopt-modal__dismiss-buttonDismiss button
completeButton.dopt-modal__complete-buttonComplete button

Types

Modal

Modal state accessors and methods for updating state along with content configured in Dopt.

interface Modal {
  id: string;

  title: string | null | undefined;
  body: RichText | null | undefined;

  completeLabel: string | null | undefined;
  dismissLabel: string | null | undefined;

  active: boolean;

  completed: boolean;
  dismissed: boolean;

  complete: () => void;
  dismiss: () => void;
}
11.0.1

7 days ago

11.0.0

14 days ago

10.0.1

20 days ago

9.0.5

2 months ago

9.0.4

2 months ago

9.0.3

2 months ago

9.0.2

3 months ago

9.0.1

3 months ago

9.0.0

6 months ago

8.1.0

7 months ago

8.0.1

7 months ago

8.0.0

7 months ago

7.0.5

7 months ago

7.0.4

8 months ago

7.0.3

8 months ago

7.0.2

8 months ago

7.0.1

8 months ago

7.0.0

8 months ago

6.2.0

9 months ago

6.1.0

9 months ago

6.0.0

9 months ago

5.0.2

9 months ago

5.0.1

9 months ago

5.0.0

9 months ago

4.2.0

9 months ago

4.1.1

9 months ago

4.1.0

9 months ago

4.0.0

9 months ago

3.0.0

10 months ago

2.0.0

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago