|
3 | 3 | @namespace BlazorDialog |
4 | 4 | @implements IDisposable |
5 | 5 | @inject ILocationChangingHandler locationChangingHandler |
| 6 | +@inject IJSRuntime jsRuntime |
6 | 7 | @if (isShowing) |
7 | 8 | { |
8 | 9 | <CascadingValue Value="this" Name="ParentDialog" IsFixed="true"> |
|
105 | 106 | /// </summary> |
106 | 107 | [Parameter] public bool PreventNavigation { get; set; } = true; |
107 | 108 |
|
| 109 | + /// <summary> |
| 110 | + /// If enabled the dialog can be close by a keyboard key (default "Escape"). Defaults to true. |
| 111 | + /// </summary> |
| 112 | + [Parameter] public bool KeyboardCloseEnabled { get; set; } = true; |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// The key to be used when <see cref="KeyboardCloseEnabled"/> is set to true. Default to "Escape". |
| 116 | + /// </summary> |
| 117 | + [Parameter] public string KeyboardCloseKey { get; set; } = "Escape"; |
| 118 | + |
| 119 | + |
108 | 120 | private bool? _forcedPreventNavigation; |
109 | 121 | protected TaskCompletionSource<object> taskCompletionSource; |
110 | 122 | internal Action<object> OnDialogHide; |
| 123 | + private DotNetObjectReference<Dialog>? dotNetObjectReference; |
| 124 | + |
111 | 125 | protected void NotifyDialogHidden(object result) => OnDialogHide?.Invoke(result); |
112 | 126 |
|
113 | 127 | protected string ContentWrapperCssClass |
|
168 | 182 | taskCompletionSource.SetResult(result); |
169 | 183 | } |
170 | 184 | }; |
| 185 | + dotNetObjectReference = DotNetObjectReference.Create(this); |
171 | 186 | } |
172 | 187 |
|
173 | 188 | public override async Task SetParametersAsync(ParameterView parameters) |
|
196 | 211 | } |
197 | 212 |
|
198 | 213 | locationChangingHandler.RegisterLocationChangingHandler(this); |
| 214 | + |
| 215 | + await jsRuntime.InvokeVoidAsync("blazorDialog.registerShownDialog", dotNetObjectReference, this.KeyboardCloseEnabled, this.KeyboardCloseKey); |
199 | 216 | } |
200 | 217 | else if (isShowingChanged && IsShowing == false && isShowing) |
201 | 218 | { |
|
211 | 228 | var args = new DialogAfterHideEventArgs(this); |
212 | 229 | await OnAfterHide.InvokeAsync(args); |
213 | 230 | } |
| 231 | + |
| 232 | + await jsRuntime.InvokeVoidAsync("blazorDialog.unregisterShownDialog", dotNetObjectReference); |
214 | 233 | } |
215 | 234 | } |
216 | 235 |
|
|
223 | 242 | taskCompletionSource.TrySetCanceled(); |
224 | 243 | taskCompletionSource = null; |
225 | 244 | } |
| 245 | + if (dotNetObjectReference != null) |
| 246 | + { |
| 247 | + dotNetObjectReference.Dispose(); |
| 248 | + dotNetObjectReference = null; |
| 249 | + } |
226 | 250 | } |
227 | 251 |
|
228 | 252 | public async Task Show() |
|
272 | 296 |
|
273 | 297 | locationChangingHandler.RegisterLocationChangingHandler(this); |
274 | 298 |
|
| 299 | + await jsRuntime.InvokeVoidAsync("blazorDialog.registerShownDialog", dotNetObjectReference, this.KeyboardCloseEnabled, this.KeyboardCloseKey); |
| 300 | + |
275 | 301 | taskCompletionSource = new TaskCompletionSource<object>(); |
276 | 302 | return await taskCompletionSource.Task; |
277 | 303 | } |
|
293 | 319 | var args = new DialogAfterHideEventArgs(this); |
294 | 320 | await OnAfterHide.InvokeAsync(args); |
295 | 321 | } |
| 322 | + |
| 323 | + await jsRuntime.InvokeVoidAsync("blazorDialog.unregisterShownDialog", dotNetObjectReference); |
296 | 324 | } |
297 | 325 |
|
298 | 326 | public async Task Hide() |
|
335 | 363 | } |
336 | 364 | return base.OnAfterRenderAsync(firstRender); |
337 | 365 | } |
| 366 | + |
| 367 | + [JSInvokable] |
| 368 | + public async Task HideFromKeyPress() |
| 369 | + { |
| 370 | + await this.Hide(); |
| 371 | + } |
338 | 372 | } |
0 commit comments