addons/isl/src/Copyable.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {Icon} from 'isl-components/Icon';
b69ab319import {Tooltip} from 'isl-components/Tooltip';
b69ab3110import {useEffect, useState} from 'react';
b69ab3111import {T} from './i18n';
b69ab3112import platform from './platform';
b69ab3113
b69ab3114import './Copyable.css';
b69ab3115
b69ab3116/** Click to copy text and show a confirmation tooltip. If content is provided, use that instead of */
b69ab3117export function Copyable({
b69ab3118 children,
b69ab3119 className,
b69ab3120 iconOnly,
b69ab3121}: {
b69ab3122 children: string;
b69ab3123 className?: string;
b69ab3124 iconOnly?: boolean;
b69ab3125}) {
b69ab3126 const [showingSuccess, setShowingSuccess] = useState(false);
b69ab3127 useEffect(() => {
b69ab3128 if (showingSuccess) {
b69ab3129 const timeout = setTimeout(() => setShowingSuccess(false), 1500);
b69ab3130 return () => clearTimeout(timeout);
b69ab3131 }
b69ab3132 }, [showingSuccess, setShowingSuccess]);
b69ab3133
b69ab3134 return (
b69ab3135 <Tooltip
b69ab3136 trigger="manual"
b69ab3137 shouldShow={showingSuccess}
b69ab3138 component={CopiedSuccessTooltipContent(children)}>
b69ab3139 <div
b69ab3140 className={
b69ab3141 'copyable' + (className ? ` ${className}` : '') + (iconOnly === true ? ' icon-only' : '')
b69ab3142 }
b69ab3143 tabIndex={0}
b69ab3144 onClick={e => {
b69ab3145 platform.clipboardCopy(children);
b69ab3146 setShowingSuccess(true);
b69ab3147 e.preventDefault();
b69ab3148 e.stopPropagation();
b69ab3149 }}>
b69ab3150 {iconOnly !== true && children}
b69ab3151 <Icon icon="copy" />
b69ab3152 </div>
b69ab3153 </Tooltip>
b69ab3154 );
b69ab3155}
b69ab3156
b69ab3157function CopiedSuccessTooltipContent(text: string) {
b69ab3158 return () => (
b69ab3159 <span className="copyable-success-tooltip">
b69ab3160 <T replace={{$copiedText: <span className="copyable-success-overflow">{text}</span>}}>
b69ab3161 Copied '$copiedText'.
b69ab3162 </T>
b69ab3163 </span>
b69ab3164 );
b69ab3165}