https://github.com/soenneker/soenneker.blazor.utils.clipboard
A Blazor interop library for clipboard management
https://github.com/soenneker/soenneker.blazor.utils.clipboard
blazor blazorclipboardutil blazorlibrary clipboard csharp dotnet razor utility utils
Last synced: about 2 months ago
JSON representation
A Blazor interop library for clipboard management
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.blazor.utils.clipboard
- Owner: soenneker
- License: mit
- Created: 2026-03-31T13:19:57.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2026-04-29T05:16:00.000Z (2 months ago)
- Last Synced: 2026-04-29T07:14:33.666Z (2 months ago)
- Topics: blazor, blazorclipboardutil, blazorlibrary, clipboard, csharp, dotnet, razor, utility, utils
- Language: CSS
- Homepage: https://soenneker.com
- Size: 234 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/soenneker.blazor.utils.clipboard/)
[](https://github.com/soenneker/soenneker.blazor.utils.clipboard/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/soenneker.blazor.utils.clipboard/)
[](https://soenneker.github.io/soenneker.blazor.utils.clipboard)
# Soenneker.Blazor.Utils.Clipboard
Blazor library for reading and writing the clipboard in the browser. Uses the Clipboard API (HTTPS or localhost). Supports plain text, HTML, images, permission checks, and no-throw helpers.
---
## Install
```bash
dotnet add package Soenneker.Blazor.Utils.Clipboard
```
---
## Setup
Register clipboard services (e.g. in `Program.cs`):
```csharp
builder.Services.AddClipboardAsScoped();
```
Inject `IClipboardUtil` where you need it:
```csharp
@inject IClipboardUtil Clipboard
```
---
## Usage
### Check if clipboard is available
Use this when you might not be in a secure context (e.g. prerender, some hosts). Result is cached.
```csharp
if (await Clipboard.HasClipboard())
{
await Clipboard.CopyText("Hello");
}
```
### Read and write plain text
```csharp
// Write
await Clipboard.WriteText("Hello, world");
await Clipboard.CopyText("Same as WriteText"); // alias
// Read (may throw if permission denied)
string text = await Clipboard.ReadText();
```
### Try read / try write (no throw)
Use these to avoid try/catch when you only care about success or failure:
```csharp
// Try read: (success, text)
var (ok, text) = await Clipboard.TryReadText();
if (ok && !string.IsNullOrEmpty(text))
Console.WriteLine(text);
// Try write: true if copied, false on error
bool copied = await Clipboard.TryWriteText("Hello");
if (!copied)
ShowMessage("Could not copy to clipboard");
```
### Permission state
Check read/write permission state (where supported; returns `Unsupported` in e.g. Firefox/Safari):
```csharp
var readState = await Clipboard.GetReadPermissionState(); // Granted, Denied, Prompt, Unsupported
var writeState = await Clipboard.GetWritePermissionState();
```
### Clear clipboard
```csharp
await Clipboard.Clear();
```
### Rich content: plain + HTML
Useful when pasting into rich editors (plain fallback + HTML):
```csharp
await Clipboard.CopyPlainAndHtml("Fallback text", "
Rich HTML
");
```
### Read/write multiple types (text, HTML, images)
For full control, use `Read()` and `Write()` with `ClipboardItemDto`. Each item has a dictionary of MIME type → content (text or data URL for images).
```csharp
// Read all items and types from clipboard
IReadOnlyList items = await Clipboard.Read();
foreach (var item in items)
{
foreach (var (mimeType, content) in item.Types)
{
if (content.StartsWith("data:image/"))
// image as data URL
else
// text (e.g. text/plain, text/html)
}
}
// Write custom items
await Clipboard.Write(new[]
{
ClipboardItems.CreatePlainAndHtml("Plain", "
HTML
"),
ClipboardItems.CreateImage("data:image/png;base64,...")
});
```