-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathlayout-controls.js
86 lines (81 loc) · 2.37 KB
/
layout-controls.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 PropTypes from 'prop-types';
import { ReactElement } from 'react';
/**
* WordPress dependencies
*/
import { Notice, SelectControl, TextControl } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
/**
* Layout controls for AMP blocks' attributes: layout, width, height.
*
* @param {Object} props Component props.
* @param {Object} props.attributes Block attributes.
* @param {Function} props.setAttributes Callback to update block attributes.
* @param {Array} props.ampLayoutOptions Layout options.
* @return {ReactElement} Controls.
*/
const LayoutControls = ( { attributes, setAttributes, ampLayoutOptions } ) => {
const { ampLayout, height, width } = attributes;
const showHeightNotice = ! height && ( 'fixed' === ampLayout || 'fixed-height' === ampLayout );
const showWidthNotice = ! width && 'fixed' === ampLayout;
return (
<>
<SelectControl
label={ __( 'Layout', 'amp' ) }
value={ ampLayout }
options={ ampLayoutOptions }
onChange={ ( value ) => ( setAttributes( { ampLayout: value } ) ) }
/>
{ showWidthNotice && (
<Notice status="error" isDismissible={ false }>
{
sprintf(
/* translators: %s is the layout name */
__( 'Width is required for %s layout', 'amp' ),
ampLayout,
)
}
</Notice>
) }
<TextControl
type="number"
label={ __( 'Width (px)', 'amp' ) }
value={ width !== undefined ? width : '' }
onChange={ ( value ) => ( setAttributes( { width: value } ) ) }
/>
{ showHeightNotice && (
<Notice status="error" isDismissible={ false }>
{
sprintf(
/* translators: %s is the layout name */
__( 'Height is required for %s layout', 'amp' ),
ampLayout,
)
}
</Notice>
) }
<TextControl
type="number"
label={ __( 'Height (px)', 'amp' ) }
value={ height }
onChange={ ( value ) => ( setAttributes( { height: value } ) ) }
/>
</>
);
};
LayoutControls.propTypes = {
attributes: PropTypes.shape( {
ampLayout: PropTypes.string,
width: PropTypes.number,
height: PropTypes.number,
} ).isRequired,
setAttributes: PropTypes.func.isRequired,
ampLayoutOptions: PropTypes.arrayOf( PropTypes.shape( {
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
} ) ).isRequired,
};
export default LayoutControls;