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.

clipboard.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import classNames from 'classnames';
  21. import Clipboard from 'clipboard';
  22. import React from 'react';
  23. import { INTERACTIVE_TOOLTIP_DELAY } from '../helpers/constants';
  24. import { translate } from '../helpers/l10n';
  25. import { ButtonSecondary } from './buttons';
  26. import CopyIcon from './icons/CopyIcon';
  27. import { IconProps } from './icons/Icon';
  28. import { DiscreetInteractiveIcon, InteractiveIcon, InteractiveIconSize } from './InteractiveIcon';
  29. import Tooltip from './Tooltip';
  30. const COPY_SUCCESS_NOTIFICATION_LIFESPAN = 1000;
  31. export interface State {
  32. copySuccess: boolean;
  33. }
  34. interface RenderProps {
  35. copySuccess: boolean;
  36. setCopyButton: (node: HTMLElement | null) => void;
  37. }
  38. interface BaseProps {
  39. children: (props: RenderProps) => React.ReactNode;
  40. }
  41. export class ClipboardBase extends React.PureComponent<BaseProps, State> {
  42. private clipboard?: Clipboard;
  43. private copyButton?: HTMLElement | null;
  44. mounted = false;
  45. state: State = { copySuccess: false };
  46. componentDidMount() {
  47. this.mounted = true;
  48. if (this.copyButton) {
  49. this.clipboard = new Clipboard(this.copyButton);
  50. this.clipboard.on('success', this.handleSuccessCopy);
  51. }
  52. }
  53. componentDidUpdate() {
  54. if (this.clipboard) {
  55. this.clipboard.destroy();
  56. }
  57. if (this.copyButton) {
  58. this.clipboard = new Clipboard(this.copyButton);
  59. this.clipboard.on('success', this.handleSuccessCopy);
  60. }
  61. }
  62. componentWillUnmount() {
  63. this.mounted = false;
  64. if (this.clipboard) {
  65. this.clipboard.destroy();
  66. }
  67. }
  68. setCopyButton = (node: HTMLElement | null) => {
  69. this.copyButton = node;
  70. };
  71. handleSuccessCopy = () => {
  72. if (this.mounted) {
  73. this.setState({ copySuccess: true });
  74. setTimeout(() => {
  75. if (this.mounted) {
  76. this.setState({ copySuccess: false });
  77. }
  78. }, COPY_SUCCESS_NOTIFICATION_LIFESPAN);
  79. }
  80. };
  81. render() {
  82. return this.props.children({
  83. setCopyButton: this.setCopyButton,
  84. copySuccess: this.state.copySuccess,
  85. });
  86. }
  87. }
  88. interface ButtonProps {
  89. children?: React.ReactNode;
  90. className?: string;
  91. copyValue: string;
  92. icon?: React.ReactNode;
  93. }
  94. export function ClipboardButton({
  95. icon = <CopyIcon />,
  96. className,
  97. children,
  98. copyValue,
  99. }: ButtonProps) {
  100. return (
  101. <ClipboardBase>
  102. {({ setCopyButton, copySuccess }) => (
  103. <Tooltip overlay={translate('copied_action')} visible={copySuccess}>
  104. <ButtonSecondary
  105. className={classNames('sw-select-none', className)}
  106. data-clipboard-text={copyValue}
  107. icon={icon}
  108. innerRef={setCopyButton}
  109. >
  110. {children || translate('copy')}
  111. </ButtonSecondary>
  112. </Tooltip>
  113. )}
  114. </ClipboardBase>
  115. );
  116. }
  117. interface IconButtonProps {
  118. Icon?: React.ComponentType<IconProps>;
  119. 'aria-label'?: string;
  120. className?: string;
  121. copyValue: string;
  122. discreet?: boolean;
  123. size?: InteractiveIconSize;
  124. }
  125. export function ClipboardIconButton(props: IconButtonProps) {
  126. const { className, copyValue, discreet, size = 'small', Icon = CopyIcon } = props;
  127. const InteractiveIconComponent = discreet ? DiscreetInteractiveIcon : InteractiveIcon;
  128. return (
  129. <ClipboardBase>
  130. {({ setCopyButton, copySuccess }) => {
  131. return (
  132. <Tooltip
  133. mouseEnterDelay={INTERACTIVE_TOOLTIP_DELAY}
  134. overlay={
  135. <div className="sw-w-abs-150 sw-text-center">
  136. {translate(copySuccess ? 'copied_action' : 'copy_to_clipboard')}
  137. </div>
  138. }
  139. {...(copySuccess ? { visible: copySuccess } : undefined)}
  140. >
  141. <InteractiveIconComponent
  142. Icon={Icon}
  143. aria-label={props['aria-label'] ?? translate('copy_to_clipboard')}
  144. className={className}
  145. data-clipboard-text={copyValue}
  146. innerRef={setCopyButton}
  147. size={size}
  148. />
  149. </Tooltip>
  150. );
  151. }}
  152. </ClipboardBase>
  153. );
  154. }