Skip to content

Commit

Permalink
reinstate clipboard functionality. Fixes toptal#336
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbos committed Feb 1, 2023
1 parent 003d7b1 commit 74ae0b4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
34 changes: 34 additions & 0 deletions components/key-code-cards/basic-cards/ClipboardCopy/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useState } from 'react'

export const ClipboardCopy = ({
children
}: {
children: string | undefined | number | null
}): JSX.Element => {
const [copied, setCopied] = useState(false)

function copyToClipboard() {
if (typeof children === 'undefined' || children === null) {
return
}
navigator.clipboard.writeText(children.toString())

setCopied(true)
setTimeout(() => {
setCopied(false)
}, 200)
}

return (
<div
style={{
cursor: 'pointer',
scale: copied ? `1.15` : `1`,
transition: 'scale 0.2s ease-in-out'
}}
onClick={copyToClipboard}
>
{children}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BasicCardContainer from '../BasicCardContainer'
import { ClipboardCopy } from '../ClipboardCopy'

import { TestIdEventDescriptionCard } from './test-ids'

Expand All @@ -16,7 +17,9 @@ const DescriptionCard = ({ description }: DescriptionCard): JSX.Element => {
description={cardDescriptions.description}
testId={TestIdEventDescriptionCard.EventDescriptionCardContainer}
>
{description ?? siteCopy.content.noDescription}
<ClipboardCopy>
{description ?? siteCopy.content.noDescription}
</ClipboardCopy>
</BasicCardContainer>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BasicCardContainer from '../BasicCardContainer'
import { ClipboardCopy } from '../ClipboardCopy'

import { TestIdEventCodeCard } from './test-ids'

Expand All @@ -17,7 +18,7 @@ const EventCodeCard = ({ code }: EventCodeCard): JSX.Element => {
description={cardDescriptions.eventCode}
testId={TestIdEventCodeCard.EventCodeCardContainer}
>
{code}
<ClipboardCopy>{code}</ClipboardCopy>
</BasicCardContainer>
)
}
Expand Down
3 changes: 2 additions & 1 deletion components/key-code-cards/basic-cards/EventKeyCard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BasicCardContainer from '../BasicCardContainer'
import { ClipboardCopy } from '../ClipboardCopy'

import { TestIdEventKeyCard } from './test-ids'

Expand All @@ -16,7 +17,7 @@ const EventKeyCard = ({ eventKey }: EventKeyCard): JSX.Element => {
description={cardDescriptions.eventKey}
testId={TestIdEventKeyCard.EventKeyCardContainer}
>
{eventKey}
<ClipboardCopy>{eventKey}</ClipboardCopy>
{eventKey === siteCopy.content.whiteSpace && (
<small>{siteCopy.content.blankSpace}</small>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BasicCardContainer from '../BasicCardContainer'
import { ClipboardCopy } from '../ClipboardCopy'

import { TestIdEvenLocationCard } from './test-ids'

Expand All @@ -23,7 +24,7 @@ const EventLocationCard = ({ keyLocation }: EventLocationCard): JSX.Element => {
title={siteCopy.cards.eventLocation}
description={cardDescriptions.eventLocation}
>
{location}
<ClipboardCopy>{location}</ClipboardCopy>
</BasicCardContainer>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BasicCardContainer from '../BasicCardContainer'
import { ClipboardCopy } from '../ClipboardCopy'

import { TestIdEvenWhichCard } from './test-ids'

Expand All @@ -16,7 +17,7 @@ const EventWhichCard = ({ which }: EventWhichCardProps): JSX.Element => {
description={cardDescriptions.eventWhich}
testId={TestIdEvenWhichCard.EventWhichCardContainer}
>
{which}
<ClipboardCopy>{which}</ClipboardCopy>
</BasicCardContainer>
)
}
Expand Down

0 comments on commit 74ae0b4

Please sign in to comment.