From 741b1edf7358698ca468606e07759d72e0ad22ef Mon Sep 17 00:00:00 2001 From: Hanzen Shou Date: Thu, 12 Jun 2025 10:50:59 -0700 Subject: [PATCH] Refactor copy buttons (#3456) * refactor: moved copy logic to CopyButtonComponents.tsx * refactor: simplified copy button components * clean: format & cleanup code * clean: removed comments * fix: fixed aria labels * clean: removed old comments * clean: reduced deltas * clean: deleted comment in ChatRow.tsx * updates * changeset --------- Co-authored-by: Toshii <94262432+0xToshii@users.noreply.github.com> --- .changeset/shiny-taxis-suffer.md | 5 + webview-ui/src/components/chat/ChatRow.tsx | 78 ++-------- .../src/components/common/CopyButton.tsx | 138 ++++++++++++++++++ .../src/components/common/MarkdownBlock.tsx | 51 ++----- 4 files changed, 174 insertions(+), 98 deletions(-) create mode 100644 .changeset/shiny-taxis-suffer.md create mode 100644 webview-ui/src/components/common/CopyButton.tsx diff --git a/.changeset/shiny-taxis-suffer.md b/.changeset/shiny-taxis-suffer.md new file mode 100644 index 00000000..66429eb6 --- /dev/null +++ b/.changeset/shiny-taxis-suffer.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +update the copy button functionality diff --git a/webview-ui/src/components/chat/ChatRow.tsx b/webview-ui/src/components/chat/ChatRow.tsx index 2a47d806..65f62efa 100644 --- a/webview-ui/src/components/chat/ChatRow.tsx +++ b/webview-ui/src/components/chat/ChatRow.tsx @@ -1,6 +1,8 @@ import { VSCodeBadge, VSCodeButton, VSCodeProgressRing } from "@vscode/webview-ui-toolkit/react" import deepEqual from "fast-deep-equal" import React, { memo, MouseEvent, useCallback, useEffect, useMemo, useRef, useState } from "react" +import styled from "styled-components" +import { useEvent, useSize } from "react-use" import CreditLimitError from "@/components/chat/CreditLimitError" import { OptionsButtons } from "@/components/chat/OptionsButtons" @@ -9,6 +11,8 @@ import { CheckmarkControl } from "@/components/common/CheckmarkControl" import CodeBlock, { CODE_BLOCK_BG_COLOR } from "@/components/common/CodeBlock" import MarkdownBlock from "@/components/common/MarkdownBlock" import SuccessButton from "@/components/common/SuccessButton" +import { WithCopyButton } from "@/components/common/CopyButton" +import Thumbnails from "@/components/common/Thumbnails" import McpResponseDisplay from "@/components/mcp/chat-display/McpResponseDisplay" import McpResourceRow from "@/components/mcp/configuration/tabs/installed/server-row/McpResourceRow" import McpToolRow from "@/components/mcp/configuration/tabs/installed/server-row/McpToolRow" @@ -28,76 +32,19 @@ import { } from "@shared/ExtensionMessage" import { COMMAND_OUTPUT_STRING, COMMAND_REQ_APP_STRING } from "@shared/combineCommandSequences" import { Int64Request, StringRequest } from "@shared/proto/common" -import { useEvent, useSize } from "react-use" -import styled from "styled-components" -import { CheckpointControls } from "../common/CheckpointControls" + import CodeAccordian, { cleanPathPrefix } from "../common/CodeAccordian" +import { CheckpointControls } from "../common/CheckpointControls" import NewTaskPreview from "./NewTaskPreview" -import QuoteButton from "./QuoteButton" import ReportBugPreview from "./ReportBugPreview" import UserMessage from "./UserMessage" - -interface CopyButtonProps { - textToCopy: string | undefined -} +import QuoteButton from "./QuoteButton" const normalColor = "var(--vscode-foreground)" const errorColor = "var(--vscode-errorForeground)" const successColor = "var(--vscode-charts-green)" const cancelledColor = "var(--vscode-descriptionForeground)" -const CopyButtonStyled = styled(VSCodeButton)` - position: absolute; - bottom: 2px; - right: 2px; - z-index: 1; - opacity: 0; -` - -interface WithCopyButtonProps { - children: React.ReactNode - textToCopy?: string - style?: React.CSSProperties - ref?: React.Ref - onMouseUp?: (event: MouseEvent) => void -} - -const StyledContainer = styled.div` - position: relative; - - &:hover ${CopyButtonStyled} { - opacity: 1; - } -` - -const WithCopyButton = React.forwardRef( - ({ children, textToCopy, style, onMouseUp, ...props }, ref) => { - const [copied, setCopied] = useState(false) - - const handleCopy = () => { - if (!textToCopy) return - - navigator.clipboard.writeText(textToCopy).then(() => { - setCopied(true) - setTimeout(() => { - setCopied(false) - }, 1500) - }) - } - - return ( - - {children} - {textToCopy && ( - - - - )} - - ) - }, -) - const ChatRowContainer = styled.div` padding: 10px 6px 10px 15px; position: relative; @@ -1086,7 +1033,11 @@ export const ChatRowContent = ({ ) case "text": return ( - + {quoteButtonState.visible && ( + string | void | null + className?: string + ariaLabel?: string +} + +interface WithCopyButtonProps { + children: React.ReactNode + textToCopy?: string + onCopy?: () => string | void | null + position?: "top-right" | "bottom-right" + style?: React.CSSProperties + className?: string + onMouseUp?: (event: React.MouseEvent) => void + ariaLabel?: string +} + +// ======== Styled Components ======== + +const StyledButton = styled(VSCodeButton)` + z-index: 1; +` + +// Unified container component +const ContentContainer = styled.div` + position: relative; +` + +// Unified button container with flexible positioning +const ButtonContainer = styled.div<{ $position?: "top-right" | "bottom-right" }>` + position: absolute; + ${(props) => { + switch (props.$position) { + case "bottom-right": + return "bottom: 2px; right: 2px;" + case "top-right": + default: + return "top: 5px; right: 5px;" + } + }} + z-index: 1; + opacity: 0; + + ${ContentContainer}:hover & { + opacity: 1; + } +` + +// ======== Component Implementations ======== + +/** + * Base copy button component with clipboard functionality + */ +export const CopyButton: React.FC = ({ textToCopy, onCopy, className = "", ariaLabel }) => { + const [copied, setCopied] = useState(false) + + const handleCopy = () => { + if (!textToCopy && !onCopy) return + + let textToCopyFinal = textToCopy + + if (onCopy) { + const result = onCopy() + if (typeof result === "string") { + textToCopyFinal = result + } + } + + if (textToCopyFinal) { + navigator.clipboard + .writeText(textToCopyFinal) + .then(() => { + setCopied(true) + setTimeout(() => setCopied(false), 1500) + }) + .catch((err) => console.error("Copy failed", err)) + } + } + + return ( + + + + ) +} + +/** + * Container component that wraps content with a copy button + */ +export const WithCopyButton = forwardRef( + ( + { + children, + textToCopy, + onCopy, + position = "top-right", + style, + className, + onMouseUp, + ariaLabel, // Destructure ariaLabel + ...props + }, + ref, + ) => { + return ( + + {children} + {(textToCopy || onCopy) && ( + + + + )} + + ) + }, +) + +// Default export for convenience if needed, though named exports are preferred for clarity +const CopyButtonComponents = { + CopyButton, + WithCopyButton, +} +export default CopyButtonComponents diff --git a/webview-ui/src/components/common/MarkdownBlock.tsx b/webview-ui/src/components/common/MarkdownBlock.tsx index 8117c454..88468182 100644 --- a/webview-ui/src/components/common/MarkdownBlock.tsx +++ b/webview-ui/src/components/common/MarkdownBlock.tsx @@ -1,17 +1,18 @@ -import { CODE_BLOCK_BG_COLOR } from "@/components/common/CodeBlock" -import MermaidBlock from "@/components/common/MermaidBlock" -import { useExtensionState } from "@/context/ExtensionStateContext" -import { StateServiceClient } from "@/services/grpc-client" -import { PlanActMode, TogglePlanActModeRequest } from "@shared/proto/state" -import type { ComponentProps } from "react" import React, { memo, useEffect, useRef, useState } from "react" +import type { ComponentProps } from "react" import { useRemark } from "react-remark" import rehypeHighlight, { Options } from "rehype-highlight" import rehypeKatex from "rehype-katex" import remarkMath from "remark-math" import styled from "styled-components" -import type { Node } from "unist" import { visit } from "unist-util-visit" +import type { Node } from "unist" +import { useExtensionState } from "@/context/ExtensionStateContext" +import CodeBlock, { CODE_BLOCK_BG_COLOR } from "@/components/common/CodeBlock" +import MermaidBlock from "@/components/common/MermaidBlock" +import { WithCopyButton } from "./CopyButton" +import { StateServiceClient } from "@/services/grpc-client" +import { PlanActMode, TogglePlanActModeRequest } from "@shared/proto/state" // Styled component for Act Mode text with more specific styling const ActModeHighlight: React.FC = () => ( @@ -178,24 +179,6 @@ const remarkPreventBoldFilenames = () => { } } -import { VSCodeButton } from "@vscode/webview-ui-toolkit/react" - -const CopyButton = styled(VSCodeButton)` - position: absolute; - top: 5px; - right: 5px; - z-index: 1; - opacity: 0; -` - -const CodeBlockContainer = styled.div` - position: relative; - - &:hover ${CopyButton} { - opacity: 1; - } -` - const StyledMarkdown = styled.div` pre { background-color: ${CODE_BLOCK_BG_COLOR}; @@ -337,7 +320,6 @@ const PreWithCopyButton = ({ ...preProps }: { theme: Record } & React.HTMLAttributes) => { const preRef = useRef(null) - const [copied, setCopied] = useState(false) const handleCopy = () => { if (preRef.current) { @@ -345,22 +327,19 @@ const PreWithCopyButton = ({ const textToCopy = codeElement ? codeElement.textContent : preRef.current.textContent if (!textToCopy) return - navigator.clipboard.writeText(textToCopy).then(() => { - setCopied(true) - setTimeout(() => setCopied(false), 1500) - }) + return textToCopy } + return null } + const styledPreProps = theme ? { ...preProps, theme } : preProps + return ( - - - - - + + {children} - + ) }