|
| 1 | +import {useEffect, useRef, useState} from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import CodeBlock from '@theme/CodeBlock'; |
| 4 | +import styles from './styles.module.css'; |
| 5 | + |
| 6 | +/* macOS and Linux run the same script -- it detects the OS and architecture |
| 7 | + itself -- so both tabs deliberately show the same command. */ |
| 8 | +const UNIX_COMMAND = |
| 9 | + 'curl -fsSL https://github.com/php-debugger/installer/releases/latest/download/install.sh | sh'; |
| 10 | +const WINDOWS_COMMAND = |
| 11 | + 'powershell -c "irm https://github.com/php-debugger/installer/releases/latest/download/install.ps1 | iex"'; |
| 12 | + |
| 13 | +const iconProps = { |
| 14 | + width: 15, |
| 15 | + height: 15, |
| 16 | + viewBox: '0 0 24 24', |
| 17 | + fill: 'none', |
| 18 | + stroke: 'currentColor', |
| 19 | + strokeWidth: 2, |
| 20 | + strokeLinecap: 'round', |
| 21 | + strokeLinejoin: 'round', |
| 22 | + 'aria-hidden': true, |
| 23 | +}; |
| 24 | + |
| 25 | +const CopyIcon = () => ( |
| 26 | + <svg {...iconProps}> |
| 27 | + <rect x="9" y="9" width="11" height="11" rx="2" /> |
| 28 | + <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" /> |
| 29 | + </svg> |
| 30 | +); |
| 31 | + |
| 32 | +const CheckIcon = () => ( |
| 33 | + <svg {...iconProps}> |
| 34 | + <path d="M20 6 9 17l-5-5" /> |
| 35 | + </svg> |
| 36 | +); |
| 37 | + |
| 38 | +const PLATFORMS = [ |
| 39 | + {id: 'macos', label: 'macOS', language: 'bash', command: UNIX_COMMAND}, |
| 40 | + {id: 'linux', label: 'Linux', language: 'bash', command: UNIX_COMMAND}, |
| 41 | + {id: 'windows', label: 'Windows', language: 'powershell', command: WINDOWS_COMMAND}, |
| 42 | +]; |
| 43 | + |
| 44 | +function detectPlatform() { |
| 45 | + const ua = navigator.userAgent; |
| 46 | + if (/Windows/i.test(ua)) { |
| 47 | + return 'windows'; |
| 48 | + } |
| 49 | + if (/Mac OS X|Macintosh/i.test(ua)) { |
| 50 | + return 'macos'; |
| 51 | + } |
| 52 | + /* Android reports Linux too, and the command is the same either way. */ |
| 53 | + if (/Linux|Android|X11/i.test(ua)) { |
| 54 | + return 'linux'; |
| 55 | + } |
| 56 | + return null; |
| 57 | +} |
| 58 | + |
| 59 | +export default function InstallCommand() { |
| 60 | + /* The page is prerendered without knowing the visitor's OS, so start on macOS |
| 61 | + and correct it after mounting. Detecting in an effect rather than during |
| 62 | + render keeps the first client render identical to the server's, which is |
| 63 | + what hydration compares. A visitor who picks a tab keeps their choice -- |
| 64 | + the effect only runs on mount. */ |
| 65 | + const [platform, setPlatform] = useState('macos'); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + const detected = detectPlatform(); |
| 69 | + if (detected) { |
| 70 | + setPlatform(detected); |
| 71 | + } |
| 72 | + }, []); |
| 73 | + |
| 74 | + const active = PLATFORMS.find((p) => p.id === platform) ?? PLATFORMS[0]; |
| 75 | + const [copied, setCopied] = useState(false); |
| 76 | + const resetTimer = useRef(null); |
| 77 | + |
| 78 | + useEffect(() => () => clearTimeout(resetTimer.current), []); |
| 79 | + |
| 80 | + /* Fallback for when the async Clipboard API is unavailable or refused: it needs |
| 81 | + a secure context, and browsers decline it when the document is not focused. |
| 82 | + A throwaway textarea and execCommand works in those cases -- it is what |
| 83 | + Docusaurus's own copy button relies on. */ |
| 84 | + function copyViaTextarea(text) { |
| 85 | + const field = document.createElement('textarea'); |
| 86 | + field.value = text; |
| 87 | + field.setAttribute('readonly', ''); |
| 88 | + field.style.position = 'fixed'; |
| 89 | + field.style.opacity = '0'; |
| 90 | + document.body.appendChild(field); |
| 91 | + field.select(); |
| 92 | + let ok = false; |
| 93 | + try { |
| 94 | + ok = document.execCommand('copy'); |
| 95 | + } catch { |
| 96 | + ok = false; |
| 97 | + } |
| 98 | + document.body.removeChild(field); |
| 99 | + return ok; |
| 100 | + } |
| 101 | + |
| 102 | + async function copy(event) { |
| 103 | + event.currentTarget.blur(); |
| 104 | + let ok = false; |
| 105 | + try { |
| 106 | + await navigator.clipboard.writeText(active.command); |
| 107 | + ok = true; |
| 108 | + } catch { |
| 109 | + ok = copyViaTextarea(active.command); |
| 110 | + } |
| 111 | + /* Only confirm a copy that actually happened. If both routes fail the label |
| 112 | + stays put rather than claiming something untrue -- the whole command is on |
| 113 | + screen and can still be selected by hand. */ |
| 114 | + if (!ok) { |
| 115 | + return; |
| 116 | + } |
| 117 | + setCopied(true); |
| 118 | + clearTimeout(resetTimer.current); |
| 119 | + resetTimer.current = setTimeout(() => setCopied(false), 2000); |
| 120 | + } |
| 121 | + |
| 122 | + return ( |
| 123 | + <div className={styles.wrapper}> |
| 124 | + <div className={styles.bar}> |
| 125 | + <div className={styles.tabs} role="tablist" aria-label="Operating system"> |
| 126 | + {PLATFORMS.map((p) => ( |
| 127 | + <button |
| 128 | + key={p.id} |
| 129 | + type="button" |
| 130 | + role="tab" |
| 131 | + aria-selected={p.id === active.id} |
| 132 | + className={clsx(styles.tab, p.id === active.id && styles.tabActive)} |
| 133 | + onClick={() => setPlatform(p.id)}> |
| 134 | + {p.label} |
| 135 | + </button> |
| 136 | + ))} |
| 137 | + </div> |
| 138 | + <button |
| 139 | + type="button" |
| 140 | + className={clsx(styles.copy, copied && styles.copied)} |
| 141 | + onClick={copy} |
| 142 | + aria-label={`Copy the ${active.label} install command`}> |
| 143 | + {copied ? <CheckIcon /> : <CopyIcon />} |
| 144 | + {copied ? 'Copied' : 'Copy'} |
| 145 | + </button> |
| 146 | + </div> |
| 147 | + <CodeBlock language={active.language}>{active.command}</CodeBlock> |
| 148 | + </div> |
| 149 | + ); |
| 150 | +} |
0 commit comments