Skip to content

Commit 9853127

Browse files
First pass at trying elements panel
1 parent 25f1c24 commit 9853127

9 files changed

Lines changed: 148 additions & 182 deletions

File tree

packages/block-editor/src/components/block-inspector/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ function StyleInspectorSlots( {
6666
/>
6767
<InspectorControls.Slot group="border" label={ borderPanelLabel } />
6868
<InspectorControls.Slot group="styles" />
69+
<InspectorControls.Slot
70+
group="elements"
71+
label={ __( 'Elements' ) }
72+
className="elements-block-support-panel__inner-wrapper"
73+
/>
6974
{ showPositionControls && <PositionControls /> }
7075
{ showBindingsControls && (
7176
<InspectorControls.Slot group="bindings" />

packages/block-editor/src/components/inspector-controls-tabs/styles-tab.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import InspectorControls from '../inspector-controls';
1212
import { useBorderPanelLabel } from '../../hooks/border';
1313
import { useBlockSettings } from '../../hooks/utils';
1414
import { store as blockEditorStore } from '../../store';
15-
import { ColorEdit } from '../../hooks/color';
15+
import { ElementsEdit } from '../../hooks/elements';
1616
import { ColorToolsPanel } from '../global-styles/color-panel';
1717

1818
function SectionBlockColorControls( {
@@ -42,16 +42,14 @@ function SectionBlockColorControls( {
4242
};
4343

4444
return (
45-
<ColorEdit
45+
<ElementsEdit
4646
clientId={ clientId }
4747
name={ blockName }
4848
settings={ settings }
4949
setAttributes={ setAttributes }
5050
asWrapper={ ColorToolsPanel }
51-
label={ __( 'Color' ) }
51+
label={ __( 'Elements' ) }
5252
defaultControls={ {
53-
text: true,
54-
background: true,
5553
button: hasButtons,
5654
heading: hasHeading,
5755
} }
@@ -72,11 +70,22 @@ const StylesTab = ( {
7270
<>
7371
{ hasBlockStyles && <BlockStyles clientId={ clientId } /> }
7472
{ isSectionBlock && (
75-
<SectionBlockColorControls
76-
blockName={ blockName }
77-
clientId={ clientId }
78-
contentClientIds={ contentClientIds }
79-
/>
73+
<>
74+
<SectionBlockColorControls
75+
blockName={ blockName }
76+
clientId={ clientId }
77+
contentClientIds={ contentClientIds }
78+
/>
79+
<InspectorControls.Slot
80+
group="background"
81+
label={ __( 'Background' ) }
82+
className="background-block-support-panel__inner-wrapper"
83+
/>
84+
<InspectorControls.Slot
85+
group="typography"
86+
label={ __( 'Typography' ) }
87+
/>
88+
</>
8089
) }
8190
{ ! isSectionBlock && (
8291
<>
@@ -104,6 +113,11 @@ const StylesTab = ( {
104113
label={ borderPanelLabel }
105114
/>
106115
<InspectorControls.Slot group="styles" />
116+
<InspectorControls.Slot
117+
group="elements"
118+
label={ __( 'Elements' ) }
119+
className="elements-block-support-panel__inner-wrapper"
120+
/>
107121
</>
108122
) }
109123
</>

packages/block-editor/src/components/inspector-controls/groups.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const InspectorControlsFilter = createSlotFill( 'InspectorControlsFilter' );
1515
const InspectorControlsDimensions = createSlotFill(
1616
'InspectorControlsDimensions'
1717
);
18+
const InspectorControlsElements = createSlotFill( 'InspectorControlsElements' );
1819
const InspectorControlsPosition = createSlotFill( 'InspectorControlsPosition' );
1920
const InspectorControlsTypography = createSlotFill(
2021
'InspectorControlsTypography'
@@ -34,6 +35,7 @@ const groups = {
3435
content: InspectorControlsContent,
3536
dimensions: InspectorControlsDimensions,
3637
effects: InspectorControlsEffects,
38+
elements: InspectorControlsElements,
3739
filter: InspectorControlsFilter,
3840
list: InspectorControlsListView,
3941
position: InspectorControlsPosition,

packages/block-editor/src/hooks/color.js

Lines changed: 2 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import clsx from 'clsx';
88
*/
99
import { addFilter } from '@wordpress/hooks';
1010
import { getBlockSupport } from '@wordpress/blocks';
11-
import { useMemo, Platform, useCallback } from '@wordpress/element';
12-
import { useSelect } from '@wordpress/data';
11+
import { useMemo, Platform } from '@wordpress/element';
1312

1413
/**
1514
* Internal dependencies
@@ -19,20 +18,11 @@ import {
1918
getColorObjectByAttributeValues,
2019
} from '../components/colors';
2120
import { __experimentalGetGradientClass } from '../components/gradients';
22-
import {
23-
cleanEmptyObject,
24-
transformStyles,
25-
shouldSkipSerialization,
26-
} from './utils';
21+
import { transformStyles, shouldSkipSerialization } from './utils';
2722
import { getBackgroundImageClasses } from './background';
2823
import { useSettings } from '../components/use-settings';
2924
import InspectorControls from '../components/inspector-controls';
30-
import {
31-
useHasColorPanel,
32-
default as StylesColorPanel,
33-
} from '../components/global-styles/color-panel';
3425
import BlockColorContrastChecker from './contrast-checker';
35-
import { store as blockEditorStore } from '../store';
3626

3727
export const COLOR_SUPPORT_KEY = 'color';
3828

@@ -189,77 +179,6 @@ export function addSaveProps( props, blockNameOrType, attributes ) {
189179
return props;
190180
}
191181

192-
function styleToAttributes( style ) {
193-
const textColorValue = style?.color?.text;
194-
const textColorSlug = textColorValue?.startsWith( 'var:preset|color|' )
195-
? textColorValue.substring( 'var:preset|color|'.length )
196-
: undefined;
197-
const backgroundColorValue = style?.color?.background;
198-
const backgroundColorSlug = backgroundColorValue?.startsWith(
199-
'var:preset|color|'
200-
)
201-
? backgroundColorValue.substring( 'var:preset|color|'.length )
202-
: undefined;
203-
const gradientValue = style?.color?.gradient;
204-
const gradientSlug = gradientValue?.startsWith( 'var:preset|gradient|' )
205-
? gradientValue.substring( 'var:preset|gradient|'.length )
206-
: undefined;
207-
const updatedStyle = { ...style };
208-
updatedStyle.color = {
209-
...updatedStyle.color,
210-
text: textColorSlug ? undefined : textColorValue,
211-
background: backgroundColorSlug ? undefined : backgroundColorValue,
212-
gradient: gradientSlug ? undefined : gradientValue,
213-
};
214-
return {
215-
style: cleanEmptyObject( updatedStyle ),
216-
textColor: textColorSlug,
217-
backgroundColor: backgroundColorSlug,
218-
gradient: gradientSlug,
219-
};
220-
}
221-
222-
function attributesToStyle( attributes ) {
223-
return {
224-
...attributes.style,
225-
color: {
226-
...attributes.style?.color,
227-
text: attributes.textColor
228-
? 'var:preset|color|' + attributes.textColor
229-
: attributes.style?.color?.text,
230-
background: attributes.backgroundColor
231-
? 'var:preset|color|' + attributes.backgroundColor
232-
: attributes.style?.color?.background,
233-
gradient: attributes.gradient
234-
? 'var:preset|gradient|' + attributes.gradient
235-
: attributes.style?.color?.gradient,
236-
},
237-
};
238-
}
239-
240-
function ColorInspectorControl( { children, resetAllFilter } ) {
241-
const attributesResetAllFilter = useCallback(
242-
( attributes ) => {
243-
const existingStyle = attributesToStyle( attributes );
244-
const updatedStyle = resetAllFilter( existingStyle );
245-
return {
246-
...attributes,
247-
...styleToAttributes( updatedStyle ),
248-
};
249-
},
250-
[ resetAllFilter ]
251-
);
252-
253-
return (
254-
<InspectorControls
255-
group="color"
256-
resetAllFilter={ attributesResetAllFilter }
257-
>
258-
{ children }
259-
</InspectorControls>
260-
);
261-
}
262-
263182
export function ContrastCheckerEdit( { clientId, name, settings } ) {
264183
const enableContrastChecking =
265184
Platform.OS === 'web' &&
@@ -281,85 +200,6 @@ export function ContrastCheckerEdit( { clientId, name, settings } ) {
281200
);
282201
}
283202

284-
export function ColorEdit( {
285-
clientId,
286-
name,
287-
setAttributes,
288-
settings,
289-
asWrapper,
290-
label,
291-
defaultControls,
292-
} ) {
293-
const isEnabled = useHasColorPanel( settings );
294-
295-
const { style, textColor, backgroundColor, gradient } = useSelect(
296-
( select ) => {
297-
// Early return to avoid subscription when disabled
298-
if ( ! isEnabled ) {
299-
return {};
300-
}
301-
const {
302-
style: _style,
303-
textColor: _textColor,
304-
backgroundColor: _backgroundColor,
305-
gradient: _gradient,
306-
} = select( blockEditorStore ).getBlockAttributes( clientId ) || {};
307-
return {
308-
style: _style,
309-
textColor: _textColor,
310-
backgroundColor: _backgroundColor,
311-
gradient: _gradient,
312-
};
313-
},
314-
[ clientId, isEnabled ]
315-
);
316-
const value = useMemo( () => {
317-
return attributesToStyle( {
318-
style,
319-
textColor,
320-
backgroundColor,
321-
gradient,
322-
} );
323-
}, [ style, textColor, backgroundColor, gradient ] );
324-
325-
const onChange = ( newStyle ) => {
326-
setAttributes( styleToAttributes( newStyle ) );
327-
};
328-
329-
if ( ! isEnabled ) {
330-
return null;
331-
}
332-
333-
defaultControls = defaultControls
334-
? defaultControls
335-
: getBlockSupport( name, [
336-
COLOR_SUPPORT_KEY,
337-
'__experimentalDefaultControls',
338-
] );
339-
340-
// Use provided wrapper or default to ColorInspectorControl
341-
const Wrapper = asWrapper || ColorInspectorControl;
342-
343-
return (
344-
<StylesColorPanel
345-
as={ Wrapper }
346-
panelId={ clientId }
347-
settings={ settings }
348-
value={ value }
349-
onChange={ onChange }
350-
defaultControls={ defaultControls }
351-
label={ label }
352-
enableContrastChecker={
353-
false !==
354-
getBlockSupport( name, [
355-
COLOR_SUPPORT_KEY,
356-
'enableContrastChecker',
357-
] )
358-
}
359-
/>
360-
);
361-
}
362-
363203
function useBlockProps( {
364204
name,
365205
backgroundColor,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { getBlockSupport } from '@wordpress/blocks';
5+
import { useMemo, useCallback } from '@wordpress/element';
6+
import { useSelect } from '@wordpress/data';
7+
8+
/**
9+
* Internal dependencies
10+
*/
11+
import InspectorControls from '../components/inspector-controls';
12+
import {
13+
useHasColorPanel,
14+
default as StylesColorPanel,
15+
} from '../components/global-styles/color-panel';
16+
import { cleanEmptyObject } from './utils';
17+
import { store as blockEditorStore } from '../store';
18+
import { COLOR_SUPPORT_KEY } from './color';
19+
20+
function ElementsInspectorControl( { children, resetAllFilter } ) {
21+
const attributesResetAllFilter = useCallback(
22+
( attributes ) => {
23+
const updatedStyle = resetAllFilter( attributes.style );
24+
return {
25+
...attributes,
26+
style: cleanEmptyObject( updatedStyle ),
27+
};
28+
},
29+
[ resetAllFilter ]
30+
);
31+
32+
return (
33+
<InspectorControls
34+
group="elements"
35+
resetAllFilter={ attributesResetAllFilter }
36+
>
37+
{ children }
38+
</InspectorControls>
39+
);
40+
}
41+
42+
export function ElementsEdit( {
43+
clientId,
44+
name,
45+
setAttributes,
46+
settings,
47+
asWrapper,
48+
label,
49+
defaultControls,
50+
} ) {
51+
const isEnabled = useHasColorPanel( settings );
52+
53+
const style = useSelect(
54+
( select ) => {
55+
if ( ! isEnabled ) {
56+
return undefined;
57+
}
58+
const attributes =
59+
select( blockEditorStore ).getBlockAttributes( clientId );
60+
return attributes?.style;
61+
},
62+
[ clientId, isEnabled ]
63+
);
64+
65+
const value = useMemo( () => style, [ style ] );
66+
67+
const onChange = ( newStyle ) => {
68+
setAttributes( { style: cleanEmptyObject( newStyle ) } );
69+
};
70+
71+
if ( ! isEnabled ) {
72+
return null;
73+
}
74+
75+
defaultControls = defaultControls
76+
? defaultControls
77+
: getBlockSupport( name, [
78+
COLOR_SUPPORT_KEY,
79+
'__experimentalDefaultControls',
80+
] );
81+
82+
const Wrapper = asWrapper || ElementsInspectorControl;
83+
84+
return (
85+
<StylesColorPanel
86+
as={ Wrapper }
87+
panelId={ clientId }
88+
settings={ settings }
89+
value={ value }
90+
onChange={ onChange }
91+
defaultControls={ defaultControls }
92+
label={ label }
93+
/>
94+
);
95+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.elements-block-support-panel {
2+
/* Increased specificity required to remove the slot wrapper's row gap */
3+
&#{&} {
4+
.elements-block-support-panel__inner-wrapper {
5+
row-gap: 0;
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)