Files
oneuptime/Common/UI/Components/CopyableButton/CopyableButton.tsx
Nawaz Dhandala e9d5a560ff Refactor migration files and improve code formatting
- Updated migration files to enhance readability by adjusting indentation and line breaks.
- Added missing commas in the index file for migration imports.
- Improved accessibility attributes in various UI components by ensuring proper aria-labels and roles.
- Refactored key event handlers in UI components for better clarity and consistency.
- Enhanced error message handling in form components to ensure proper display and accessibility.
- Updated legacy function comments for clarity and maintainability.
2026-01-26 19:23:58 +00:00

69 lines
1.8 KiB
TypeScript

import Icon, { SizeProp, ThickProp } from "../Icon/Icon";
import Tooltip from "../Tooltip/Tooltip";
import IconProp from "../../../Types/Icon/IconProp";
import React, { FunctionComponent, ReactElement, useState } from "react";
export interface ComponentProps {
textToBeCopied: string;
}
const CopyableButton: FunctionComponent<ComponentProps> = (
props: ComponentProps,
): ReactElement => {
const [copiedToClipboard, setCopyToClipboard] = useState<boolean>(false);
const refreshCopyToClipboardState: VoidFunction = (): void => {
setCopyToClipboard(true);
setTimeout(() => {
setCopyToClipboard(false);
}, 2000);
};
const handleCopy: () => Promise<void> = async (): Promise<void> => {
refreshCopyToClipboardState();
await navigator.clipboard?.writeText(props.textToBeCopied);
};
const handleKeyDown: (event: React.KeyboardEvent) => Promise<void> = async (
event: React.KeyboardEvent,
): Promise<void> => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
await handleCopy();
}
};
return (
<div
className={`${
copiedToClipboard ? "" : "cursor-pointer mt-0.5"
} flex ml-1 text-gray-500`}
onClick={handleCopy}
onKeyDown={handleKeyDown}
role="button"
tabIndex={0}
aria-label={
copiedToClipboard ? "Copied to clipboard" : "Copy to clipboard"
}
aria-live="polite"
>
{" "}
{copiedToClipboard ? (
"Copied to Clipboard"
) : (
<Tooltip text="Copy to Clipboard">
<Icon
className="h-4 w-4"
data-testid="copy-to-clipboard-icon"
icon={IconProp.Copy}
size={SizeProp.Small}
thick={ThickProp.Thick}
/>
</Tooltip>
)}{" "}
</div>
);
};
export default CopyableButton;