import { useState } from 'react'; import styled, { css, type AnyStyledComponent } from 'styled-components'; import { ButtonAction } from '@/constants/buttons'; import { STRING_KEYS } from '@/constants/localization'; import { useStringGetter } from '@/hooks'; import { layoutMixins } from '@/styles/layoutMixins'; import { Button, ButtonProps } from './Button'; import { Icon, IconName } from './Icon'; import { IconButton } from './IconButton'; import { WithTooltip } from './WithTooltip'; export type CopyButtonProps = { value?: string; buttonType?: 'text' | 'icon' | 'default'; children?: React.ReactNode; } & ButtonProps; export const CopyButton = ({ value, buttonType = 'default', children, ...buttonProps }: CopyButtonProps) => { const stringGetter = useStringGetter(); const [copied, setCopied] = useState(false); const onCopy = () => { if (!value) return; setCopied(true); navigator.clipboard.writeText(value); setTimeout(() => setCopied(false), 500); }; return buttonType === 'text' ? ( {children} ) : buttonType === 'icon' ? ( ) : ( ); }; const Styled: Record = {}; Styled.InlineRow = styled.div<{ copied: boolean }>` ${layoutMixins.inlineRow} cursor: pointer; ${({ copied }) => copied ? css` filter: brightness(0.8); ` : css` &:hover { filter: brightness(var(--hover-filter-base)); text-decoration: underline; } `} `; Styled.Icon = styled(Icon)<{ copied: boolean }>` ${({ copied }) => copied && css` color: var(--color-success); `} `; Styled.IconButton = styled(IconButton)<{ copied: boolean }>` ${({ copied }) => copied && css` svg { color: var(--color-success); } `} `;