Blazored Modal Documentation
repository·main·Indexed 21 days ago
https://github.com/blazored/modalA customizable library for Blazor applications providing a powerful implementation for displaying and managing modal windows. It includes features for configuring modal sizes, positions, animations, and accessibility options like focus traps, as well as global and instance-level configuration via CascadingBlazoredModal and ModalOptions.
What's inside Blazored Modal
- Blazored Modal is a powerful and customizable modal implementation designed specifically for Blazor applications. It allows developers to easily trigger and manage modal windows within their Blazor UI.
Configure modal position using ModalPosition
mainBy default, modals are centered near the top of the viewport. You can change this behavior using the
Positionoption, which accepts aModalPositionenum value.Available predefined positions:
ModalPosition.TopLeftModalPosition.TopRightModalPosition.TopCenterModalPosition.MiddleModalPosition.BottomLeftModalPosition.BottomRightModalPosition.Custom
Implement a component for use in Blazored Modal
mainWhen creating a component to be displayed in a modal, you should define the data you wish to receive as standard Blazor
[Parameter]properties. To allow the component to close itself, include a[CascadingParameter]of typeBlazoredModalInstance.<div class="modal-content"> <p>@Message</p> <button @onclick="Close">Close</button> </div> @code { [CascadingParameter] BlazoredModalInstance BlazoredModal { get; set; } = default!; [Parameter] public string? Message { get; set; } private async Task Close() => await BlazoredModal.CloseAsync(); }Hide the header for a single modal using ModalOptions
mainTo hide the header for a specific modal instance, create a
ModalOptionsobject and set theHideHeaderproperty totrue. Pass this options object as the second argument to theModal.Show<T>method.var options = new ModalOptions() { HideHeader = true }; Modal.Show<Confirm>("Are you sure?", options);Configure Blazored Modal imports
mainTo make Blazored Modal namespaces available throughout your project without repetitive using statements, add the following to your root
_Imports.razorfile:@using Blazored.Modal @using Blazored.Modal.ServicesInstall the website dependencies
mainTo install the necessary dependencies for the documentation website, runyarnin the root directory.$ yarnShow multiple modals sequentially or stacked
mainBlazored Modal allows displaying multiple modals simultaneously. To stack modals, you must trigger the opening of a new modal from the currently active modal instance using the
IModalService.When a new modal is shown from an existing one, it will render on top of the current modal. If you wish to close the underlying modal after the new one is dismissed, you can await the
Resultof the new modal and then callCloseAsync()on the currentBlazoredModalInstance.@* Inside the first modal (ModalOne.razor) *@ [CascadingParameter] BlazoredModalInstance ModalOne { get; set; } = default!; [CascadingParameter] IModalService ModalService { get; set; } = default!; private async Task ShowModalTwo() { // 1. Show the second modal from the first one var modalTwo = ModalService.Show<ModalTwo>("Second Modal"); // 2. Wait for the second modal to be closed _ = await modalTwo.Result; // 3. Close the first modal await ModalOne.CloseAsync(); }Install Blazored.Modal via NuGet or dotnet CLI
mainInstall the
Blazored.ModalNuGet package into your Blazor project using the NuGet package manager, PowerShell, or the dotnet CLI.Install-Package Blazored.Modaldotnet add package Blazored.ModalRetrieve data from a modal result
mainWhen a modal is invoked using
Modal.Show<T>(), you can retrieve the returned data by awaiting theResultproperty of the returned modal instance.To safely access the data, check the
Confirmedproperty of theModalResult. IfConfirmedis true, you can access the returned value via theDataproperty. Note thatDatais returned as anobject, so you may need to cast it or call.ToString()depending on your requirements.var messageForm = Modal.Show<MessageForm>(); var result = await messageForm.Result; if (result.Confirmed) { // Access the returned data via the Data property _message = result.Data.ToString(); }Configure predefined modal sizes
mainBlazored Modal provides several built-in sizes. If no size is specified, the default is
ModalSize.Medium.Available predefined sizes:
ModalSize.Small(300px)ModalSize.Medium(500px)ModalSize.Large(800px)ModalSize.ExtraLarge(1140px)- Automatic (Size determined by content)
You can apply a predefined size globally via the
CascadingBlazoredModalcomponent or for a specific instance usingModalOptionspassed to theShowmethod.<!-- Global configuration --> <CascadingBlazoredModal Size="ModalSize.Large" />// Single modal configuration var options = new ModalOptions() { Size = ModalSize.Large }; Modal.Show<Confirm>("Are you sure?", options);Await the result of a modal
mainWhen opening a modal using
IModalService.Show<TComponent>, you can capture a reference to the modal instance and await itsResultproperty. This allows you to perform logic based on whether the user confirmed the action or cancelled the modal.The
Resultobject provides two boolean properties:Cancelled: True if the modal was closed without confirmation.Confirmed: True if the modal was closed via a confirmation action.
To use this, inject
IModalServiceas a[CascadingParameter]into your component.@page "/movies" <h1 @onclick="ShowModal">Movies</h1> <button @onclick="ShowModal">View Movies</button> @code { [CascadingParameter] IModalService Modal { get; set; } = default!; private async Task ShowModal() { // Capture the reference to the modal instance var moviesModal = Modal.Show<Movies>("My Movies"); // Await the result var result = await moviesModal.Result; if (result.Cancelled) { Console.WriteLine("Modal was cancelled"); } else if (result.Confirmed) { Console.WriteLine("Modal was closed"); } } }Customize Modal Styles
mainYou can override the default look of Blazored Modal by providing your own CSS classes. These custom classes will replace the default style classes applied by the library, giving you complete control over the modal's appearance.
Depending on your needs, you can apply these styles globally to all modals or specifically to a single modal instance.
// Example of applying a custom class to a single modal var options = new ModalOptions() { Class = "my-custom-modal-class" }; Modal.Show<Confirm>("Are you sure?", options);