|
| 1 | +# Dialogs and User Feedback |
| 2 | + |
| 3 | +## Opening a Dialog (parent / page side) |
| 4 | + |
| 5 | +```csharp |
| 6 | +@inject IDialogService DialogService |
| 7 | + |
| 8 | +private async Task OpenAddDialog() |
| 9 | +{ |
| 10 | + var options = new DialogOptions |
| 11 | + { |
| 12 | + CloseOnEscapeKey = true, |
| 13 | + MaxWidth = MaxWidth.Small, |
| 14 | + FullWidth = true |
| 15 | + }; |
| 16 | + var dialog = await DialogService.ShowAsync<CreateWidgetDialog>("New Widget", options); |
| 17 | + var result = await dialog.Result; |
| 18 | + |
| 19 | + if (result is { Canceled: false }) |
| 20 | + await _table.ReloadServerData(); |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +### Passing Parameters to a Dialog |
| 25 | + |
| 26 | +Use the strongly-typed `DialogParameters<TDialog>` overload — compile-time safe: |
| 27 | + |
| 28 | +```csharp |
| 29 | +private async Task EditWidget(AdminWidgetDto widget) |
| 30 | +{ |
| 31 | + var parameters = new DialogParameters<UpdateWidgetDialog> |
| 32 | + { |
| 33 | + { x => x.Widget, widget }, |
| 34 | + { x => x.ETag, widget.ETag } |
| 35 | + }; |
| 36 | + var options = new DialogOptions { CloseOnEscapeKey = true, MaxWidth = MaxWidth.Small, FullWidth = true }; |
| 37 | + var dialog = await DialogService.ShowAsync<UpdateWidgetDialog>("Edit Widget", parameters, options); |
| 38 | + var result = await dialog.Result; |
| 39 | + |
| 40 | + if (result is { Canceled: false }) |
| 41 | + await _table.ReloadServerData(); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Dialog Component |
| 48 | + |
| 49 | +Dialog components **do not** declare `@rendermode` — they inherit it from their parent page. The cascading parameter is `IMudDialogInstance` (not `MudDialogInstance`). |
| 50 | + |
| 51 | +```razor |
| 52 | +@* CreateWidgetDialog.razor — NO @rendermode directive *@ |
| 53 | +@inject IWidgetsClient WidgetsClient |
| 54 | +@inject ISnackbar Snackbar |
| 55 | +
|
| 56 | +<MudDialog> |
| 57 | + <TitleContent> |
| 58 | + <MudText Typo="Typo.h6"> |
| 59 | + <MudIcon Icon="@Icons.Material.Filled.Widgets" Class="mr-3 mb-n1"/> |
| 60 | + New Widget |
| 61 | + </MudText> |
| 62 | + </TitleContent> |
| 63 | + <DialogContent> |
| 64 | + <MudForm @ref="_form" @bind-IsValid="_success"> |
| 65 | + <MudTextField T="string" |
| 66 | + Label="Name" |
| 67 | + @bind-Value="_model.Name" |
| 68 | + Required="true" |
| 69 | + RequiredError="Name is required!" |
| 70 | + Variant="Variant.Outlined" |
| 71 | + Immediate="true"/> |
| 72 | + </MudForm> |
| 73 | + </DialogContent> |
| 74 | + <DialogActions> |
| 75 | + <MudButton OnClick="Cancel">Cancel</MudButton> |
| 76 | + <MudButton Color="Color.Primary" |
| 77 | + Variant="Variant.Filled" |
| 78 | + OnClick="Submit" |
| 79 | + Disabled="@(!_success)">Create</MudButton> |
| 80 | + </DialogActions> |
| 81 | +</MudDialog> |
| 82 | +
|
| 83 | +@code { |
| 84 | + [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = null!; |
| 85 | +
|
| 86 | + private MudForm _form = null!; |
| 87 | + private bool _success; |
| 88 | + private CreateWidgetRequest _model = new(""); |
| 89 | +
|
| 90 | + private void Cancel() => MudDialog.Cancel(); |
| 91 | +
|
| 92 | + private async Task Submit() |
| 93 | + { |
| 94 | + await _form.Validate(); |
| 95 | + if (!_success) return; |
| 96 | +
|
| 97 | + var result = await WidgetsClient.CreateWidgetAsync(_model); |
| 98 | + if (result.IsSuccess) |
| 99 | + MudDialog.Close(DialogResult.Ok(true)); |
| 100 | + else |
| 101 | + Snackbar.Add(result.Error.Message, Severity.Error); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### Dialog with pre-populated model (edit) |
| 107 | + |
| 108 | +```razor |
| 109 | +@code { |
| 110 | + [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = null!; |
| 111 | + [Parameter] public AdminWidgetDto Widget { get; set; } = null!; |
| 112 | + [Parameter] public string ETag { get; set; } = ""; |
| 113 | +
|
| 114 | + protected override void OnParametersSet() |
| 115 | + { |
| 116 | + _model = new UpdateWidgetRequest(Widget.Name, Widget.Description); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## Confirmation Dialog |
| 124 | + |
| 125 | +For destructive actions, use `IDialogService` with a simple confirm dialog: |
| 126 | + |
| 127 | +```csharp |
| 128 | +private async Task DeleteWidget(Guid id, string etag) |
| 129 | +{ |
| 130 | + var confirm = await DialogService.ShowMessageBox( |
| 131 | + "Delete Widget", |
| 132 | + "Are you sure you want to delete this widget? This cannot be undone.", |
| 133 | + yesText: "Delete", cancelText: "Cancel"); |
| 134 | + |
| 135 | + if (confirm != true) return; |
| 136 | + |
| 137 | + var result = await WidgetsClient.DeleteWidgetAsync(id, etag); |
| 138 | + if (result.IsSuccess) |
| 139 | + Snackbar.Add("Widget deleted.", Severity.Success); |
| 140 | + else if (result.Error.Code == ErrorCode.ConcurrencyConflict) |
| 141 | + Snackbar.Add("Another user modified this item. Please refresh.", Severity.Warning); |
| 142 | + else |
| 143 | + Snackbar.Add(result.Error.Message, Severity.Error); |
| 144 | + |
| 145 | + await _table.ReloadServerData(); |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## ISnackbar — Toast Notifications |
| 152 | + |
| 153 | +Inject once per component: `@inject ISnackbar Snackbar` |
| 154 | + |
| 155 | +```csharp |
| 156 | +Snackbar.Add("Operation successful!", Severity.Success); |
| 157 | +Snackbar.Add("Something went wrong.", Severity.Error); |
| 158 | +Snackbar.Add("Check your input.", Severity.Warning); |
| 159 | +Snackbar.Add("Refreshing in background...", Severity.Info); |
| 160 | +``` |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## MudAlert — Inline Alerts |
| 165 | + |
| 166 | +```razor |
| 167 | +<MudAlert Severity="Severity.Warning" Dense="true"> |
| 168 | + At least one author is required. |
| 169 | +</MudAlert> |
| 170 | +
|
| 171 | +<MudAlert Severity="Severity.Error" Variant="Variant.Filled"> |
| 172 | + @_errorMessage |
| 173 | +</MudAlert> |
| 174 | +``` |
| 175 | + |
| 176 | +Use `Dense="true"` inside dialogs and cards; `Variant="Variant.Filled"` for high-emphasis alerts. |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## MudSkeleton — Loading States |
| 181 | + |
| 182 | +Show while initial data is loading (before `ReactiveQuery<T>` resolves): |
| 183 | + |
| 184 | +```razor |
| 185 | +@if (_query?.IsLoading == true && _query.Data == null) |
| 186 | +{ |
| 187 | + <MudStack> |
| 188 | + <MudSkeleton SkeletonType="SkeletonType.Rectangle" Height="40px"/> |
| 189 | + <MudSkeleton Width="60%"/> |
| 190 | + <MudSkeleton Width="80%"/> |
| 191 | + </MudStack> |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +## MudProgressCircular — Spinner |
| 198 | + |
| 199 | +Inline spinner (e.g., in AppBar while tenant loads): |
| 200 | + |
| 201 | +```razor |
| 202 | +@if (TenantService.IsLoading) |
| 203 | +{ |
| 204 | + <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2"/> |
| 205 | +} |
| 206 | +``` |
0 commit comments