-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
constrained.js
335 lines (316 loc) · 8.78 KB
/
constrained.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* WordPress dependencies
*/
import {
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon,
__experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import {
Icon,
alignNone,
stretchWide,
justifyLeft,
justifyCenter,
justifyRight,
} from '@wordpress/icons';
import { getCSSRules } from '@wordpress/style-engine';
/**
* Internal dependencies
*/
import { useSettings } from '../components/use-settings';
import { appendSelectors, getBlockGapCSS, getAlignmentsInfo } from './utils';
import { getGapCSSValue } from '../hooks/gap';
import { BlockControls, JustifyContentControl } from '../components';
import { shouldSkipSerialization } from '../hooks/utils';
import { LAYOUT_DEFINITIONS } from './definitions';
export default {
name: 'constrained',
label: __( 'Constrained' ),
inspectorControls: function DefaultLayoutInspectorControls( {
layout,
onChange,
layoutBlockSupport = {},
} ) {
const { wideSize, contentSize, justifyContent = 'center' } = layout;
const {
allowJustification = true,
allowCustomContentAndWideSize = true,
} = layoutBlockSupport;
const onJustificationChange = ( value ) => {
onChange( {
...layout,
justifyContent: value,
} );
};
const justificationOptions = [
{
value: 'left',
icon: justifyLeft,
label: __( 'Justify items left' ),
},
{
value: 'center',
icon: justifyCenter,
label: __( 'Justify items center' ),
},
{
value: 'right',
icon: justifyRight,
label: __( 'Justify items right' ),
},
];
const [ availableUnits ] = useSettings( 'spacing.units' );
const units = useCustomUnits( {
availableUnits: availableUnits || [ '%', 'px', 'em', 'rem', 'vw' ],
} );
return (
<VStack
spacing={ 4 }
className="block-editor-hooks__layout-constrained"
>
{ allowCustomContentAndWideSize && (
<>
<UnitControl
__next40pxDefaultSize
label={ __( 'Content width' ) }
labelPosition="top"
value={ contentSize || wideSize || '' }
onChange={ ( nextWidth ) => {
nextWidth =
0 > parseFloat( nextWidth )
? '0'
: nextWidth;
onChange( {
...layout,
contentSize: nextWidth,
} );
} }
units={ units }
prefix={
<InputControlPrefixWrapper variant="icon">
<Icon icon={ alignNone } />
</InputControlPrefixWrapper>
}
/>
<UnitControl
__next40pxDefaultSize
label={ __( 'Wide width' ) }
labelPosition="top"
value={ wideSize || contentSize || '' }
onChange={ ( nextWidth ) => {
nextWidth =
0 > parseFloat( nextWidth )
? '0'
: nextWidth;
onChange( {
...layout,
wideSize: nextWidth,
} );
} }
units={ units }
prefix={
<InputControlPrefixWrapper variant="icon">
<Icon icon={ stretchWide } />
</InputControlPrefixWrapper>
}
/>
<p className="block-editor-hooks__layout-constrained-helptext">
{ __(
'Customize the width for all elements that are assigned to the center or wide columns.'
) }
</p>
</>
) }
{ allowJustification && (
<ToggleGroupControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Justification' ) }
value={ justifyContent }
onChange={ onJustificationChange }
>
{ justificationOptions.map(
( { value, icon, label } ) => {
return (
<ToggleGroupControlOptionIcon
key={ value }
value={ value }
icon={ icon }
label={ label }
/>
);
}
) }
</ToggleGroupControl>
) }
</VStack>
);
},
toolBarControls: function DefaultLayoutToolbarControls( {
layout = {},
onChange,
layoutBlockSupport,
} ) {
const { allowJustification = true } = layoutBlockSupport;
if ( ! allowJustification ) {
return null;
}
return (
<BlockControls group="block" __experimentalShareWithChildBlocks>
<DefaultLayoutJustifyContentControl
layout={ layout }
onChange={ onChange }
/>
</BlockControls>
);
},
getLayoutStyle: function getLayoutStyle( {
selector,
layout = {},
style,
blockName,
hasBlockGapSupport,
layoutDefinitions = LAYOUT_DEFINITIONS,
} ) {
const { contentSize, wideSize, justifyContent } = layout;
const blockGapStyleValue = getGapCSSValue( style?.spacing?.blockGap );
// If a block's block.json skips serialization for spacing or
// spacing.blockGap, don't apply the user-defined value to the styles.
let blockGapValue = '';
if ( ! shouldSkipSerialization( blockName, 'spacing', 'blockGap' ) ) {
// If an object is provided only use the 'top' value for this kind of gap.
if ( blockGapStyleValue?.top ) {
blockGapValue = getGapCSSValue( blockGapStyleValue?.top );
} else if ( typeof blockGapStyleValue === 'string' ) {
blockGapValue = getGapCSSValue( blockGapStyleValue );
}
}
const marginLeft =
justifyContent === 'left' ? '0 !important' : 'auto !important';
const marginRight =
justifyContent === 'right' ? '0 !important' : 'auto !important';
let output =
!! contentSize || !! wideSize
? `
${ appendSelectors(
selector,
'> :where(:not(.alignleft):not(.alignright):not(.alignfull))'
) } {
max-width: ${ contentSize ?? wideSize };
margin-left: ${ marginLeft };
margin-right: ${ marginRight };
}
${ appendSelectors( selector, '> .alignwide' ) } {
max-width: ${ wideSize ?? contentSize };
}
${ appendSelectors( selector, '> .alignfull' ) } {
max-width: none;
}
`
: '';
if ( justifyContent === 'left' ) {
output += `${ appendSelectors(
selector,
'> :where(:not(.alignleft):not(.alignright):not(.alignfull))'
) }
{ margin-left: ${ marginLeft }; }`;
} else if ( justifyContent === 'right' ) {
output += `${ appendSelectors(
selector,
'> :where(:not(.alignleft):not(.alignright):not(.alignfull))'
) }
{ margin-right: ${ marginRight }; }`;
}
// If there is custom padding, add negative margins for alignfull blocks.
if ( style?.spacing?.padding ) {
// The style object might be storing a preset so we need to make sure we get a usable value.
const paddingValues = getCSSRules( style );
paddingValues.forEach( ( rule ) => {
if ( rule.key === 'paddingRight' ) {
// Add unit if 0, to avoid calc(0 * -1) which is invalid.
const paddingRightValue =
rule.value === '0' ? '0px' : rule.value;
output += `
${ appendSelectors( selector, '> .alignfull' ) } {
margin-right: calc(${ paddingRightValue } * -1);
}
`;
} else if ( rule.key === 'paddingLeft' ) {
// Add unit if 0, to avoid calc(0 * -1) which is invalid.
const paddingLeftValue =
rule.value === '0' ? '0px' : rule.value;
output += `
${ appendSelectors( selector, '> .alignfull' ) } {
margin-left: calc(${ paddingLeftValue } * -1);
}
`;
}
} );
}
// Output blockGap styles based on rules contained in layout definitions in theme.json.
if ( hasBlockGapSupport && blockGapValue ) {
output += getBlockGapCSS(
selector,
layoutDefinitions,
'constrained',
blockGapValue
);
}
return output;
},
getOrientation() {
return 'vertical';
},
getAlignments( layout ) {
const alignmentInfo = getAlignmentsInfo( layout );
if ( layout.alignments !== undefined ) {
if ( ! layout.alignments.includes( 'none' ) ) {
layout.alignments.unshift( 'none' );
}
return layout.alignments.map( ( alignment ) => ( {
name: alignment,
info: alignmentInfo[ alignment ],
} ) );
}
const { contentSize, wideSize } = layout;
const alignments = [
{ name: 'left' },
{ name: 'center' },
{ name: 'right' },
];
if ( contentSize ) {
alignments.unshift( { name: 'full' } );
}
if ( wideSize ) {
alignments.unshift( { name: 'wide', info: alignmentInfo.wide } );
}
alignments.unshift( { name: 'none', info: alignmentInfo.none } );
return alignments;
},
};
const POPOVER_PROPS = {
placement: 'bottom-start',
};
function DefaultLayoutJustifyContentControl( { layout, onChange } ) {
const { justifyContent = 'center' } = layout;
const onJustificationChange = ( value ) => {
onChange( {
...layout,
justifyContent: value,
} );
};
const allowedControls = [ 'left', 'center', 'right' ];
return (
<JustifyContentControl
allowedControls={ allowedControls }
value={ justifyContent }
onChange={ onJustificationChange }
popoverProps={ POPOVER_PROPS }
/>
);
}