Skip to content

Commit ea707ba

Browse files
Add copy code button
1 parent 36b8c76 commit ea707ba

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

assets/javascripts/application.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,24 @@ layout: source
8787
}
8888
})
8989
}
90+
91+
if (navigator && navigator.clipboard) {
92+
const copyCodeButtons = document.querySelectorAll('[class^="language-"] button');
93+
94+
copyCodeButtons.forEach(function(button) {
95+
const codeElement = button.parentElement.querySelector('code');
96+
const originalText = button.innerText;
97+
98+
button.addEventListener('mousedown', async function() {
99+
await navigator.clipboard.writeText(codeElement.innerText);
100+
101+
button.innerText = 'Copied!';
102+
103+
setTimeout(() => {
104+
button.innerText = originalText;
105+
}, 1000);
106+
});
107+
});
108+
}
90109
});
91110
})();

assets/stylesheets/elements/_grid.scss

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
background-color: var(--color-fill-secondary);
1010
border: 1px solid var(--color-fill-tertiary);
1111
border-radius: var(--border-radius);
12-
12+
1313
padding: 1rem;
1414
display: flex;
1515
flex-direction: column;
@@ -77,7 +77,7 @@
7777
}
7878

7979
// Button typically embedded in a grid-level-1
80-
a.cta-secondary {
80+
a.cta-secondary, [class^="language-"] button {
8181
background-color: var(--color-fill-tertiary);
8282
border: 1px solid var(--color-fill-quaternary);
8383
border-radius: var(--border-radius);
@@ -99,7 +99,7 @@ a.cta-secondary {
9999
&:hover {
100100
color: var(--color-link);
101101
background-color: var(--color-fill-quaternary);
102-
102+
103103
text-decoration: none;
104104

105105
@media (prefers-contrast: more) {
@@ -112,3 +112,10 @@ a.cta-secondary {
112112
content: "";
113113
}
114114
}
115+
116+
[class^="language-"] button {
117+
cursor: pointer;
118+
padding: 0.1rem 0.4rem;
119+
margin-left: auto;
120+
font-size: 0.8rem;
121+
}

install/macos/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ title: Install Swift
1313
To download toolchains from Swift.org, use the Swiftly toolchain installer. Swift.org toolchains include experimental features like Embedded Swift and support for WebAssembly.
1414
</p>
1515
<h4>Run this in a terminal:</h4>
16-
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>curl -O https://download.swift.org/swiftly/darwin/swiftly-{{ site.data.builds.swiftly_release.version }}.pkg &amp;&amp; \
16+
<div class="language-plaintext highlighter-rouge"><button>Copy code</button><div class="highlight"><pre class="highlight"><code>curl -O https://download.swift.org/swiftly/darwin/swiftly-{{ site.data.builds.swiftly_release.version }}.pkg &amp;&amp; \
1717
installer -pkg swiftly-{{ site.data.builds.swiftly_release.version }}.pkg -target CurrentUserHomeDirectory &amp;&amp; \
1818
~/usr/local/bin/swiftly init --quiet-shell-followup &amp;&amp; \
1919
. ~/Library/Application\ Support/swiftly/env.sh &amp;&amp; \

0 commit comments

Comments
 (0)