diff --git a/.changelog/1683.bugfix.md b/.changelog/1683.bugfix.md new file mode 100644 index 000000000..eb311e22d --- /dev/null +++ b/.changelog/1683.bugfix.md @@ -0,0 +1 @@ +Add optional floating copy to clipboard button to JSON code preview diff --git a/src/app/components/CodeDisplay/index.tsx b/src/app/components/CodeDisplay/index.tsx index 783b8305c..a1eb1d3d0 100644 --- a/src/app/components/CodeDisplay/index.tsx +++ b/src/app/components/CodeDisplay/index.tsx @@ -1,10 +1,10 @@ -import { FC, ReactNode } from 'react' +import { FC, ReactNode, useState } from 'react' import { useTranslation } from 'react-i18next' import Box from '@mui/material/Box' import Typography from '@mui/material/Typography' import { styled } from '@mui/material/styles' import { ScrollableDataDisplay } from '../../components/ScrollableDataDisplay' -import { CopyToClipboard } from '../../components/CopyToClipboard' +import { CopyToClipboard, FloatingCopyToClipboard } from '../../components/CopyToClipboard' import { useScreenSize } from '../../hooks/useScreensize' import { base64ToHex } from '../../utils/helpers' @@ -13,7 +13,7 @@ type CodeDisplayProps = { copyToClipboardValue: string label?: string extraTopPadding?: boolean - withCopyButton?: boolean + floatingCopyButton?: boolean } const CodeDisplay: FC = ({ @@ -21,23 +21,28 @@ const CodeDisplay: FC = ({ copyToClipboardValue, label, extraTopPadding, - withCopyButton = true, + floatingCopyButton = false, }) => { const { t } = useTranslation() const { isMobile } = useScreenSize() + const [isHovering, setIsHovering] = useState(false) if (!code) { return null } return ( - <> + setIsHovering(true)} + onMouseLeave={() => setIsHovering(false)} + > @@ -46,16 +51,17 @@ const CodeDisplay: FC = ({ {label} )} - {withCopyButton && ( + {floatingCopyButton ? ( + + ) : ( )} - - + ) } @@ -83,10 +89,10 @@ const StyledPre = styled('pre')({ type JsonCodeDisplayProps = { data: Record label?: string - withCopyButton?: boolean + floatingCopyButton?: boolean } -export const JsonCodeDisplay: FC = ({ data, label, withCopyButton = true }) => { +export const JsonCodeDisplay: FC = ({ data, label, floatingCopyButton }) => { const formattedJson = JSON.stringify(data, null, 2) return ( @@ -94,7 +100,7 @@ export const JsonCodeDisplay: FC = ({ data, label, withCop code={{formattedJson}} copyToClipboardValue={formattedJson} label={label} - withCopyButton={withCopyButton} + floatingCopyButton={floatingCopyButton} /> ) } diff --git a/src/app/components/CopyToClipboard/index.tsx b/src/app/components/CopyToClipboard/index.tsx index 563aa8913..1f85aaed5 100644 --- a/src/app/components/CopyToClipboard/index.tsx +++ b/src/app/components/CopyToClipboard/index.tsx @@ -10,11 +10,15 @@ import Button from '@mui/material/Button' const clipboardTooltipDuration = 2000 type CopyToClipboardProps = { + floating?: boolean + isFloatingVisible?: boolean value: string label?: string } -const StyledIconButton = styled(ButtonBase)(({ theme }) => ({ +const StyledIconButton = styled(ButtonBase, { + shouldForwardProp: prop => prop !== 'floating' && prop !== 'isFloatingVisible', +})<{ floating?: boolean; isFloatingVisible?: boolean }>(({ theme, floating, isFloatingVisible }) => ({ display: 'inline-flex', alignItems: 'center', border: 0, @@ -24,9 +28,34 @@ const StyledIconButton = styled(ButtonBase)(({ theme }) => ({ fontFamily: 'inherit', padding: 0, marginLeft: theme.spacing(4), + ...(floating && { + position: 'absolute', + right: theme.spacing(5), + top: theme.spacing(4), + opacity: isFloatingVisible ? 1 : 0, + transition: 'opacity 0.2s ease-in-out', + zIndex: theme.zIndex.fab, + boxShadow: theme.shadows[1], + background: COLORS.white, + borderRadius: '50%', + width: 40, + height: 40, + display: 'flex', + justifyContent: 'center', + marginLeft: 0, + }), })) -export const CopyToClipboard: FC = ({ value, label }) => { +type FloatingCopyToClipboardProps = { + isVisible: boolean + value: string +} + +export const FloatingCopyToClipboard: FC = ({ isVisible, value }) => { + return +} + +export const CopyToClipboard: FC = ({ label, floating, isFloatingVisible, value }) => { const { t } = useTranslation() const timeout = useRef(undefined) const ariaLabel = t('clipboard.label') @@ -59,7 +88,13 @@ export const CopyToClipboard: FC = ({ value, label }) => { {label} ) : ( - + )} diff --git a/src/app/pages/RuntimeTransactionDetailPage/index.tsx b/src/app/pages/RuntimeTransactionDetailPage/index.tsx index 62e42abe8..5dbff666a 100644 --- a/src/app/pages/RuntimeTransactionDetailPage/index.tsx +++ b/src/app/pages/RuntimeTransactionDetailPage/index.tsx @@ -254,7 +254,7 @@ export const RuntimeTransactionDetailView: FC<{ <>
{t('transaction.rawData')}
- +
)}