2
2
* WordPress dependencies
3
3
*/
4
4
import {
5
- PanelBody ,
6
5
__experimentalUseSlotFills as useSlotFills ,
6
+ __experimentalToolsPanel as ToolsPanel ,
7
+ __experimentalToolsPanelItem as ToolsPanelItem ,
7
8
} from '@wordpress/components' ;
8
- import { useSelect } from '@wordpress/data' ;
9
- import { useLayoutEffect , useState } from '@wordpress/element' ;
9
+ import { useDispatch , useSelect } from '@wordpress/data' ;
10
10
import { __ } from '@wordpress/i18n' ;
11
11
12
12
/**
@@ -15,40 +15,75 @@ import { __ } from '@wordpress/i18n';
15
15
import InspectorControlsGroups from '../inspector-controls/groups' ;
16
16
import { default as InspectorControls } from '../inspector-controls' ;
17
17
import { store as blockEditorStore } from '../../store' ;
18
+ import { useToolsPanelDropdownMenuProps } from '../global-styles/utils' ;
19
+ import { cleanEmptyObject } from '../../hooks/utils' ;
18
20
19
21
const PositionControlsPanel = ( ) => {
20
- const [ initialOpen , setInitialOpen ] = useState ( ) ;
22
+ const { selectedClientIds, selectedBlocks, hasPositionAttribute } =
23
+ useSelect ( ( select ) => {
24
+ const { getBlocksByClientId, getSelectedBlockClientIds } =
25
+ select ( blockEditorStore ) ;
21
26
22
- // Determine whether the panel should be expanded.
23
- const { multiSelectedBlocks } = useSelect ( ( select ) => {
24
- const { getBlocksByClientId, getSelectedBlockClientIds } =
25
- select ( blockEditorStore ) ;
26
- const clientIds = getSelectedBlockClientIds ( ) ;
27
- return {
28
- multiSelectedBlocks : getBlocksByClientId ( clientIds ) ,
29
- } ;
30
- } , [ ] ) ;
27
+ const selectedBlockClientIds = getSelectedBlockClientIds ( ) ;
28
+ const _selectedBlocks = getBlocksByClientId (
29
+ selectedBlockClientIds
30
+ ) ;
31
31
32
- useLayoutEffect ( ( ) => {
33
- // If any selected block has a position set, open the panel by default.
34
- // The first block's value will still be used within the control though.
35
- if ( initialOpen === undefined ) {
36
- setInitialOpen (
37
- multiSelectedBlocks . some (
32
+ return {
33
+ selectedClientIds : selectedBlockClientIds ,
34
+ selectedBlocks : _selectedBlocks ,
35
+ hasPositionAttribute : _selectedBlocks ?. some (
38
36
( { attributes } ) => ! ! attributes ?. style ?. position ?. type
39
- )
40
- ) ;
37
+ ) ,
38
+ } ;
39
+ } , [ ] ) ;
40
+
41
+ const { updateBlockAttributes } = useDispatch ( blockEditorStore ) ;
42
+ const dropdownMenuProps = useToolsPanelDropdownMenuProps ( ) ;
43
+
44
+ function resetPosition ( ) {
45
+ if ( ! selectedClientIds ?. length || ! selectedBlocks ?. length ) {
46
+ return ;
41
47
}
42
- } , [ initialOpen , multiSelectedBlocks , setInitialOpen ] ) ;
48
+
49
+ const attributesByClientId = Object . fromEntries (
50
+ selectedBlocks ?. map ( ( { clientId, attributes } ) => [
51
+ clientId ,
52
+ {
53
+ style : cleanEmptyObject ( {
54
+ ...attributes ?. style ,
55
+ position : {
56
+ ...attributes ?. style ?. position ,
57
+ type : undefined ,
58
+ top : undefined ,
59
+ right : undefined ,
60
+ bottom : undefined ,
61
+ left : undefined ,
62
+ } ,
63
+ } ) ,
64
+ } ,
65
+ ] )
66
+ ) ;
67
+
68
+ updateBlockAttributes ( selectedClientIds , attributesByClientId , true ) ;
69
+ }
43
70
44
71
return (
45
- < PanelBody
72
+ < ToolsPanel
46
73
className = "block-editor-block-inspector__position"
47
- title = { __ ( 'Position' ) }
48
- initialOpen = { initialOpen ?? false }
74
+ label = { __ ( 'Position' ) }
75
+ resetAll = { resetPosition }
76
+ dropdownMenuProps = { dropdownMenuProps }
49
77
>
50
- < InspectorControls . Slot group = "position" />
51
- </ PanelBody >
78
+ < ToolsPanelItem
79
+ isShownByDefault = { hasPositionAttribute }
80
+ label = { __ ( 'Position' ) }
81
+ hasValue = { ( ) => hasPositionAttribute }
82
+ onDeselect = { resetPosition }
83
+ >
84
+ < InspectorControls . Slot group = "position" />
85
+ </ ToolsPanelItem >
86
+ </ ToolsPanel >
52
87
) ;
53
88
} ;
54
89
0 commit comments