Overlays

Modal

A modal dialog on the native <dialog>, opened and closed by HTML command buttons, with no JavaScript. JxModalTrigger opens it.

JxModal import 'package:jx_cli/ui.dart' Available since 0.0.2

Preview

Delete this project?

The project, its pages and its deployments are removed. This cannot be undone.

When to use it

  • Confirming a destructive action.
  • A short form that should not leave the page.

Minimal example

Dart
JxModalTrigger(target: 'hello', [.text('Open')])

JxModal(id: 'hello', title: 'Hello', [p([.text('A native dialog.')])])

Complete example

A full Jaspr component you can paste into a project.

Dart
import 'package:jaspr/jaspr.dart';
import 'package:jx_cli/ui.dart';

/// Invite form in a dialog. The send button submits, cancel closes.
class InviteDialog extends StatelessComponent {
  const InviteDialog({required this.onInvite, super.key});

  final VoidCallback onInvite;

  @override
  Component build(BuildContext context) {
    return Component.fragment([
      const JxModalTrigger(target: 'invite', variant: JxButtonVariant.outline, [.text('Invite a teammate')]),
      JxModal(
        id: 'invite',
        title: 'Invite a teammate',
        description: 'They get an email with a link to join this workspace.',
        actions: [
          JxButton(variant: JxButtonVariant.ghost, attributes: JxModal.closeAttributes('invite'), [.text('Cancel')]),
          JxButton(onClick: onInvite, attributes: JxModal.closeAttributes('invite'), [.text('Send invite')]),
        ],
        const [JxTextField(id: 'invite-email', label: 'Email', type: InputType.email)],
      ),
    ]);
  }
}

Properties

Property Type Default Description
children List<Component> required Body of the dialog.
id String required Referenced by JxModalTrigger.target .
title String required Heading, also the accessible name ( aria-labelledby ).
description String? null Text under the title, linked with aria-describedby .
actions List<Component>? null Buttons at the bottom, aligned to the end.
size JxModalSize md Maximum width: sm 24rem, md 32rem, lg 48rem.
dismissible bool true A click on the backdrop closes it ( closedby="any" ).
closeLabel String 'Close' Accessible name of the × button.
classes String? null Extra JX classes, appended after the component classes.

Related types

Name Role
JxModalTrigger(children, target:, variant:, size:) A JxButton that opens the dialog ( command="show-modal" ).
JxModal.closeAttributes(id) Attributes that turn any button into a close button.

Customizing

  • Pass classes to add or override utilities: they come last, so classes: 'w:full' wins over the default width.
  • The backdrop is dimmed and blurred with backdrop: classes.

Responsive

  • The dialog is calc(100% - 2rem) wide on phones and scrolls inside when taller than 85% of the screen.

Accessibility

  • Native modal: focus moves inside and stays there, the page behind is inert, Escape closes it and focus returns to the trigger.
  • Named by its title, described by its description.
  • The fade-in is disabled when the system asks for reduced motion.

Limitations

  • Command buttons (command, commandfor) are supported by current browsers; on older ones call showModal() from a client component.
  • Closing on a backdrop click relies on closedby, not yet available everywhere; Escape and the buttons always work.