Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate clipboard functionality #339

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider waiting for permission returned from writeText resolves or rejects, and handle potential rejection.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to clear it on component unmount, WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for an animation, so it needs to zoom out after quickly. Otherwise they would just stay scaled up until the component unmounts

}

return (
<div
style={{
cursor: 'pointer',
scale: copied ? `1.15` : `1`,
transition: 'scale 0.2s ease-in-out'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest defining styles in scss file, to keep it consistent with other components

}}
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
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
2 changes: 1 addition & 1 deletion lib/utils/get-key-code-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const getKeyCodeEvent = (
): KeyCodeEvent | undefined => {
return keyCodeEventValues.find(
keyCodeEvent =>
keyCodeEvent.code === keyCode.code && keyCodeEvent.key === keyCode.key
keyCodeEvent.code === keyCode.code || keyCodeEvent.key === keyCode.key
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you consider removing that change here, and keep it in #338? That would make tracking it easier

)
}