1import React, { useCallback, useEffect, useRef, useState } from 'react';
2import { LinkButton } from 'indigo-react';
3import cn from 'classnames';
4import copy from 'copy-to-clipboard';
6const COPY_DELAY = 2 * 1000; // ms
7const renderDefaultCopyText = didCopy => (didCopy ? 'Copied!' : 'Copy');
9export default function CopyButton({
12 children = renderDefaultCopyText,
16 const [didCopy, setDidCopy] = useState(false);
18 // what we really want here is a useDebouncedCallback from use-debounced
19 // but that's a relatively large dep for a single simple usage so ¯\_(ツ)_/¯
20 const listener = useRef(); // <NodeJS.Listener>
21 useEffect(() => () => clearTimeout(listener.current), []);
23 const doCopy = useCallback(() => {
27 clearTimeout(listener.current);
28 listener.current = setTimeout(() => setDidCopy(false), COPY_DELAY);
34 className={cn(className, {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list