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