Dialog

As defined by the W3C:

A dialog is a window overlaid on either the primary window or another dialog window. Windows under a modal dialog are inert. That is, users cannot interact with content outside an active dialog window. Inert content outside an active dialog is typically visually obscured or dimmed so it is difficult to discern, and in some implementations, attempts to interact with the inert content cause the dialog to close.

Like non-modal dialogs, modal dialogs contain their tab sequence. That is, Tab and Shift + Tab do not move focus outside the dialog. However, unlike most non-modal dialogs, modal dialogs do not provide means for moving keyboard focus outside the dialog window without closing the dialog.

<FluentDialog> wraps the <fluent-dialog> element, a web component implementation of a dialog leveraging the Fluent UI design system. <FluentDialog> acts as a shell for the dialog content, which can be specified in a number of ways.

DialogService

The DialogService is a service which is used to show different types of dialogs. It is registered as a scoped service, so it can be injected into pages/components that use it. For more information on the DialogService, see the Dialog Service page.

Dialog content

Normally, the dialog content is specified by a component which implements IDialogContentComponent<T>. This component is then passed to the DialogService to be shown. The DialogService will then render a <FluentDialog> with the component inside of it.

Alternatively, the dialog content can be specified manually by setting the ChildContent parameter of <FluentDialog>. This is useful if you want to show a simple dialog without having to create a component for it or if you do not want to use the DialogService for it.
When using the DialogService, for displaying a regular dialog, the dialog will always be shown centered on the screen.

During the WASM DotNet Publication process, the unused classes are automatically removed from the final library. The Dialog razor file will be removed if no reference are set by your code. For example, if you call var = await DialogService.ShowDialogAsync<SimpleDialog>(simplePerson, parameters); the SimpleDialog will be removed by the WASM Publication process. You can configure this behavior by setting the PublishTrimmed property in your project file or you can create a temporary instance var temp1 = new SimpleDialog(); to force the Publication process to keep this class in the final library.

Exchange data between dialog and calling component

There are two ways available to exchange data between the dialog and the component which shows it. The first is by capturing the returned IDialogReference from one of the DialogService.Show...Async methods and then use that reference to get the dialog's result (of type DialogResult). The second is by using an EventCalback parameter as part of the DialogParameters. Both ways are demonstated in the samples below.

Dialog provider

IMPORTANT!!

Dialogs are rendered by the <FluentDialogProvider /> component. This component needs to be added to the main layout of your application/site. You typically do this in the MainLayout.razor file at the end of the <main> section like this:

IMPORTANT:

For the Dialogs to work properly, the <FluentDialogProvider/> needs interactivity! If you are using "per page" interactivity, make sure to add a @rendermode to either the provider itself or the component the provider is placed in.


    <main>
    <nav>
    :
    </nav>
    <div class="content">
    <article id="article">
    @Body
    </article>
    </div>
    <FluentDialogProvider />
    </main>

Examples

These examples show how to use the DialogService to display a dialog. The content of the dialog is specified by a component which implements IDialogContentComponent<T>. Here, that is done in SimpleDialog.razor. The dialog is automatically styled and centered.

Interaction with parent dialog can be made by injecting FluentDialog as Cascading Parameter. See SimpleDialog.razor for more details.

DialogService with IDialogReference

This example shows how to use async methods to display a dialog and get the result back from it.
Example

When 'Modal' is checked, the dialog can be dismissed by clicking outside of the dialog (anywhere on the overlay). When unchecked, the dialog can be dismissed only by the 'ESC' key.
The dialog can always be closed by using the 'Close dialog' button.

When 'Trap focus' is checked, only the elements within the dialog will receive focus. When unchecked, focus will also move outside of the dialog.

Modal Trap focus
Open Dialog
Download: 

Customized Dialog

This example shows how to open a Dialog using the DialogService. The dialog is customized by specifying a header, footer and body by leveraging the FluentDialogHeader, FluentDialogFooter and FluentDialogBody parameters.

The dialog can not be dismissed by clicking on the overlay. This is achieved by setting the dialog parameter PreventDismissOnOverlayClick to true.

Example
Open dialog

Name: Bill - Age: 42

Download: 

Editable Dialog

This example shows how to open a Dialog containing editable fields. The first button uses the till now standard way of doing that. The second button uses alternative code which offers a type safe way of using IDialogContentComponent<TContent>
This has been added in 4.11.4 and uses the new DialogHelper class. See the Razor tab and the documentation below on how this new method can be used.

Example
Edit Edit (type safe)

Name: Denis - Age: 24

Download: 

DialogService with EventCallback

This example shows how to use an EventCallback parameter to get data back from the dialog.
Example

When 'Modal' is checked, the dialog can be dismissed by clicking outside of the dialog (anywhere on the overlay). When unchecked, the dialog can be dismissed only by the 'ESC' key.
The dialog can always be closed by using the 'Close dialog' button.

When 'Trap focus' is checked, only the elements within the dialog will receive focus. When unchecked, focus will also move outside of the dialog.

Modal Trap focus
Open Dialog
Download: 

DialogService with Custom Animations

This example shows how to use EventCallback parameters to animate dialogs when they appear and disappear.
Example

When 'Open Dialog' is clicked, a dialog should fade in. When that dialog is closed, it should fade out.

Open Dialog
Download: 

Dialog without using DialogService

This example shows a simple dialog created by specifying its content manually (see the Razor tab). Because of this, the dialog is not automatically styled. A CSS file has been added to set the width, height and padding of the dialog.
Example

When 'Modal' is checked, the dialog can be dismissed by clicking outside of the dialog (anywhere on the overlay). When unchecked, the dialog can be dismissed only by the 'ESC' key.
The dialog can always be closed by using the 'Close dialog' button.

When 'Trap focus' is checked, only the elements within the dialog will receive focus. When unchecked, focus will also move outside of the dialog.

When 'Prevent scroll' is checked, the body of the page will not be scrollable when the dialog is shown.

Hidden is bound to dialog visibility. You can show/hide dialog by changing this property or calling Show() / Hide() on component reference.

Modal Trap focus Prevent scroll Hidden

Status:

Open dialog
Download: 

SimpleDialog as component

For a component to be useable in a dialog, it just needs to inherit from IDialogContentComponent<T>. They can still be used as a normal component as well. This example shows the SimpleDialog component from the previous example being rendered directly in the page.
Example

Your lastname is Burke and you are 18 years young

Your firstname: Your lastname: Your age:
Download: 

RenderFragment as dialog

This example shows how to display a dialog using only a RenderFragment.
Example
Open dialog
Download: 

Documentation

FluentDialogProvider Class

Methods

Name
Parameters
Type
Description
DismissAllvoid

FluentDialog Class

Parameters

Name
Type
Default
Description
AriaDescribedbystring?
Gets or sets the id of the element describing the dialog.
AriaLabelstring?
Gets or sets the label surfaced to assistive technologies.
AriaLabelledbystring?
Gets or sets the id of the element labeling the dialog.
ChildContentRenderFragment?
Used when not calling the DialogService to show a dialog.
Hiddenbool
False
Gets or sets a value indicating whether the dialog is hidden.
InstanceDialogInstance
Gets or sets the instance containing the programmatic API for the dialog.
Modalbool?
Gets or sets a value indicating whether the element is modal. When modal, user mouse interaction will be limited to the contents of the element by a modal
overlay. Clicks on the overlay will cause the dialog to emit a 'dismiss' event.
PreventScrollbool
True
Prevents scrolling outside of the dialog while it is shown.
TrapFocusbool?
Gets or sets a value indicating whether that the dialog should trap focus.

EventCallbacks

Name
Type
Description
HiddenChangedEventCallback<bool>
The event callback invoked when FluentDialog.Hidden change.
OnDialogResultEventCallback<DialogResult>
The event callback invoked to return the dialog result.

Methods

Name
Parameters
Type
Description
CancelAsyncTask
Closes the dialog with a cancel result.
CancelAsync<T>T returnValue
Task
Closes the dialog with a cancel result.
CloseAsyncTask
Closes the dialog
CloseAsyncDialogResult dialogResult
Task
Closes the dialog
CloseAsync<T>T returnValue
Task
Closes the dialog with a OK result.
Hidevoid
Hides the dialog
Showvoid
Shows the dialog
TogglePrimaryActionButtonbool isEnabled
void
Toggle the primary action button
ToggleSecondaryActionButtonbool isEnabled
void
Toggle the secondary action button

To alter the properties of a dialog which uses the DialogService to display it, use the DialogParameters class.

Note: Not all properties can be used for all types of dialogs. For example, the DialogParameters class has a Height property, which is ignored when displaying a Panel.

DialogParameters<TData> Class

Properties

Name
Type
Default
Description
AlignmentHorizontalAlignment
Center
Gets or sets the dialog position:
left (full height), right (full height)
or screen middle (using Width and Height properties).
HorizontalAlignment.Stretch is not supported for this property.
AriaDescribedbystring?
Gets or sets the element that describes the element on which the attribute is set.
AriaLabelstring?
Gets or sets the value that labels an interactive element.
AriaLabelledbystring?
Gets or sets the element that labels the element it is applied to.
ContentTContent
Gets or sets the content to pass to and from the dialog.
DialogBodyStylestring
Gets or sets the extra styles applied to the Body content.
DialogTypeDialogType
Dialog
Gets or sets the type of dialog.
DismissTitlestring?
Close
Gets or sets the Title of the dismiss button, display in a tooltip.
Defaults to 'Close'.
Heightstring?
Gets or sets the height of the dialog. Must be a valid CSS height value like '600px' or '3em'
Only used if Alignment is set to 'HorizontalAlignment.Center'
ItemObject
Modalbool?
True
Determines if the dialog is modal. Defaults to true.
Obscures the area around the dialog.
PreventDismissOnOverlayClickbool
False
Determines if a modal dialog is dismissible by clicking
outside the dialog. Defaults to false.
PreventScrollbool
True
Prevents scrolling outside of the dialog while it is shown.
PrimaryActionstring?
OK
Gets or sets the text to display for the primary action.
PrimaryActionEnabledbool
True
When true, primary action's button is enabled.
SecondaryActionstring?
Cancel
Gets or sets the text to display for the secondary action.
SecondaryActionEnabledbool
True
When true, secondary action's button is enabled.
ShowDismissbool
True
Gets or sets a value indicating whether show the dismiss button in the header.
Defaults to true.
ShowTitlebool
True
Gets or sets a value indicating whether show the title in the header.
Defaults to true.
Titlestring?
Gets or sets the title of the dialog.
TrapFocusbool?
True
Gets or sets a value indicating whether if dialog should trap focus.
Defaults to true.
ValidateDialogAsyncFunc<Task<bool>>
Function that is called and awaited before the dialog is closed.
Visiblebool
True
Gets or sets if a dialog is visible or not (Hidden)
Widthstring?
Gets or sets the width of the dialog. Must be a valid CSS width value like '600px' or '3em'

EventCallbacks

Name
Type
Description
OnDialogClosingEventCallback<DialogInstance>
Callback function that is called and awaited before the dialog fully closes.
OnDialogOpenedEventCallback<DialogInstance>
Callback function that is called and awaited after the dialog renders for the first time.
OnDialogResultEventCallback<DialogResult>
Callback function for the result.

Methods

Name
Parameters
Type
Description
Addstring parameterName
Object value
void
Get<T>string parameterName
T?
GetDictionaryDictionary<string, Object>
GetEnumeratorIEnumerator<KeyValuePair<string, Object>>
TryGet<T>string parameterName
T?

Dialog header and footer

The dialog header and footer can be changed by using the FluentDialogHeader and FluentDialogFooter component.

The default implementation uses the FluentDialogHeader and FluentDialogFooter components (see documentation below). You can use the content of these components as the base for your own implementation:

Default dialog header (simplified version)


    <FluentStack Orientation="Orientation.Horizontal" VerticalAlignment="VerticalAlignment.Top">
    <div style="width: 100%;">
    <FluentLabel Typo="Typography.PaneHeader">@Title</FluentLabel>
    </div>
    <FluentButton Appearance="Appearance.Stealth">
    <FluentIcon Icon="CoreIcons.Regular.Size24.Dismiss" />
    <FluentButton>
    </FluentStack>

Default dialog footer (simplified version)


    <FluentStack Orientation="Orientation.Horizontal" HorizontalAlignment="HorizontalAlignment.Right" VerticalAlignment="VerticalAlignment.Bottom">
    <FluentButton Title="@PrimaryAction" Appearance="Appearance.Accent" Disabled="@PrimaryActionEnabled">
    @PrimaryAction
    </FluentButton>
    <FluentButton Title="@SecondaryAction" Appearance="Appearance.Neutral" Disabled="@SecondaryActionEnabled">
    @SecondaryAction
    </FluentButton>
    </FluentStack>

FluentDialogHeader Class

Parameters

Name
Type
Default
Description
ChildContentRenderFragment?
Gets or sets the content to be rendered inside the component.
ShowDismissbool?
When true, shows the dismiss button in the header.
If defined, this value will replace the one defined in the DialogParameters.
ShowDismissTooltipbool?
True
When true, shows the 'Close' button tooltip in the header.
TabIndexint?
0
Allows developers to make elements sequentially focusable and determine their relative ordering for navigation (usually with the Tab key).
Visiblebool
True
When true, the header is visible.
Default is True.

FluentDialogFooter Class

Parameters

Name
Type
Default
Description
ChildContentRenderFragment?
Gets or sets the content to be rendered inside the component.
Visiblebool
True
When true, the footer is visible.
Default is True.

FluentDialogBody Class

Parameters

Name
Type
Default
Description
ChildContentRenderFragment?
Gets or sets the content to be rendered inside the component.

DialogHelper Class

Methods

Name
Parameters
Type
Description
From<TDialog>DialogHelper<TDialog>
Create a dialog helper for the specified dialog type
ShowDialogAsync<TDialog, TData>IDialogService svc
DialogHelper<TDialog> dialogHelper
TData data
DialogParameters parameters
Task<IDialogReference>
Shows a dialog with the component type as the body,
passing the specified data
ShowDialogAsync<TDialog, TData>DialogHelper<TDialog> dialogHelper
TData data
IDialogService svc
DialogParameters parameters
Task<IDialogReference>
Shows a dialog with the component type as the body,
passing the specified data
Left
Start
Center
Right
End
Stretch
SpaceBetween
Dialog
MessageBox
Panel
SplashScreen
An error has occurred. This application may no longer respond until reloaded. Reload 🗙