2024-12-22 19:00:06 +01:00
|
|
|
import React, { useState, useContext } from 'react'
|
|
|
|
import { CopyToClipboard as Copy } from 'react-copy-to-clipboard'
|
|
|
|
import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus'
|
|
|
|
|
|
|
|
export const KeyVal = ({ k, val, color = 'primary', small = false }) => {
|
|
|
|
const [copied, setCopied] = useState(false)
|
|
|
|
const { setLoadingStatus } = useContext(LoadingStatusContext)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Copy text={val} onCopy={() => handleCopied(setCopied, setLoadingStatus, k)}>
|
|
|
|
<button>
|
|
|
|
<span
|
2024-12-26 18:33:49 +01:00
|
|
|
className={`${sharedClasses} tw-rounded-l${small ? '' : '-lg'} tw-text-${color}-content tw-bg-${color} tw-border-${color} ${small ? 'tw-text-xs' : ''}`}
|
2024-12-22 19:00:06 +01:00
|
|
|
>
|
|
|
|
{k}
|
|
|
|
</span>
|
|
|
|
<span
|
2024-12-26 18:33:49 +01:00
|
|
|
className={`${sharedClasses} tw-rounded-r${small ? '' : '-lg'} tw-text-${color} tw-bg-base-100 tw-border-${color} ${small ? 'tw-text-xs' : ''}`}
|
2024-12-22 19:00:06 +01:00
|
|
|
>
|
|
|
|
{val}
|
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
</Copy>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-26 18:33:49 +01:00
|
|
|
const sharedClasses = `tw-px-1 tw-text-sm tw-font-medium tw-whitespace-nowrap tw-border-2`
|
2024-12-22 19:00:06 +01:00
|
|
|
|
|
|
|
const handleCopied = (setCopied, setLoadingStatus, label) => {
|
|
|
|
setCopied(true)
|
|
|
|
setLoadingStatus([
|
|
|
|
true,
|
|
|
|
label ? `${label} copied to clipboard` : 'Copied to clipboard',
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
])
|
|
|
|
setTimeout(() => setCopied(false), 1000)
|
|
|
|
}
|