You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CopyButton.web.tsx 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* eslint-disable react/jsx-no-bind */
  2. import React, { useEffect, useState } from 'react';
  3. import { makeStyles } from 'tss-react/mui';
  4. import Icon from '../icons/components/Icon';
  5. import { IconCheck, IconCopy } from '../icons/svg';
  6. import { withPixelLineHeight } from '../styles/functions.web';
  7. import { copyText } from '../util/copyText.web';
  8. const useStyles = makeStyles()(theme => {
  9. return {
  10. copyButton: {
  11. ...withPixelLineHeight(theme.typography.bodyShortBold),
  12. borderRadius: theme.shape.borderRadius,
  13. display: 'flex',
  14. justifyContent: 'flex-start',
  15. alignItems: 'center',
  16. padding: `${theme.spacing(2)} ${theme.spacing(3)}`,
  17. width: '100%',
  18. boxSizing: 'border-box',
  19. background: theme.palette.action01,
  20. cursor: 'pointer',
  21. color: theme.palette.text01,
  22. '&:hover': {
  23. backgroundColor: theme.palette.action01Hover
  24. },
  25. '&.clicked': {
  26. background: theme.palette.success02
  27. },
  28. '& > div > svg': {
  29. fill: theme.palette.icon01
  30. }
  31. },
  32. content: {
  33. overflow: 'hidden',
  34. textOverflow: 'ellipsis',
  35. whiteSpace: 'nowrap' as const,
  36. maxWidth: 292,
  37. marginRight: theme.spacing(3),
  38. '&.selected': {
  39. fontWeight: 600
  40. }
  41. },
  42. icon: {
  43. marginRight: theme.spacing(2)
  44. }
  45. };
  46. });
  47. let mounted: boolean;
  48. interface IProps {
  49. /**
  50. * The invisible text for screen readers.
  51. *
  52. * Intended to give the same info as `displayedText`, but can be customized to give more necessary context.
  53. * If not given, `displayedText` will be used.
  54. */
  55. accessibilityText?: string;
  56. /**
  57. * Css class to apply on container.
  58. */
  59. className?: string;
  60. /**
  61. * The displayed text.
  62. */
  63. displayedText: string;
  64. /**
  65. * The id of the button.
  66. */
  67. id?: string;
  68. /**
  69. * The text displayed on copy success.
  70. */
  71. textOnCopySuccess: string;
  72. /**
  73. * The text displayed on mouse hover.
  74. */
  75. textOnHover: string;
  76. /**
  77. * The text that needs to be copied (might differ from the displayedText).
  78. */
  79. textToCopy: string;
  80. }
  81. /**
  82. * Component meant to enable users to copy the conference URL.
  83. *
  84. * @returns {React$Element<any>}
  85. */
  86. function CopyButton({
  87. accessibilityText,
  88. className = '',
  89. displayedText,
  90. textToCopy,
  91. textOnHover,
  92. textOnCopySuccess,
  93. id
  94. }: IProps) {
  95. const { classes, cx } = useStyles();
  96. const [ isClicked, setIsClicked ] = useState(false);
  97. const [ isHovered, setIsHovered ] = useState(false);
  98. useEffect(() => {
  99. mounted = true;
  100. return () => {
  101. mounted = false;
  102. };
  103. }, []);
  104. /**
  105. * Click handler for the element.
  106. *
  107. * @returns {void}
  108. */
  109. async function onClick() {
  110. setIsHovered(false);
  111. const isCopied = await copyText(textToCopy);
  112. if (isCopied) {
  113. setIsClicked(true);
  114. setTimeout(() => {
  115. // avoid: Can't perform a React state update on an unmounted component
  116. if (mounted) {
  117. setIsClicked(false);
  118. }
  119. }, 2500);
  120. }
  121. }
  122. /**
  123. * Hover handler for the element.
  124. *
  125. * @returns {void}
  126. */
  127. function onHoverIn() {
  128. if (!isClicked) {
  129. setIsHovered(true);
  130. }
  131. }
  132. /**
  133. * Hover handler for the element.
  134. *
  135. * @returns {void}
  136. */
  137. function onHoverOut() {
  138. setIsHovered(false);
  139. }
  140. /**
  141. * KeyPress handler for accessibility.
  142. *
  143. * @param {React.KeyboardEventHandler<HTMLDivElement>} e - The key event to handle.
  144. *
  145. * @returns {void}
  146. */
  147. function onKeyPress(e: React.KeyboardEvent) {
  148. if (onClick && (e.key === ' ' || e.key === 'Enter')) {
  149. e.preventDefault();
  150. onClick();
  151. }
  152. }
  153. /**
  154. * Renders the content of the link based on the state.
  155. *
  156. * @returns {React$Element<any>}
  157. */
  158. function renderContent() {
  159. if (isClicked) {
  160. return (
  161. <>
  162. <Icon
  163. className = { classes.icon }
  164. size = { 24 }
  165. src = { IconCheck } />
  166. <div className = { cx(classes.content, 'selected') }>
  167. <span role = { 'alert' }>{ textOnCopySuccess }</span>
  168. </div>
  169. </>
  170. );
  171. }
  172. return (
  173. <>
  174. <Icon
  175. className = { classes.icon }
  176. size = { 24 }
  177. src = { IconCopy } />
  178. <div className = { classes.content }>
  179. <span> { isHovered ? textOnHover : displayedText } </span>
  180. </div>
  181. </>
  182. );
  183. }
  184. return (
  185. <>
  186. <div
  187. aria-describedby = { displayedText === textOnHover
  188. ? undefined
  189. : `${id}-sr-text` }
  190. aria-label = { displayedText === textOnHover ? accessibilityText : textOnHover }
  191. className = { cx(className, classes.copyButton, isClicked ? ' clicked' : '') }
  192. id = { id }
  193. onBlur = { onHoverOut }
  194. onClick = { onClick }
  195. onFocus = { onHoverIn }
  196. onKeyPress = { onKeyPress }
  197. onMouseOut = { onHoverOut }
  198. onMouseOver = { onHoverIn }
  199. role = 'button'
  200. tabIndex = { 0 }>
  201. { renderContent() }
  202. </div>
  203. { displayedText !== textOnHover && (
  204. <span
  205. className = 'sr-only'
  206. id = { `${id}-sr-text` }>
  207. { accessibilityText }
  208. </span>
  209. )}
  210. </>
  211. );
  212. }
  213. export default CopyButton;