forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlockExpandable.tsx
More file actions
97 lines (89 loc) · 2.84 KB
/
CodeBlockExpandable.tsx
File metadata and controls
97 lines (89 loc) · 2.84 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
import React from 'react';
import {
CodeBlock,
CodeBlockAction,
CodeBlockCode,
ClipboardCopyButton,
ExpandableSection,
ExpandableSectionToggle,
Button,
Tooltip
} from '@patternfly/react-core';
import PlayIcon from '@patternfly/react-icons/dist/esm/icons/play-icon';
export const ExpandableCodeBlock: React.FunctionComponent = () => {
const [isExpanded, setIsExpanded] = React.useState(false);
const [copied, setCopied] = React.useState(false);
const [isRunning, setisRunning] = React.useState(false);
const runText: string = 'Run in web terminal';
const doneRunText: string = 'Running in web terminal';
const onToggle = (isExpanded) => {
setIsExpanded(isExpanded);
};
const clipboardCopyFunc = (event, text) => {
navigator.clipboard.writeText(text.toString());
};
const onClick = (event, text) => {
clipboardCopyFunc(event, text);
setCopied(true);
};
const copyBlock = String.raw`apiVersion: helm.openshift.io/v1beta1/
kind: HelmChartRepository
metadata:
name: azure-sample-repo
spec:
connectionConfig:
url: https://raw.githubusercontent.com/Azure-Samples/helm-charts/master/docs`;
const code = String.raw`apiVersion: helm.openshift.io/v1beta1/
kind: HelmChartRepository
metadata:
name: azure-sample-repo`;
const expandedCode = String.raw`spec:
connectionConfig:
url: https://raw.githubusercontent.com/Azure-Samples/helm-charts/master/docs`;
const actions = (
<React.Fragment>
<CodeBlockAction>
<ClipboardCopyButton
id="expandable-copy-button"
textId="code-content"
aria-label="Copy to clipboard"
onClick={(e) => onClick(e, copyBlock)}
exitDelay={copied ? 1500 : 600}
maxWidth="110px"
variant="plain"
onTooltipHidden={() => setCopied(false)}
>
{copied ? 'Successfully copied to clipboard!' : 'Copy to clipboard'}
</ClipboardCopyButton>
</CodeBlockAction>
<CodeBlockAction>
<Tooltip
aria="none"
aria-live="polite"
content={isRunning ? doneRunText : runText}
onTooltipHidden={() => setisRunning(false)}
>
<Button
variant="plain"
aria-label="Run in web terminal"
icon={<PlayIcon />}
onClick={() => setisRunning(!isRunning)}
/>
</Tooltip>
</CodeBlockAction>
</React.Fragment>
);
return (
<CodeBlock actions={actions}>
<CodeBlockCode>
{code}
<ExpandableSection isExpanded={isExpanded} isDetached contentId="code-block-expand">
{expandedCode}
</ExpandableSection>
</CodeBlockCode>
<ExpandableSectionToggle isExpanded={isExpanded} onToggle={onToggle} contentId="code-block-expand" direction="up">
{isExpanded ? 'Show Less' : 'Show More'}
</ExpandableSectionToggle>
</CodeBlock>
);
};