-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: add copy code btn #1841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add copy code btn #1841
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fc6c972
test: copy code btn
ShubhamOulkar 06110c5
Merge branch 'gh-pages' into copy-code-btn
ShubhamOulkar f045d2f
add copy btn svg
ShubhamOulkar fd12e54
add copy code btn
ShubhamOulkar f136b80
Merge branch 'gh-pages' into copy-code-btn
ShubhamOulkar 21067f4
fix: keyboard a11y and improve design
ShubhamOulkar f15c2e9
show "copied !" text on the copy btn
ShubhamOulkar 44b08f3
remove space in copied text
ShubhamOulkar bbc8743
Remove text shift
ShubhamOulkar 04fdc30
handle failed copy code
ShubhamOulkar 8f3d9c0
Merge branch 'gh-pages' into copy-code-btn
ShubhamOulkar 784571e
Minimize delay
ShubhamOulkar fa07661
Merge branch 'gh-pages' into copy-code-btn
carlosstenzel 2c76bf0
Merge branch 'gh-pages' into copy-code-btn
ShubhamOulkar 375826e
Merge branch 'gh-pages' into copy-code-btn
ShubhamOulkar 849bfac
remove outline on code blocks
ShubhamOulkar 47cd041
Merge branch 'gh-pages' into copy-code-btn
ShubhamOulkar bcbe344
refactor copycode.js
ShubhamOulkar a21869a
Remove border width
ShubhamOulkar 38d082a
Convert timerId into a Number()
ShubhamOulkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const codeBlocks = document.querySelectorAll("pre:has(code)"); | ||
|
||
codeBlocks.forEach((block) => { | ||
// Only add button if browser supports Clipboard API | ||
if (!navigator.clipboard) return; | ||
|
||
const button = createCopyButton(); | ||
block.appendChild(button); | ||
block.setAttribute("tabindex", 0); // Add keyboard a11y for <pre></pre> | ||
|
||
button.addEventListener("click", async () => { | ||
await copyCode(block, button); | ||
}); | ||
}); | ||
|
||
function createCopyButton() { | ||
const button = document.createElement("button"); | ||
setButtonAttributes(button, { | ||
type: "button", // button doesn't act as a submit button | ||
title: "copy code", | ||
"aria-label": "click to copy code", | ||
}); | ||
return button; | ||
} | ||
|
||
function setButtonAttributes(button, attributes) { | ||
for (const [key, value] of Object.entries(attributes)) { | ||
button.setAttribute(key, value); | ||
} | ||
} | ||
|
||
async function copyCode(block, button) { | ||
const code = block.querySelector("code"); | ||
const text = code.innerText; | ||
|
||
try { | ||
await navigator.clipboard.writeText(text); | ||
ShubhamOulkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
updateButtonState(button, "copied", "code is copied!"); | ||
} catch { | ||
updateButtonState(button, "failed", "failed!"); | ||
} | ||
} | ||
|
||
function updateButtonState(button, statusClass, ariaLabel) { | ||
button.setAttribute("aria-live", "polite"); | ||
button.setAttribute("aria-label", ariaLabel); | ||
button.classList.add(statusClass); | ||
|
||
// Clear any existing timer | ||
if (button.dataset.timerId) clearTimeout(Number(button.dataset.timerId)); | ||
const timer = setTimeout(() => { | ||
button.classList.remove(statusClass); | ||
button.setAttribute("aria-label", "click to copy code"); | ||
}, 1000); | ||
|
||
button.dataset.timerId = timer; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.