|
| 1 | +import { useDoc } from '@docusaurus/plugin-content-docs/client'; |
| 2 | +import { useEffect, useRef, useState } from 'react'; |
| 3 | +import styles from './styles.module.css'; |
| 4 | + |
| 5 | +const ACTIONS = [ |
| 6 | + { label: 'Copy Markdown', value: 'copy' }, |
| 7 | + { label: 'Open in ChatGPT', value: 'chatgpt', href: 'https://chatgpt.com' }, |
| 8 | + { label: 'Open in Claude', value: 'claude', href: 'https://claude.ai/new' }, |
| 9 | +] as const; |
| 10 | + |
| 11 | +type ActionType = (typeof ACTIONS)[number]; |
| 12 | + |
| 13 | +export function CopyButton() { |
| 14 | + const { frontMatter } = useDoc(); |
| 15 | + |
| 16 | + const markdown = |
| 17 | + 'rawMarkdown' in frontMatter && typeof frontMatter.rawMarkdown === 'string' |
| 18 | + ? frontMatter.rawMarkdown |
| 19 | + : null; |
| 20 | + |
| 21 | + const [isOpen, setIsOpen] = useState(false); |
| 22 | + const [isVisible, setIsVisible] = useState(false); |
| 23 | + const [copied, setCopied] = useState(false); |
| 24 | + |
| 25 | + const containerRef = useRef<HTMLDivElement>(null); |
| 26 | + const dropdownRef = useRef<HTMLDivElement>(null); |
| 27 | + const buttonRef = useRef<HTMLButtonElement>(null); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (!isOpen) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const onClickOutside = (event: MouseEvent) => { |
| 35 | + if ( |
| 36 | + containerRef.current && |
| 37 | + !containerRef.current.contains(event.target as Node) |
| 38 | + ) { |
| 39 | + setIsOpen(false); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + document.addEventListener('mousedown', onClickOutside); |
| 44 | + |
| 45 | + return () => document.removeEventListener('mousedown', onClickOutside); |
| 46 | + }, [isOpen]); |
| 47 | + |
| 48 | + const onClose = () => { |
| 49 | + setIsOpen(false); |
| 50 | + |
| 51 | + buttonRef.current?.focus(); |
| 52 | + }; |
| 53 | + |
| 54 | + const onAnimationEnd = () => { |
| 55 | + if (!isOpen) { |
| 56 | + setIsVisible(false); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const onAction = (action: ActionType) => { |
| 61 | + const prompt = `Read from ${window.location.href} so I can ask questions about it.`; |
| 62 | + |
| 63 | + switch (action.value) { |
| 64 | + case 'copy': |
| 65 | + if (markdown) { |
| 66 | + navigator.clipboard.writeText(markdown).then(() => { |
| 67 | + setCopied(true); |
| 68 | + setTimeout(() => setCopied(false), 2000); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + break; |
| 73 | + case 'chatgpt': |
| 74 | + case 'claude': |
| 75 | + window.open( |
| 76 | + `${action.href}?q=${encodeURIComponent(prompt)}`, |
| 77 | + |
| 78 | + '_blank' |
| 79 | + ); |
| 80 | + |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + onClose(); |
| 85 | + }; |
| 86 | + |
| 87 | + const onKeyDown = (event: React.KeyboardEvent) => { |
| 88 | + if (!isOpen || !dropdownRef.current) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const items = Array.from(dropdownRef.current.querySelectorAll('button')); |
| 93 | + const currentIndex = items.indexOf( |
| 94 | + document.activeElement as HTMLButtonElement |
| 95 | + ); |
| 96 | + |
| 97 | + switch (event.key) { |
| 98 | + case 'Escape': |
| 99 | + event.preventDefault(); |
| 100 | + |
| 101 | + onClose(); |
| 102 | + |
| 103 | + break; |
| 104 | + case 'ArrowUp': |
| 105 | + case 'ArrowDown': |
| 106 | + event.preventDefault(); |
| 107 | + |
| 108 | + const nextIndex = |
| 109 | + event.key === 'ArrowDown' |
| 110 | + ? (currentIndex + 1) % items.length |
| 111 | + : currentIndex === -1 |
| 112 | + ? items.length - 1 |
| 113 | + : (currentIndex - 1 + items.length) % items.length; |
| 114 | + |
| 115 | + items[nextIndex]?.focus(); |
| 116 | + |
| 117 | + break; |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + const onButtonClick = () => { |
| 122 | + if (isOpen) { |
| 123 | + setIsOpen(false); |
| 124 | + } else { |
| 125 | + setIsOpen(true); |
| 126 | + setIsVisible(true); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + if (!markdown) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + return ( |
| 135 | + <div className={styles.container} ref={containerRef} onKeyDown={onKeyDown}> |
| 136 | + <button |
| 137 | + ref={buttonRef} |
| 138 | + type="button" |
| 139 | + onClick={onButtonClick} |
| 140 | + className={styles.button} |
| 141 | + title="Copy page" |
| 142 | + aria-expanded={isOpen} |
| 143 | + aria-haspopup="menu" |
| 144 | + > |
| 145 | + <span className={styles.iconContainer}> |
| 146 | + <svg |
| 147 | + className={`${styles.icon} ${copied ? styles.visible : styles.hidden}`} |
| 148 | + xmlns="http://www.w3.org/2000/svg" |
| 149 | + width="16" |
| 150 | + height="16" |
| 151 | + viewBox="0 0 24 24" |
| 152 | + fill="none" |
| 153 | + stroke="currentColor" |
| 154 | + strokeWidth="2" |
| 155 | + strokeLinecap="round" |
| 156 | + strokeLinejoin="round" |
| 157 | + > |
| 158 | + <polyline points="20 6 9 17 4 12" /> |
| 159 | + </svg> |
| 160 | + |
| 161 | + <svg |
| 162 | + className={`${styles.icon} ${copied ? styles.hidden : styles.visible}`} |
| 163 | + xmlns="http://www.w3.org/2000/svg" |
| 164 | + width="16" |
| 165 | + height="16" |
| 166 | + viewBox="0 0 24 24" |
| 167 | + fill="none" |
| 168 | + stroke="currentColor" |
| 169 | + strokeWidth="2" |
| 170 | + strokeLinecap="round" |
| 171 | + strokeLinejoin="round" |
| 172 | + > |
| 173 | + <rect x="9" y="9" width="13" height="13" rx="2" ry="2" /> |
| 174 | + |
| 175 | + <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" /> |
| 176 | + </svg> |
| 177 | + </span> |
| 178 | + Copy page |
| 179 | + <svg |
| 180 | + className={`${styles.chevron} ${isOpen ? styles.open : ''}`} |
| 181 | + xmlns="http://www.w3.org/2000/svg" |
| 182 | + width="12" |
| 183 | + height="12" |
| 184 | + viewBox="0 0 24 24" |
| 185 | + fill="none" |
| 186 | + stroke="currentColor" |
| 187 | + strokeWidth="2" |
| 188 | + strokeLinecap="round" |
| 189 | + strokeLinejoin="round" |
| 190 | + > |
| 191 | + <polyline points="6 9 12 15 18 9" /> |
| 192 | + </svg> |
| 193 | + </button> |
| 194 | + |
| 195 | + {isVisible && ( |
| 196 | + <div |
| 197 | + ref={dropdownRef} |
| 198 | + className={`${styles.dropdown} ${!isOpen ? styles.closing : ''}`} |
| 199 | + onAnimationEnd={onAnimationEnd} |
| 200 | + role="menu" |
| 201 | + > |
| 202 | + {ACTIONS.map((action) => ( |
| 203 | + <button |
| 204 | + key={action.value} |
| 205 | + type="button" |
| 206 | + className={styles.item} |
| 207 | + onClick={() => onAction(action)} |
| 208 | + onMouseEnter={(e) => e.currentTarget.focus()} |
| 209 | + role="menuitem" |
| 210 | + tabIndex={-1} |
| 211 | + > |
| 212 | + {action.label} |
| 213 | + </button> |
| 214 | + ))} |
| 215 | + </div> |
| 216 | + )} |
| 217 | + </div> |
| 218 | + ); |
| 219 | +} |
0 commit comments