Skip to content

feat(preview): show a copy icon on every code block's copy button - #1064

Merged
PathGao merged 2 commits into
sftwrdotdev:masterfrom
bapsespace:feat/visible-code-copy-button
Nov 8, 2026
Merged

PathGao merged 2 commits into
sftwrdotdev:masterfrom
bapsespace:feat/visible-code-copy-button

Conversation

@bapsespace

Copy link
Copy Markdown
Contributor

What this is

Code blocks in the preview already have a copy button: the label in the top-right corner. But on a block with a language, the label is just the language name (BASH, PYTHON) in 9 px text at 30% opacity. It reads as a caption, not a button. I went looking for a copy button, didn't find one, and only learned the label was it by reading the source. This makes the button look like a button.

  • Every label now shows the copy icon, the one already used for blocks without a language. When the block has a language, the name sits beside the icon.
  • Text goes from 9 px to 11 px, and resting opacity from 0.3 to 0.6. Hover goes to full opacity, and :focus-visible matches hover, so the label shows up when reached with the keyboard too.

Clicking it works exactly as before: it copies the code and the label flashes "Copied!".

Mechanism

In renderRichContent (src/lib/utils/richContent.ts), the label got COPY_ICON_SVG only in the else branch, when the block had no language. With a language, it got textContent = language and nothing else. Now the icon is always set, and the language goes in a <span class="lang-label-name"> after it.

The icon has no text, so the label's textContent is still exactly the language. The existing tests that read .lang-label text (preview.richContent.test.ts, the idempotence tests in preview.spec.ts) are unchanged and still pass. flashCopiedLabel saves and restores innerHTML, so the icon comes back after "Copied!".

Scope

  • The label's title stays the hard-coded English "Click to copy code". Moving it to i18n would touch every locale file, and that's a separate change.
  • The export is untouched: @media print and the exported HTML already hide .lang-label.
  • No new setting. It's a visual change to an existing control.

Tests

New describe('copy button markup') in scripts/preview.spec.ts (vitest, real DOM). It runs renderRichContent with stub libraries and checks three things:

  • a block with a language gets the icon and a .lang-label-name holding the language, and textContent is still the language
  • a block without a language gets the icon alone
  • clicking the label passes the block's code to onCopyCode

With the richContent.ts change reverted and the test kept, the first test goes red, as it should (Tests 1 failed | 2 passed).

Verification

On Windows 11:

npm run check        # 0 errors, 0 warnings
npm run lint         # clean
npm test             # all pass
npm run test:vitest  # 35 files, 746 tests pass
npm run tauri build -- --no-bundle   # release build OK

Manually, in the release build on Windows 11: bash and python blocks show the icon plus the language name, a plain block shows the icon alone, hover brightens it, and clicking copies the code (without the label) and flashes "Copied!".

Not verified: macOS and Linux. The change is CSS and DOM only, with nothing platform-specific.

🤖 Generated with Claude Code

A fenced block with a language showed only the language name, in 9 px
text at 30% opacity, so the copy button read as a caption and readers
did not find it. The label now always carries the copy icon, with the
language name beside it, and is larger and less faint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PathGao
PathGao merged commit bf81059 into sftwrdotdev:master Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants