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

CopyButton.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // @flow
  2. /* eslint-disable react/jsx-no-bind */
  3. import { withStyles } from '@material-ui/styles';
  4. import clsx from 'clsx';
  5. import React, { useEffect, useState } from 'react';
  6. import { Icon, IconCheck, IconCopy } from '../../base/icons';
  7. import { withPixelLineHeight } from '../styles/functions.web';
  8. import { copyText } from '../util';
  9. const styles = theme => {
  10. return {
  11. copyButton: {
  12. ...withPixelLineHeight(theme.typography.bodyLongRegular),
  13. borderRadius: theme.shape.borderRadius,
  14. display: 'flex',
  15. justifyContent: 'space-between',
  16. alignItems: 'center',
  17. padding: '8px 8px 8px 16px',
  18. marginTop: 5,
  19. width: 'calc(100% - 24px)',
  20. height: 24,
  21. background: theme.palette.action01,
  22. cursor: 'pointer',
  23. '&:hover': {
  24. backgroundColor: theme.palette.action01Hover,
  25. fontWeight: 600
  26. },
  27. '&.clicked': {
  28. background: theme.palette.success02
  29. },
  30. '& > div > svg > path': {
  31. fill: theme.palette.text01
  32. }
  33. },
  34. content: {
  35. overflow: 'hidden',
  36. textOverflow: 'ellipsis',
  37. whiteSpace: 'nowrap',
  38. maxWidth: 292,
  39. marginRight: theme.spacing(3),
  40. '&.selected': {
  41. fontWeight: 600
  42. }
  43. }
  44. };
  45. };
  46. let mounted;
  47. type Props = {
  48. /**
  49. * An object containing the CSS classes.
  50. */
  51. classes: Object,
  52. /**
  53. * Css class to apply on container.
  54. */
  55. className: string,
  56. /**
  57. * The displayed text.
  58. */
  59. displayedText: string,
  60. /**
  61. * The text that needs to be copied (might differ from the displayedText).
  62. */
  63. textToCopy: string,
  64. /**
  65. * The text displayed on mouse hover.
  66. */
  67. textOnHover: string,
  68. /**
  69. * The text displayed on copy success.
  70. */
  71. textOnCopySuccess: string,
  72. /**
  73. * The id of the button.
  74. */
  75. id?: string,
  76. };
  77. /**
  78. * Component meant to enable users to copy the conference URL.
  79. *
  80. * @returns {React$Element<any>}
  81. */
  82. function CopyButton({ classes, className, displayedText, textToCopy, textOnHover, textOnCopySuccess, id }: Props) {
  83. const [ isClicked, setIsClicked ] = useState(false);
  84. const [ isHovered, setIsHovered ] = useState(false);
  85. useEffect(() => {
  86. mounted = true;
  87. return () => {
  88. mounted = false;
  89. };
  90. }, []);
  91. /**
  92. * Click handler for the element.
  93. *
  94. * @returns {void}
  95. */
  96. async function onClick() {
  97. setIsHovered(false);
  98. const isCopied = await copyText(textToCopy);
  99. if (isCopied) {
  100. setIsClicked(true);
  101. setTimeout(() => {
  102. // avoid: Can't perform a React state update on an unmounted component
  103. if (mounted) {
  104. setIsClicked(false);
  105. }
  106. }, 2500);
  107. }
  108. }
  109. /**
  110. * Hover handler for the element.
  111. *
  112. * @returns {void}
  113. */
  114. function onHoverIn() {
  115. if (!isClicked) {
  116. setIsHovered(true);
  117. }
  118. }
  119. /**
  120. * Hover handler for the element.
  121. *
  122. * @returns {void}
  123. */
  124. function onHoverOut() {
  125. setIsHovered(false);
  126. }
  127. /**
  128. * KeyPress handler for accessibility.
  129. *
  130. * @param {React.KeyboardEventHandler<HTMLDivElement>} e - The key event to handle.
  131. *
  132. * @returns {void}
  133. */
  134. function onKeyPress(e) {
  135. if (onClick && (e.key === ' ' || e.key === 'Enter')) {
  136. e.preventDefault();
  137. onClick();
  138. }
  139. }
  140. /**
  141. * Renders the content of the link based on the state.
  142. *
  143. * @returns {React$Element<any>}
  144. */
  145. function renderContent() {
  146. if (isClicked) {
  147. return (
  148. <>
  149. <div className = { clsx(classes.content, 'selected') }>
  150. <span role = { 'alert' }>{ textOnCopySuccess }</span>
  151. </div>
  152. <Icon src = { IconCheck } />
  153. </>
  154. );
  155. }
  156. return (
  157. <>
  158. <div className = { clsx(classes.content) }>
  159. <span> { isHovered ? textOnHover : displayedText } </span>
  160. </div>
  161. <Icon src = { IconCopy } />
  162. </>
  163. );
  164. }
  165. return (
  166. <div
  167. aria-label = { textOnHover }
  168. className = { clsx(className, classes.copyButton, isClicked ? ' clicked' : '') }
  169. id = { id }
  170. onBlur = { onHoverOut }
  171. onClick = { onClick }
  172. onFocus = { onHoverIn }
  173. onKeyPress = { onKeyPress }
  174. onMouseOut = { onHoverOut }
  175. onMouseOver = { onHoverIn }
  176. role = 'button'
  177. tabIndex = { 0 }>
  178. { renderContent() }
  179. </div>
  180. );
  181. }
  182. CopyButton.defaultProps = {
  183. className: ''
  184. };
  185. export default withStyles(styles)(CopyButton);