CopyToClipboard.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // import ClipboardJS from "clipboard";
  2. import ClipboardJS from "clipboard";
  3. import React, { Component, RefObject } from "react";
  4. import Tooltip from "@material-ui/core/Tooltip";
  5. import styled from "styled-components";
  6. import { styled as materialStyled } from "@material-ui/core/styles";
  7. type PropsType = {
  8. text: string;
  9. onSuccess?: (e: ClipboardJS.Event) => void;
  10. onError?: (e: ClipboardJS.Event) => void;
  11. wrapperProps?: any;
  12. as?: any;
  13. };
  14. type StateType = {
  15. clipboard: ClipboardJS | undefined;
  16. success: boolean;
  17. };
  18. /**
  19. * Dynamic component to enable copy to clipboard.
  20. * By default, it will be displayed as a span, when the user clicks over the span
  21. * it will copy the text provided
  22. *
  23. * Examples of usage:
  24. * <CopyToClipboard
  25. * as={MyCustomComponent}
  26. * text={`some usefull text ${var}`}
  27. * onSuccess={(e) => console.log("Success event:", e)}
  28. * onError={(e) => console.log("Error event:", e)}
  29. * >
  30. * Some content
  31. * </CopyToClipboard>
  32. */
  33. export default class CopyToClipboard extends Component<PropsType, StateType> {
  34. triggerRef: RefObject<HTMLSpanElement>;
  35. state: StateType = {
  36. clipboard: undefined,
  37. success: false,
  38. };
  39. constructor(props: PropsType) {
  40. super(props);
  41. this.triggerRef = React.createRef();
  42. }
  43. componentDidMount() {
  44. const trigger = this.triggerRef.current;
  45. if (!trigger) {
  46. console.error("Couldn't mount clipboardjs on wrapper component");
  47. return;
  48. }
  49. const clipboard = new ClipboardJS(trigger, {
  50. text: () => {
  51. return this.props.text;
  52. },
  53. });
  54. clipboard.on("success", (e) => {
  55. this.setState({ success: true });
  56. this.props.onSuccess && this.props.onSuccess(e);
  57. setTimeout(() => {
  58. this.setState({ success: false });
  59. }, 2000);
  60. });
  61. this.props.onError && clipboard.on("error", this.props.onError);
  62. this.setState({ clipboard });
  63. }
  64. componentWillUnmount() {
  65. if (this.state.clipboard && this.state.clipboard.destroy) {
  66. this.state.clipboard.destroy();
  67. }
  68. }
  69. render() {
  70. return (
  71. <Tooltip
  72. title={
  73. <div
  74. style={{
  75. fontFamily: "Work Sans, sans-serif",
  76. fontSize: "12px",
  77. fontWeight: "normal",
  78. padding: "5px 6px",
  79. }}
  80. >
  81. Copied to clipboard
  82. </div>
  83. }
  84. open={this.state.success}
  85. placement="bottom"
  86. arrow
  87. >
  88. <DynamicSpanComponent
  89. as={this.props.as || "span"}
  90. ref={this.triggerRef}
  91. {...(this.props.wrapperProps || {})}
  92. >
  93. {this.props.children}
  94. </DynamicSpanComponent>
  95. </Tooltip>
  96. );
  97. }
  98. }
  99. const DynamicSpanComponent = styled.span``;