This repository was archived by the owner on Dec 26, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathBlazoredModalInstance.razor.cs
More file actions
376 lines (301 loc) · 11.1 KB
/
BlazoredModalInstance.razor.cs
File metadata and controls
376 lines (301 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Blazored.Modal.Services;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;
namespace Blazored.Modal;
public partial class BlazoredModalInstance : IDisposable
{
[CascadingParameter] private BlazoredModal Parent { get; set; } = default!;
[CascadingParameter] private ModalOptions GlobalModalOptions { get; set; } = default!;
[Parameter, EditorRequired] public RenderFragment Content { get; set; } = default!;
[Parameter, EditorRequired] public ModalOptions Options { get; set; } = default!;
[Parameter] public string? Title { get; set; }
[Parameter] public Guid Id { get; set; }
private string? Position { get; set; }
private string? ModalClass { get; set; }
private bool HideHeader { get; set; }
private bool HideCloseButton { get; set; }
private bool DisableBackgroundCancel { get; set; }
private bool DisableEscapeKey { get; set; }
private string? OverlayAnimationClass { get; set; }
private string? OverlayCustomClass { get; set; }
private ModalAnimationType? AnimationType { get; set; }
private bool ActivateFocusTrap { get; set; }
public bool UseCustomLayout { get; set; }
public FocusTrap? FocusTrap { get; set; }
[SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "This is assigned in Razor code and isn't currently picked up by the tooling.")]
private ElementReference _modalReference;
private bool _setFocus;
private bool _disableNextRender;
private bool _listenToBackgroundClicks;
// Temporarily add a tabindex of -1 to the close button so it doesn't get selected as the first element by activateFocusTrap
private readonly Dictionary<string, object> _closeBtnAttributes = new() { { "tabindex", "-1" } };
protected override bool ShouldRender()
{
if (!_disableNextRender)
{
return true;
}
_disableNextRender = false;
return false;
}
protected override void OnInitialized()
=> ConfigureInstance();
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
_closeBtnAttributes.Clear();
StateHasChanged();
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (_setFocus)
{
if (FocusTrap is not null
&& ActivateFocusTrap)
{
await FocusTrap.SetFocus();
}
_setFocus = false;
}
}
/// <summary>
/// Sets the title for the modal being displayed
/// </summary>
/// <param name="title">Text to display as the title of the modal</param>
public void SetTitle(string title)
{
Title = title;
StateHasChanged();
}
/// <summary>
/// Closes the modal with a default Ok result />.
/// </summary>
public async Task CloseAsync()
=> await CloseAsync(ModalResult.Ok());
/// <summary>
/// Closes the modal with the specified <paramref name="modalResult"/>.
/// </summary>
/// <param name="modalResult"></param>
public async Task CloseAsync(ModalResult modalResult)
{
// Fade out the modal, and after that actually remove it
if (AnimationType is ModalAnimationType.FadeInOut)
{
OverlayAnimationClass += " fade-out";
StateHasChanged();
await Task.Delay(400); // Needs to be a bit more than the animation time because of delays in the animation being applied between server and client (at least when using blazor server side), I think.
}
else if (AnimationType is ModalAnimationType.PopInOut)
{
OverlayAnimationClass += " pop-out";
StateHasChanged();
await Task.Delay(400);
}
await Parent.DismissInstance(Id, modalResult);
}
/// <summary>
/// Closes the modal and returns a cancelled ModalResult.
/// </summary>
public async Task CancelAsync()
=> await CloseAsync(ModalResult.Cancel());
/// <summary>
/// Closes the modal returning the specified <paramref name="payload"/> in a cancelled ModalResult.
/// </summary>
public async Task CancelAsync<TPayload>(TPayload payload)
=> await CloseAsync(ModalResult.Cancel(payload));
private void ConfigureInstance()
{
AnimationType = SetAnimation();
Position = SetPosition();
ModalClass = SetModalClass();
HideHeader = SetHideHeader();
HideCloseButton = SetHideCloseButton();
DisableBackgroundCancel = SetDisableBackgroundCancel();
DisableEscapeKey = SetDisableEscapeKey();
UseCustomLayout = SetUseCustomLayout();
OverlayCustomClass = SetOverlayCustomClass();
ActivateFocusTrap = SetActivateFocusTrap();
OverlayAnimationClass = SetAnimationClass();
Parent.OnModalClosed += AttemptFocus;
}
private void AttemptFocus()
=> _setFocus = true;
private bool SetUseCustomLayout()
{
if (Options.UseCustomLayout.HasValue)
{
return Options.UseCustomLayout.Value;
}
if (GlobalModalOptions.UseCustomLayout.HasValue)
{
return GlobalModalOptions.UseCustomLayout.Value;
}
return false;
}
private string SetPosition()
{
ModalPosition position;
if (Options.Position.HasValue)
{
position = Options.Position.Value;
}
else if (GlobalModalOptions.Position.HasValue)
{
position = GlobalModalOptions.Position.Value;
}
else
{
position = ModalPosition.TopCenter;
}
switch (position)
{
case ModalPosition.TopCenter:
return "";
case ModalPosition.TopLeft:
return "position-topleft";
case ModalPosition.TopRight:
return "position-topright";
case ModalPosition.Middle:
return "position-middle";
case ModalPosition.BottomLeft:
return "position-bottomleft";
case ModalPosition.BottomRight:
return "position-bottomright";
case ModalPosition.Custom:
if (!string.IsNullOrWhiteSpace(Options.PositionCustomClass))
return Options.PositionCustomClass;
if (!string.IsNullOrWhiteSpace(GlobalModalOptions.PositionCustomClass))
return GlobalModalOptions.PositionCustomClass;
throw new InvalidOperationException("Position set to Custom without a PositionCustomClass set");
default:
return "";
}
}
private string SetSize()
{
ModalSize size;
if (Options.Size.HasValue)
{
size = Options.Size.Value;
}
else if (GlobalModalOptions.Size.HasValue)
{
size = GlobalModalOptions.Size.Value;
}
else
{
size = ModalSize.Medium;
}
switch (size)
{
case ModalSize.Small:
return "size-small";
case ModalSize.Medium:
return "size-medium";
case ModalSize.Large:
return "size-large";
case ModalSize.ExtraLarge:
return "size-extra-large";
case ModalSize.Custom:
if (!string.IsNullOrWhiteSpace(Options.SizeCustomClass))
return Options.SizeCustomClass;
if (!string.IsNullOrWhiteSpace(GlobalModalOptions.SizeCustomClass))
return GlobalModalOptions.SizeCustomClass;
throw new InvalidOperationException("Size set to Custom without a SizeCustomClass set");
case ModalSize.Automatic:
return "size-automatic";
default:
return "size-medium";
}
}
private string SetModalClass()
{
var modalClass = string.Empty;
if (!string.IsNullOrWhiteSpace(Options.Class))
modalClass = Options.Class;
if (string.IsNullOrWhiteSpace(modalClass) && !string.IsNullOrWhiteSpace(GlobalModalOptions.Class))
modalClass = GlobalModalOptions.Class;
if (string.IsNullOrWhiteSpace(modalClass))
{
modalClass = "blazored-modal";
modalClass += $" {SetSize()}";
}
return modalClass;
}
private ModalAnimationType SetAnimation()
=> Options.AnimationType ?? GlobalModalOptions.AnimationType ?? ModalAnimationType.FadeInOut;
private string SetAnimationClass() => AnimationType switch
{
ModalAnimationType.FadeInOut => "fade-in",
ModalAnimationType.PopInOut or ModalAnimationType.PopIn => "pop-in",
_ => string.Empty,
};
private bool SetHideHeader()
{
if (Options.HideHeader.HasValue)
return Options.HideHeader.Value;
if (GlobalModalOptions.HideHeader.HasValue)
return GlobalModalOptions.HideHeader.Value;
return false;
}
private bool SetHideCloseButton()
{
if (Options.HideCloseButton.HasValue)
return Options.HideCloseButton.Value;
if (GlobalModalOptions.HideCloseButton.HasValue)
return GlobalModalOptions.HideCloseButton.Value;
return false;
}
private bool SetDisableBackgroundCancel()
{
if (Options.DisableBackgroundCancel.HasValue)
return Options.DisableBackgroundCancel.Value;
if (GlobalModalOptions.DisableBackgroundCancel.HasValue)
return GlobalModalOptions.DisableBackgroundCancel.Value;
return false;
}
private string SetOverlayCustomClass()
{
if (!string.IsNullOrWhiteSpace(Options.OverlayCustomClass))
return Options.OverlayCustomClass;
if (!string.IsNullOrWhiteSpace(GlobalModalOptions.OverlayCustomClass))
return GlobalModalOptions.OverlayCustomClass;
return string.Empty;
}
private bool SetActivateFocusTrap()
{
if (Options.ActivateFocusTrap.HasValue)
return Options.ActivateFocusTrap.Value;
if (GlobalModalOptions.ActivateFocusTrap.HasValue)
return GlobalModalOptions.ActivateFocusTrap.Value;
return true;
}
private async Task HandleBackgroundClick()
{
if (DisableBackgroundCancel)
{
_disableNextRender = true;
return;
}
if (_listenToBackgroundClicks)
{
await CancelAsync();
_listenToBackgroundClicks = false;
}
}
private bool SetDisableEscapeKey()
{
if (Options.DisableEscapeKey.HasValue)
return Options.DisableEscapeKey.Value;
return GlobalModalOptions.DisableEscapeKey.HasValue &&
GlobalModalOptions.DisableEscapeKey.Value;
}
private void ListenToBackgroundClick()
=> _listenToBackgroundClicks = true;
private void StopListeningToBackgroundClick()
=> _listenToBackgroundClicks = false;
void IDisposable.Dispose()
=> Parent.OnModalClosed -= AttemptFocus;
}