| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { |
| 3 |
CodeBlock, |
| 4 |
CodeBlockAction, |
| 5 |
CodeBlockCode, |
| 6 |
ClipboardCopyButton, |
| 7 |
Button, |
| 8 |
Tooltip |
| 9 |
} from '@patternfly/react-core'; |
| 10 |
import PlayIcon from '@patternfly/react-icons/dist/esm/icons/play-icon'; |
| 11 |
|
| 12 |
export const BasicCodeBlock: React.FunctionComponent = () => { |
| 13 |
const [copied, setCopied] = useState(false); |
| 14 |
const [isRunning, setisRunning] = useState(false); |
| 15 |
const runText: string = 'Run in web terminal'; |
| 16 |
const doneRunText: string = 'Running in web terminal'; |
| 17 |
|
| 18 |
const clipboardCopyFunc = (event, text) => { |
| 19 |
navigator.clipboard.writeText(text.toString()); |
| 20 |
}; |
| 21 |
|
| 22 |
const onClick = (event, text) => { |
| 23 |
clipboardCopyFunc(event, text); |
| 24 |
setCopied(true); |
| 25 |
}; |
| 26 |
|
| 27 |
const code = String.raw`apiVersion: helm.openshift.io/v1beta1/ |
| 28 |
kind: HelmChartRepository |
| 29 |
metadata: |
| 30 |
name: azure-sample-repo0oooo00ooo |
| 31 |
spec: |
| 32 |
connectionConfig: |
| 33 |
url: https://raw.githubusercontent.com/Azure-Samples/helm-charts/master/docs`; |
| 34 |
|
| 35 |
const actions = ( |
| 36 |
<Fragment> |
| 37 |
<CodeBlockAction> |
| 38 |
<ClipboardCopyButton |
| 39 |
id="basic-copy-button" |
| 40 |
aria-label="Copy to clipboard basic example code block" |
| 41 |
onClick={(e) => onClick(e, code)} |
| 42 |
exitDelay={copied ? 1500 : 600} |
| 43 |
maxWidth="110px" |
| 44 |
variant="plain" |
| 45 |
onTooltipHidden={() => setCopied(false)} |
| 46 |
> |
| 47 |
{copied ? 'Successfully copied to clipboard!' : 'Copy to clipboard'} |
| 48 |
</ClipboardCopyButton> |
| 49 |
</CodeBlockAction> |
| 50 |
<CodeBlockAction> |
| 51 |
<Tooltip |
| 52 |
aria="none" |
| 53 |
aria-live="polite" |
| 54 |
content={isRunning ? doneRunText : runText} |
| 55 |
onTooltipHidden={() => setisRunning(false)} |
| 56 |
> |
| 57 |
<Button |
| 58 |
variant="plain" |
| 59 |
aria-label="Run in web terminal" |
| 60 |
icon={<PlayIcon />} |
| 61 |
onClick={() => setisRunning(!isRunning)} |
| 62 |
/> |
| 63 |
</Tooltip> |
| 64 |
</CodeBlockAction> |
| 65 |
</Fragment> |
| 66 |
); |
| 67 |
|
| 68 |
return ( |
| 69 |
<CodeBlock actions={actions}> |
| 70 |
<CodeBlockCode id="code-content">{code}</CodeBlockCode> |
| 71 |
</CodeBlock> |
| 72 |
); |
| 73 |
}; |