Debian Codegraph @

node/@patternfly/react-core/src/components/CodeBlock/examples/CodeBlockExpandable.tsx

raw
1 import { Fragment, useState } from 'react';
2 import {
3 CodeBlock,
4 CodeBlockAction,
5 CodeBlockCode,
6 ClipboardCopyButton,
7 ExpandableSection,
8 ExpandableSectionToggle,
9 Button,
10 Tooltip
11 } from '@patternfly/react-core';
12 import PlayIcon from '@patternfly/react-icons/dist/esm/icons/play-icon';
13
14 export const ExpandableCodeBlock: React.FunctionComponent = () => {
15 const [isExpanded, setIsExpanded] = useState(false);
16 const [copied, setCopied] = useState(false);
17 const [isRunning, setisRunning] = useState(false);
18 const runText: string = 'Run in web terminal';
19 const doneRunText: string = 'Running in web terminal';
20 const toggleId: string = 'expandable-code-toggle';
21
22 const onToggle = (isExpanded) => {
23 setIsExpanded(isExpanded);
24 };
25
26 const clipboardCopyFunc = (event, text) => {
27 navigator.clipboard.writeText(text.toString());
28 };
29
30 const onClick = (event, text) => {
31 clipboardCopyFunc(event, text);
32 setCopied(true);
33 };
34
35 const copyBlock = String.raw`apiVersion: helm.openshift.io/v1beta1/
36 kind: HelmChartRepository
37 metadata:
38 name: azure-sample-repo
39 spec:
40 connectionConfig:
41 url: https://raw.githubusercontent.com/Azure-Samples/helm-charts/master/docs`;
42
43 const code = String.raw`apiVersion: helm.openshift.io/v1beta1/
44 kind: HelmChartRepository
45 metadata:
46 name: azure-sample-repo`;
47 const expandedCode = String.raw`spec:
48 connectionConfig:
49 url: https://raw.githubusercontent.com/Azure-Samples/helm-charts/master/docs`;
50
51 const actions = (
52 <Fragment>
53 <CodeBlockAction>
54 <ClipboardCopyButton
55 id="expandable-copy-button"
56 aria-label="Copy to clipboard"
57 onClick={(e) => onClick(e, copyBlock)}
58 exitDelay={copied ? 1500 : 600}
59 maxWidth="110px"
60 variant="plain"
61 onTooltipHidden={() => setCopied(false)}
62 >
63 {copied ? 'Successfully copied to clipboard!' : 'Copy to clipboard'}
64 </ClipboardCopyButton>
65 </CodeBlockAction>
66 <CodeBlockAction>
67 <Tooltip
68 aria="none"
69 aria-live="polite"
70 content={isRunning ? doneRunText : runText}
71 onTooltipHidden={() => setisRunning(false)}
72 >
73 <Button
74 variant="plain"
75 aria-label="Run in web terminal"
76 icon={<PlayIcon />}
77 onClick={() => setisRunning(!isRunning)}
78 />
79 </Tooltip>
80 </CodeBlockAction>
81 </Fragment>
82 );
83
84 return (
85 <CodeBlock actions={actions}>
86 <CodeBlockCode>
87 {code}
88 <ExpandableSection isExpanded={isExpanded} isDetached contentId="code-block-expand" toggleId={toggleId}>
89 {expandedCode}
90 </ExpandableSection>
91 </CodeBlockCode>
92 <ExpandableSectionToggle
93 isExpanded={isExpanded}
94 onToggle={onToggle}
95 contentId="code-block-expand"
96 direction="up"
97 toggleId={toggleId}
98 >
99 {isExpanded ? 'Show less expandable example content' : 'Show more expandable example content'}
100 </ExpandableSectionToggle>
101 </CodeBlock>
102 );
103 };