Actions

Modal

A native dialog with contained focus, Escape to close and focus returned to its trigger.

Class nameTypeDescription
modalplf:modalComponentA native dialog
modal-headerplf:modal-headerPartTitle row with the close button
modal-titleplf:modal-titlePartThe dialog's heading
modal-descriptionplf:modal-descriptionPartMuted text under the title
modal-closeplf:modal-closePartThe close button, which draws an x while empty
modal-actionplf:modal-actionPartThe row of actions at the end
modal-boxplf:modal-boxPartThe content box beside a floating close button
modal-floating-closeplf:modal-floating-closeModifierThe close button floats on the corner

The dialog’s first heading names it; a dialog without one needs aria-label.

Floating close button

Keep the close button as the dialog’s first child and wrap the rest in modal-box.

Save from a dialog

Run your request from the form’s submit; when it succeeds, close the dialog with PLF.Modal.getOrCreateInstance(dialog).close() and confirm with a Toast.

JavaScript

Modal needs JavaScript. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-modal Marks the <dialog>
data-plf-trigger="modal:open" Opens the dialog named by data-plf-target; its aria-keyshortcuts, such as Control+K Meta+K, also opens it outside text fields
data-plf-target The dialog’s id, without #, in the same root
data-plf-trigger="modal:close" Inside the dialog, closes it; so does a submitted <form method="dialog">
data-plf-close-on-backdrop Closes on a click outside the dialog
Method Behavior
PLF.Modal.getOrCreateInstance(dialog) Returns the instance, creating it for valid markup
PLF.Modal.getInstance(dialog) Returns the instance or null
instance.open(trigger) Opens; focus returns to the optional trigger. Returns false when canceled or another modal is open
instance.close() Closes; returns false when canceled
instance.destroy() Closes without events and removes listeners
PLF.Dialog.fire(options), fire(title, text, icon) Opens a confirmation or a result with SweetAlert’s options, on dialogs it creates in the root, and returns a promise for the outcome (Dialogs from JavaScript)
PLF.Dialog.mixin(options) Returns a Dialog whose calls start from these options, such as labels in your language
Event When
plf:modal:before-open Before opening; cancelable
plf:modal:open After opening
plf:modal:before-close Before closing; cancelable
plf:modal:close After closing

Each event’s detail:

Property Events Value
instance All The Modal instance
reason All api, trigger, keyboard or backdrop
trigger All The open or close button, or the trigger passed to open(); otherwise null
action before-close, close Only on an Alert Dialog: confirm when the confirm button closes it, otherwise cancel
Key Action
Tab, Shift+Tab Move between controls inside the dialog
Escape Closes the dialog, through plf:modal:before-close

One modal is open at a time; stacked modals are not supported.

React

Modal from @pelatform/react renders this dialog: the application opens it with open and closes it in onOpenChange, and each part is a component of its own.

Prop Values Default
open true while the dialog is open Required
onOpenChange (open, reason), called when the dialog asks to close: trigger (its close button), keyboard (Escape) or backdrop; keep open to stay open None
closeOnBackdrop true closes on a click outside the dialog false
floatingClose true floats the close button on the corner: put ModalClose first and the rest in ModalBox false

The parts:

Part Element
ModalHeader A div with modal-header
ModalTitle An h2 with modal-title, or another heading with level, which names the dialog
ModalDescription A p with modal-description, which describes the dialog
ModalClose A button with modal-close: the drawn x and the name Close unless given others
ModalAction A div with modal-action
ModalBox A div with modal-box

The content mounts when the dialog opens, so autoFocus takes the first focus and a form starts fresh each time.

Variable Default
--plf-modal-padding Space 6 (24px)
--plf-modal-radius Extra large radius (12px)
--plf-modal-bg, --plf-modal-color Surface and text
--plf-modal-border Subtle border
--plf-modal-shadow Large shadow
--plf-modal-backdrop-bg --plf-color-backdrop

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons