您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CopyButton.web.tsx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * Css class to apply on container.
  51. */
  52. className?: string;
  53. /**
  54. * The displayed text.
  55. */
  56. displayedText: string;
  57. /**
  58. * The id of the button.
  59. */
  60. id?: string;
  61. /**
  62. * The text displayed on copy success.
  63. */
  64. textOnCopySuccess: string;
  65. /**
  66. * The text displayed on mouse hover.
  67. */
  68. textOnHover: string;
  69. /**
  70. * The text that needs to be copied (might differ from the displayedText).
  71. */
  72. textToCopy: string;
  73. }
  74. /**
  75. * Component meant to enable users to copy the conference URL.
  76. *
  77. * @returns {React$Element<any>}
  78. */
  79. function CopyButton({ className = '', displayedText, textToCopy, textOnHover, textOnCopySuccess, id }: IProps) {
  80. const { classes, cx } = useStyles();
  81. const [ isClicked, setIsClicked ] = useState(false);
  82. const [ isHovered, setIsHovered ] = useState(false);
  83. useEffect(() => {
  84. mounted = true;
  85. return () => {
  86. mounted = false;
  87. };
  88. }, []);
  89. /**
  90. * Click handler for the element.
  91. *
  92. * @returns {void}
  93. */
  94. async function onClick() {
  95. setIsHovered(false);
  96. const isCopied = await copyText(textToCopy);
  97. if (isCopied) {
  98. setIsClicked(true);
  99. setTimeout(() => {
  100. // avoid: Can't perform a React state update on an unmounted component
  101. if (mounted) {
  102. setIsClicked(false);
  103. }
  104. }, 2500);
  105. }
  106. }
  107. /**
  108. * Hover handler for the element.
  109. *
  110. * @returns {void}
  111. */
  112. function onHoverIn() {
  113. if (!isClicked) {
  114. setIsHovered(true);
  115. }
  116. }
  117. /**
  118. * Hover handler for the element.
  119. *
  120. * @returns {void}
  121. */
  122. function onHoverOut() {
  123. setIsHovered(false);
  124. }
  125. /**
  126. * KeyPress handler for accessibility.
  127. *
  128. * @param {React.KeyboardEventHandler<HTMLDivElement>} e - The key event to handle.
  129. *
  130. * @returns {void}
  131. */
  132. function onKeyPress(e: React.KeyboardEvent) {
  133. if (onClick && (e.key === ' ' || e.key === 'Enter')) {
  134. e.preventDefault();
  135. onClick();
  136. }
  137. }
  138. /**
  139. * Renders the content of the link based on the state.
  140. *
  141. * @returns {React$Element<any>}
  142. */
  143. function renderContent() {
  144. if (isClicked) {
  145. return (
  146. <>
  147. <Icon
  148. className = { classes.icon }
  149. size = { 24 }
  150. src = { IconCheck } />
  151. <div className = { cx(classes.content, 'selected') }>
  152. <span role = { 'alert' }>{ textOnCopySuccess }</span>
  153. </div>
  154. </>
  155. );
  156. }
  157. return (
  158. <>
  159. <Icon
  160. className = { classes.icon }
  161. size = { 24 }
  162. src = { IconCopy } />
  163. <div className = { classes.content }>
  164. <span> { isHovered ? textOnHover : displayedText } </span>
  165. </div>
  166. </>
  167. );
  168. }
  169. return (
  170. <div
  171. aria-label = { textOnHover }
  172. className = { cx(className, classes.copyButton, isClicked ? ' clicked' : '') }
  173. id = { id }
  174. onBlur = { onHoverOut }
  175. onClick = { onClick }
  176. onFocus = { onHoverIn }
  177. onKeyPress = { onKeyPress }
  178. onMouseOut = { onHoverOut }
  179. onMouseOver = { onHoverIn }
  180. role = 'button'
  181. tabIndex = { 0 }>
  182. { renderContent() }
  183. </div>
  184. );
  185. }
  186. export default CopyButton;