forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClipboardCopy.tsx
More file actions
337 lines (320 loc) · 12.2 KB
/
ClipboardCopy.tsx
File metadata and controls
337 lines (320 loc) · 12.2 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { Component, Fragment, createRef } from 'react';
import styles from '@patternfly/react-styles/css/components/ClipboardCopy/clipboard-copy';
import { css } from '@patternfly/react-styles';
import { PickOptional } from '../../helpers/typeUtils';
import { TooltipPosition } from '../Tooltip';
import { TextInput } from '../TextInput';
import { Truncate, TruncateProps } from '../Truncate';
import { GenerateId } from '../../helpers/GenerateId/GenerateId';
import { ClipboardCopyButton } from './ClipboardCopyButton';
import { ClipboardCopyToggle } from './ClipboardCopyToggle';
import { ClipboardCopyExpanded } from './ClipboardCopyExpanded';
import { getOUIAProps, OUIAProps } from '../../helpers';
export const clipboardCopyFunc = (_event: React.ClipboardEvent<HTMLDivElement>, text?: React.ReactNode) => {
try {
navigator.clipboard.writeText(text.toString());
} catch (error) {
// eslint-disable-next-line no-console
console.warn(
"Clipboard API not found, this copy function will not work. This is likely because you're using an",
"unsupported browser or you're not using HTTPS. \n\nIf you're a developer building an application which needs",
"to support copying to the clipboard without the clipboard API, you'll have to create your own copy",
'function and pass it to the ClipboardCopy component as the onCopy prop. For more information see',
'https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard'
);
// eslint-disable-next-line no-console
console.error(error);
}
};
export enum ClipboardCopyVariant {
inline = 'inline',
expansion = 'expansion',
inlineCompact = 'inline-compact'
}
export interface ClipboardCopyState {
text: string;
expanded: boolean;
copied: boolean;
textWhenExpanded: string;
}
export interface ClipboardCopyProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange' | 'children'>, OUIAProps {
/** Additional classes added to the clipboard copy container. */
className?: string;
/** Tooltip message to display when hover the copy button */
hoverTip?: string;
/** Tooltip message to display when clicking the copy button */
clickTip?: string;
/** Aria-label to use on the TextInput. */
textAriaLabel?: string;
/** Aria-label to use on the ClipboardCopyToggle. */
toggleAriaLabel?: string;
/** Flag to show if the input is read only. */
isReadOnly?: boolean;
/** Flag to determine if clipboard copy is in the expanded state initially */
isExpanded?: boolean;
/** Flag to determine if clipboard copy content includes code */
isCode?: boolean;
/** Flag to determine if inline clipboard copy should be block styling */
isBlock?: boolean;
/** Adds Clipboard Copy variant styles. */
variant?: typeof ClipboardCopyVariant | 'inline' | 'expansion' | 'inline-compact';
/** Copy button tooltip position. */
position?:
| TooltipPosition
| 'auto'
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'top-start'
| 'top-end'
| 'bottom-start'
| 'bottom-end'
| 'left-start'
| 'left-end'
| 'right-start'
| 'right-end';
/** Maximum width of the tooltip (default 150px). */
maxWidth?: string;
/** Delay in ms before the tooltip disappears. */
exitDelay?: number;
/** Delay in ms before the tooltip appears. */
entryDelay?: number;
/** A function that is triggered on clicking the copy button. This will replace the existing clipboard copy functionality entirely. */
onCopy?: (event: React.ClipboardEvent<HTMLDivElement>, text?: React.ReactNode) => void;
/** A function that is triggered on changing the text. */
onChange?: (event: React.FormEvent, text?: string) => void;
/** The text which is copied. */
children: string | string[];
/** Additional actions for inline clipboard copy. Should be wrapped with ClipboardCopyAction. */
additionalActions?: React.ReactNode;
/** Enables and customizes truncation for an inline-compact ClipboardCopy. */
truncation?: boolean | Omit<TruncateProps, 'content'>;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
}
class ClipboardCopy extends Component<ClipboardCopyProps, ClipboardCopyState> {
static displayName = 'ClipboardCopy';
timer = null as number;
private clipboardRef: React.RefObject<any>;
constructor(props: ClipboardCopyProps) {
super(props);
const text = Array.isArray(this.props.children) ? this.props.children.join(' ') : (this.props.children as string);
this.state = {
text,
expanded: this.props.isExpanded,
copied: false,
textWhenExpanded: text
};
this.clipboardRef = createRef();
}
static defaultProps: PickOptional<ClipboardCopyProps> = {
hoverTip: 'Copy to clipboard',
clickTip: 'Successfully copied to clipboard!',
isReadOnly: false,
isExpanded: false,
isCode: false,
variant: 'inline',
position: TooltipPosition.top,
maxWidth: '150px',
exitDelay: 1500,
entryDelay: 300,
onCopy: clipboardCopyFunc,
onChange: (): any => undefined,
textAriaLabel: 'Copyable input',
toggleAriaLabel: 'Show content',
additionalActions: null,
truncation: false,
ouiaSafe: true
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
componentDidUpdate = (prevProps: ClipboardCopyProps, prevState: ClipboardCopyState) => {
if (prevProps.children !== this.props.children) {
const newText = Array.isArray(this.props.children)
? this.props.children.join(' ')
: (this.props.children as string);
this.setState({ text: newText, textWhenExpanded: newText });
}
};
componentWillUnmount = () => {
if (this.timer) {
window.clearTimeout(this.timer);
}
};
expandContent = (_event: React.MouseEvent<Element, MouseEvent>) => {
this.setState((prevState) => ({
expanded: !prevState.expanded
}));
};
updateText = (event: React.FormEvent, text: string) => {
this.setState({ text });
this.props.onChange(event, text);
};
updateTextWhenExpanded = (event: React.FormEvent, text: string) => {
this.setState({ textWhenExpanded: text });
this.props.onChange(event, text);
};
render = () => {
const {
/* eslint-disable @typescript-eslint/no-unused-vars */
isExpanded,
onChange, // Don't pass to <div>
/* eslint-enable @typescript-eslint/no-unused-vars */
isReadOnly,
isCode,
isBlock,
exitDelay,
maxWidth,
entryDelay,
onCopy,
hoverTip,
clickTip,
textAriaLabel,
toggleAriaLabel,
variant,
position,
className,
additionalActions,
truncation,
ouiaId,
ouiaSafe,
...divProps
} = this.props;
const textIdPrefix = 'text-input-';
const toggleIdPrefix = 'toggle-';
const contentIdPrefix = 'content-';
const copyableText = this.state.text;
const shouldTruncate = variant === ClipboardCopyVariant.inlineCompact && truncation;
const inlineCompactContent = shouldTruncate ? (
<Truncate
tooltipProps={{ triggerRef: this.clipboardRef }}
content={copyableText}
{...(typeof truncation === 'object' && truncation)}
/>
) : (
copyableText
);
return (
<div
className={css(
styles.clipboardCopy,
variant === ClipboardCopyVariant.inlineCompact && styles.modifiers.inline,
isBlock && styles.modifiers.block,
this.state.expanded && styles.modifiers.expanded,
shouldTruncate && styles.modifiers.truncate,
className
)}
ref={this.clipboardRef}
{...(shouldTruncate && { tabIndex: 0 })}
{...divProps}
{...getOUIAProps(ClipboardCopy.displayName, ouiaId, ouiaSafe)}
>
{variant === ClipboardCopyVariant.inlineCompact && (
<GenerateId prefix="">
{(id) => (
<Fragment>
{!isCode && (
<span className={css(styles.clipboardCopyText)} id={`${textIdPrefix}${id}`}>
{inlineCompactContent}
</span>
)}
{isCode && (
<code className={css(styles.clipboardCopyText, styles.modifiers.code)} id={`${textIdPrefix}${id}`}>
{inlineCompactContent}
</code>
)}
<span className={css(styles.clipboardCopyActions)}>
<span className={css(styles.clipboardCopyActionsItem)}>
<ClipboardCopyButton
variant="plain"
exitDelay={exitDelay}
entryDelay={entryDelay}
maxWidth={maxWidth}
position={position}
id={`copy-button-${id}`}
textId={`text-input-${id}`}
aria-label={hoverTip}
onClick={(event: any) => {
onCopy(event, copyableText);
this.setState({ copied: true });
}}
onTooltipHidden={() => this.setState({ copied: false })}
hasNoPadding
>
{this.state.copied ? clickTip : hoverTip}
</ClipboardCopyButton>
</span>
{additionalActions && additionalActions}
</span>
</Fragment>
)}
</GenerateId>
)}
{variant !== ClipboardCopyVariant.inlineCompact && (
<GenerateId prefix="">
{(id) => (
<Fragment>
<div className={css(styles.clipboardCopyGroup)}>
{variant === ClipboardCopyVariant.expansion && (
<ClipboardCopyToggle
isExpanded={this.state.expanded}
onClick={(_event) => {
this.expandContent(_event);
if (this.state.expanded) {
this.setState({ text: this.state.textWhenExpanded });
} else {
this.setState({ textWhenExpanded: copyableText });
}
}}
id={`${toggleIdPrefix}${id}`}
textId={`${textIdPrefix}${id}`}
contentId={`${contentIdPrefix}${id}`}
aria-label={toggleAriaLabel}
/>
)}
<TextInput
readOnlyVariant={isReadOnly || this.state.expanded ? 'default' : undefined}
onChange={this.updateText}
value={this.state.expanded ? this.state.textWhenExpanded : copyableText}
id={`text-input-${id}`}
aria-label={textAriaLabel}
{...(isCode && { dir: 'ltr' })}
/>
<ClipboardCopyButton
exitDelay={exitDelay}
entryDelay={entryDelay}
maxWidth={maxWidth}
position={position}
id={`copy-button-${id}`}
textId={`text-input-${id}`}
aria-label={hoverTip}
onClick={(event: any) => {
onCopy(event, this.state.expanded ? this.state.textWhenExpanded : copyableText);
this.setState({ copied: true });
}}
onTooltipHidden={() => this.setState({ copied: false })}
>
{this.state.copied ? clickTip : hoverTip}
</ClipboardCopyButton>
</div>
{this.state.expanded && (
<ClipboardCopyExpanded
isReadOnly={isReadOnly}
isCode={isCode}
id={`content-${id}`}
onChange={this.updateTextWhenExpanded}
>
{copyableText}
</ClipboardCopyExpanded>
)}
</Fragment>
)}
</GenerateId>
)}
</div>
);
};
}
export { ClipboardCopy };