-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathui.js
165 lines (153 loc) · 3.67 KB
/
ui.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
/**
* External dependencies
*/
import classNames from 'classnames';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
ToolbarDropdownMenu,
ToolbarGroup,
MenuGroup,
MenuItem,
} from '@wordpress/components';
import {
alignNone,
positionCenter,
positionLeft,
positionRight,
stretchFullWidth,
stretchWide,
} from '@wordpress/icons';
import { Platform } from '@wordpress/element';
/**
* Internal dependencies
*/
import useAvailableAlignments from './use-available-alignments';
const BLOCK_ALIGNMENTS_CONTROLS = {
none: {
icon: alignNone,
title: __( 'None' ),
},
left: {
icon: positionLeft,
title: __( 'Align left' ),
},
center: {
icon: positionCenter,
title: __( 'Align center' ),
},
right: {
icon: positionRight,
title: __( 'Align right' ),
},
wide: {
icon: stretchWide,
title: __( 'Wide width' ),
},
full: {
icon: stretchFullWidth,
title: __( 'Full width' ),
},
};
const DEFAULT_CONTROL = 'none';
const POPOVER_PROPS = {
isAlternate: true,
};
function BlockAlignmentUI( {
value,
onChange,
controls,
isToolbar,
isCollapsed = true,
} ) {
const enabledControls = useAvailableAlignments( controls );
const hasEnabledControls = !! enabledControls.length;
if ( ! hasEnabledControls ) {
return null;
}
function onChangeAlignment( align ) {
onChange( [ value, 'none' ].includes( align ) ? undefined : align );
}
const activeAlignmentControl = BLOCK_ALIGNMENTS_CONTROLS[ value ];
const defaultAlignmentControl =
BLOCK_ALIGNMENTS_CONTROLS[ DEFAULT_CONTROL ];
const UIComponent = isToolbar ? ToolbarGroup : ToolbarDropdownMenu;
const commonProps = {
popoverProps: POPOVER_PROPS,
icon: activeAlignmentControl
? activeAlignmentControl.icon
: defaultAlignmentControl.icon,
label: __( 'Align' ),
toggleProps: { describedBy: __( 'Change alignment' ) },
};
const extraProps =
isToolbar || Platform.isNative
? {
isCollapsed: isToolbar ? isCollapsed : undefined,
controls: enabledControls.map(
( { name: controlName } ) => {
return {
...BLOCK_ALIGNMENTS_CONTROLS[ controlName ],
isActive:
value === controlName ||
( ! value && controlName === 'none' ),
role: isCollapsed ? 'menuitemradio' : undefined,
onClick: () => onChangeAlignment( controlName ),
};
}
),
}
: {
children: ( { onClose } ) => {
return (
<>
<MenuGroup className="block-editor-block-alignment-control__menu-group">
{ enabledControls.map(
( { name: controlName, info } ) => {
const {
icon,
title,
} = BLOCK_ALIGNMENTS_CONTROLS[
controlName
];
// If no value is provided, mark as selected the `none` option.
const isSelected =
controlName === value ||
( ! value &&
controlName === 'none' );
return (
<MenuItem
key={ controlName }
icon={ icon }
iconPosition="left"
className={ classNames(
'components-dropdown-menu__menu-item',
{
'is-active': isSelected,
}
) }
isSelected={ isSelected }
onClick={ () => {
onChangeAlignment(
controlName
);
onClose();
} }
role="menuitemradio"
info={ info }
>
{ title }
</MenuItem>
);
}
) }
</MenuGroup>
</>
);
},
};
return <UIComponent { ...commonProps } { ...extraProps } />;
}
export default BlockAlignmentUI;