-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCopyPasteBlock.tsx
More file actions
239 lines (219 loc) · 5.13 KB
/
CopyPasteBlock.tsx
File metadata and controls
239 lines (219 loc) · 5.13 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { ForwardedRef, forwardRef, ReactNode, useState } from 'react';
import { TextDropItem, useClipboard } from 'react-aria';
import copy from 'clipboard-copy';
import { Button } from '../../actions';
import { Card, CubeCardProps } from '../Card/Card';
import {
extractStyles,
POSITION_STYLES,
PositionStyleProps,
Styles,
tasty,
} from '../../../tasty';
import { useToastsApi } from '../../overlays/Toasts';
import { useTimer } from '../../../_internal';
import { CopyIcon } from '../../../icons';
const StyledBlock = tasty({
styles: {
display: 'grid',
flow: 'column',
placeContent: 'center space-between',
placeItems: 'center stretch',
gap: '1x',
position: 'relative',
maxWidth: '100%',
color: 'inherit',
padding: '0 1.5x',
// height: {
// '': '4.5x',
// '[data-size="small"]': '3.5x',
// '[data-size="large"]': '5.5x',
// },
userSelect: 'none',
Label: {
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
});
const CopyPasteBlockElement = tasty(Card, {
qa: 'CopyPasteBlock',
styles: {
display: 'grid',
fill: '#grey-light',
radius: '1r',
padding: {
'': '0 1px',
':focus': '0',
},
color: {
'': '#dark',
error: '#danger-text',
},
border: {
'': 'dashed #dark-03',
':focus': '2px dashed #purple-text',
error: 'dashed #danger',
},
cursor: 'pointer',
preset: {
'': 't3',
'[data-size="large"]': 't2',
},
height: {
'': '5x',
'[data-size="small"]': '4x',
'[data-size="large"]': '6x',
},
boxSizing: 'border-box',
Grid: {
display: 'grid',
flow: 'row',
gridColumns: 'minmax(0, 1fr) min-content',
placeContent: 'stretch',
width: 'min 20x',
radius: '1r',
position: 'relative',
},
Shortcut: {
display: {
'': 'none',
':focus & !error': 'inline',
},
},
},
});
const CopyButton = tasty(Button, {
type: 'clear',
icon: <CopyIcon />,
'aria-label': 'Copy to clipboard',
styles: {
placeSelf: 'stretch',
border: '#clear',
shadow: {
'': '0 0 0 1ow #purple-03.0 inset',
focused: '0 0 0 1ow #purple-03 inset',
},
radius: {
'': '0 1r 1r 0',
'multiline | with-scroll': '0 1r 0 0',
},
height: 'auto',
outline: false,
},
});
export interface CubeCopyPasteBlockProps
extends CubeCardProps,
PositionStyleProps {
padding?: Styles['padding'];
/** The code snippet */
value?: string;
placeholder?: ReactNode;
/** The title of the snippet */
title?: string;
onPaste?: (text: string) => void | Promise<void | string>;
onCopy?: () => void;
size?: 'small' | 'medium' | 'large';
}
function CopyPasteBlock(
allProps: CubeCopyPasteBlockProps,
ref: ForwardedRef<HTMLDivElement>,
) {
const {
value = '',
onPaste,
placeholder,
title,
size = 'medium',
...props
} = allProps;
const styles = extractStyles(props, POSITION_STYLES);
const [error, setError] = useState<string | null>(null);
const { clipboardProps } = useClipboard({
async onPaste(items) {
let pasted = await Promise.all(
items
.filter(
(item) => item.kind === 'text' && item.types.has('text/plain'),
)
.map((item) => (item as TextDropItem).getText('text/plain')),
);
try {
await onPaste?.(pasted.join('\n'));
} catch (e) {
setError(String(e));
}
},
getItems() {
return value
? [
{
'text/plain': value,
},
]
: [];
},
onCopy() {
if (value) {
toast.success(`${title || 'Text'} copied`);
}
},
});
useTimer({
delay: 1000,
callback() {
setError(null);
},
isDisabled: !error,
});
const { toast } = useToastsApi();
async function onCopy() {
await copy(value);
toast.success(`${title || 'Text'} copied`);
}
const pristineValue = value.replace(/\n/, ' ');
return (
<CopyPasteBlockElement
ref={ref}
mods={{ error: !!error }}
data-size={size}
styles={styles}
tabIndex="0"
{...props}
{...clipboardProps}
>
<div data-element="Grid">
<StyledBlock
data-size={size}
mods={{
placeholder: !!placeholder || !value,
}}
>
<div data-element="Label">
{error != null ? (
error || 'Invalid data'
) : value ? (
pristineValue
) : (
<>{placeholder ? placeholder : 'Select and paste'}</>
)}
</div>
<span data-element="Shortcut">
<kbd>Cmd</kbd> + <kbd>V</kbd>
</span>
</StyledBlock>
{value && !error && (
<CopyButton
size={size}
aria-label={`Copy ${title}`}
onPress={onCopy}
/>
)}
</div>
</CopyPasteBlockElement>
);
}
const _CopyPasteBlock = forwardRef(CopyPasteBlock);
_CopyPasteBlock.displayName = 'CopyPasteBlock';
export { _CopyPasteBlock as CopyPasteBlock };