Skip to main content

Modal Component

The <Modal> component provides a flexible way to create modal dialogs within your application. It supports customizable headers and provides imperative control through a ref.

Basic Usage​

The modal can be controlled imperatively using a ref.

import { useRef } from "react";
import { Div, Text, Button, Modal, ModalRef } from "react-better-html";

function App() {
const myModalRef = useRef<ModalRef>(null);

return (
<Div>
<Button
text="Open Modal"
onClick={() => {
myModalRef.current?.open();
}}
/>

<Modal title="My Modal" ref={myModalRef}>
<Text>Modal content here</Text>
</Modal>
</Div>
);
}

Common Props​

  • maxWidth - the maximum width of the modal. Defaults to 30% smaller than app.contentMaxWidth from the <BetterHtmlProvider> config. 660 looks good for small modals.
  • icon - an icon rendered before the title.
  • title - this prop enables the header of the modal and renders a title for the modal.
  • titleColor - the color of the title text.
  • description - this prop requires title prop and renders a description text underneath it.
  • descriptionColor - the color of the description text.
  • headerTextAlign - aligns the header content to the left (default) or to the center.
  • headerBackgroundColor - the background color of the modal's header.
  • backgroundColor - the background color of the modal.
  • withoutCloseButton - hides the X button.
  • closeButtonLight - renders the X button in a light color (useful with a dark headerBackgroundColor).
  • defaultIsOpened - opens the modal on mount.
  • overflow - the overflow of the modal's body.
  • outsideComponentAbove / outsideComponentBellow - elements rendered outside of the modal box.
  • onOpen - a callback function that is called when the modal is opened.
  • onClose - a callback function that is called when the modal is closed.
  • children - the content of the modal.

All padding props (padding, paddingInline, ...) are applied to the modal's body.

tip

The modal closes when the Escape key is pressed.

Ref API​

The modal can be controlled using a ref with the following methods:

type ModalRef = {
isOpened: boolean;
open: () => void;
close: () => void;
};
  • open() - opens the modal
  • close() - closes the modal
  • isOpened - a boolean indicating whether the modal is currently opened or not

Subcomponents​

A number of components in the library have a subcomponent feature witch is like a preset of the same component that is frequently used.

Modal.confirmation​

This component is a confirmation modal with Continue and Cancel buttons. It closes when any of the buttons is clicked.

import { useRef } from "react";
import { Div, Button, Modal, ModalRef } from "react-better-html";

function App() {
const confirmationModalRef = useRef<ModalRef>(null);

return (
<Div>
<Button
text="Continue"
onClick={() => {
confirmationModalRef.current?.open();
}}
/>

<Modal.confirmation
onContinue={() => {
console.log("Confirmed!");
}}
ref={confirmationModalRef}
/>
</Div>
);
}
  • message - the text inside the modal (has a default value)
  • continueButtonText - the text of the continue button (defaults to Continue)
  • continueButtonLoaderName - the loader name for the continue button
  • cancelButtonText - the text of the cancel button (defaults to Cancel)
  • onContinue - called when the continue button is clicked
  • onCancel - called when the cancel button is clicked

Modal.destructive​

This component is a destructive modal with Delete and Cancel buttons.

import { useRef } from "react";
import { Div, Button, Modal, ModalRef } from "react-better-html";

function App() {
const deleteModalRef = useRef<ModalRef>(null);

return (
<Div>
<Button
text="Delete"
onClick={() => {
deleteModalRef.current?.open();
}}
/>

<Modal.destructive
onDelete={() => {
console.log("Deleted!");
}}
ref={deleteModalRef}
/>
</Div>
);
}
  • message - the text inside the modal (has a default value)
  • deleteButtonText - the text of the delete button (defaults to Delete)
  • deleteButtonIconName - the icon of the delete button (defaults to trash)
  • deleteButtonLoaderName - the loader name for the delete button
  • cancelButtonText - the text of the cancel button (defaults to Cancel)
  • onDelete - called when the delete button is clicked
  • onCancel - called when the cancel button is clicked

Used With Plugins​

The <Modal> component can be used with the react-router-dom plugin to set a query parameter when the modal is opened. This is especially useful when you want the path to changed when a modal is opened for analytics purposes. The only thing you need to do beside applying the plugin is to pass a name prop to the modal.

<Modal title="Edit user" name="edit-user" ref={editUserModalRef}>
...
</Modal>

While the modal is opened the URL contains the ?edit-user-modal=opened query parameter.