-
Notifications
You must be signed in to change notification settings - Fork 64
Add copy-to-clipboard support to code blocks #961
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
Changes from all commits
Commits
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
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,35 @@ | ||
<!-- | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
--> | ||
|
||
<template> | ||
<SVGIcon | ||
class="CheckmarkIcon" | ||
viewBox="0 0 24 24" | ||
themeId="checkmark" | ||
> | ||
<path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"/> | ||
</SVGIcon> | ||
</template> | ||
|
||
<script> | ||
import SVGIcon from 'docc-render/components/SVGIcon.vue'; | ||
|
||
export default { | ||
name: 'CheckmarkIcon', | ||
components: { SVGIcon }, | ||
}; | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.CheckmarkIcon { | ||
opacity: 1; | ||
stroke: currentColor; | ||
} | ||
</style> |
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,37 @@ | ||
<!-- | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
--> | ||
|
||
<template> | ||
<SVGIcon | ||
class="CopyIcon" | ||
viewBox="0 0 24 24" | ||
themeId="copy" | ||
> | ||
<title>{{ $t('icons.copy') }}</title> | ||
<path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 | ||
.9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 | ||
16H8V7h11v14z"/> | ||
</SVGIcon> | ||
</template> | ||
|
||
<script> | ||
import SVGIcon from 'docc-render/components/SVGIcon.vue'; | ||
|
||
export default { | ||
name: 'CopyIcon', | ||
components: { SVGIcon }, | ||
}; | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.CopyIcon { | ||
opacity: 0.8; | ||
} | ||
</style> |
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,37 @@ | ||
<!-- | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
--> | ||
|
||
<template> | ||
<SVGIcon | ||
class="CrossIcon" | ||
viewBox="0 0 24 24" | ||
themeId="cross" | ||
> | ||
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 | ||
10.59 12 5 17.59 6.41 19 12 13.41 | ||
17.59 19 19 17.59 13.41 12z"/> | ||
</SVGIcon> | ||
</template> | ||
|
||
<script> | ||
import SVGIcon from 'docc-render/components/SVGIcon.vue'; | ||
|
||
export default { | ||
name: 'CrossIcon', | ||
components: { SVGIcon }, | ||
}; | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.CrossIcon { | ||
opacity: 1; | ||
stroke: currentColor; | ||
} | ||
</style> |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Design-wise, I think the button can be a little distracting for code listings with a long first-line of code since the button always shows directly over the text (when enabled).
If we were to enable this button by default for code listings (which I personally think would be great), we may want to consider only showing it when hovering over the listing to try and eliminate some of that distraction maybe? That's just my personal opinion though. We could also consider a different layout where the button doesn't appear directly over the text as an alternative, although the positioning could get tricky in that case.
As a concrete example, 2 of the 3 code listings on this screenshot from the PR description are obscured by the button, regardless of where the user focus is:

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it’s nicer to show the copy button only when hovering over code blocks. Originally I had the button only show when hovering over code blocks. However, Joe and I got some feedback on the forums that it’d be better for mobile use to have the button always visible.
I think I’ve got a decent solution here in my latest commit. I’m using
@media (hover: hover)
for devices that support hover and I added@media (hover: none)
, when hover isn’t supported, to set the button to be always visible. Let me know what you think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to share the same pictures from the forums, here's what those changes look like on desktop and mobile.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a pretty reasonable compromise for now, especially since this is only enabled with an experimental flag for now.
I think we may want to consider disabling it entirely or using an alternate layout for mobile/non-hover devices in the future due to the smaller benefit there weighed against the downside of how it obscures a lot of content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Glad to see the discussion on the forums.