Skip to main content

Working with Memberstack DOM Package Modals

Josh Lopez
Josh Lopez
  • Updated

Memberstack's DOM package includes ready-made modals for signing members up, logging them in, resetting forgotten passwords and editing a member's profile. This article explains what the modals are, how to open them, how to customize them, how to close them from your code, and how to read the data they return.

View the full Pre-built Modals documentation for more guidance.

Note: If your site uses the Memberstack Webflow script instead of installing the DOM package, the same methods are available on window.$memberstackDom, for example window.$memberstackDom.openModal("SIGNUP") instead of memberstack.openModal("SIGNUP").

1) What is a modal?

A modal is a dialog box that appears on top of the main view of a web page. Modals are commonly used for things like:

  • Signup/login forms
  • Displaying messages or notifications
  • Getting user input on a focused task

When a Memberstack modal opens, it covers the page with a dark overlay, so the page behind it can't be clicked until the modal is closed.

2) Opening Memberstack's pre-built modals

Open a modal with the openModal() method, passing the type of modal you want:

// Signup form
memberstack.openModal("SIGNUP");

// Login form
memberstack.openModal("LOGIN");

// Forgot password form: the member enters their email and is sent a 6-digit reset code
memberstack.openModal("FORGOT_PASSWORD");

// Reset password form: the member enters that code and a new password
memberstack.openModal("RESET_PASSWORD");

// Profile editor (only opens for a logged-in member)
memberstack.openModal("PROFILE");

Only one modal is shown at a time: opening a modal closes any modal that is already open. "PROFILE" needs a logged-in member; if nobody is logged in, it closes straight away.

The modals are not bundled inside the DOM package. The first openModal() call downloads them from Memberstack's CDN (static.memberstack.com), so the first modal can take a moment to appear. To download them in advance, call memberstack.preloadModals() when your page loads. If your site has a Content Security Policy, allow scripts from static.memberstack.com. If the modal code can't be downloaded, the openModal() promise rejects instead of resolving.

3) Customizing the modals

Pass a configuration object as the second parameter to openModal(). To add a "Don't have an account?" link to the login modal, pass signup.plans:

// Adds a "Don't have an account?" link to the login modal.
// It switches to the signup form, and new members get the free plan(s) you list.
memberstack.openModal("LOGIN", {
  signup: { plans: ["pln_free-abc123"] }
});

On the signup modal, signup.plans adds the listed plans to every member who signs up there. The modal does not show the plans to the member.

// New members who sign up in this modal are added to the listed free plan(s)
memberstack.openModal("SIGNUP", {
  signup: { plans: ["pln_free-abc123"] }
});

Use the IDs of free plans only. If the list contains a paid plan (or an ID that doesn't exist), the signup fails with the error "There is no valid plan with this identifier connected to this app." To sell a paid plan, see the Member Billing and Checkout article in this series.

To change the text in a modal, pass translations:

// Change the modal's text. Keys you leave out keep their default English text.
memberstack.openModal("LOGIN", {
  translations: {
    login_to_your_account: "Welcome back", // modal title
    login: "Sign in" // submit button
  }
});

The package's TypeScript types list every available key under MemberstackTranslations.

4) Closing modals programmatically

Members can close a modal themselves with its close (X) button, by clicking the dark overlay outside it, or by pressing Escape while they are in the modal. A modal does not close by itself after a successful login or signup, or after the member logs out from the profile modal: it stays open until you close it. To close the open modal from your code, call hideModal():

memberstack.hideModal();

For example, to close the login modal once the member has logged in:

memberstack.openModal("LOGIN").then(({ type, data }) => {
  if (type === "LOGIN") {
    console.log("User logged in!", data.member.id);
    // The modal stays open after a successful login, so close it here
    memberstack.hideModal();
  }
});

5) Handling data returned from modals

openModal() returns a promise. It resolves once, with an object whose type says what happened:

  • "LOGIN": the member logged in
  • "SIGNUP": a new member signed up
  • "LOGOUT": the member logged out from the "PROFILE" modal
  • "CLOSED": the modal was closed first, by the member or by hideModal()

A login modal resolves with "SIGNUP" if the member creates an account through its "Don't have an account?" link, so check for both types if you pass signup.plans to a login modal.

After a login or signup, data.member is the member and data.tokens holds their access token. A "CLOSED" result has no data, so always check type before you use it:

const result = await memberstack.openModal("SIGNUP");

if (result.type === "SIGNUP") {
  console.log("User signed up!", result.data.member.id, result.data.member.auth.email);
  memberstack.hideModal();
} else if (result.type === "CLOSED") {
  console.log("The modal was closed without signing up");
}

Hope this helps explain how to work with Memberstack's out-of-the-box modals!

Check out the other articles in this series:

  1. Getting Started with the Memberstack DOM Package (Front-end API)
  2. Working with Memberstack DOM Package Modals
  3. Managing App Data with the Memberstack DOM Package
  4. Member Authentication with the Memberstack DOM Package
  5. Member Billing and Checkout with the Memberstack DOM Package
  6. Member Management with the Memberstack DOM Package
  7. Events and Webhooks with Memberstack DOM

Was this article helpful?

Didn’t find your answer?

Get an instant answer from Rey, or reach a human. Either way, we’re happy to help.

Comments

No comments yet. Start the conversation below.

Please sign in to leave a comment.