Skip to content

Commit 92c28e6

Browse files
committed
more subtle copying
1 parent 5102964 commit 92c28e6

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/components/codeHighlights/codeHighlights.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export function HighlightBlock({
7777
setShowCopyButton(true);
7878
}, []);
7979

80+
const [copied, setCopied] = useState(false);
81+
8082
async function copyCodeOnClick() {
8183
if (codeRef.current === null) {
8284
return;
@@ -85,7 +87,10 @@ export function HighlightBlock({
8587
const code = cleanCodeSnippet(codeRef.current.innerText, {language});
8688

8789
try {
90+
setCopied(false);
8891
await navigator.clipboard.writeText(code);
92+
setCopied(true);
93+
setTimeout(() => setCopied(false), 1200);
8994
} catch (error) {
9095
// eslint-disable-next-line no-console
9196
console.error('Failed to copy:', error);
@@ -95,8 +100,14 @@ export function HighlightBlock({
95100
return (
96101
<HighlightBlockContainer key={`highlight-block-${groupId}`}>
97102
<CodeLinesContainer ref={codeRef}>{children}</CodeLinesContainer>
98-
<ClipBoardContainer onClick={copyCodeOnClick}>
99-
{showCopyButton && <Clipboard size={14} opacity={70} />}
103+
<ClipBoardContainer onClick={copyCodeOnClick} className=".clipboard">
104+
{showCopyButton && (
105+
<Clipboard
106+
size={16}
107+
opacity={copied ? 1 : 0.15}
108+
stroke={copied ? 'green' : 'white'}
109+
/>
110+
)}
100111
</ClipBoardContainer>
101112
</HighlightBlockContainer>
102113
);
@@ -111,6 +122,14 @@ const HighlightBlockContainer = styled('div')`
111122
min-width: 100%;
112123
box-sizing: border-box;
113124
background-color: rgba(239, 239, 239, 0.06);
125+
position: relative;
126+
127+
:hover svg {
128+
opacity: 1;
129+
}
130+
svg {
131+
transition: all 150ms linear;
132+
}
114133
`;
115134

116135
const CodeLinesContainer = styled('div')`
@@ -119,16 +138,13 @@ const CodeLinesContainer = styled('div')`
119138
`;
120139

121140
const ClipBoardContainer = styled('div')`
141+
position: absolute;
142+
right: 0;
143+
top: 0;
144+
bottom: 0;
122145
width: 48px;
123146
display: flex;
124147
justify-content: center;
125148
align-items: center;
126149
cursor: pointer;
127-
:hover {
128-
background-color: rgba(239, 239, 239, 0.1);
129-
}
130-
:active {
131-
background-color: rgba(239, 239, 239, 0.15);
132-
}
133-
transition: background-color 150ms linear;
134150
`;

0 commit comments

Comments
 (0)