summaryrefslogtreecommitdiffhomepage
path: root/client/web/src/ui/quick-copy.tsx
blob: 0c51f820ccf333c1b6d64ed84fcf1c9da58fa936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

import cx from "classnames"
import React, { useEffect, useRef, useState } from "react"
import useToaster from "src/hooks/toaster"
import { copyText } from "src/utils/clipboard"

type Props = {
  className?: string
  hideAffordance?: boolean
  /**
   * primaryActionSubject is the subject of the toast confirmation message
   * "Copied <subject> to clipboard"
   */
  primaryActionSubject: string
  primaryActionValue: string
  secondaryActionName?: string
  secondaryActionValue?: string
  /**
   * secondaryActionSubject is the subject of the toast confirmation message
   * prompted by the secondary action "Copied <subject> to clipboard"
   */
  secondaryActionSubject?: string
  children?: React.ReactNode

  /**
   * onSecondaryAction is used to trigger events when the secondary copy
   * function is used. It is not used when the secondary action is hidden.
   */
  onSecondaryAction?: () => void
}

/**
 * QuickCopy is a UI component that allows for copying textual content in one click.
 */
export default function QuickCopy(props: Props) {
  const {
    className,
    hideAffordance,
    primaryActionSubject,
    primaryActionValue,
    secondaryActionValue,
    secondaryActionName,
    secondaryActionSubject,
    onSecondaryAction,
    children,
  } = props
  const toaster = useToaster()
  const containerRef = useRef<HTMLDivElement>(null)
  const buttonRef = useRef<HTMLDivElement>(null)
  const [showButton, setShowButton] = useState(false)

  useEffect(() => {
    if (!showButton) {
      return
    }
    if (!containerRef.current || !buttonRef.current) {
      return
    }
    // We don't need to watch any `resize` event because it's pretty unlikely
    // the browser will resize while their cursor is over one of these items.
    const rect = containerRef.current.getBoundingClientRect()
    const maximumPossibleWidth = window.innerWidth - rect.left + 4

    // We add the border-width (1px * 2 sides) and the padding (0.5rem * 2 sides)
    // and add 1px for rounding up the calculation in order to get the final
    // maxWidth value. This should be kept in sync with the CSS classes below.
    buttonRef.current.style.maxWidth = `${maximumPossibleWidth}px`
    buttonRef.current.style.visibility = "visible"
  }, [showButton])

  const handlePrimaryAction = () => {
    copyText(primaryActionValue)
    toaster.show({
      message: `Copied ${primaryActionSubject} to the clipboard`,
    })
  }

  const handleSecondaryAction = () => {
    if (!secondaryActionValue) {
      return
    }
    copyText(secondaryActionValue)
    toaster.show({
      message: `Copied ${
        secondaryActionSubject || secondaryActionName
      } to the clipboard`,
    })
    onSecondaryAction?.()
  }

  return (
    <div
      className="flex relative min-w-0"
      ref={containerRef}
      // Since the affordance is a child of this element, we assign both event
      // handlers here.
      onMouseLeave={() => setShowButton(false)}
    >
      <div
        onMouseEnter={() => setShowButton(true)}
        className={cx("truncate", className)}
      >
        {children}
      </div>
      {!hideAffordance && (
        <button
          onMouseEnter={() => setShowButton(true)}
          onClick={handlePrimaryAction}
          className={cx("cursor-pointer text-blue-500", { "ml-2": children })}
        >
          Copy
        </button>
      )}

      {showButton && (
        <div
          className="absolute -mt-1 -ml-2 -top-px -left-px
          shadow-md cursor-pointer rounded-md active:shadow-sm
          transition-shadow duration-100 ease-in-out z-50"
          style={{ visibility: "hidden" }}
          ref={buttonRef}
        >
          <div className="flex border rounded-md button-outline bg-white">
            <div
              className={cx("flex min-w-0 py-1 px-2 hover:bg-gray-0", {
                "rounded-md": !secondaryActionValue,
                "rounded-l-md": secondaryActionValue,
              })}
              onClick={handlePrimaryAction}
            >
              <span
                className={cx(className, "inline-block select-none truncate")}
              >
                {children}
              </span>
              <button
                className={cx("cursor-pointer text-blue-500", {
                  "ml-2": children,
                })}
              >
                Copy
              </button>
            </div>

            {secondaryActionValue && (
              <div
                className="text-blue-500 py-1 px-2 border-l hover:bg-gray-100 rounded-r-md"
                onClick={handleSecondaryAction}
              >
                {secondaryActionName}
              </div>
            )}
          </div>
        </div>
      )}
    </div>
  )
}