Skip to content

Commit 6d6c60b

Browse files
authored
Docs: added copy button to non-inline code snippets (mrdoob#29700)
* docs: added copy button to non-inline code snippets * docs: copy button icons img path update
1 parent 4c36f5f commit 6d6c60b

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

docs/page.css

+36
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ ul code {
156156
margin: 16px 0;
157157
}
158158

159+
code {
160+
position: relative;
161+
}
162+
159163
code.inline {
160164
display: inline-block;
161165
vertical-align: middle;
@@ -165,6 +169,32 @@ code.inline {
165169
margin: 0;
166170
}
167171

172+
.copy-btn {
173+
cursor: pointer;
174+
position: absolute;
175+
top: 16px;
176+
right: 16px;
177+
width: 24px;
178+
height: 24px;
179+
background-color: transparent;
180+
background-image: url('../files/ic_copy_grey_24dp.svg');
181+
background-size: contain;
182+
background-position: center;
183+
background-repeat: no-repeat;
184+
opacity: 0.9;
185+
border: none;
186+
}
187+
188+
.copy-btn:hover {
189+
opacity: 1;
190+
}
191+
192+
.copy-btn.copied {
193+
pointer-events: none;
194+
opacity: 1;
195+
background-image: url('../files/ic_tick_green_24dp.svg');
196+
}
197+
168198
table {
169199
width: 100%;
170200
border-collapse: collapse;
@@ -315,4 +345,10 @@ a.param:hover {
315345
line-height: 28px;
316346
}
317347

348+
.copy-btn {
349+
top: 12px;
350+
right: 8px;
351+
width: 20px;
352+
height: 20px;
353+
}
318354
}

docs/page.js

+34
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,34 @@ function onDocumentLoad() {
125125

126126
}
127127

128+
// create copy button for copying code snippets
129+
130+
function addCopyButton( element ) {
131+
132+
const copyButton = document.createElement( 'button' );
133+
copyButton.className = 'copy-btn';
134+
135+
element.appendChild( copyButton );
136+
137+
copyButton.addEventListener( 'click', function () {
138+
139+
const codeContent = element.textContent;
140+
navigator.clipboard.writeText( codeContent ).then( () => {
141+
142+
copyButton.classList.add( 'copied' );
143+
144+
setTimeout( () => {
145+
146+
copyButton.classList.remove( 'copied' );
147+
148+
}, 1000 );
149+
150+
} );
151+
152+
} );
153+
154+
}
155+
128156
const elements = document.getElementsByTagName( 'code' );
129157

130158
for ( let i = 0; i < elements.length; i ++ ) {
@@ -133,6 +161,12 @@ function onDocumentLoad() {
133161

134162
element.textContent = dedent( element.textContent );
135163

164+
if ( ! element.classList.contains( 'inline' ) ) {
165+
166+
addCopyButton( element );
167+
168+
}
169+
136170
}
137171

138172
// Edit button

files/ic_copy_grey_24dp.svg

+3
Loading

files/ic_tick_green_24dp.svg

+3
Loading

0 commit comments

Comments
 (0)