2021-11-04 21:10:43 +00:00
|
|
|
/* eslint-disable react/jsx-no-bind */
|
2022-09-13 07:36:00 +00:00
|
|
|
import { Theme } from '@mui/material';
|
2021-11-15 08:37:54 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-10-17 09:28:04 +00:00
|
|
|
import { makeStyles } from 'tss-react/mui';
|
2020-07-16 13:59:26 +00:00
|
|
|
|
2022-08-26 09:54:03 +00:00
|
|
|
import Icon from '../icons/components/Icon';
|
2022-09-06 17:32:20 +00:00
|
|
|
import { IconCheck, IconCopy } from '../icons/svg';
|
2021-11-15 08:37:54 +00:00
|
|
|
import { withPixelLineHeight } from '../styles/functions.web';
|
2022-10-06 08:11:06 +00:00
|
|
|
import { copyText } from '../util/copyText.web';
|
2020-07-16 13:59:26 +00:00
|
|
|
|
2022-10-17 09:28:04 +00:00
|
|
|
const useStyles = makeStyles()((theme: Theme) => {
|
2021-11-15 08:37:54 +00:00
|
|
|
return {
|
|
|
|
copyButton: {
|
|
|
|
...withPixelLineHeight(theme.typography.bodyLongRegular),
|
|
|
|
borderRadius: theme.shape.borderRadius,
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center',
|
|
|
|
padding: '8px 8px 8px 16px',
|
|
|
|
marginTop: 5,
|
|
|
|
width: 'calc(100% - 24px)',
|
|
|
|
height: 24,
|
|
|
|
|
|
|
|
background: theme.palette.action01,
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
|
|
|
'&:hover': {
|
|
|
|
backgroundColor: theme.palette.action01Hover,
|
|
|
|
fontWeight: 600
|
|
|
|
},
|
|
|
|
|
|
|
|
'&.clicked': {
|
|
|
|
background: theme.palette.success02
|
|
|
|
},
|
|
|
|
|
|
|
|
'& > div > svg > path': {
|
|
|
|
fill: theme.palette.text01
|
|
|
|
}
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
textOverflow: 'ellipsis',
|
2022-09-13 07:36:00 +00:00
|
|
|
whiteSpace: 'nowrap' as const,
|
2021-11-15 08:37:54 +00:00
|
|
|
maxWidth: 292,
|
2021-12-21 10:16:13 +00:00
|
|
|
marginRight: theme.spacing(3),
|
2021-11-15 08:37:54 +00:00
|
|
|
|
|
|
|
'&.selected': {
|
|
|
|
fontWeight: 600
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-10-17 09:28:04 +00:00
|
|
|
});
|
2021-11-15 08:37:54 +00:00
|
|
|
|
2022-08-26 09:54:03 +00:00
|
|
|
let mounted: boolean;
|
2021-11-15 08:37:54 +00:00
|
|
|
|
2020-07-16 13:59:26 +00:00
|
|
|
type Props = {
|
|
|
|
|
2021-11-15 08:37:54 +00:00
|
|
|
/**
|
2022-08-26 09:54:03 +00:00
|
|
|
* Css class to apply on container.
|
2021-11-15 08:37:54 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
className: string;
|
2021-11-15 08:37:54 +00:00
|
|
|
|
2020-07-16 13:59:26 +00:00
|
|
|
/**
|
2021-11-04 21:10:43 +00:00
|
|
|
* The displayed text.
|
2020-07-16 13:59:26 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
displayedText: string;
|
2020-07-16 13:59:26 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-26 09:54:03 +00:00
|
|
|
* The id of the button.
|
2020-07-16 13:59:26 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
id?: string;
|
2020-07-16 13:59:26 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-26 09:54:03 +00:00
|
|
|
* The text displayed on copy success.
|
2020-07-16 13:59:26 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
textOnCopySuccess: string;
|
2020-07-16 13:59:26 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-26 09:54:03 +00:00
|
|
|
* The text displayed on mouse hover.
|
2020-07-16 13:59:26 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
textOnHover: string;
|
2021-06-10 12:48:44 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-26 09:54:03 +00:00
|
|
|
* The text that needs to be copied (might differ from the displayedText).
|
2021-06-10 12:48:44 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
textToCopy: string;
|
2020-07-16 13:59:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component meant to enable users to copy the conference URL.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<any>}
|
|
|
|
*/
|
2022-10-17 09:28:04 +00:00
|
|
|
function CopyButton({ className = '', displayedText, textToCopy, textOnHover, textOnCopySuccess, id }: Props) {
|
|
|
|
const { classes, cx } = useStyles();
|
2020-07-16 13:59:26 +00:00
|
|
|
const [ isClicked, setIsClicked ] = useState(false);
|
|
|
|
const [ isHovered, setIsHovered ] = useState(false);
|
|
|
|
|
2021-11-15 08:37:54 +00:00
|
|
|
useEffect(() => {
|
|
|
|
mounted = true;
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
mounted = false;
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2020-07-16 13:59:26 +00:00
|
|
|
/**
|
|
|
|
* 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(() => {
|
2021-11-15 08:37:54 +00:00
|
|
|
// avoid: Can't perform a React state update on an unmounted component
|
|
|
|
if (mounted) {
|
|
|
|
setIsClicked(false);
|
|
|
|
}
|
2020-07-16 13:59:26 +00:00
|
|
|
}, 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}
|
|
|
|
*/
|
2022-08-26 09:54:03 +00:00
|
|
|
function onKeyPress(e: React.KeyboardEvent) {
|
2021-06-10 12:48:44 +00:00
|
|
|
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 (
|
|
|
|
<>
|
2022-10-17 09:28:04 +00:00
|
|
|
<div className = { cx(classes.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 (
|
|
|
|
<>
|
2022-10-17 09:28:04 +00:00
|
|
|
<div className = { classes.content }>
|
2021-12-21 10:16:13 +00:00
|
|
|
<span> { isHovered ? textOnHover : displayedText } </span>
|
2020-07-16 13:59:26 +00:00
|
|
|
</div>
|
|
|
|
<Icon src = { IconCopy } />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2021-06-10 12:48:44 +00:00
|
|
|
aria-label = { textOnHover }
|
2022-10-17 09:28:04 +00:00
|
|
|
className = { cx(className, classes.copyButton, 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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:28:04 +00:00
|
|
|
export default CopyButton;
|