Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dismiss on Escape #88

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions NuGet-Packages.sln
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ Global
{892EA5E4-A98F-4A36-B901-8A1FAB27623E} = {4AADAD6A-4BA0-427E-9EA1-4AFE211C1200}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
RESX_SortFileContentOnSave = True
RESX_NeutralResourcesLanguage = de-DE
SolutionGuid = {46ECC226-1874-4539-8589-8B2029909C4A}
RESX_NeutralResourcesLanguage = de-DE
RESX_SortFileContentOnSave = True
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@
<None Include="Authentication\*.razor.css" />
</ItemGroup>

<ItemGroup>
<None Include="Typescript\Modal.ts" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="[7.0.14,)" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="[7.0.14,)" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="[7.0.0,)" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="[7.0.0,)" />
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="[8.0.0,)" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.4.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\JGUZDV.Extensions.Json\src\JGUZDV.Extensions.Json.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\js\" />
</ItemGroup>

</Project>
20 changes: 16 additions & 4 deletions libraries/JGUZDV.Blazor.Components/src/Modals/Modal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,20 @@
[Parameter]
public string? ModalFooterClasses { get; set; }

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_jsModule = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/JGUZDV.Blazor.Components/js/Modal.js").AsTask();
var reference = DotNetObjectReference.Create(this);
_ = _jsModule.InvokeVoidAsync("CloseListener", _componentId, reference);
}
}

protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
_jsModule = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/JGUZDV.Blazor.Components/js/Modal.js").AsTask();


if (Title != null && Header != null)
throw new InvalidOperationException($"{nameof(Title)} cannot be set, if {nameof(Header)} is set.");

Expand Down Expand Up @@ -103,7 +112,8 @@
: (ctx) =>
@<div class="modal-dialog @ModalDialogClasses" @onclick:stopPropagation="true">
@content.Invoke(ctx)
</div>;
</div>
;
}

public Task<ModalResult> ShowModal(TModel model)
Expand All @@ -119,10 +129,12 @@

public void Close(ModalResult result)
{
_ = _jsModule!.InvokeVoidAsync("CloseDialog", _componentId);
if (_taskCompletionSource.Task.IsCompleted) { return; }
_ = _jsModule!.InvokeVoidAsync("CloseDialog", _componentId, DotNetObjectReference.Create(this));
_taskCompletionSource.SetResult(result);
}

[JSInvokable]
public void Close()
=> Close(ModalResult.Undefined);

Expand Down
37 changes: 37 additions & 0 deletions libraries/JGUZDV.Blazor.Components/src/Typescript/Modal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export class closeListener {
constructor() {
this.handleCloseEvent = this.handleCloseEvent.bind(this);
};

public addListener(guid, reference) {
var dialog = document.getElementById(guid);
dialog.addEventListener("close", this.handleCloseEvent(reference));
}

public removeListener(guid, reference) {
var dialog = document.getElementById(guid);
dialog.removeEventListener("close", this.handleCloseEvent(reference));

}

private handleCloseEvent(reference) {
return () => reference.invokeMethodAsync("Close");
}
}

const listener = new closeListener();

export function OpenDialog(guid) {
var dialog = document.getElementById(guid) as HTMLDialogElement;
dialog.showModal();
};

export function CloseDialog(guid, reference) {
var dialog = document.getElementById(guid) as HTMLDialogElement;
listener.removeListener(guid, reference);
dialog.close();
};

export function CloseListener(guid, reference) {
listener.addListener(guid, reference);
};
18 changes: 18 additions & 0 deletions libraries/JGUZDV.Blazor.Components/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"alwaysStrict": false,
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"outDir": "./wwwroot/js",
"rootDir": "./Typescript",
"module": "ES6",
"target": "ES6",
"moduleResolution": "node",
"sourceMap": true,
"esModuleInterop": true
},
"include": [
"Typescript/**/*.ts"
]
}
35 changes: 30 additions & 5 deletions libraries/JGUZDV.Blazor.Components/src/wwwroot/js/Modal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading