-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
NpmCopyButton.tsx
92 lines (90 loc) · 2.53 KB
/
NpmCopyButton.tsx
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
/* eslint-disable react/prop-types */
import * as React from 'react';
import copy from 'clipboard-copy';
import { SxProps } from '@mui/system';
import { styled, alpha, Theme } from '@mui/material/styles';
import ContentCopyRounded from '@mui/icons-material/ContentCopyRounded';
import CheckRounded from '@mui/icons-material/CheckRounded';
const Button = styled('button')(({ theme }) => ({
boxSizing: 'border-box',
minWidth: 64,
margin: 0,
marginTop: 16,
cursor: 'copy',
padding: 0,
position: 'relative',
display: 'inline-flex',
alignItems: 'flex-start',
justifyContent: 'center',
verticalAlign: 'middle',
gap: 8,
outline: 0,
border: 0,
boxShadow: 'none',
backgroundColor: 'transparent',
fontFamily: theme.typography.fontFamilyCode,
fontSize: theme.typography.pxToRem(12),
textDecoration: 'none',
textTransform: 'initial',
lineHeight: 1.5,
letterSpacing: 0,
transition: theme.transitions.create('color', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.shortest,
}),
WebkitTapHighlightColor: 'transparent',
WebkitFontSmoothing: 'subpixel-antialiased',
color: (theme.vars || theme).palette.text.tertiary,
'&:hover, &:focus-visible': {
color: (theme.vars || theme).palette.primary.main,
'@media (hover: none)': {
color: (theme.vars || theme).palette.text.tertiary,
},
},
'& svg': {
display: 'inline-block',
position: 'absolute',
right: -24,
top: 1,
opacity: 0,
transition: theme.transitions.create('opacity', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.shortest,
}),
},
'&:focus, &:hover svg': {
opacity: 1,
},
'&:focus-visible': {
outline: `3px solid ${alpha(theme.palette.primary[500], 0.5)}`,
outlineOffset: '2px',
},
}));
export default function NpmCopyButton(
props: React.HTMLAttributes<HTMLButtonElement> & { installation: string; sx?: SxProps<Theme> },
) {
const { installation, onClick, sx, ...other } = props;
const [copied, setCopied] = React.useState(false);
const handleCopy = () => {
setCopied(true);
copy(installation).then(() => {
setTimeout(() => setCopied(false), 2000);
});
};
return (
<Button
onClick={(event: any) => {
handleCopy();
onClick?.(event);
}}
{...other}
>
$ {installation}
{copied ? (
<CheckRounded color="inherit" sx={{ fontSize: 15 }} />
) : (
<ContentCopyRounded color="inherit" sx={{ fontSize: 15 }} />
)}
</Button>
);
}