-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement copy to clipboard button
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
web/src/app/_features/CopyToClipboardButton/CopyToClipboardButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { FC } from 'react' | ||
import { useCallback, useState } from 'react' | ||
|
||
import { Tooltip } from './Tooltip' | ||
import { useSuccessTooltip } from './useSuccessTooltip' | ||
|
||
import { Button } from '@/app/_components' | ||
|
||
interface Props { | ||
text: string | ||
} | ||
|
||
export const CopyToClipboardButton: FC<Props> = ({ text }) => { | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [showTooltip, setShowTooltip] = useSuccessTooltip() | ||
|
||
const handleClick = useCallback(() => { | ||
setIsLoading(true) | ||
navigator.clipboard | ||
.writeText(text) | ||
.then(() => setShowTooltip(true)) | ||
.catch((e) => console.error(e)) | ||
setIsLoading(false) | ||
}, [text, setShowTooltip]) | ||
|
||
return ( | ||
<span className="relative"> | ||
<Tooltip show={showTooltip} /> | ||
<Button isLoading={isLoading} onClick={handleClick} variant="secondary"> | ||
Copy to Clipboard | ||
</Button> | ||
</span> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import clsx from 'clsx' | ||
import type { FC } from 'react' | ||
|
||
interface Props { | ||
show: boolean | ||
} | ||
|
||
export const Tooltip: FC<Props> = ({ show: show }) => { | ||
return ( | ||
<span | ||
className={clsx( | ||
show ? 'opacity-100' : 'opacity-0', | ||
`absolute -top-12 left-1/2 -translate-x-1/2 | ||
whitespace-nowrap rounded bg-slate-800 | ||
px-2 py-1 text-white | ||
transition before:absolute before:left-1/2 | ||
before:top-full before:-translate-x-1/2 before:border-4 before:border-transparent | ||
before:border-t-slate-800 before:content-[''] | ||
`, | ||
)} | ||
> | ||
Copied!! | ||
</span> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './CopyToClipboardButton' |
16 changes: 16 additions & 0 deletions
16
web/src/app/_features/CopyToClipboardButton/useSuccessTooltip.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
export const useSuccessTooltip = () => { | ||
const [show, setShow] = useState(false) | ||
|
||
useEffect(() => { | ||
if (!show) return | ||
|
||
const timer = setTimeout(() => { | ||
setShow(false) | ||
}, 2000) | ||
return () => clearTimeout(timer) | ||
}, [show]) | ||
|
||
return [show, setShow] as const | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters