Skip to content

Commit 6aa9944

Browse files
authored
Components: Apply width-based modifier classes to Placeholder only when width is known (#19825)
* Components: Apply `is-small` modifier class to Placeholder only when width known * E2E Tests: Wait for placeholder error display in embed tests * Testing: Update snapshots for Placeholder class assignment
1 parent 539b1fc commit 6aa9944

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

packages/block-library/src/embed/test/__snapshots__/index.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`core/embed block edit matches snapshot 1`] = `
44
<div
5-
class="components-placeholder is-small wp-block-embed"
5+
class="components-placeholder wp-block-embed"
66
>
77
<iframe
88
aria-hidden="true"

packages/components/src/placeholder/index.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ function Placeholder( {
2727
...additionalProps
2828
} ) {
2929
const [ resizeListener, { width } ] = useResizeAware();
30-
const classes = classnames(
31-
'components-placeholder',
32-
width >= 320 ? 'is-large' : '',
33-
width >= 160 && width < 320 ? 'is-medium' : '',
34-
width < 160 ? 'is-small' : '',
35-
className
36-
);
30+
31+
// Since `useResizeAware` will report a width of `null` until after the
32+
// first render, avoid applying any modifier classes until width is known.
33+
let modifierClassNames;
34+
if ( typeof width === 'number' ) {
35+
modifierClassNames = {
36+
'is-large': width >= 320,
37+
'is-medium': width >= 160 && width < 320,
38+
'is-small': width < 160,
39+
};
40+
}
41+
42+
const classes = classnames( 'components-placeholder', className, modifierClassNames );
3743
const fieldsetClasses = classnames( 'components-placeholder__fieldset', {
3844
'is-column-layout': isColumnLayout,
3945
} );

packages/components/src/placeholder/test/index.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
jest.mock( 'react-resize-aware' );
21
/**
32
* External dependencies
43
*/
@@ -10,8 +9,12 @@ import useResizeAware from 'react-resize-aware';
109
*/
1110
import Placeholder from '../';
1211

12+
jest.mock( 'react-resize-aware' );
13+
1314
describe( 'Placeholder', () => {
14-
useResizeAware.mockReturnValue( [ <div key="1" />, { width: 320 } ] );
15+
beforeEach( () => {
16+
useResizeAware.mockReturnValue( [ <div key="1" />, { width: 320 } ] );
17+
} );
1518

1619
describe( 'basic rendering', () => {
1720
it( 'should by default render label section and fieldset.', () => {
@@ -77,4 +80,26 @@ describe( 'Placeholder', () => {
7780
expect( placeholder.prop( 'test' ) ).toBe( 'test' );
7881
} );
7982
} );
83+
84+
describe( 'resize aware', () => {
85+
it( 'should not assign modifier class in first-pass `null` width from `useResizeAware`', () => {
86+
useResizeAware.mockReturnValue( [ <div key="1" />, { width: 320 } ] );
87+
88+
const placeholder = shallow( <Placeholder /> );
89+
90+
expect( placeholder.hasClass( 'is-large' ) ).toBe( true );
91+
expect( placeholder.hasClass( 'is-medium' ) ).toBe( false );
92+
expect( placeholder.hasClass( 'is-small' ) ).toBe( false );
93+
} );
94+
95+
it( 'should assign modifier class', () => {
96+
useResizeAware.mockReturnValue( [ <div key="1" />, { width: null } ] );
97+
98+
const placeholder = shallow( <Placeholder /> );
99+
100+
expect( placeholder.hasClass( 'is-large' ) ).toBe( false );
101+
expect( placeholder.hasClass( 'is-medium' ) ).toBe( false );
102+
expect( placeholder.hasClass( 'is-small' ) ).toBe( false );
103+
} );
104+
} );
80105
} );

packages/e2e-tests/specs/editor/various/embedding.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ describe( 'Embedding content', () => {
210210
await page.keyboard.type( 'https://twitter.com/wooyaygutenberg123454312' );
211211
await page.keyboard.press( 'Enter' );
212212

213+
// Wait for the request to fail and present an error.
214+
await page.waitForSelector( '.components-placeholder__error' );
215+
213216
await clickButton( 'Convert to link' );
214217
expect( await getEditedPostContent() ).toMatchSnapshot();
215218
} );
@@ -232,6 +235,10 @@ describe( 'Embedding content', () => {
232235
await page.keyboard.press( 'Enter' );
233236
await page.keyboard.type( 'https://twitter.com/wooyaygutenberg123454312' );
234237
await page.keyboard.press( 'Enter' );
238+
239+
// Wait for the request to fail and present an error.
240+
await page.waitForSelector( '.components-placeholder__error' );
241+
235242
// Set up a different mock to make sure that try again actually does make the request again.
236243
await setUpResponseMocking( [
237244
{

storybook/test/__snapshots__/index.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -4062,7 +4062,7 @@ exports[`Storyshots Components/Panel With Icon 1`] = `
40624062

40634063
exports[`Storyshots Components/Placeholder Default 1`] = `
40644064
<div
4065-
className="components-placeholder is-small"
4065+
className="components-placeholder"
40664066
>
40674067
<iframe
40684068
aria-hidden={true}

0 commit comments

Comments
 (0)