From 44033bfd2a8b60e4d161030ee6e7a67f6f07c383 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 21 May 2024 17:31:04 +0800 Subject: [PATCH] feat: new copy code animate Signed-off-by: Innei --- .../code-highlighter/shiki/ShikiWrapper.tsx | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/components/ui/code-highlighter/shiki/ShikiWrapper.tsx b/src/components/ui/code-highlighter/shiki/ShikiWrapper.tsx index 09eb7e1764..fa15531d6a 100644 --- a/src/components/ui/code-highlighter/shiki/ShikiWrapper.tsx +++ b/src/components/ui/code-highlighter/shiki/ShikiWrapper.tsx @@ -4,9 +4,12 @@ import { useEffect, useImperativeHandle, useMemo, + useRef, useState, } from 'react' import clsx from 'clsx' +import { AnimatePresence, m } from 'framer-motion' +import type { Variants } from 'framer-motion' import type { PropsWithChildren } from 'react' import { getViewport } from '~/atoms/hooks' @@ -28,6 +31,20 @@ interface Props { renderedHTML?: string } +const copyIconVariants: Variants = { + initial: { + opacity: 1, + scale: 1, + }, + animate: { + opacity: 1, + scale: 1, + }, + exit: { + opacity: 0, + scale: 0, + }, +} export const ShikiHighLighterWrapper = forwardRef< HTMLDivElement, PropsWithChildren< @@ -43,9 +60,16 @@ export const ShikiHighLighterWrapper = forwardRef< attrs, } = props + const [copied, setCopied] = useState(false) + const copiedTimerRef = useRef() const handleCopy = useCallback(() => { navigator.clipboard.writeText(value) - toast.success('已复制到剪贴板') + setCopied(true) + + clearTimeout(copiedTimerRef.current) + copiedTimerRef.current = setTimeout(() => { + setCopied(false) + }, 2000) }, [value]) const [codeBlockRef, setCodeBlockRef] = useState(null) @@ -122,16 +146,31 @@ export const ShikiHighLighterWrapper = forwardRef< - + + {copied ? ( + + ) : ( + + )} +