Skip to content

Commit

Permalink
fix: check current text in promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov committed Sep 12, 2024
1 parent 8afb15d commit cc4705e
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/components/CopyToClipboard/CopyToClipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const INITIAL_STATUS: CopyToClipboardStatus = 'pending';
export function CopyToClipboard(props: CopyToClipboardProps) {
const {children, text, options, timeout, nativeCopy, onCopy} = props;

const textRef = React.useRef(text);
const [status, setStatus] = React.useState<CopyToClipboardStatus>(INITIAL_STATUS);

const timerIdRef = React.useRef<number>();
Expand All @@ -34,14 +35,28 @@ export function CopyToClipboard(props: CopyToClipboardProps) {

const onClickWithCopy: React.MouseEventHandler<HTMLElement> = React.useCallback(
(event) => {
textRef.current = text;

copyText(text).then(
() => handleCopy(text, true),
() => handleCopy(text, false),
() => {
if (text === textRef.current) {
handleCopy(text, true);

if (typeof content.props?.onClick === 'function') {
content.props.onClick(event);
}
}
},
() => {
if (text === textRef.current) {
handleCopy(text, false);

if (typeof content.props?.onClick === 'function') {
content.props.onClick(event);
}
}
},
);

if (typeof content.props?.onClick === 'function') {
content.props.onClick(event);
}
},
[content.props, handleCopy, text],
);
Expand Down

0 comments on commit cc4705e

Please sign in to comment.