Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components: add reset timeout to ColorPicker's copy functionality. #34601

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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