2020-07-16 13:59:26 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
|
|
|
import { Icon, IconCheck, IconCopy } from '../../base/icons';
|
2021-02-24 15:12:41 +00:00
|
|
|
import { copyText } from '../util';
|
2020-07-16 13:59:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class to apply on container
|
|
|
|
*/
|
|
|
|
className: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The displayed text
|
|
|
|
*/
|
|
|
|
displayedText: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The text that needs to be copied (might differ from the displayedText)
|
|
|
|
*/
|
|
|
|
textToCopy: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The text displayed on mouse hover
|
|
|
|
*/
|
|
|
|
textOnHover: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The text displayed on copy success
|
|
|
|
*/
|
2021-06-10 12:48:44 +00:00
|
|
|
textOnCopySuccess: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The id of the button
|
|
|
|
*/
|
|
|
|
id?: string,
|
2020-07-16 13:59:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component meant to enable users to copy the conference URL.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<any>}
|
|
|
|
*/
|
2021-06-10 12:48:44 +00:00
|
|
|
function CopyButton({ className, displayedText, textToCopy, textOnHover, textOnCopySuccess, id }: Props) {
|
2020-07-16 13:59:26 +00:00
|
|
|
const [ isClicked, setIsClicked ] = useState(false);
|
|
|
|
const [ isHovered, setIsHovered ] = useState(false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Click handler for the element.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-03-03 21:04:00 +00:00
|
|
|
async function onClick() {
|
2020-07-16 13:59:26 +00:00
|
|
|
setIsHovered(false);
|
2021-03-03 21:04:00 +00:00
|
|
|
|
|
|
|
const isCopied = await copyText(textToCopy);
|
|
|
|
|
|
|
|
if (isCopied) {
|
2020-07-16 13:59:26 +00:00
|
|
|
setIsClicked(true);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
setIsClicked(false);
|
|
|
|
}, 2500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hover handler for the element.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function onHoverIn() {
|
|
|
|
if (!isClicked) {
|
|
|
|
setIsHovered(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hover handler for the element.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function onHoverOut() {
|
|
|
|
setIsHovered(false);
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
/**
|
|
|
|
* KeyPress handler for accessibility.
|
|
|
|
*
|
|
|
|
* @param {React.KeyboardEventHandler<HTMLDivElement>} e - The key event to handle.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function onKeyPress(e) {
|
|
|
|
if (onClick && (e.key === ' ' || e.key === 'Enter')) {
|
|
|
|
e.preventDefault();
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 13:59:26 +00:00
|
|
|
/**
|
|
|
|
* Renders the content of the link based on the state.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<any>}
|
|
|
|
*/
|
|
|
|
function renderContent() {
|
|
|
|
if (isClicked) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className = 'copy-button-content selected'>
|
2021-06-10 12:48:44 +00:00
|
|
|
<span role = { 'alert' }>{ textOnCopySuccess }</span>
|
2020-07-16 13:59:26 +00:00
|
|
|
</div>
|
|
|
|
<Icon src = { IconCheck } />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className = 'copy-button-content'>
|
|
|
|
{isHovered ? textOnHover : displayedText}
|
|
|
|
</div>
|
|
|
|
<Icon src = { IconCopy } />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2021-06-10 12:48:44 +00:00
|
|
|
aria-label = { textOnHover }
|
2020-07-16 13:59:26 +00:00
|
|
|
className = { `${className} copy-button${isClicked ? ' clicked' : ''}` }
|
2021-06-10 12:48:44 +00:00
|
|
|
id = { id }
|
|
|
|
onBlur = { onHoverOut }
|
2020-07-16 13:59:26 +00:00
|
|
|
onClick = { onClick }
|
2021-06-10 12:48:44 +00:00
|
|
|
onFocus = { onHoverIn }
|
|
|
|
onKeyPress = { onKeyPress }
|
2020-07-16 13:59:26 +00:00
|
|
|
onMouseOut = { onHoverOut }
|
2021-06-10 12:48:44 +00:00
|
|
|
onMouseOver = { onHoverIn }
|
|
|
|
role = 'button'
|
|
|
|
tabIndex = { 0 }>
|
2020-07-16 13:59:26 +00:00
|
|
|
{ renderContent() }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyButton.defaultProps = {
|
|
|
|
className: ''
|
|
|
|
};
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
export default CopyButton;
|