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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 * as React from 'react';
  23. import { translate } from '../../helpers/l10n';
  24. import CopyIcon from '../icons/CopyIcon';
  25. import { Button, ButtonIcon } from './buttons';
  26. import Tooltip from './Tooltip';
  27. export interface State {
  28. copySuccess: boolean;
  29. }
  30. interface RenderProps {
  31. setCopyButton: (node: HTMLElement | null) => void;
  32. copySuccess: boolean;
  33. role: string;
  34. }
  35. interface Props {
  36. children: (props: RenderProps) => React.ReactNode;
  37. }
  38. export class ClipboardBase extends React.PureComponent<Props, State> {
  39. private clipboard?: Clipboard;
  40. private copyButton?: HTMLElement | null;
  41. mounted = false;
  42. state: State = { copySuccess: false };
  43. componentDidMount() {
  44. this.mounted = true;
  45. if (this.copyButton) {
  46. this.clipboard = new Clipboard(this.copyButton);
  47. this.clipboard.on('success', this.handleSuccessCopy);
  48. }
  49. }
  50. componentDidUpdate() {
  51. if (this.clipboard) {
  52. this.clipboard.destroy();
  53. }
  54. if (this.copyButton) {
  55. this.clipboard = new Clipboard(this.copyButton);
  56. this.clipboard.on('success', this.handleSuccessCopy);
  57. }
  58. }
  59. componentWillUnmount() {
  60. this.mounted = false;
  61. if (this.clipboard) {
  62. this.clipboard.destroy();
  63. }
  64. }
  65. setCopyButton = (node: HTMLElement | null) => {
  66. this.copyButton = node;
  67. };
  68. handleSuccessCopy = () => {
  69. if (this.mounted) {
  70. this.setState({ copySuccess: true });
  71. setTimeout(() => {
  72. if (this.mounted) {
  73. this.setState({ copySuccess: false });
  74. }
  75. }, 1000);
  76. }
  77. };
  78. render() {
  79. return this.props.children({
  80. setCopyButton: this.setCopyButton,
  81. copySuccess: this.state.copySuccess,
  82. role: 'button'
  83. });
  84. }
  85. }
  86. export interface ClipboardButtonProps {
  87. 'aria-label'?: string;
  88. className?: string;
  89. copyValue: string;
  90. children?: React.ReactNode;
  91. }
  92. export function ClipboardButton({
  93. className,
  94. children,
  95. copyValue,
  96. 'aria-label': ariaLabel
  97. }: ClipboardButtonProps) {
  98. return (
  99. <ClipboardBase>
  100. {({ setCopyButton, copySuccess }) => (
  101. <Tooltip overlay={translate('copied_action')} visible={copySuccess}>
  102. <Button
  103. className={classNames('no-select', className)}
  104. data-clipboard-text={copyValue}
  105. innerRef={setCopyButton}
  106. aria-label={ariaLabel ?? translate('copy_to_clipboard')}>
  107. {children || (
  108. <>
  109. <CopyIcon className="little-spacer-right" />
  110. {translate('copy')}
  111. </>
  112. )}
  113. </Button>
  114. </Tooltip>
  115. )}
  116. </ClipboardBase>
  117. );
  118. }
  119. export interface ClipboardIconButtonProps {
  120. 'aria-label'?: string;
  121. className?: string;
  122. copyValue: string;
  123. }
  124. export function ClipboardIconButton(props: ClipboardIconButtonProps) {
  125. const { className, copyValue } = props;
  126. return (
  127. <ClipboardBase>
  128. {({ setCopyButton, copySuccess }) => {
  129. return (
  130. <ButtonIcon
  131. aria-label={props['aria-label'] ?? translate('copy_to_clipboard')}
  132. className={classNames('no-select', className)}
  133. data-clipboard-text={copyValue}
  134. innerRef={setCopyButton}
  135. tooltip={translate(copySuccess ? 'copied_action' : 'copy_to_clipboard')}
  136. tooltipProps={copySuccess ? { visible: copySuccess } : undefined}>
  137. <CopyIcon />
  138. </ButtonIcon>
  139. );
  140. }}
  141. </ClipboardBase>
  142. );
  143. }