|
1 | | -import {useEffect, useState} from 'react'; |
| 1 | +import {useEffect, useRef, useState} from 'react'; |
2 | 2 | import clsx from 'clsx'; |
3 | 3 | import CodeBlock from '@theme/CodeBlock'; |
4 | 4 | import styles from './styles.module.css'; |
@@ -47,21 +47,75 @@ export default function InstallCommand() { |
47 | 47 | }, []); |
48 | 48 |
|
49 | 49 | const active = PLATFORMS.find((p) => p.id === platform) ?? PLATFORMS[0]; |
| 50 | + const [copied, setCopied] = useState(false); |
| 51 | + const resetTimer = useRef(null); |
| 52 | + |
| 53 | + useEffect(() => () => clearTimeout(resetTimer.current), []); |
| 54 | + |
| 55 | + /* Fallback for when the async Clipboard API is unavailable or refused: it needs |
| 56 | + a secure context, and browsers decline it when the document is not focused. |
| 57 | + A throwaway textarea and execCommand works in those cases -- it is what |
| 58 | + Docusaurus's own copy button relies on. */ |
| 59 | + function copyViaTextarea(text) { |
| 60 | + const field = document.createElement('textarea'); |
| 61 | + field.value = text; |
| 62 | + field.setAttribute('readonly', ''); |
| 63 | + field.style.position = 'fixed'; |
| 64 | + field.style.opacity = '0'; |
| 65 | + document.body.appendChild(field); |
| 66 | + field.select(); |
| 67 | + let ok = false; |
| 68 | + try { |
| 69 | + ok = document.execCommand('copy'); |
| 70 | + } catch { |
| 71 | + ok = false; |
| 72 | + } |
| 73 | + document.body.removeChild(field); |
| 74 | + return ok; |
| 75 | + } |
| 76 | + |
| 77 | + async function copy() { |
| 78 | + let ok = false; |
| 79 | + try { |
| 80 | + await navigator.clipboard.writeText(active.command); |
| 81 | + ok = true; |
| 82 | + } catch { |
| 83 | + ok = copyViaTextarea(active.command); |
| 84 | + } |
| 85 | + /* Only confirm a copy that actually happened. If both routes fail the label |
| 86 | + stays put rather than claiming something untrue -- the whole command is on |
| 87 | + screen and can still be selected by hand. */ |
| 88 | + if (!ok) { |
| 89 | + return; |
| 90 | + } |
| 91 | + setCopied(true); |
| 92 | + clearTimeout(resetTimer.current); |
| 93 | + resetTimer.current = setTimeout(() => setCopied(false), 2000); |
| 94 | + } |
50 | 95 |
|
51 | 96 | return ( |
52 | 97 | <div className={styles.wrapper}> |
53 | | - <div className={styles.tabs} role="tablist" aria-label="Operating system"> |
54 | | - {PLATFORMS.map((p) => ( |
55 | | - <button |
56 | | - key={p.id} |
57 | | - type="button" |
58 | | - role="tab" |
59 | | - aria-selected={p.id === active.id} |
60 | | - className={clsx(styles.tab, p.id === active.id && styles.tabActive)} |
61 | | - onClick={() => setPlatform(p.id)}> |
62 | | - {p.label} |
63 | | - </button> |
64 | | - ))} |
| 98 | + <div className={styles.bar}> |
| 99 | + <div className={styles.tabs} role="tablist" aria-label="Operating system"> |
| 100 | + {PLATFORMS.map((p) => ( |
| 101 | + <button |
| 102 | + key={p.id} |
| 103 | + type="button" |
| 104 | + role="tab" |
| 105 | + aria-selected={p.id === active.id} |
| 106 | + className={clsx(styles.tab, p.id === active.id && styles.tabActive)} |
| 107 | + onClick={() => setPlatform(p.id)}> |
| 108 | + {p.label} |
| 109 | + </button> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + <button |
| 113 | + type="button" |
| 114 | + className={styles.copy} |
| 115 | + onClick={copy} |
| 116 | + aria-label={`Copy the ${active.label} install command`}> |
| 117 | + {copied ? 'Copied' : 'Copy'} |
| 118 | + </button> |
65 | 119 | </div> |
66 | 120 | <CodeBlock language={active.language}>{active.command}</CodeBlock> |
67 | 121 | </div> |
|
0 commit comments