-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathlayout-child.js
194 lines (178 loc) · 4.99 KB
/
layout-child.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
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../store';
import { useStyleOverride } from './utils';
import { useLayout } from '../components/block-list/layout';
import { GridVisualizer, GridItemResizer } from '../components/grid-visualizer';
function useBlockPropsChildLayoutStyles( { style } ) {
const shouldRenderChildLayoutStyles = useSelect( ( select ) => {
return ! select( blockEditorStore ).getSettings().disableLayoutStyles;
} );
const layout = style?.layout ?? {};
const {
selfStretch,
flexSize,
columnStart,
rowStart,
columnSpan,
rowSpan,
} = layout;
const parentLayout = useLayout() || {};
const { columnCount, minimumColumnWidth } = parentLayout;
const id = useInstanceId( useBlockPropsChildLayoutStyles );
const selector = `.wp-container-content-${ id }`;
let css = '';
if ( shouldRenderChildLayoutStyles ) {
if ( selfStretch === 'fixed' && flexSize ) {
css = `${ selector } {
flex-basis: ${ flexSize };
box-sizing: border-box;
}`;
} else if ( selfStretch === 'fill' ) {
css = `${ selector } {
flex-grow: 1;
}`;
} else if ( columnStart && columnSpan ) {
css = `${ selector } {
grid-column: ${ columnStart } / span ${ columnSpan };
}`;
} else if ( columnStart ) {
css = `${ selector } {
grid-column: ${ columnStart };
}`;
} else if ( columnSpan ) {
css = `${ selector } {
grid-column: span ${ columnSpan };
}`;
}
/**
* If minimumColumnWidth is set on the parent, or if no
* columnCount is set, the grid is responsive so a
* container query is needed for the span to resize.
*/
if (
( columnSpan || columnStart ) &&
( minimumColumnWidth || ! columnCount )
) {
// Check if columnSpan and columnStart are numbers so Math.max doesn't break.
const columnSpanNumber = columnSpan ? parseInt( columnSpan ) : null;
const columnStartNumber = columnStart
? parseInt( columnStart )
: null;
const highestNumber = Math.max(
columnSpanNumber,
columnStartNumber
);
let parentColumnValue = parseFloat( minimumColumnWidth );
/**
* 12rem is the default minimumColumnWidth value.
* If parentColumnValue is not a number, default to 12.
*/
if ( isNaN( parentColumnValue ) ) {
parentColumnValue = 12;
}
let parentColumnUnit = minimumColumnWidth?.replace(
parentColumnValue,
''
);
/**
* Check that parent column unit is either 'px', 'rem' or 'em'.
* If not, default to 'rem'.
*/
if ( ! [ 'px', 'rem', 'em' ].includes( parentColumnUnit ) ) {
parentColumnUnit = 'rem';
}
const defaultGapValue = parentColumnUnit === 'px' ? 24 : 1.5;
const containerQueryValue =
highestNumber * parentColumnValue +
( highestNumber - 1 ) * defaultGapValue;
// If a span is set we want to preserve it as long as possible, otherwise we just reset the value.
const gridColumnValue = columnSpan ? '1/-1' : 'auto';
css += `@container (max-width: ${ containerQueryValue }${ parentColumnUnit }) {
${ selector } {
grid-column: ${ gridColumnValue };
}
}`;
}
if ( rowStart && rowSpan ) {
css += `${ selector } {
grid-row: ${ rowStart } / span ${ rowSpan };
}`;
} else if ( rowStart ) {
css += `${ selector } {
grid-row: ${ rowStart };
}`;
} else if ( rowSpan ) {
css += `${ selector } {
grid-row: span ${ rowSpan };
}`;
}
}
useStyleOverride( { css } );
// Only attach a container class if there is generated CSS to be attached.
if ( ! css ) {
return;
}
// Attach a `wp-container-content` id-based classname.
return { className: `wp-container-content-${ id }` };
}
function ChildLayoutControlsPure( { clientId, style, setAttributes } ) {
const {
type: parentLayoutType = 'default',
allowSizingOnChildren = false,
} = useLayout() || {};
const rootClientId = useSelect(
( select ) => {
return select( blockEditorStore ).getBlockRootClientId( clientId );
},
[ clientId ]
);
// Use useState() instead of useRef() so that GridItemResizer updates when ref is set.
const [ resizerBounds, setResizerBounds ] = useState();
if ( parentLayoutType !== 'grid' ) {
return null;
}
return (
<>
<GridVisualizer
clientId={ rootClientId }
contentRef={ setResizerBounds }
/>
{ allowSizingOnChildren && (
<GridItemResizer
clientId={ clientId }
// Don't allow resizing beyond the grid visualizer.
bounds={ resizerBounds }
onChange={ ( { columnSpan, rowSpan } ) => {
setAttributes( {
style: {
...style,
layout: {
...style?.layout,
columnSpan,
rowSpan,
},
},
} );
} }
/>
) }
</>
);
}
export default {
useBlockProps: useBlockPropsChildLayoutStyles,
edit: ChildLayoutControlsPure,
attributeKeys: [ 'style' ],
isMatch: ( { style } ) => !! style?.layout,
hasSupport() {
return true;
},
};