Skip to content

[feature] Add useClipboard hook for copying text to system clipboard #3746

Description

@pixeltannu

What problem does this solve?

TermUI apps often need to let users copy output (logs, generated code,
config values, IDs, etc.) to the system clipboard. Right now there's no
built-in way to do this — developers have to manually shell out to
platform-specific commands (pbcopy, xclip/xsel, clip.exe) inside
their own components, which is repetitive and error-prone across OSes.

Proposed solution

Add a useClipboard hook to @termuijs/jsx (or a new @termuijs/clipboard
utility) that exposes a simple copy/read API and handles OS detection
internally.

import { useClipboard } from '@termuijs/jsx'

function CopyableOutput({ text }: { text: string }) {
  const { copy, copied } = useClipboard()

  useKeymap([
    { key: 'c', ctrl: true, action: () => copy(text) },
  ])

  return (
    <Box border="round" padding={1}>
      <Text>{text}</Text>
      <Text dim>{copied ? '✓ Copied!' : 'Press ctrl+c to copy'}</Text>
    </Box>
  )
}
  • copy(text: string): Promise<void> — writes text to system clipboard
  • read(): Promise<string> — reads current clipboard content (optional, phase 2)
  • copied: boolean — transient state, auto-resets after ~2s, useful for UI feedback
  • Should detect platform (macOS/Linux/Windows) and gracefully no-op with a warning if no clipboard utility is found (e.g., headless CI environment)

Alternatives considered

  • Leaving it to userland (current state) — rejected, since it forces every
    app to reimplement OS-specific clipboard logic
  • Depending on an external npm package like clipboardy — viable, but
    adds a runtime dependency; worth discussing whether TermUI wants to stay
    fully dependency-free per its "No native dependencies" philosophy

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

assignedIssue claimed by a contributor.type:feature+10 pts. New feature.

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions