Heray-Was-Here
Server : Apache
System : Linux mail.lomejor.cr 6.8.0-1059-azure #65~22.04.1-Ubuntu SMP Thu May 28 16:59:19 UTC 2026 x86_64
User : www-data ( 33)
PHP Version : 8.2.31
Disable Function : NONE
Directory :  /var/www/outlet/wp-content/plugins/code-snippets/js/components/common/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/outlet/wp-content/plugins/code-snippets/js/components/common/CopyToClipboardButton.tsx
import React, { useState } from 'react'
import { handleUnknownError } from '../../utils/errors'
import type { HTMLAttributes, MouseEventHandler } from 'react'

const TIMEOUT = 3000

export interface CopyToClipboardButtonProps extends HTMLAttributes<HTMLAnchorElement> {
	text: string
	copyIcon?: string
	successIcon?: string
	timeout?: number
}

export const CopyToClipboardButton: React.FC<CopyToClipboardButtonProps> = ({
	text,
	copyIcon = 'clipboard',
	successIcon = 'yes',
	...props
}) => {
	const [isSuccess, setIsSuccess] = useState(false)
	const clipboard = window.navigator.clipboard as Clipboard | undefined

	const clickHandler: MouseEventHandler<HTMLAnchorElement> = event => {
		event.preventDefault()

		clipboard?.writeText(text)
			.then(() => {
				setIsSuccess(true)
				setTimeout(() => setIsSuccess(false), TIMEOUT)
			})
			.catch(handleUnknownError)
	}

	return clipboard
		? <a
			href="#"
			className={`code-snippets-copy-text dashicons dashicons-${isSuccess ? successIcon : copyIcon}`}
			onClick={clickHandler}
			{...props}
		></a>
		: null
}

Hry