Modals
Modals, like panels, are essential components in web applications, often used for tasks such as editing values, displaying messages, or gathering user input. While they may be slightly more complex than panels, they are still relatively straightforward to implement.
Before creating a modal, it's essential to understand how to trigger it on your page. Typically, modals are invoked using hyperlinks (a elements). To do this, create an a element and link it to your controller as usual. Then, add the class showModal to your a element, and the modal setup is nearly complete! You can also use icons to call your modal by adding a span element with appropriate icon classes inside the a element.
Creating a Modal
To create a modal, follow these steps:
- Create a
divelement and give it the classmodal-dialog. - Optionally, add a
formelement inside the modal if you need to submit data to the server. This should be nested inside themodal-dialog. - Create another
divelement with the classmodal-headerto define the header section. Nest this inside themodal-contentor within theformif you added one. - Create a
divelement with the classmodal-content. This contains the modal's main content and should also be nested inside themodal-dialogorform. - Create a
divelement with the classmodal-footerto define the footer section. This should be nested inside themodal-contentor within theformif applicable. - Include a button to close the modal. Create a
buttonelement with the attributesclass="btn btn-info"anddata-dismiss="modal". The button text should be "Cancel". - If you added a form to your modal, include an
inputelement with the type set to submit. Add the classesbtn btn-primaryto style it as a primary button, and provide a value for the button text.
Once your modal's structure matches the code in the spoiler below, you've successfully created a modal.
<div class="modal-dialog">
<div class="modal-content">
@using (Html.BeginForm("action", "controller", null, FormMethod.Post, new { role = "form", @id = "formChange" }))
{
<div class="modal-header">
<button type="button" data-dismiss="modal" aria-hidden="true" class="close md-close"><i class="fal fa-times"></i></button>
<h3 class="modal-title">title</h3>
</div>
<div class="modal-body">
Content
</div>
<div class="modal-footer">
<input type="submit" value="@Generic.Resources.Common.Button.Save" class="btn btn-primary" name="submitButton">
<button type="submit" class="btn btn-info" data-dismiss="modal">@Generic.Resources.Common.Button.Cancel</button>
</div>
}
</div>
</div>
Additionally, we provide a snippet for the HTML code of a modal to facilitate its implementation in Visual Studio.
Best Practices for Modal Usage
- Keep it Simple: Avoid overcrowding the modal with excessive content. Keep it focused on a single task or piece of information.
- Use Responsively: Ensure the modal is responsive and looks good on various screen sizes, from desktop to mobile devices.
- Provide Clear Actions: Clearly label buttons and actions within the modal to guide users on what to do next.
- Accessibility: Ensure the modal is accessible to all users, including those who rely on assistive technologies. Use appropriate ARIA attributes and keyboard navigation.
- Testing: Thoroughly test the modal functionality across different browsers and devices to ensure a consistent user experience.
Things to Avoid
- Overusing Modals: Avoid excessive use of modals, as they can disrupt the user flow and lead to user frustration.
- Hidden Navigation: Avoid hiding critical navigation or functionality within modals, as it may confuse users.
- Non-Responsive Design: Ensure the modal design adapts gracefully to different screen sizes and orientations.