Skip to content

Add copy-to-clipboard buttons for code blocks in /docs routes#23

Open
shibadityadeb wants to merge 1 commit intotheatre-js:mainfrom
shibadityadeb:copy-button-added
Open

Add copy-to-clipboard buttons for code blocks in /docs routes#23
shibadityadeb wants to merge 1 commit intotheatre-js:mainfrom
shibadityadeb:copy-button-added

Conversation

@shibadityadeb
Copy link
Copy Markdown

Adds interactive copy buttons to all code blocks displayed in the /docs routes and child pages. The feature enhances user experience by allowing readers to quickly copy code snippets with a single click.

Features

Button Behavior:

  • Compact chip-style button positioned at top-right corner of each code block
  • Works on all elements with ch-code* or ch-codeblock* classes, with fallback to <pre><code>
  • One button per code block with duplicate prevention via dataset tracking

User Interactions:

  • Click to copy code to clipboard
  • Keyboard support: Enter or Space keys to activate
  • Visual feedback: Button text changes to "Copied!" (green) for 1.5 seconds on success
  • Error state displays "Failed" (red) if copy operation fails
  • Smooth hover effects with opacity and shadow transitions

Technical Implementation:

  • Inline styles with !important flags to override inherited page CSS
  • Responsive positioning: uses parent element with relative positioning
  • Preserves line breaks using innerText extraction
  • Modern Clipboard API with fallback to execCommand('copy') for older browsers
  • MutationObserver watches for dynamically loaded code blocks
  • Accessibility: full ARIA labels and keyboard support
  • No global CSS files modified—styles applied directly to elements

Scope:

  • Only activates on routes starting with /docs
  • Does not modify existing code, styles, or documentation
  • Non-intrusive enhancement that preserves all existing functionality

Testing

Verified on multiple code block types with various content lengths. Button displays correctly in all states and integrates seamlessly with existing documentation layout.

Screenshot 2025-11-22 at 7 14 38 PM

@netlify
Copy link
Copy Markdown

netlify bot commented Nov 22, 2025

Deploy Preview for theatrejs-dot-com ready!

Name Link
🔨 Latest commit 1b95c09
🔍 Latest deploy log https://app.netlify.com/projects/theatrejs-dot-com/deploys/6921bf59ab9de90008da040e
😎 Deploy Preview https://deploy-preview-23--theatrejs-dot-com.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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.

1 participant