@wordpress/block-editor
Version:
89 lines (75 loc) • 2.68 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _useAvailableAlignments = _interopRequireDefault(require("./use-available-alignments"));
var _constants = require("./constants");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function BlockAlignmentUI({
value,
onChange,
controls,
isToolbar,
isCollapsed = true,
isBottomSheetControl = false
}) {
const enabledControls = (0, _useAvailableAlignments.default)(controls);
const hasEnabledControls = !!enabledControls.length;
if (!hasEnabledControls) {
return null;
}
function onChangeAlignment(align) {
onChange([value, 'none'].includes(align) ? undefined : align);
}
const activeAlignmentControl = _constants.BLOCK_ALIGNMENTS_CONTROLS[value];
const defaultAlignmentControl = _constants.BLOCK_ALIGNMENTS_CONTROLS[_constants.DEFAULT_CONTROL];
const toolbarUIComponent = isToolbar ? _components.ToolbarGroup : _components.ToolbarDropdownMenu;
const UIComponent = isBottomSheetControl ? _components.BottomSheetSelectControl : toolbarUIComponent;
const commonProps = {
label: (0, _i18n.__)('Align')
};
const extraProps = isBottomSheetControl ? {
options: enabledControls.map(({
name: controlName
}) => {
const control = _constants.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 { ..._constants.BLOCK_ALIGNMENTS_CONTROLS[controlName],
isActive: value === controlName || !value && controlName === 'none',
onClick: () => onChangeAlignment(controlName)
};
}),
popoverProps: _constants.POPOVER_PROPS,
toggleProps: {
describedBy: (0, _i18n.__)('Change alignment')
}
};
return (0, _element.createElement)(UIComponent, (0, _extends2.default)({}, commonProps, extraProps));
}
var _default = BlockAlignmentUI;
exports.default = _default;
//# sourceMappingURL=ui.native.js.map