Skip to content

Commit

Permalink
Add try/catch to CopyToClipboard. Remove old ClipboardService.
Browse files Browse the repository at this point in the history
  • Loading branch information
onerdy committed Mar 10, 2024
1 parent 5df072f commit 4ff83d9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 36 deletions.
19 changes: 12 additions & 7 deletions Components/CopyToClipboard.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
@*
Copying to clipboard using a Blazor component with JSInterop as seen below does not work on iOS:
await _jsInterop.InvokeVoidAsync("navigator.clipboard.writeText", text);
To work on iOS we are using JS code directly with the onclick function from an html input button.
The initial attempt to get CopyToClipboard working with Blazor JSInterop did not work on iOS.
So this component is using JS code directly with the onclick function from an html input button.
*@

<input type="button"
Expand All @@ -14,9 +12,16 @@ To work on iOS we are using JS code directly with the onclick function from an h
@($"color:{MyMudThemeProvider.MyTheme.Palette.Secondary};")
cursor: pointer;"
onclick="
navigator.clipboard.writeText('@TextToCopy');
this.value='Password copied!' ;
setTimeout(()=> (this.value = 'Copy'), 2000);">
try{
navigator.clipboard.writeText('@TextToCopy');
this.value='Password copied!' ;
setTimeout(()=> (this.value = 'Copy'), 2000);
}
catch (err)
{
alert (err);
}
">

@code {
[Parameter] public string? TextToCopy { get; set; }
Expand Down
3 changes: 0 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using MudBlazor.Services;
using passgen.Components;
using passgen.Services;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -9,8 +8,6 @@
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

builder.Services.AddScoped<IClipboardService, ClipboardService>();

builder.Services.AddMudServices();

var app = builder.Build();
Expand Down
19 changes: 0 additions & 19 deletions Services/ClipboardService.cs

This file was deleted.

7 changes: 0 additions & 7 deletions Services/IClipboardService.cs

This file was deleted.

0 comments on commit 4ff83d9

Please sign in to comment.