Skip to content

Commit

Permalink
fix(theme-classic): minor code copy button improvements (#6986)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored Mar 24, 2022
1 parent c3e7ecc commit 21ff25e
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import React, {useState} from 'react';
import React, {useCallback, useState, useRef, useEffect} from 'react';
import clsx from 'clsx';
import copy from 'copy-text-to-clipboard';
import {translate} from '@docusaurus/Translate';
Expand All @@ -15,14 +15,16 @@ import styles from './styles.module.css';

export default function CopyButton({code}: Props): JSX.Element {
const [isCopied, setIsCopied] = useState(false);
const handleCopyCode = () => {
const copyTimeout = useRef<number | undefined>(undefined);
const handleCopyCode = useCallback(() => {
copy(code);
setIsCopied(true);

setTimeout(() => {
copyTimeout.current = window.setTimeout(() => {
setIsCopied(false);
}, 2000);
};
}, 1000);
}, [code]);

useEffect(() => () => window.clearTimeout(copyTimeout.current), []);

return (
<button
Expand Down

0 comments on commit 21ff25e

Please sign in to comment.