-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix format * Remove unnecessary useMemo * Update index.tsx * Update index.tsx * Update helpers.ts * Update resize-handle.tsx * Update resize-handle.tsx * Update utils.ts * Update index.tsx * Fix `getDisplayName`
- Loading branch information
Showing
17 changed files
with
778 additions
and
678 deletions.
There are no files selected for viewing
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
128 changes: 67 additions & 61 deletions
128
packages/scan/src/web/components/copy-to-clipboard/index.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 |
---|---|---|
@@ -1,82 +1,88 @@ | ||
import { useRef, useState, useEffect, useCallback, useMemo } from "preact/hooks"; | ||
import { memo } from "preact/compat"; | ||
import { cn } from "~web/utils/helpers"; | ||
import { Icon } from "../icon"; | ||
import { memo } from 'preact/compat'; | ||
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'; | ||
import { cn } from '~web/utils/helpers'; | ||
import { Icon } from '../icon'; | ||
|
||
interface CopyToClipboardProps { | ||
text: string; | ||
children?: (props: { ClipboardIcon: JSX.Element; onClick: (e: MouseEvent) => void }) => JSX.Element; | ||
children?: (props: { | ||
ClipboardIcon: JSX.Element; | ||
onClick: (e: MouseEvent) => void; | ||
}) => JSX.Element; | ||
onCopy?: (success: boolean, text: string) => void; | ||
className?: string; | ||
iconSize?: number; | ||
} | ||
|
||
export const CopyToClipboard = memo((props: CopyToClipboardProps): JSX.Element => { | ||
const { | ||
export const CopyToClipboard = memo( | ||
({ | ||
text, | ||
children, | ||
onCopy, | ||
className, | ||
iconSize = 14, | ||
} = props; | ||
}: CopyToClipboardProps): JSX.Element => { | ||
const refTimeout = useRef<TTimer>(); | ||
const [isCopied, setIsCopied] = useState(false); | ||
|
||
const refTimeout = useRef<TTimer>(); | ||
const [isCopied, setIsCopied] = useState(false); | ||
useEffect(() => { | ||
if (isCopied) { | ||
refTimeout.current = setTimeout(() => setIsCopied(false), 600); | ||
return () => { | ||
clearTimeout(refTimeout.current); | ||
}; | ||
} | ||
}, [isCopied]); | ||
|
||
useEffect(() => { | ||
if (isCopied) { | ||
refTimeout.current = setTimeout(() => setIsCopied(false), 600); | ||
return () => { | ||
clearTimeout(refTimeout?.current); | ||
}; | ||
} | ||
}, [isCopied]); | ||
|
||
const copyToClipboard = useCallback((e: MouseEvent) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
const copyToClipboard = useCallback( | ||
(e: MouseEvent) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
navigator.clipboard.writeText(text).then( | ||
() => { | ||
setIsCopied(true); | ||
onCopy?.(true, text); | ||
}, | ||
() => { | ||
onCopy?.(false, text); | ||
navigator.clipboard.writeText(text).then( | ||
() => { | ||
setIsCopied(true); | ||
onCopy?.(true, text); | ||
}, | ||
() => { | ||
onCopy?.(false, text); | ||
}, | ||
); | ||
}, | ||
[text, onCopy], | ||
); | ||
}, [text, onCopy]); | ||
|
||
const ClipboardIcon = useMemo((): JSX.Element => ( | ||
<button | ||
onClick={copyToClipboard} | ||
type="button" | ||
className={cn( | ||
'z-10', | ||
'flex items-center justify-center', | ||
'hover:text-dev-pink-400', | ||
'transition-colors duration-200 ease-in-out', | ||
'cursor-pointer', | ||
`size-[${iconSize}px]`, | ||
className, | ||
)} | ||
> | ||
<Icon | ||
name={`icon-${isCopied ? 'check' : 'copy'}`} | ||
size={[iconSize]} | ||
className={cn({ | ||
'text-green-500': isCopied, | ||
})} | ||
/> | ||
</button> | ||
), [className, copyToClipboard, iconSize, isCopied]); | ||
const ClipboardIcon = ( | ||
<button | ||
onClick={copyToClipboard} | ||
type="button" | ||
className={cn( | ||
'z-10', | ||
'flex items-center justify-center', | ||
'hover:text-dev-pink-400', | ||
'transition-colors duration-200 ease-in-out', | ||
'cursor-pointer', | ||
`size-[${iconSize}px]`, | ||
className, | ||
)} | ||
> | ||
<Icon | ||
name={`icon-${isCopied ? 'check' : 'copy'}`} | ||
size={[iconSize]} | ||
className={cn({ | ||
'text-green-500': isCopied, | ||
})} | ||
/> | ||
</button> | ||
); | ||
|
||
if (!children) { | ||
return ClipboardIcon; | ||
} | ||
if (!children) { | ||
return ClipboardIcon; | ||
} | ||
|
||
return children({ | ||
ClipboardIcon, | ||
onClick: copyToClipboard, | ||
}); | ||
}); | ||
return children({ | ||
ClipboardIcon, | ||
onClick: copyToClipboard, | ||
}); | ||
}, | ||
); |
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
Oops, something went wrong.