Commit 6d375ea
feature: Add Clipboard API support to uni-dom (#404)
## Summary
- Add `Clipboard` object for reading and writing to the system clipboard
- Provides both async (Future-based) and reactive (Rx-based) APIs
- Includes event handlers for copy/cut/paste interception
- Falls back to `execCommand` for older browsers
## Features
```scala
import wvlet.uni.dom.all.*
// Write text to clipboard
Clipboard.writeText("Hello, World!").foreach(_ => println("Copied!"))
// Read text from clipboard
Clipboard.readText().foreach(text => println(s"Pasted: ${text}"))
// One-click copy button
button(
Clipboard.copyOnClick("Copy this text", onSuccess = () => showToast("Copied!")),
"Copy"
)
// Dynamic copy (evaluates at click time)
button(
Clipboard.copyOnClickDynamic(() => inputRef.current.map(_.value).getOrElse("")),
"Copy Input"
)
// Intercept paste events
input(
Clipboard.onPaste { text =>
println(s"User pasted: ${text}")
}
)
// Customize what gets copied
div(
Clipboard.copyAs(() => "Custom formatted text"),
"Select and copy me"
)
// Check browser support
if Clipboard.isSupported then
// Use modern API
else
// Fallback behavior
```
## API
| Method | Description |
|--------|-------------|
| `writeText(text)` | Write text to clipboard (Future) |
| `readText()` | Read text from clipboard (Future) |
| `writeTextRx(text)` | Write text, returns Rx[Boolean] |
| `onCopy(handler)` | Handle copy events |
| `onCut(handler)` | Handle cut events |
| `onPaste(handler)` | Handle paste events |
| `copyAs(getText)` | Customize copied content |
| `cutAs(getText)` | Customize cut content |
| `copyOnClick(text)` | Copy on click |
| `copyOnClickDynamic(getText)` | Copy dynamic text on click |
| `isSupported` | Check if Clipboard API is available |
## Test plan
- [x] All 257 tests pass (17 new clipboard tests)
- [x] Compilation successful
- [x] Code formatted with scalafmtAll
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.5 <[email protected]>1 parent ceeb28e commit 6d375ea
File tree
3 files changed
+453
-0
lines changed- uni-dom-test/src/test/scala/wvlet/uni/dom
- uni/.js/src/main/scala/wvlet/uni/dom
3 files changed
+453
-0
lines changedLines changed: 130 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
0 commit comments