forked from ionic-team/ionic-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.tsx
More file actions
51 lines (44 loc) · 1.41 KB
/
Copy pathcode.tsx
File metadata and controls
51 lines (44 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Component, Element, Host, Prop, State, h } from '@stencil/core';
import { CheckmarkSoft } from '../../icons';
@Component({
tag: 'docs-code',
styleUrl: 'code.css'
})
export class DocsCode {
@Element() el!: HTMLElement;
@Prop({ reflect: true }) language = '';
@State() showCopyConfirmation = false;
copyCodeText() {
this.showCopyConfirmation = true;
const codeEl = this.el.querySelector('code');
const codeText = codeEl && codeEl.textContent || '';
const el = document.createElement('textarea');
el.value = codeText;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
setTimeout(() => {
this.showCopyConfirmation = false;
}, 2000);
}
render() {
// Only show copy button if the area is not a shell script.
return (
<Host>
{ !['shell', 'bash', 'sh'].includes(this.language) ?
<div class={{ 'code-text__copy': true, 'show-confirmation': this.showCopyConfirmation }} >
<a class="code-text__copy-link" onClick={this.copyCodeText.bind(this)}>Copy</a>
<span class="code-text__copy-confirmation">
{CheckmarkSoft({ height: '26px', width: '26px' })}
Copied
</span>
</div>
: '' }
</Host>
);
}
}