Skip to content

Commit 17f7572

Browse files
author
Jesse Haigh
committed
position copy button relative to code listing with CSS
1 parent cce81b7 commit 17f7572

File tree

1 file changed

+4
-52
lines changed

1 file changed

+4
-52
lines changed

src/components/ContentNode/CodeListing.vue

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
:fileType="fileType"
2222
>{{ fileName }}
2323
</Filename>
24-
<div class="container-general" ref="scrollContainer">
24+
<div class="container-general">
2525
<button
2626
v-if="copyToClipboard"
27-
v-show="showCopyButton"
2827
class="copy-button"
29-
ref="copyButton"
30-
:class="{ copied: isCopied, visible: buttonPositioned }"
28+
:class="{ copied: isCopied }"
3129
@click="copyCodeToClipboard"
32-
@update="handleScroll"
3330
aria-label="Copy code to clipboard"
3431
>
3532
<svg
@@ -72,7 +69,6 @@
7269
</template>
7370

7471
<script>
75-
import debounce from 'docc-render/utils/debounce';
7672
import { escapeHtml } from 'docc-render/utils/strings';
7773
import Language from 'docc-render/constants/Language';
7874
import CodeBlock from 'docc-render/components/CodeBlock.vue';
@@ -87,20 +83,8 @@ export default {
8783
return {
8884
syntaxHighlightedLines: [],
8985
isCopied: false,
90-
showCopyButton: true,
91-
buttonPositioned: false,
92-
scrollTimeout: null,
9386
};
9487
},
95-
mounted() {
96-
this.$nextTick(() => {
97-
this.updateCopyButtonPosition();
98-
const container = this.$refs.scrollContainer;
99-
if (container) {
100-
container.addEventListener('scroll', this.handleScroll, { passive: true });
101-
}
102-
});
103-
},
10488
props: {
10589
fileName: String,
10690
isFileNameActionable: {
@@ -170,29 +154,6 @@ export default {
170154
line === '' ? '\n' : line
171155
));
172156
},
173-
updateCopyButtonPosition() {
174-
const container = this.$refs.scrollContainer;
175-
const button = this.$refs.copyButton;
176-
177-
if (!container || !button) return;
178-
179-
const { scrollLeft } = container;
180-
181-
button.style.transform = `translateX(${scrollLeft}px)`;
182-
this.buttonPositioned = true;
183-
},
184-
handleScroll: debounce(function handleScroll() {
185-
this.showCopyButton = false;
186-
this.updateCopyButtonPosition();
187-
188-
if (this.scrollTimeout) {
189-
clearTimeout(this.scrollTimeout);
190-
}
191-
192-
this.scrollTimeout = window.setTimeout(() => {
193-
this.showCopyButton = true;
194-
}, 500);
195-
}, 100),
196157
copyCodeToClipboard() {
197158
const lines = this.content;
198159
const text = lines.join('\n');
@@ -272,6 +233,7 @@ code {
272233
flex-direction: column;
273234
border-radius: var(--code-border-radius, $border-radius);
274235
overflow: hidden;
236+
position: relative;
275237
// we need to establish a new stacking context to resolve a Safari bug where
276238
// the scrollbar is not clipped by this element depending on its border-radius
277239
@include new-stacking-context;
@@ -283,7 +245,6 @@ code {
283245
284246
.container-general {
285247
overflow: auto;
286-
position: relative;
287248
}
288249
289250
.container-general,
@@ -301,13 +262,8 @@ pre {
301262
border-radius: 6px;
302263
padding: 7px 6px;
303264
cursor: pointer;
304-
display: none;
305-
opacity: 0;
306-
transition: all 0.2s ease-in-out;
307-
}
308-
309-
.copy-button.visible {
310265
opacity: 1;
266+
transition: all 0.2s ease-in-out;
311267
}
312268
313269
.copy-button svg {
@@ -328,8 +284,4 @@ pre {
328284
color: var(--color-figure-blue);
329285
}
330286
331-
.container-general:hover .copy-button {
332-
display: flex;
333-
}
334-
335287
</style>

0 commit comments

Comments
 (0)