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.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // @flow
  2. import React, { useState } from 'react';
  3. import { translate } from '../../base/i18n';
  4. import { Icon, IconCheck, IconCopy } from '../../base/icons';
  5. import { copyText } from '../../base/util';
  6. type Props = {
  7. /**
  8. * Css class to apply on container
  9. */
  10. className: string,
  11. /**
  12. * The displayed text
  13. */
  14. displayedText: string,
  15. /**
  16. * The text that needs to be copied (might differ from the displayedText)
  17. */
  18. textToCopy: string,
  19. /**
  20. * The text displayed on mouse hover
  21. */
  22. textOnHover: string,
  23. /**
  24. * The text displayed on copy success
  25. */
  26. textOnCopySuccess: string
  27. };
  28. /**
  29. * Component meant to enable users to copy the conference URL.
  30. *
  31. * @returns {React$Element<any>}
  32. */
  33. function CopyButton({ className, displayedText, textToCopy, textOnHover, textOnCopySuccess }: Props) {
  34. const [ isClicked, setIsClicked ] = useState(false);
  35. const [ isHovered, setIsHovered ] = useState(false);
  36. /**
  37. * Click handler for the element.
  38. *
  39. * @returns {void}
  40. */
  41. function onClick() {
  42. setIsHovered(false);
  43. if (copyText(textToCopy)) {
  44. setIsClicked(true);
  45. setTimeout(() => {
  46. setIsClicked(false);
  47. }, 2500);
  48. }
  49. }
  50. /**
  51. * Hover handler for the element.
  52. *
  53. * @returns {void}
  54. */
  55. function onHoverIn() {
  56. if (!isClicked) {
  57. setIsHovered(true);
  58. }
  59. }
  60. /**
  61. * Hover handler for the element.
  62. *
  63. * @returns {void}
  64. */
  65. function onHoverOut() {
  66. setIsHovered(false);
  67. }
  68. /**
  69. * Renders the content of the link based on the state.
  70. *
  71. * @returns {React$Element<any>}
  72. */
  73. function renderContent() {
  74. if (isClicked) {
  75. return (
  76. <>
  77. <div className = 'copy-button-content selected'>
  78. {textOnCopySuccess}
  79. </div>
  80. <Icon src = { IconCheck } />
  81. </>
  82. );
  83. }
  84. return (
  85. <>
  86. <div className = 'copy-button-content'>
  87. {isHovered ? textOnHover : displayedText}
  88. </div>
  89. <Icon src = { IconCopy } />
  90. </>
  91. );
  92. }
  93. return (
  94. <div
  95. className = { `${className} copy-button${isClicked ? ' clicked' : ''}` }
  96. onClick = { onClick }
  97. onMouseOut = { onHoverOut }
  98. onMouseOver = { onHoverIn }>
  99. { renderContent() }
  100. </div>
  101. );
  102. }
  103. CopyButton.defaultProps = {
  104. className: ''
  105. };
  106. export default translate(CopyButton);