bClipboard is a Blazor library that simplifies clipboard operations for Blazor applications. It provides methods to copy text to and read text from the clipboard using JavaScript interop.
- Copy to Clipboard: Easily copy text to the clipboard.
- Read from Clipboard: Read text from the clipboard.
To install bClipboard, you need to add the NuGet package to your Blazor project:
dotnet add package bClipboard
In your Program.cs
file, register the ClipboardService
with the dependency injection container.
[...]
using bClipboard.Extensions;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddBClipboardService();
await builder.Build().RunAsync();
Inject the IClipboardService
into your Blazor component and use its methods to copy to and read from the clipboard.
@page "/clipboard"
@inject IClipboardService ClipboardService
<h3>Clipboard Example</h3>
<input @bind="textToCopy" placeholder="Enter text to copy" />
<button @onclick="CopyToClipboard">Copy Text</button>
<p>Clipboard Content: @clipboardContent</p>
<button @onclick="ReadFromClipboard">Read Clipboard</button>
@code {
private string textToCopy;
private string clipboardContent;
private async Task CopyToClipboard()
{
await ClipboardService.CopyToClipboardAsync(textToCopy);
}
private async Task ReadFromClipboard()
{
clipboardContent = await ClipboardService.ReadFromClipboardAsync();
}
}
The library uses a JavaScript file (bclipboard.js
) for clipboard operations.
We welcome contributions! Please see our contributing guidelines for more details.