CopyButton.tsx 1.47 KB
Newer Older
1
2
3
import React, { useRef } from 'react';
import copy from 'copy-to-clipboard';
import { IconButton, FontSizes, TooltipHost } from '@fluentui/react';
4
import { TOOLTIPSTYLE } from '@static/const';
5

6
7
8
9
10
interface CopyButtonProps {
    value: string;
    hideTooltip?: boolean;
}

11
12
const COPIED_TOOLTIP_CLOSE_DELAY = 1000;

13
14
const CopyButton = (props: CopyButtonProps): any => {
    const { value, hideTooltip } = props;
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
    const ref = useRef(null);
    return (
        <div>
            <IconButton
                iconProps={{ iconName: 'Copy' }}
                styles={{ icon: [{ fontSize: FontSizes.small }] }}
                onClick={(event: React.SyntheticEvent<EventTarget>): void => {
                    event.stopPropagation();
                    copy(value);
                    ref.current && (ref as any).current.show();
                    setTimeout(() => {
                        ref.current !== null && (ref as any).current.dismiss();
                    }, COPIED_TOOLTIP_CLOSE_DELAY);
                }}
                onMouseDown={(e): void => {
                    e.stopPropagation();
                }}
                onMouseUp={(e): void => {
                    e.stopPropagation();
                }}
            />
            <TooltipHost
                hidden={hideTooltip}
                content='Copied'
                componentRef={ref}
                delay={0}
41
                tooltipProps={TOOLTIPSTYLE}
42
43
44
45
46
47
            />
        </div>
    );
};

export default CopyButton;