-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCopyableInput.jsx
More file actions
91 lines (78 loc) · 2.25 KB
/
CopyableInput.jsx
File metadata and controls
91 lines (78 loc) · 2.25 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import PropTypes from 'prop-types';
import React, { useEffect, useRef, useState } from 'react';
import Clipboard from 'clipboard';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import ShareIcon from '../../../images/share.svg';
const CopyableInput = ({ label, value, hasPreviewLink }) => {
const { t } = useTranslation();
const [isCopied, setIsCopied] = useState(false);
const inputRef = useRef(null);
useEffect(() => {
const input = inputRef.current;
if (!input) return; // should never happen
const clipboard = new Clipboard(input, {
target: () => input
});
clipboard.on('success', () => {
setIsCopied(true);
});
// eslint-disable-next-line consistent-return
return () => {
clipboard.destroy();
};
}, [inputRef, setIsCopied]);
return (
<div
className={classNames(
'copyable-input',
hasPreviewLink && 'copyable-input--with-preview'
)}
>
<div
className={classNames(
'copyable-input__value-container',
'tooltipped-no-delay',
isCopied && 'tooltipped tooltipped-n'
)}
aria-label={t('CopyableInput.CopiedARIA')}
onMouseLeave={() => setIsCopied(false)}
>
<label
className="copyable-input__label"
htmlFor={`copyable-input__value-${label}`}
>
<div className="copyable-input__label-container">{label}</div>
<input
type="text"
className="copyable-input__value"
id={`copyable-input__value-${label}`}
value={value}
ref={inputRef}
readOnly
/>
</label>
</div>
{hasPreviewLink && (
<a
target="_blank"
rel="noopener noreferrer"
href={value}
className="copyable-input__preview"
aria-label={t('CopyableInput.CopiedARIA', { label })}
>
<ShareIcon focusable="false" aria-hidden="true" />
</a>
)}
</div>
);
};
CopyableInput.propTypes = {
label: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
hasPreviewLink: PropTypes.bool
};
CopyableInput.defaultProps = {
hasPreviewLink: false
};
export default CopyableInput;