|
| 1 | +/* |
| 2 | + * Copyright 2025 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Icon } from '@iconify/react'; |
| 18 | +import Box from '@mui/material/Box'; |
| 19 | +import IconButton from '@mui/material/IconButton'; |
| 20 | +import Tooltip from '@mui/material/Tooltip'; |
| 21 | +import type { MouseEvent } from 'react'; |
| 22 | +import { ReactNode, useState } from 'react'; |
| 23 | +import { useTranslation } from 'react-i18next'; |
| 24 | + |
| 25 | +export interface CopyableCellProps { |
| 26 | + /** The text value to copy to clipboard */ |
| 27 | + value: string; |
| 28 | + /** The content to display in the cell */ |
| 29 | + children: ReactNode; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * A wrapper component that adds a copy-to-clipboard button on hover. |
| 34 | + * Used for table cells containing values that users commonly need to copy, |
| 35 | + * such as IP addresses, hostnames, etc. |
| 36 | + */ |
| 37 | +export default function CopyableCell({ value, children }: CopyableCellProps) { |
| 38 | + const { t } = useTranslation(['translation']); |
| 39 | + const [copied, setCopied] = useState(false); |
| 40 | + |
| 41 | + if (!value) { |
| 42 | + return <>{children}</>; |
| 43 | + } |
| 44 | + |
| 45 | + async function handleCopy(e: MouseEvent) { |
| 46 | + e.stopPropagation(); |
| 47 | + try { |
| 48 | + await navigator.clipboard.writeText(value); |
| 49 | + setCopied(true); |
| 50 | + setTimeout(() => setCopied(false), 1500); |
| 51 | + } catch (err) { |
| 52 | + console.error('Failed to copy to clipboard:', err); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const tooltipTitle = copied ? t('translation|Copied!') : t('translation|Copy to clipboard'); |
| 57 | + |
| 58 | + return ( |
| 59 | + <Box |
| 60 | + sx={{ |
| 61 | + display: 'flex', |
| 62 | + alignItems: 'center', |
| 63 | + gap: 0.5, |
| 64 | + '& .copy-button': { |
| 65 | + opacity: 0, |
| 66 | + transition: 'opacity 0.2s', |
| 67 | + }, |
| 68 | + '&:hover .copy-button': { |
| 69 | + opacity: 1, |
| 70 | + }, |
| 71 | + }} |
| 72 | + > |
| 73 | + <Box sx={{ flex: 1, minWidth: 0 }}>{children}</Box> |
| 74 | + <Tooltip title={tooltipTitle}> |
| 75 | + <IconButton |
| 76 | + className="copy-button" |
| 77 | + size="small" |
| 78 | + onClick={handleCopy} |
| 79 | + aria-label={t('translation|Copy to clipboard')} |
| 80 | + sx={{ |
| 81 | + padding: '2px', |
| 82 | + flexShrink: 0, |
| 83 | + }} |
| 84 | + > |
| 85 | + <Icon icon={copied ? 'mdi:check' : 'mdi:content-copy'} width="16" height="16" /> |
| 86 | + </IconButton> |
| 87 | + </Tooltip> |
| 88 | + </Box> |
| 89 | + ); |
| 90 | +} |
0 commit comments