-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
ui.native.js
86 lines (77 loc) · 2.16 KB
/
ui.native.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
ToolbarDropdownMenu,
ToolbarGroup,
BottomSheetSelectControl,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import useAvailableAlignments from './use-available-alignments';
import {
BLOCK_ALIGNMENTS_CONTROLS,
DEFAULT_CONTROL,
POPOVER_PROPS,
} from './constants';
function BlockAlignmentUI( {
value,
onChange,
controls,
isToolbar,
isCollapsed = true,
isBottomSheetControl = false,
} ) {
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 toolbarUIComponent = isToolbar ? ToolbarGroup : ToolbarDropdownMenu;
const UIComponent = isBottomSheetControl
? BottomSheetSelectControl
: toolbarUIComponent;
const commonProps = {
label: __( 'Align' ),
};
const extraProps = isBottomSheetControl
? {
options: enabledControls.map( ( { name: controlName } ) => {
const control = BLOCK_ALIGNMENTS_CONTROLS[ controlName ];
return {
value: controlName,
label: control.title,
icon: control.icon,
};
} ),
value: activeAlignmentControl ? value : 'none',
onChange: ( align ) => onChangeAlignment( align ),
}
: {
icon: activeAlignmentControl
? activeAlignmentControl.icon
: defaultAlignmentControl.icon,
isCollapsed: isToolbar ? isCollapsed : undefined,
controls: enabledControls.map( ( { name: controlName } ) => {
return {
...BLOCK_ALIGNMENTS_CONTROLS[ controlName ],
isActive:
value === controlName ||
( ! value && controlName === 'none' ),
onClick: () => onChangeAlignment( controlName ),
};
} ),
popoverProps: POPOVER_PROPS,
toggleProps: { describedBy: __( 'Change alignment' ) },
};
return <UIComponent { ...commonProps } { ...extraProps } />;
}
export default BlockAlignmentUI;