Skip to content

Commit

Permalink
Components: add reset timeout to ColorPicker's copy functionality. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored Sep 14, 2021
1 parent 5710513 commit 7f13c37
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/components/src/ui/color-picker/color-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import colorize, { ColorFormats } from 'tinycolor2';
* WordPress dependencies
*/
import { useCopyToClipboard } from '@wordpress/compose';
import { useState } from '@wordpress/element';
import { useState, useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -114,6 +114,7 @@ export const ColorDisplay = ( {
enableAlpha,
}: ColorDisplayProps ) => {
const [ copiedColor, setCopiedColor ] = useState< string | null >( null );
const copyTimer = useRef< number | undefined >();
const props = { color, enableAlpha };
const Component = getComponent( colorType );
const copyRef = useCopyToClipboard< HTMLDivElement >(
Expand All @@ -134,8 +135,25 @@ export const ColorDisplay = ( {
}
}
},
() => setCopiedColor( colorize( color ).toHex8String() )
() => {
if ( copyTimer.current ) {
clearTimeout( copyTimer.current );
}
setCopiedColor( colorize( color ).toHex8String() );
copyTimer.current = setTimeout( () => {
setCopiedColor( null );
copyTimer.current = undefined;
}, 3000 );
}
);
useEffect( () => {
// clear copyTimer on component unmount.
return () => {
if ( copyTimer.current ) {
clearTimeout( copyTimer.current );
}
};
}, [] );
return (
<Tooltip
content={
Expand Down

0 comments on commit 7f13c37

Please sign in to comment.