Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 style(conversation): reveal tool argument copy buttons on row hover (#23627)

AmAzing-d11037974ae7
diff --git a/src/components/CopyableLabel/index.tsx b/src/components/CopyableLabel/index.tsx
index 36347f8b165..364d542e8a1 100644
--- a/src/components/CopyableLabel/index.tsx
+++ b/src/components/CopyableLabel/index.tsx
@@ -1,75 +1,109 @@
import { CopyButton, Flexbox, Text } from '@lobehub/ui';
+import * as stylex from '@stylexjs/stylex';
import { type CSSProperties } from 'react';
import { memo } from 'react';
+import { sx } from '@/styles/sx';
+
+import { copyableRevealMarker } from './markers.stylex';
+
+const styles = stylex.create({
+ hoverCopy: {
+ opacity: {
+ 'default': 0,
+ // Touch screens cannot hover, so the button stays visible there
+ '@media (hover: none)': 1,
+ [stylex.when.ancestor(':hover', copyableRevealMarker)]: 1,
+ [stylex.when.ancestor(':focus-within', copyableRevealMarker)]: 1,
+ },
+ transition: 'opacity 0.15s',
+ },
+});
+
interface CopyableLabelProps {
className?: string;
+ /**
+ * Hide the copy button until the label, or an ancestor carrying
+ * `copyableRevealMarker`, is hovered or focused.
+ */
+ revealCopyOnHover?: boolean;
style?: CSSProperties;
value?: string | null;
wrap?: boolean;
}
-const CopyableLabel = memo<CopyableLabelProps>(({ className, style, value = '--', wrap }) => {
- if (wrap) {
+const CopyableLabel = memo<CopyableLabelProps>(
+ ({ className, revealCopyOnHover, style, value = '--', wrap }) => {
+ const rootClassName = sx(revealCopyOnHover && copyableRevealMarker, className ?? '').className;
+ const copyButton = (
+ <CopyButton
+ className={revealCopyOnHover ? stylex.props(styles.hoverCopy).className : undefined}
+ content={value || '--'}
+ size={'small'}
+ />
+ );
+
+ if (wrap) {
+ return (
+ <Flexbox
+ horizontal
+ align={'flex-start'}
+ className={rootClassName}
+ gap={4}
+ style={{
+ position: 'relative',
+ width: '100%',
+ ...style,
+ }}
+ >
+ <Text
+ style={{
+ color: 'inherit',
+ flex: 1,
+ fontFamily: 'inherit',
+ fontSize: 'inherit',
+ margin: 0,
+ minWidth: 0,
+ overflowWrap: 'anywhere',
+ whiteSpace: 'pre-wrap',
+ }}
+ >
+ {value || '--'}
+ </Text>
+ {copyButton}
+ </Flexbox>
+ );
+ }
+
return (
<Flexbox
horizontal
- align={'flex-start'}
- className={className}
+ align={'center'}
+ className={rootClassName}
gap={4}
style={{
+ overflow: 'hidden',
position: 'relative',
- width: '100%',
...style,
}}
>
<Text
+ ellipsis
style={{
color: 'inherit',
- flex: 1,
fontFamily: 'inherit',
fontSize: 'inherit',
margin: 0,
- minWidth: 0,
- overflowWrap: 'anywhere',
- whiteSpace: 'pre-wrap',
+ overflow: 'hidden',
+ width: '100%',
}}
>
{value || '--'}
</Text>
- <CopyButton content={value || '--'} size={'small'} />
+ {copyButton}
</Flexbox>
);
- }
-
- return (
- <Flexbox
- horizontal
- align={'center'}
- className={className}
- gap={4}
- style={{
- overflow: 'hidden',
- position: 'relative',
- ...style,
- }}
- >
- <Text
- ellipsis
- style={{
- color: 'inherit',
- fontFamily: 'inherit',
- fontSize: 'inherit',
- margin: 0,
- overflow: 'hidden',
- width: '100%',
- }}
- >
- {value || '--'}
- </Text>
- <CopyButton content={value || '--'} size={'small'} />
- </Flexbox>
- );
-});
+ },
+);
export default CopyableLabel;
diff --git a/src/components/CopyableLabel/markers.stylex.ts b/src/components/CopyableLabel/markers.stylex.ts
new file mode 100644
index 00000000000..4e78b21029f
--- /dev/null
+++ b/src/components/CopyableLabel/markers.stylex.ts
@@ -0,0 +1,4 @@
+import * as stylex from '@stylexjs/stylex';
+
+/** Hovering or focusing an element with this marker reveals a hover-only copy button inside it. */
+export const copyableRevealMarker = stylex.defineMarker();
diff --git a/src/components/Descriptions/index.tsx b/src/components/Descriptions/index.tsx
index bd5334d353b..afcfbb52ec6 100644
--- a/src/components/Descriptions/index.tsx
+++ b/src/components/Descriptions/index.tsx
@@ -9,6 +9,7 @@ import { memo } from 'react';
import { sx } from '@/styles/sx';
import CopyableLabel from '../CopyableLabel';
+import { copyableRevealMarker } from '../CopyableLabel/markers.stylex';
const styles = stylex.create({
bordered: {
@@ -61,6 +62,8 @@ interface DescriptionsProps extends Omit<GridProps, 'children' | 'wrap'> {
};
items: DescriptionItem[];
labelWidth?: number | string;
+ /** Show each copyable value's copy button only while its row is hovered or focused. */
+ revealCopyOnHover?: boolean;
styles?: {
item?: CSSProperties;
label?: CSSProperties;
@@ -77,6 +80,7 @@ const Descriptions = memo<DescriptionsProps>(
className,
items,
classNames,
+ revealCopyOnHover,
styles: customStyles,
wrap,
...rest
@@ -91,7 +95,11 @@ const Descriptions = memo<DescriptionsProps>(
align={wrap ? 'flex-start' : 'center'}
flex={1}
key={item.key}
- {...sx(bordered && styles.cell, cx(item.className, classNames?.item))}
+ {...sx(
+ bordered && styles.cell,
+ revealCopyOnHover && copyableRevealMarker,
+ cx(item.className, classNames?.item),
+ )}
style={{
overflow: wrap ? undefined : 'hidden',
position: 'relative',
@@ -139,6 +147,7 @@ const Descriptions = memo<DescriptionsProps>(
{item.copyable ? (
<CopyableLabel
className={cx(classNames?.value, item.classNames?.value)}
+ revealCopyOnHover={revealCopyOnHover}
style={{ ...customStyles?.value, ...item.styles?.value }}
value={item.value ? String(item.value) : '--'}
wrap={wrap}
diff --git a/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Arguments/index.tsx b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Arguments/index.tsx
index 26bb7a323ba..1750736512e 100644
--- a/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Arguments/index.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Arguments/index.tsx
@@ -73,6 +73,7 @@ const Arguments = memo<ArgumentsProps>(
contentNode = (
<Flexbox paddingBlock={4} paddingInline={16}>
<Descriptions
+ revealCopyOnHover
bordered={false}
items={items}
labelWidth={140}