CopyButton.tsx 1.84 KB
Newer Older
1
2
3
import React, { useRef } from 'react';
import copy from 'copy-to-clipboard';
import { IconButton, FontSizes, TooltipHost } from '@fluentui/react';
Lijiaoa's avatar
Lijiaoa committed
4
import { TOOLTIP_BACKGROUND_COLOR } 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    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}
                tooltipProps={{
                    calloutProps: {
                        styles: {
                            beak: { background: TOOLTIP_BACKGROUND_COLOR },
                            beakCurtain: { background: TOOLTIP_BACKGROUND_COLOR },
                            calloutMain: { background: TOOLTIP_BACKGROUND_COLOR }
                        }
                    }
                }}
            />
        </div>
    );
};

export default CopyButton;