-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
86 lines (80 loc) · 2.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useResizeObserver } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Icon from '../icon';
/**
* Renders a placeholder. Normally used by blocks to render their empty state.
*
* @param {Object} props The component props.
* @param {WPIcon} props.icon An icon rendered before the label.
* @param {WPElement} props.children Children to be rendered.
* @param {string} props.label Title of the placeholder.
* @param {string} props.instructions Instructions of the placeholder.
* @param {string} props.className Class to set on the container div.
* @param {Object} props.notices A rendered notices list.
* @param {Object} props.preview Preview to be rendered in the placeholder.
* @param {boolean} props.isColumnLayout Whether a column layout should be used.
*
* @return {Object} The rendered placeholder.
*/
function Placeholder( {
icon,
children,
label,
instructions,
className,
notices,
preview,
isColumnLayout,
...additionalProps
} ) {
const [ resizeListener, { width } ] = useResizeObserver();
// Since `useResizeObserver` will report a width of `null` until after the
// first render, avoid applying any modifier classes until width is known.
let modifierClassNames;
if ( typeof width === 'number' ) {
modifierClassNames = {
'is-large': width >= 480,
'is-medium': width >= 160 && width < 480,
'is-small': width < 160,
};
}
const classes = classnames(
'components-placeholder',
className,
modifierClassNames
);
const fieldsetClasses = classnames( 'components-placeholder__fieldset', {
'is-column-layout': isColumnLayout,
} );
return (
<div { ...additionalProps } className={ classes }>
{ resizeListener }
{ notices }
{ preview && (
<div className="components-placeholder__preview">
{ preview }
</div>
) }
<div className="components-placeholder__label">
<Icon icon={ icon } />
{ label }
</div>
{ !! instructions && (
<div className="components-placeholder__instructions">
{ instructions }
</div>
) }
<div className={ fieldsetClasses }>{ children }</div>
</div>
);
}
export default Placeholder;