diff --git a/packages/block-editor/src/components/link-control/link-preview.js b/packages/block-editor/src/components/link-control/link-preview.js index a8f6b5cff78ffc..7ef2687cbbf68f 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -90,36 +90,43 @@ export default function LinkPreview( { - { ( hasRichData || isFetching ) && ( + { ( ( hasRichData && + ( richData?.image || richData?.description ) ) || + isFetching ) && (
-
- { richData?.image && ( - - ) } -
-
- { richData?.description && ( - - { richData.description } - - ) } -
+ { ( richData?.image || isFetching ) && ( +
+ { richData?.image && ( + + ) } +
+ ) } + + { ( richData?.description || isFetching ) && ( +
+ { richData?.description && ( + + { richData.description } + + ) } +
+ ) }
) } diff --git a/packages/block-editor/src/components/link-control/style.scss b/packages/block-editor/src/components/link-control/style.scss index e6976e47a16c74..90cb978a31380c 100644 --- a/packages/block-editor/src/components/link-control/style.scss +++ b/packages/block-editor/src/components/link-control/style.scss @@ -305,7 +305,6 @@ $preview-image-height: 140px; .block-editor-link-control__search-item-bottom { transition: opacity 1.5s; - min-height: 100px; width: 100%; } diff --git a/packages/block-editor/src/components/link-control/test/index.js b/packages/block-editor/src/components/link-control/test/index.js index 250e4656d3708d..99e2302fa21ae7 100644 --- a/packages/block-editor/src/components/link-control/test/index.js +++ b/packages/block-editor/src/components/link-control/test/index.js @@ -1878,6 +1878,46 @@ describe( 'Rich link previews', () => { expect( isRichLinkPreview ).toBe( true ); } ); + it( 'should not display placeholders for the image and description if neither is available in the data', async () => { + mockFetchRichUrlData.mockImplementation( () => + Promise.resolve( { + title: '', + icon: 'https://s.w.org/favicon.ico?2', + description: '', + image: '', + } ) + ); + + act( () => { + render( + , + container + ); + } ); + + // mockFetchRichUrlData resolves on next "tick" of event loop + await act( async () => { + await eventLoopTick(); + } ); + + const linkPreview = container.querySelector( + "[aria-label='Currently selected']" + ); + + // Todo: refactor to use user-facing queries. + const hasRichImagePreview = linkPreview.querySelector( + '.block-editor-link-control__search-item-image' + ); + + // Todo: refactor to use user-facing queries. + const hasRichDescriptionPreview = linkPreview.querySelector( + '.block-editor-link-control__search-item-description' + ); + + expect( hasRichImagePreview ).toBeFalsy(); + expect( hasRichDescriptionPreview ).toBeFalsy(); + } ); + it( 'should display a fallback when title is missing from rich data', async () => { mockFetchRichUrlData.mockImplementation( () => Promise.resolve( { @@ -1958,7 +1998,7 @@ describe( 'Rich link previews', () => { } ); it.each( [ 'image', 'description' ] )( - 'should display a fallback placeholder when %s it is missing from the rich data', + 'should not display the rich %s when it is missing from the data', async ( dataItem ) => { mockFetchRichUrlData.mockImplementation( () => { const data = { @@ -1995,10 +2035,10 @@ describe( 'Rich link previews', () => { expect( isRichLinkPreview ).toBe( true ); const missingDataItem = linkPreview.querySelector( - `.block-editor-link-control__search-item-${ dataItem }.is-placeholder` + `.block-editor-link-control__search-item-${ dataItem }` ); - expect( missingDataItem ).toBeTruthy(); + expect( missingDataItem ).toBeFalsy(); } );