Synchronous, GLFW-backed text access to the operating system clipboard. morphClipboard is a native global: it is available without an import and works in module initializers, component bodies, and event handlers.
morphClipboard.writeText("hello")
const text = morphClipboard.readText() // stringnavigator.clipboard?navigator.clipboard is permission-gated, Promise-based, and limited to secure browser contexts. Its contract includes asynchronous writeText/readText, rejection on denial or missing permission, arbitrary MIME types through ClipboardItem, and clipboardchange events. Morph's clipboard is directly backed by the OS through GLFW: it is synchronous, text-only, and has no permission prompt. A same-name API would promise an async failure model Morph does not have, so the name changes under Name Follows Behavior.
navigator.clipboard |
morphClipboard |
|
|---|---|---|
| Access | Async Promise methods | Synchronous methods |
| Permissions | Prompt / rejection | None |
| Text read | Promise<string> |
string (empty when unavailable) |
| Text write | Promise<void> |
boolean |
| Rich content | ClipboardItem MIME types |
Plain text only |
| Change events | clipboardchange |
None |
| Context | Secure browser origin | Native desktop app |
morphClipboard is a namespace, not a value. Only these direct calls lower:
morphClipboard.readText(): string
morphClipboard.writeText(value): boolean| Method | Parameters | Returns | Behavior |
|---|---|---|---|
readText |
— | string |
Current clipboard text, or "" when unavailable |
writeText |
value |
boolean |
true when the OS accepted the write, false otherwise |
writeText converts its argument with Morph's runtime conversion, which matches JavaScript String(): strings pass through, numbers and booleans format as JS does, null becomes "null", undefined becomes "undefined", arrays become "[object Array]", and objects "[object Object]". writeText(42) writes "42".
The read result is an ordinary string, so string members chain directly:
const upper = morphClipboard.readText().toUpperCase()
const size = morphClipboard.readText().lengthClipboard access requires a live native window. Module initializers run before GLFW creates the first window, so at module scope:
readText() returns ""writeText(...) returns falseThe same degradation applies when a headless test installs no backend. The API never throws and never blocks waiting for permission; check the boolean when a failed copy matters:
const copied = morphClipboard.writeText(shareLink)
status = copied ? "Copied" : "Clipboard unavailable"readText() returning "" means either an empty clipboard or an unavailable one — the two are deliberately indistinguishable because the native OS clipboard does not expose a reliable error channel.
import { morphState } from 'morph'
export const windowConfig = { title: "Clipboard", width: 640, height: 480 }
export default function ClipboardDemo() {
const [draft, setDraft] = morphState("")
const [status, setStatus] = morphState("idle")
const copy = () => {
const ok = morphClipboard.writeText(draft)
setStatus(ok ? "copied" : "unavailable")
}
const paste = () => {
const text = morphClipboard.readText()
setDraft(text)
setStatus(text === "" ? "empty" : "pasted")
}
return (
<body>
<input value={draft} onInput={(e) => setDraft(e.target.value)} />
<text>{status}</text>
<button onClick={copy}>Copy</button>
<button onClick={paste}>Paste</button>
</body>
)
}// Normalize pasted text before use.
const pasted = morphClipboard.readText().trim()
if (pasted !== "") {
console.log("pasted:", pasted)
}morph check rejects anything outside the method-only surface, so unsupported code never compiles to a different meaning:
| Form | Error |
|---|---|
morphClipboard.readTextX() |
mx-js-member |
morphClipboard.readText (property) |
mx-js-member |
morphClipboard["readText"]() |
mx-js-member |
const clip = morphClipboard |
mx-js-member |
morphClipboard.writeText() (wrong arity) |
mx-js-syntax |
morphClipboard.writeText(...args) (spread) |
mx-js-syntax |
navigator.clipboard remains a browser API and is rejected with mx-js-global.