Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CopyButton.web.tsx 5.0KB

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