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.
- Basic
- Customizable
- Outside Components
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>
);
}
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
icon="infoI"
headerBackgroundColor="#0000ff"
title="My Modal"
titleColor="#ffffff"
description="This is my modal for reference"
descriptionColor="#e8e8e8"
headerTextAlign="center"
closeButtonLight
ref={myModalRef}
>
<Text>Modal content here</Text>
</Modal>
</Div>
);
}
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="Image preview"
outsideComponentAbove={<Text color="#ffffff">1 / 12</Text>}
outsideComponentBellow={<Button.secondary text="Download" />}
ref={myModalRef}
>
<Text>Modal content here</Text>
</Modal>
</Div>
);
}
Elements rendered outside of the modal box, above and bellow it.
Common Props
maxWidth- the maximum width of the modal. Defaults to 30% smaller thanapp.contentMaxWidthfrom the<BetterHtmlProvider>config.660looks 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 requirestitleprop and renders a description text underneath it.descriptionColor- the color of the description text.headerTextAlign- aligns the header content to theleft(default) or to thecenter.headerBackgroundColor- the background color of the modal's header.backgroundColor- the background color of the modal.withoutCloseButton- hides theXbutton.closeButtonLight- renders theXbutton in a light color (useful with a darkheaderBackgroundColor).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.
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 modalclose()- closes the modalisOpened- 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 toContinue)continueButtonLoaderName- the loader name for the continue buttoncancelButtonText- the text of the cancel button (defaults toCancel)onContinue- called when the continue button is clickedonCancel- 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 toDelete)deleteButtonIconName- the icon of the delete button (defaults totrash)deleteButtonLoaderName- the loader name for the delete buttoncancelButtonText- the text of the cancel button (defaults toCancel)onDelete- called when the delete button is clickedonCancel- 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.