-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Describe the bug
We are encountering an issue with clipboard interaction in a Blazor application using JavaScript interop. Specifically, when invoking navigator.clipboard.writeText via JS interop, the clipboard operation fails only in Safari on macOS.
The error message observed is:
Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
This issue does not occur in other browsers such as Chrome or Edge, where the clipboard operation works as expected.
To replicate the issue, we created a minimal sample where a button click triggers a JS interop call to copy text to the clipboard.
Sample to replicate issue
JavaScript:
window.copyToClipboard = (text) => {
navigator.clipboard.writeText(text).then(() => {
console.log("Text copied to clipboard:", text);
}).catch(err => {
console.error("Clipboard copy failed:", err);
});
};
Blazor Razor Page:
@rendermode InteractiveServer
@inject IJSRuntime JS
<h3>Clipboard Copy Example</h3>
<button @onclick="HandleClick">Copy Text</button>
@code {
private async Task HandleClick()
{
await JS.InvokeVoidAsync("copyToClipboard", "TextToCopy");
}
}
Expected Behavior
Clicking the button should copy the text "TextToCopy" to the clipboard without any errors across all modern browsers.
Steps To Reproduce
- Run the sample application in Safari browser on macOS.
- Open the browser console (Developer Tools).
- Click the "Copy Text" button.
- Observe the console for error.
Exceptions (if any)
No response
.NET Version
9.0.101
Anything else?
Environment Details
Device: MacBook Air 13-inch, M3, 2024
Chip Type: Apple M3
Browser: Safari
OS: macOS
Blazor Version: Server-side with @rendermode InteractiveServer