Modal | jaspr_daisyui_components
jaspr_daisyui_components
pub.dev ↗

Modal

Modal is used to show a dialog or a box when you click a button.

Class name Type Description
modal Component Modal
modal-box Part The content part
modal-action Part Actions part (buttons, etc.)
modal-backdrop Part Label that covers the page when modal is open so we can close the modal by clicking outside
modal-toggle Part Hidden checkbox that controls the state of modal
modal-open Modifier Keeps the modal open (you can add this class using JS)
modal-top Placement Moves the modal to top
modal-middle Placement Moves the modal to middle
modal-bottom Placement Moves the modal to bottom
modal-start Placement Moves the modal to start horizontally
modal-end Placement Moves the modal to end horizontally

There are 4 methods to use modals#

Method Opens/closes by Esc key can close it Locks background interactions
1 Dialog (Recommended) JavaScript ✅ Yes ✅ Yes
2 Popover HTML attributes ✅ Yes ❌ No
3 Checkbox (Legacy) Hidden checkbox ❌ No ❌ No
4 Anchor Link (Legacy) URL parameters ❌ No ❌ No

Opening a modal adds a scrollbar-gutter to the page to avoid layout shift on operating systems that have a fixed scrollbar. On recent Chromium based browsers vertical scrollbar presence is detected automatically. On Safari and on mobile devices the scrollbar is displayed as overlay so there will not be gutter. On Firefox you need to detect the presence of vertical scrollbar and set the scrollbar-gutter: stable or scrollbar-gutter: unset on :root element yourself. If you don't want to use this feature, you can exclude rootscrollgutter.

HTML dialog element is a native way to create modals. It is accessible and we can close the modal using Esc key.
We can open the modal using JS ID.showModal() method and close it using ID.close() method.
The ID must be unique for each modal.

Dialog modal

opens on click using ID.showModal() method. can be closed using ID.close() method
<!-- Open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_1.showModal()">open modal</button>
<dialog id="my_modal_1" class="modal">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="modal-action">
      <form method="dialog">
        <!-- if there is a button in form, it will close the modal -->
        <button class="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
Component.fragment([
  Btn([Component.text('open modal')], attributes: {'onclick': 'my_modal_1.showModal()'}),
  dialog(
    [
      ModalBox(
        [
          h3([Component.text('Hello!')], classes: 'text-lg font-bold'),
          p([Component.text('Press ESC key or click the button below to close')], classes: 'py-4'),
          ModalAction([form([Btn([Component.text('Close')])], attributes: {'method': 'dialog'})]),
        ],
      ),
    ],
    classes: 'modal',
    id: 'my_modal_1',
  ),
]);

Dialog modal, closes when clicked outside

there is a second form with 'modal-backdrop' class and it covers the screen so we can close the modal when clicked outside
<!-- Open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_2.showModal()">open modal</button>
<dialog id="my_modal_2" class="modal">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click outside to close</p>
  </div>
  <form method="dialog" class="modal-backdrop">
    <button>close</button>
  </form>
</dialog>
Component.fragment([
  Btn([Component.text('open modal')], attributes: {'onclick': 'my_modal_2.showModal()'}),
  dialog(
    [
      ModalBox(
        [
          h3([Component.text('Hello!')], classes: 'text-lg font-bold'),
          p([Component.text('Press ESC key or click outside to close')], classes: 'py-4'),
        ],
      ),
      ModalBackdrop([button([Component.text('close')])], attributes: {'method': 'dialog'}),
    ],
    classes: 'modal',
    id: 'my_modal_2',
  ),
]);

Dialog modal with a close button at corner

<!-- You can open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_3.showModal()">open modal</button>
<dialog id="my_modal_3" class="modal">
  <div class="modal-box">
    <form method="dialog">
      <button class="btn btn-sm btn-circle btn-ghost absolute right-2 top-2">✕</button>
    </form>
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click on ✕ button to close</p>
  </div>
</dialog>
Component.fragment([
  Btn([Component.text('open modal')], attributes: {'onclick': 'my_modal_3.showModal()'}),
  dialog(
    [
      ModalBox(
        [
          form(
            [
              Btn(
                [Component.text('✕')],
                classes: 'absolute right-2 top-2',
                style: [BtnStyle.ghost],
                size: BtnSize.sm,
                modifier: BtnModifier.circle,
              ),
            ],
            attributes: {'method': 'dialog'},
          ),
          h3([Component.text('Hello!')], classes: 'text-lg font-bold'),
          p([Component.text('Press ESC key or click on ✕ button to close')], classes: 'py-4'),
        ],
      ),
    ],
    classes: 'modal',
    id: 'my_modal_3',
  ),
]);

Dialog modal with custom width

You can use any w-* and max-w-* utility class to customize the width
<!-- You can open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_4.showModal()">open modal</button>
<dialog id="my_modal_4" class="modal">
  <div class="modal-box w-11/12 max-w-5xl">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Click the button below to close</p>
    <div class="modal-action">
      <form method="dialog">
        <!-- if there is a button, it will close the modal -->
        <button class="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
Component.fragment([
  Btn([Component.text('open modal')], attributes: {'onclick': 'my_modal_4.showModal()'}),
  dialog(
    [
      ModalBox(
        [
          h3([Component.text('Hello!')], classes: 'text-lg font-bold'),
          p([Component.text('Click the button below to close')], classes: 'py-4'),
          ModalAction([form([Btn([Component.text('Close')])], attributes: {'method': 'dialog'})]),
        ],
        classes: 'w-11/12 max-w-5xl',
      ),
    ],
    classes: 'modal',
    id: 'my_modal_4',
  ),
]);

Responsive

Modal goes bottom on SM screen size, goes middle on MD screen size
<!-- Open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_5.showModal()">open modal</button>
<dialog id="my_modal_5" class="modal modal-bottom sm:modal-middle">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="modal-action">
      <form method="dialog">
        <!-- if there is a button in form, it will close the modal -->
        <button class="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
Component.fragment([
  Btn([Component.text('open modal')], attributes: {'onclick': 'my_modal_5.showModal()'}),
  dialog(
    [
      ModalBox(
        [
          h3([Component.text('Hello!')], classes: 'text-lg font-bold'),
          p([Component.text('Press ESC key or click the button below to close')], classes: 'py-4'),
          ModalAction([form([Btn([Component.text('Close')])], attributes: {'method': 'dialog'})]),
        ],
      ),
    ],
    classes: 'modal modal-bottom sm:modal-middle',
    id: 'my_modal_5',
  ),
]);

Method 2. HTML popover#

Popover is similar to dialog but it still allows focusing on the background elements. It is accessible and we can close the modal using Esc key.

Popover modal

<!-- The button's popovertarget attribute points to the modal's ID. ID must be unique for each modal -->
<button class="btn" popovertarget="my-modal-1">Open</button>
<div class="modal" id="my-modal-1" popover="">
  <div class="modal-box">
    <h3 class="font-bold text-lg">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="modal-action">
      <button popovertarget="my-modal-1" popovertargetaction="hide">close</button>
    </div>
  </div>
</div>
Component.fragment([
  Btn([Component.text('Open')], attributes: {'popovertarget': 'my-modal-1'}),
  Modal(
    [
      ModalBox(
        [
          h3([Component.text('Hello!')], classes: 'font-bold text-lg'),
          p([Component.text('Press ESC key or click the button below to close')], classes: 'py-4'),
          ModalAction(
            [
              button(
                [Component.text('close')],
                attributes: {
                  'popovertarget': 'my-modal-1',
                  'popovertargetaction': 'hide',
                },
              ),
            ],
          ),
        ],
      ),
    ],
    id: 'my-modal-1',
    attributes: {'popover': ''},
  ),
]);

Popover modal closes when clicked outside

<!-- The button's popovertarget attribute points to the modal's ID. ID must be unique for each modal -->
<button class="btn" popovertarget="my-modal-2">Open</button>
<div class="modal" id="my-modal-2" popover="">
  <div class="modal-box">
    <h3 class="font-bold text-lg">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
  </div>
  <div class="modal-backdrop">
    <button popovertarget="my-modal-2" popovertargetaction="hide">close</button>
  </div>
</div>
Component.fragment([
  Btn([Component.text('Open')], attributes: {'popovertarget': 'my-modal-2'}),
  Modal(
    [
      ModalBox(
        [
          h3([Component.text('Hello!')], classes: 'font-bold text-lg'),
          p([Component.text('Press ESC key or click the button below to close')], classes: 'py-4'),
        ],
      ),
      div(
        [
          button(
            [Component.text('close')],
            attributes: {'popovertarget': 'my-modal-2', 'popovertargetaction': 'hide'},
          ),
        ],
        classes: 'modal-backdrop',
      ),
    ],
    id: 'my-modal-2',
    attributes: {'popover': ''},
  ),
]);

Method 3. checkbox legacy#

A hidden checkbox can control the state of modal and labels can toggle the checkbox so we can open/close the modal.

A link adds a parameter to the URL and you only see the modal when the URL has that parameter
When modal is closed, the page will scroll to the top because of the anchor link. Anchor links might not work well on some SPA frameworks. If there are problems, use the other methods