From f9d4e492c559473381f3fdcc2552f625098219e0 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 28 Nov 2024 20:30:46 +0400 Subject: [PATCH] Extract copy button --- .../post-publish-panel/postpublish.js | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/packages/editor/src/components/post-publish-panel/postpublish.js b/packages/editor/src/components/post-publish-panel/postpublish.js index d8f8b8f9a56c11..98afb4d1d573e7 100644 --- a/packages/editor/src/components/post-publish-panel/postpublish.js +++ b/packages/editor/src/components/post-publish-panel/postpublish.js @@ -41,6 +41,34 @@ const getFuturePostUrl = ( post ) => { return post.permalink_template; }; +function CopyButton( { text } ) { + const [ showCopyConfirmation, setShowCopyConfirmation ] = useState( false ); + const timeoutIdRef = useRef(); + const ref = useCopyToClipboard( text, () => { + setShowCopyConfirmation( true ); + if ( timeoutIdRef.current ) { + clearTimeout( timeoutIdRef.current ); + } + timeoutIdRef.current = setTimeout( () => { + setShowCopyConfirmation( false ); + }, 4000 ); + } ); + + useEffect( () => { + return () => { + if ( timeoutIdRef.current ) { + clearTimeout( timeoutIdRef.current ); + } + }; + }, [] ); + + return ( + + ); +} + export default function PostPublishPanelPostpublish( { focusOnMount, children, @@ -59,16 +87,6 @@ export default function PostPublishPanelPostpublish( { isScheduled: isCurrentPostScheduled(), }; }, [] ); - const [ showCopyConfirmation, setShowCopyConfirmation ] = useState( false ); - const timeoutIdRef = useRef(); - - useEffect( () => { - return () => { - if ( timeoutIdRef.current ) { - clearTimeout( timeoutIdRef.current ); - } - }; - }, [] ); const postLabel = postType?.labels?.singular_name; const viewPostLabel = postType?.labels?.view_item; @@ -79,15 +97,6 @@ export default function PostPublishPanelPostpublish( { post_type: post.type, } ); - const copyRef = useCopyToClipboard( link, () => { - setShowCopyConfirmation( true ); - if ( timeoutIdRef.current ) { - clearTimeout( timeoutIdRef.current ); - } - timeoutIdRef.current = setTimeout( () => { - setShowCopyConfirmation( false ); - }, 4000 ); - } ); const postLinkRef = useCallback( ( node ) => { if ( focusOnMount && node ) { @@ -134,15 +143,7 @@ export default function PostPublishPanelPostpublish( { />
- +