How to close a modal Form

In my project, there is a contact form displayed as a modal. When you press the button, the modal opens. You enter your name, email, and message, then press “Send”—the email is sent. So far, so good, but the modal remains open. How can I close it automatically after sending the email?

Additionally, I added a “Success” message below the form, which is also shown as a modal. This second modal says, “Thanks for the mail, we’ll get back to you soon.”
How can I ensure that this success modal appears in front of the contact modal (e.g., by setting the z-index)?
And how can I close both modals automatically after 10 seconds?

Linking to another page is not an option because the contact form is in the footer on every page, and I want to stay on the current page.

@elementsbot Can you help Ulrich?

Great questions, Ulrich! Let’s harness the Power of Elements to create a seamless modal experience for your contact form. Here’s how to handle modal close actions, show a success modal above your contact modal, and auto-close everything after a delay—no page reloads required!

1. Automatically Close the Contact Modal After Sending

If you’re using the built-in Form and Modal components in Elements, you’ll want to:

  1. Trigger the Modal Close when the form submits successfully.
  2. Show a Success Modal above the Contact Modal.
  3. Automatically close both modals after 10 seconds.

Example: Setting it Up

a) Template Structure (Overview)

  • Modal 1: Your Contact Form
  • Modal 2: Success Message (hidden until form submits)

b) Use AlpineJS for Logic (supported in Elements!)

Here’s some template pseudocode you can adapt directly in Elements:

<div x-data="{
    showContact: false,
    showSuccess: false,
    handleSubmit() {
        // Simulate successful form submit
        this.showContact = false;
        this.showSuccess = true;
        setTimeout(() => { this.showSuccess = false }, 10000);
    }
}">
    <!-- Trigger Button -->
    <button @click="showContact = true">Contact Us</button>

    <!-- Contact Modal -->
    <modal x-show="showContact" :z-index="50">
        <form @submit.prevent="handleSubmit">
            <!-- form fields here (name, email, message) -->
            <button type="submit">Send</button>
        </form>
        <modal-close />
    </modal>

    <!-- Success Modal -->
    <modal x-show="showSuccess" :z-index="60">
        <div>Thanks for the mail, we'll get back to you soon.</div>
        <modal-close />
    </modal>
</div>

Key details:

  • Use x-show and Alpine’s data binding to control modal visibility.
  • Set z-index (or use Modal properties in Elements) so the Success modal sits above the Contact modal (60 above 50).
  • The handleSubmit() function:
    • Hides the Contact modal
    • Shows the Success modal
    • Then hides the Success modal after 10 seconds.

Tip: You can place this AlpineJS code directly within Elements’ components, or adapt to theme controls if you’re building a custom component.

2. Modal Close Components

Use the <modal-close /> component within each modal for additional close buttons.

  • Users can manually close modals instantly.
  • You could use Alpine timers for automatic closing, as shown above.

3. Real-World Use (No Coding!)

Of course, if you’re just using the drag-and-drop Form, Modal, and Success components:

  • Add a Success component to the Form, and set up conditional logic on submit to show/hide.
  • Use the Modal component’s built-in close button (Modal Close) for user-initiated closing.
  • For fine control (e.g., timers/media overlays), AlpineJS lets you go next-level.

Need visual step-by-step? The Elements Modal documentation shows how easy it is to trigger/show/hide modals. Also, check out the YouTube Modal tutorial from Realmac Software.

If you get stuck, let me know what your setup looks like and I can write a more tailored example! Or just @elementsbot to ask anything else :speech_balloon:

The Power of Elements—one modal at a time! :rocket:

The Elements Modal documentation link is NOT working

Great catch, PMUK! It looks like the Elements Modal documentation link isn’t currently working. Sorry about that—sometimes links move faster than I do! :sweat_smile:

But don’t worry, I’ve got your back. Here’s everything you need to set up and control modals in Elements, straight from the latest docs and my digital brain:


How to Close a Modal Automatically in Elements

1. Use AlpineJS to Control and Close Modals

Elements fully supports AlpineJS, allowing you to manage visibility, stacking (z-index), and timers with ease.

Here’s a complete example you can drop directly into a custom component in Elements (or adapt to your setup):

<div x-data="{
    showContact: false,
    showSuccess: false,
    handleSubmit() {
        this.showContact = false;
        this.showSuccess = true;
        setTimeout(() => { this.showSuccess = false }, 10000);
    }
}">
    <!-- Trigger Button -->
    <button @click="showContact = true">Contact Us</button>

    <!-- Contact Modal -->
    <modal x-show="showContact" style="z-index:50;">
        <form @submit.prevent="handleSubmit">
            <!-- form fields here -->
            <button type="submit">Send</button>
        </form>
        <modal-close />
    </modal>

    <!-- Success Modal (above Contact Modal) -->
    <modal x-show="showSuccess" style="z-index:60;">
        <div>Thanks for the mail, we'll get back to you soon.</div>
        <modal-close />
    </modal>
</div>
  • x-show toggles the display of each modal component.
  • Z-Index: You control which modal is above by simply setting style="z-index:XX;", or use the Modal background property in the Elements UI.
  • Timer: When the form submits, the Contact modal closes, the Success modal pops up, and after 10 seconds, everything closes automatically.
  • Manual Close: Modal Close components give users the option to close either modal right away.

No coding needed for simple use: Drag the Modal and Form components into your canvas! AlpineJS just gives you extra control if you want things like timeouts, custom stacking, etc.


2. Modal and Modal Close Components — The Essentials

From the Elements docs:

  • Modal: Triggers a modal window above your content. You can show/hide via Alpine or the built-in trigger settings.
  • Modal Close: Add this anywhere in your modal to provide a close button/icon. You can style it to fit your theme.

Extra Help


If you want to flag the broken docs link for the Realmac Team, just let me know! Otherwise, you’re all set to build elegant, user-friendly modals with Elements—the most modern website builder for Mac!

Happy building, and enjoy the Power of Elements! :rocket:

Thanks for the idea of ​​asking the bot. I’ll keep that in mind in the future.

@elementsbot I already have a modal with lots of elements and colors and so on. I’m looking for a way to do this with my form already created in Elements.

By the way: There is no Modal Component Close button.

Thanks for the details, Ulrich! Let’s fine-tune the solution for your current Elements setup:


1. Built-in Modal Close Button in Elements

You’re right—the Modal Component itself doesn’t automatically add a visible “close” button. Instead, Elements provides a separate Modal Close component that you add inside your modal content wherever you want a close trigger (e.g., top right, bottom, etc.).

How to add a close button:

  • Step 1: In your modal, drag in the Modal Close component from the components list.
  • Step 2: Place it wherever makes sense (top-right is typical).
  • Step 3: Style it as you like! It can be a button, icon, text, or any element.

The Modal Close component instantly closes the modal when clicked—no coding or extra setup needed.
Full Modal Close docs here :up_right_arrow:


2. Show/Hide Success Message:

If your form is already built with Elements and you want to show a success message after the form submits, you have two options:

Option A: Use the Form’s Built-In Success State

  • Elements’ Form component includes a Success child component slot.
  • Add a Success component inside your form. It will show automatically after the form submits, replacing the form content, without needing modals/z-index/timers.

Option B: Advanced — Auto-close with AlpineJS for Maximum Control

If you want the “success” message as a separate modal (above your main modal), AlpineJS makes it possible—even without major changes to your layout.

You can wrap your existing modal in an AlpineJS root and handle state like this (even if your modal is complex):

<div x-data="{
    showSuccess: false,
    closeAfterSubmit() {
        this.showSuccess = true;
        setTimeout(() => { this.showSuccess = false }, 10000);
    }
}">
    <modal>
        <form @submit.prevent="closeAfterSubmit">
            <!-- your existing form fields and elements here -->
            <button type="submit">Send</button>
        </form>
        <!-- Place Modal Close anywhere for manual close: -->
        <modal-close />
        <!-- Success Overlay -->
        <div 
          x-show="showSuccess"
          style="position:absolute;top:0;left:0;width:100%;height:100%;z-index:50;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,0.8)">
            <div style="padding:2rem;background:#fff;border-radius:1rem;box-shadow:0 2px 8px #0002;">
                Thanks for the mail, we'll get back to you soon.
            </div>
        </div>
    </modal>
</div>
  • When the form submits, the “Success” overlay appears for 10 seconds and disappears.
  • The Modal Close component always gives the user manual control.
  • You can style everything to match your brand/colors.

3. Stacking Modals / Z-Index

If you want a FULL SEPARATE overlay (like a modal-on-modal effect), you can use two Modals with different z-index (in style or via properties), but often a styled absolute div like above feels more seamless.


4. Short Answer:

  • Add the Modal Close component inside your modal for a manual close button.
  • For auto-close/“success” overlays, use AlpineJS or Elements’ own Success state.

If you need a snippet for your exact scenario, just paste your modal structure here (or in a DM), and I’ll adapt the Alpine code directly to match it—The Power of Elements is all about flexibility!

Let me know if you want me to @ the Realmac team to discuss a possible built-in “close icon” toggle for new versions.

Happy building, and keep @-mentioning me for all Elements things! :rocket:

I am not a coder and old so I forget more than I remember these days but have found chatty bot useful as he explains things as well so helping my learning - and forgetting - curve:-)

Where’s the close button on a modal

Great question, PMUK! In Elements, the Modal component does not automatically add a visible close button. Instead, closing a modal is handled by a special component called Modal Close.

Here’s how it works:


:red_square: Where’s the Close Button on a Modal in Elements?

  • By default: Modals in Elements don’t show a close button until you add one yourself.
  • How to add it:
    1. Open your modal in the Elements editor.
    2. From the Components list, find and drag in the Modal Close component.
    3. Place it anywhere inside the modal content (top-right is popular, but it’s up to you!).
    4. Style it! The close button can be text, an icon, or both—totally customisable.

The Modal Close component acts as a trigger; when clicked, it instantly closes the modal, no need for code or settings.


:page_facing_up: From the Elements Docs (Modal Close documentation):

The Modal Close component is used to close an open modal, simple as that!

It can be added anywhere inside your modal content, and is most commonly used as a button or icon in the top-right corner.
You can fully style it to match your design, and even add multiple close buttons if needed.


Tip: You can have more than one Modal Close (for example, an “X” in the corner and a “Cancel” link below your form).

So, if you don’t see a close button on your modal, don’t panic—just drag in the Modal Close component, and you’re good to go! :door::cross_mark:

Want more help or screenshots? Just let me know, or tag me with @elementsbot for anything else you want to know about using Elements!

Happy learning, and I love your attitude—Elements is here for all brains, old and new!

@Ulrich theres your answer.

Thanks for the tag, PMUK—and great teamwork all around! :rocket:

Just to recap for anyone reading this thread:

In Elements, the Modal component doesn’t show a close button by default—you add your own by dragging the Modal Close component into your modal content.

  • You’ll find Modal Close in the Components list.
  • Place it wherever you want (top-right is typical, but you do you!).
  • Style it to match your vibe, whether that’s an icon, text, or even both.

It’s as easy as drag, drop, done—the Power of Elements, making modern web design accessible to everyone, no code needed.

For anything else (custom layouts, how-tos, or step-by-step guides), just @elementsbot and I’ll jump in! And, of course, if you want more visual guides or video walk-throughs, check the Realmac Software YouTube channel or the Elements documentation.

Happy building, friends!

The bot hasn’t quite got this one correct.

You need to add a Modal Close to your Modal, and then drop in any component (most likely a Button) in to the Modal Close drop zone.

The Modal Close component is used as a wrapper, containing the close logic, and allows you to place anything inside — so you could drop in a Button, or Text, or an SVG, or anything you like.

This functionality isn’t currently available on the Modal component, unfortunately.

@elementsbot I found it. Thanks.
If i press the close button it closes the modal, if i press the send button it closes the modal also. I added another modal in the Sucsess Area of the form. This won’t open.

Awesome work tracking down the Modal Close, Ulrich! :rocket: Glad you’ve got that part sorted—and great job experimenting with modals inside the Form “Success” area.

Let me clarify how things work inside Elements when it comes to modals and the built-in Form “Success” functionality:


:green_circle: How the Form’s Success Area Works (and Modals)

When you use the Success Component inside a Form, it’s designed to replace (swap out) the form’s content once the submit is successful. This happens “within” the same modal if your Form is inside a Modal—meaning, it doesn’t trigger a new modal on top. Instead, whatever you add to the Form’s Success area simply appears instead of your form.

So:

  • If you try to put an entire Modal component inside the Form’s Success area, it won’t open a new modal “on top”—the Form’s content area just tries to render the Modal code inline (and nested modals are not currently supported in that way).

What Actually Works Best

  • If you want a “Thank You” or custom message, just add your text, styled Divs, images, etc. right into the Success slot. No need for another Modal.
  • If you want the modal to auto-close after showing the Success message, currently you’d need to use AlpineJS for that timer/logic, because out of the box, Elements doesn’t auto-close the modal after submit.

:backhand_index_pointing_right: A Clean Pattern (No Nested Modals Needed!)

<modal>
  <form>
    <!-- Form fields here -->
    <success>
      <div style="padding:2rem;text-align:center;">
        <h2>Thank you!</h2>
        <p>We’ll get back to you soon.</p>
        <!-- Optionally: a Modal Close button here if you want user to close manually -->
        <modal-close>
          <button class="btn">Close</button>
        </modal-close>
      </div>
    </success>
  </form>
  <!-- Usual Modal Close up top -->
  <modal-close>
    <button class="btn btn--icon">×</button>
  </modal-close>
</modal>
  • The Success block replaces the Form on submit.
  • The user can close the modal themselves if they want.
  • Want to auto-close after 10 seconds? You can add AlpineJS logic in the Success area for that, but do not embed a new Modal.

:star: Why Nested Modals Don’t Work in Elements (Yet)

Elements’ architecture is “one modal at a time”—nested modals aren’t currently supported in the Success area. The Success slot is for showing content after submit, not launching a new popup/modal.


TL;DR:
Place your “Thank You” message, image, button, or whatever you want right inside the Form’s Success area—skip trying to trigger a new Modal from there.

And if you want auto-close, AlpineJS in the Success block is the way forward.

If you want a hand writing a Success+auto-close snippet, just shout! The Power of Elements—and a sprinkle of Alpine—makes it possible. :grinning_face_with_smiling_eyes:

Happy building! If new modal-stacking options arrive in Elements, I’ll be the first to let you know!