Hooks
usePaste
Listen for paste events on the canvas
The usePaste hook listens for paste events (Ctrl+V / Cmd+V) on the canvas.
import { usePaste } from "@gridland/web"Usage
function Editor() {
const [text, setText] = useState("")
usePaste((pasted) => {
setText((prev) => prev + pasted)
})
return <text>{text}</text>
}Examples
Append to Input
function Editor() {
const [text, setText] = useState("")
usePaste((pasted) => {
setText((prev) => prev + pasted)
})
return <text>{text}</text>
}Paste Validation
Filter or transform pasted content before using it.
function UrlInput() {
const [url, setUrl] = useState("")
const [error, setError] = useState("")
usePaste((pasted) => {
const trimmed = pasted.trim()
if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
setUrl(trimmed)
setError("")
} else {
setError("Only URLs are accepted")
}
})
return (
<box>
<text>{url || "Paste a URL..."}</text>
{error && <text color="red">{error}</text>}
</box>
)
}Parameters
| Param | Type | Description |
|---|---|---|
callback | (text: string) => void | Called with the pasted text |