Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/src/packages/frontend/components/copy-to-clipboard.tsx
Views: 923
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { CopyOutlined } from "@ant-design/icons";6import { Button, Input, Space, Tooltip } from "antd";7import { ReactNode, useEffect, useMemo, useState } from "react";8import { CopyToClipboard } from "react-copy-to-clipboard";910import { CSS } from "@cocalc/frontend/app-framework";1112interface Props {13value: string;14display?: string;15style?: CSS;16label?: ReactNode;17labelStyle?: CSS;18inputStyle?: CSS;19outerStyle?: CSS;20inputWidth?: string;21size?: "large" | "middle" | "small";22before?: boolean;23copyTip?: string;24}2526const INPUT_STYLE: CSS = { display: "inline-block", flex: 1 } as const;2728const LABEL_STYLE: CSS = {29marginRight: "15px",30display: "flex",31flex: "0 0 auto",32justifyContent: "center",33alignContent: "center",34flexDirection: "column",35} as const;3637export default function CopyToClipBoard({38value,39display,40style,41size,42label,43labelStyle,44inputStyle,45outerStyle,46inputWidth,47copyTip,48before,49}: Props) {50const [copied, setCopied] = useState<boolean>(false);5152useEffect(() => {53setCopied(false);54}, [value]);5556let copy = useMemo(() => {57const btn = (58<CopyToClipboard text={value} onCopy={() => setCopied(true)}>59<Button size={size} icon={<CopyOutlined />} />60</CopyToClipboard>61);62if (!copied) return btn;63return (64<Tooltip title={copyTip ?? "Copied!"} defaultOpen>65{btn}66</Tooltip>67);68}, [value, copied, size]);6970// ws: See https://ant.design/components/input for why using Input.Group is the71// right way to do this.72// hsy: Input.Group is deprecated, using Space.Compact instead73const input = (74<Space.Compact style={outerStyle}>75{before ? copy : undefined}76<Input77style={{78width: inputWidth ?? `${(display ?? value).length + 8}ex`,79fontFamily: "monospace",80...inputStyle,81}}82readOnly83size={size}84value={display ?? value}85onFocus={(e) => e.target.select()}86/>87{!before ? copy : undefined}88</Space.Compact>89);90if (!label) {91return <div style={style}>{input}</div>;92}93return (94<div style={{ display: "flex", ...style }}>95<div style={{ ...LABEL_STYLE, ...labelStyle }}>{label}</div>{" "}96<div style={{ ...INPUT_STYLE }}>{input}</div>97</div>98);99}100101102