Skip to content

Commit 4e45a71

Browse files
Merge pull request #27 from stavroskasidis/release/4.0.0
Version 4.0: Add closing by keyboard key press (escape by default).
2 parents 495f438 + 27b80d4 commit 4e45a71

13 files changed

Lines changed: 3929 additions & 2692 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,4 @@ __pycache__/
264264
/Artifacts/
265265
/DemoApp/BlazorDialog.DemoApp/Server/ClientSources/
266266
/BlazorDialog/wwwroot/styles.min.css
267+
/BlazorDialog/wwwroot/blazordialog.min.js

BlazorDialog/BlazorDialog.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Copyright />
1313
<PackageTags>blazor blazor-component blazor-dialog dialog modal blazor-modal blazordialog blazormodaldialog blazormodal razor razor-components razorcomponents</PackageTags>
1414
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
15-
<Version>3.2.0</Version>
15+
<Version>4.0.0</Version>
1616
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
1717
<Product>BlazorDialog</Product>
1818
</PropertyGroup>

BlazorDialog/Components/ComponentAsDialogContainer.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
99
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
1010
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow"
11-
PreventNavigation="ComponentDialog.Options.PreventNavigation">
11+
PreventNavigation="ComponentDialog.Options.PreventNavigation" KeyboardCloseEnabled="ComponentDialog.Options.KeyboardCloseEnabled"
12+
KeyboardCloseKey="@ComponentDialog.Options.KeyboardCloseKey">
1213
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
1314
</Dialog>
1415
}
@@ -18,7 +19,8 @@
1819
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
1920
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
2021
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow"
21-
PreventNavigation="ComponentDialog.Options.PreventNavigation">
22+
PreventNavigation="ComponentDialog.Options.PreventNavigation" KeyboardCloseEnabled="ComponentDialog.Options.KeyboardCloseEnabled"
23+
KeyboardCloseKey="@ComponentDialog.Options.KeyboardCloseKey">
2224
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
2325
</Dialog>
2426
}

BlazorDialog/Components/Dialog.razor

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@namespace BlazorDialog
44
@implements IDisposable
55
@inject ILocationChangingHandler locationChangingHandler
6+
@inject IJSRuntime jsRuntime
67
@if (isShowing)
78
{
89
<CascadingValue Value="this" Name="ParentDialog" IsFixed="true">
@@ -105,9 +106,22 @@
105106
/// </summary>
106107
[Parameter] public bool PreventNavigation { get; set; } = true;
107108

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+
108120
private bool? _forcedPreventNavigation;
109121
protected TaskCompletionSource<object> taskCompletionSource;
110122
internal Action<object> OnDialogHide;
123+
private DotNetObjectReference<Dialog>? dotNetObjectReference;
124+
111125
protected void NotifyDialogHidden(object result) => OnDialogHide?.Invoke(result);
112126

113127
protected string ContentWrapperCssClass
@@ -168,6 +182,7 @@
168182
taskCompletionSource.SetResult(result);
169183
}
170184
};
185+
dotNetObjectReference = DotNetObjectReference.Create(this);
171186
}
172187

173188
public override async Task SetParametersAsync(ParameterView parameters)
@@ -196,6 +211,8 @@
196211
}
197212

198213
locationChangingHandler.RegisterLocationChangingHandler(this);
214+
215+
await jsRuntime.InvokeVoidAsync("blazorDialog.registerShownDialog", dotNetObjectReference, this.KeyboardCloseEnabled, this.KeyboardCloseKey);
199216
}
200217
else if (isShowingChanged && IsShowing == false && isShowing)
201218
{
@@ -211,6 +228,8 @@
211228
var args = new DialogAfterHideEventArgs(this);
212229
await OnAfterHide.InvokeAsync(args);
213230
}
231+
232+
await jsRuntime.InvokeVoidAsync("blazorDialog.unregisterShownDialog", dotNetObjectReference);
214233
}
215234
}
216235

@@ -223,6 +242,11 @@
223242
taskCompletionSource.TrySetCanceled();
224243
taskCompletionSource = null;
225244
}
245+
if (dotNetObjectReference != null)
246+
{
247+
dotNetObjectReference.Dispose();
248+
dotNetObjectReference = null;
249+
}
226250
}
227251

228252
public async Task Show()
@@ -272,6 +296,8 @@
272296

273297
locationChangingHandler.RegisterLocationChangingHandler(this);
274298

299+
await jsRuntime.InvokeVoidAsync("blazorDialog.registerShownDialog", dotNetObjectReference, this.KeyboardCloseEnabled, this.KeyboardCloseKey);
300+
275301
taskCompletionSource = new TaskCompletionSource<object>();
276302
return await taskCompletionSource.Task;
277303
}
@@ -293,6 +319,8 @@
293319
var args = new DialogAfterHideEventArgs(this);
294320
await OnAfterHide.InvokeAsync(args);
295321
}
322+
323+
await jsRuntime.InvokeVoidAsync("blazorDialog.unregisterShownDialog", dotNetObjectReference);
296324
}
297325

298326
public async Task Hide()
@@ -335,4 +363,10 @@
335363
}
336364
return base.OnAfterRenderAsync(firstRender);
337365
}
366+
367+
[JSInvokable]
368+
public async Task HideFromKeyPress()
369+
{
370+
await this.Hide();
371+
}
338372
}

BlazorDialog/IBlazorDialogStore.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public ComponentAsDialogOptions(Type componentType)
7878
/// </summary>
7979
public Func<DialogAfterHideEventArgs, Task>? OnAfterHide { get; protected set; }
8080

81+
/// <summary>
82+
/// If enabled the dialog can be close by a keyboard key (default "Escape"). Defaults to true.
83+
/// </summary>
84+
public bool KeyboardCloseEnabled { get; set; } = true;
85+
86+
/// <summary>
87+
/// The key to be used when <see cref="KeyboardCloseEnabled"/> is set to true. Default to "Escape".
88+
/// </summary>
89+
public string KeyboardCloseKey { get; set; } = "Escape";
90+
8191
public Func<bool, Task>? OnAfterRender { get; protected set; }
8292

8393
internal event Func<Task> OnOptionsChanged;

BlazorDialog/gulpfile.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
rimraf = require("rimraf");
66

77

8-
// gulp.task("clean:js", function (cb) {
9-
// rimraf("wwwroot/**/*.min.js", cb);
10-
// });
8+
gulp.task("clean:js", function (cb) {
9+
rimraf("wwwroot/**/*.min.js", cb);
10+
});
1111

1212
gulp.task("clean:css", function (cb) {
1313
rimraf("wwwroot/**/*.min.css", cb);
@@ -22,15 +22,15 @@ gulp.task("min:css", function () {
2222
.pipe(gulp.dest("."));
2323
});
2424

25-
// gulp.task("min:js", function () {
26-
// return gulp.src(["wwwroot/**/*.js", "!wwwroot/**/*.min.js"], { base: "." })
27-
// .pipe(uglify())
28-
// .pipe(rename({
29-
// suffix: ".min"
30-
// }))
31-
// .pipe(gulp.dest("."));
32-
// });
25+
gulp.task("min:js", function () {
26+
return gulp.src(["wwwroot/**/*.js", "!wwwroot/**/*.min.js"], { base: "." })
27+
.pipe(uglify())
28+
.pipe(rename({
29+
suffix: ".min"
30+
}))
31+
.pipe(gulp.dest("."));
32+
});
3333

34-
gulp.task("clean", gulp.parallel( /* "clean:js",*/ "clean:css"));
35-
gulp.task("min", gulp.parallel(/* "min:js" ,*/ "min:css"));
34+
gulp.task("clean", gulp.parallel( "clean:js", "clean:css"));
35+
gulp.task("min", gulp.parallel("min:js" , "min:css"));
3636
gulp.task("all", gulp.series("clean", "min"));

0 commit comments

Comments
 (0)