Modal

A dialog that opens over the page, loaded from the server.

Overlays Added in v0.53.0 Updated

Default


{ui button,
	title: 'Delete page',
	class: 'bg-destructive',
	attrs: [
		'hx-get' => "{$page->url()}:modal",
		'hx-swap' => 'none',
	],
/}

A modal is not a hidden element that a script reveals. It is a fragment the server returns, filled into an empty region of the layout, so the server decides what is inside and nothing about it is stored in the browser.

The layout holds one <div id="modal"></div>. The trigger is a request for a :block of the page, and that block wraps the modal in {response 'modal', target: '#modal'}, so the response lands in the region and the trigger needs only hx-swap="none". A response is never part of the page: it renders only when its own :block is requested, so the modal never shows on a plain visit.

{ui button,
	title: 'Delete page',
	class: 'bg-destructive',
	attrs: [
		'hx-get' => "{$page->url()}:modal",
		'hx-swap' => 'none',
	],
/}

{response 'modal', target: '#modal'}
	{component modal, title: 'Delete this page?'}
		The page and everything below it will be removed.
	{/component}
{/response}

This is what the server returns for the button above:

{component modal,
	title: 'Delete this page?',
	description: 'This cannot be undone.',
}
	The page and everything below it will be removed.
	{block footer}
		{ui button,
			title: 'Cancel',
			class: 'button-outline',
			attrs: ['data-modal-close' => true],
		/}
		{ui button,
			title: 'Delete',
			class: 'bg-destructive',
			attrs: ['data-modal-close' => true],
		/}
	{/block}
{/component}

Closing and focus

The shared ui.js does what CSS cannot. When a modal appears it moves focus to the dialog, keeps Tab and Shift+Tab inside it, and stops the page behind from scrolling. Escape, a click on the backdrop and any element with data-modal-close remove it, and focus goes back to the button that opened it. The close button in the corner is always there.

To close it after a save, respond with a swap that empties the region, as any other fragment does. The dialog is a div with role="dialog" and aria-modal, not the native dialog element, whose showModal() is newer than the browsers the UI supports.

modal-sm and modal-lg set --modal-width, which is 32rem by default.