Framework7 Modals: Build, Control, and Optimize Your Pop‑Ups
Framework7’s modal component lets you create alerts, confirmations, and custom dialogs without external libraries. Learn how to build, customize, and optimize pop‑ups, plus a worked example and best‑practice checklist.
22 Nov 2025, 03:51 UTC

Why Framework7 Modals Matter
When building a mobile‑first web app, you’ll need to interrupt the user with alerts, confirmations, or custom dialogs. Framework7 ships a Modal component that replaces the need for external libraries like SweetAlert or Bootstrap’s modal. It’s a lightweight, fully‑styled, and accessible solution that keeps your page’s CSS and JavaScript isolated from the modal’s internals.
Declarative vs Programmatic Creation
Framework7 lets you create a modal in two ways:
- Declaratively – add
data-f7-modalattributes to an element and let the framework build it on page load. - Programmatically – call
app.modalsmethods from JavaScript. This is useful when you need to generate a modal at runtime or reuse the same modal template across routes.
Both approaches expose the same API for opening, closing, and listening to events. The declarative method is great for static UI, while the programmatic method gives you full control over lifecycle events and dynamic content.
Customizing Animation and Theme
Framework7’s modal system supports several built‑in types:
popup– general content window.confirm– yes/no dialog.alert– single‑button message.login– username/password form.
openAnimation and closeAnimation options or using CSS classes. The modal content is wrapped in a .f7-modal element that automatically applies the theme’s colors, shadows, and padding. Because the modal is rendered into a dedicated container, its styles do not bleed into the rest of the page.
Performance & Accessibility Tips
- Framework7 automatically prevents background scrolling and traps focus inside the modal, which is essential for keyboard‑only users.
- Keep the modal content lightweight. Heavy DOM nodes or large images can block the main thread during the opening animation, causing jank.
- Destroy a modal after it’s closed if you’re creating it programmatically:
app.modals.destroy(modalEl);to avoid memory leaks and stale event listeners. - When stacking multiple modals, be mindful of the order of
z-indexand the overlay’s opacity to preserve the visual hierarchy.
Example: A Confirmation Dialog
Below is a complete, self‑contained example that shows both declarative and programmatic ways to create a confirmation modal. The code runs in any browser that hosts a Framework7 bundle.
// HTML – Declarative modal
<button data-f7-modal="confirm" data-f7-modal-title="Delete Item" data-f7-modal-text="Are you sure you want to delete this item?" data-f7-modal-buttons="{\"text\":\"Cancel\",\"close\":true},{\"text\":\"Delete\",\"close\":true,\"onClick\":\"deleteItem\"}">Open Confirm</button>
// JavaScript – Programmatic modal
const app = new Framework7();
function openDeleteConfirm() {
const modal = app.modals.create({
title: 'Delete Item',
text: 'Are you sure you want to delete this item?',
buttons: [
{ text: 'Cancel', close: true },
{ text: 'Delete', close: true, onClick: deleteItem }
],
closeOnBackdropClick: false,
openAnimation: 'f7-modal-slide-in-down',
closeAnimation: 'f7-modal-slide-out-up'
});
modal.open();
}
function deleteItem() {
console.log('Item deleted');
}
After opening the modal, Framework7 adds .f7-modal to the DOM, sets aria-modal="true", and traps focus. Closing the modal removes the element unless you set destroy: false in the options.
Trade‑offs & Limitations
- Because a modal blocks background interaction, excessive use can frustrate users. Use them only for critical or time‑sensitive actions.
- Framework7’s modal component is not designed for extremely complex forms that require custom validation logic. In those cases, consider a separate page or a lightweight overlay.
- While the component works on modern browsers, legacy browsers (IE 11 and earlier) may not support the CSS features used for animations or focus trapping. Test on your target audience’s browsers.
Actionable Checklist
- Decide whether the modal is static (declarative) or dynamic (programmatic).
- Choose the modal type that best fits the UX –
confirmfor binary choices,alertfor simple messages. - Keep content minimal; preload images if necessary.
- Use
app.modals.destroy()for programmatic modals that are no longer needed. - Test keyboard navigation and screen‑reader announcements.
- Run a performance audit to confirm that opening the modal does not cause layout thrashing.
With these guidelines, you can confidently integrate Framework7 modals into your app, delivering a polished, accessible, and efficient user experience.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.