@wordpress/block-editor
Version:
89 lines (76 loc) • 2.21 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 _icons = require("@wordpress/icons");
/**
* WordPress dependencies
*/
const DEFAULT_ALIGNMENT_CONTROLS = [{
icon: _icons.alignLeft,
title: (0, _i18n.__)('Align text left'),
align: 'left'
}, {
icon: _icons.alignCenter,
title: (0, _i18n.__)('Align text center'),
align: 'center'
}, {
icon: _icons.alignRight,
title: (0, _i18n.__)('Align text right'),
align: 'right'
}];
const POPOVER_PROPS = {
position: 'bottom right',
variant: 'toolbar'
};
function AlignmentUI({
value,
onChange,
alignmentControls = DEFAULT_ALIGNMENT_CONTROLS,
label = (0, _i18n.__)('Align text'),
describedBy = (0, _i18n.__)('Change text alignment'),
isCollapsed = true,
isToolbar
}) {
function applyOrUnset(align) {
return () => onChange(value === align ? undefined : align);
}
const activeAlignment = alignmentControls.find(control => control.align === value);
function setIcon() {
if (activeAlignment) return activeAlignment.icon;
return (0, _i18n.isRTL)() ? _icons.alignRight : _icons.alignLeft;
}
const UIComponent = isToolbar ? _components.ToolbarGroup : _components.ToolbarDropdownMenu;
const extraProps = isToolbar ? {
isCollapsed
} : {
toggleProps: {
describedBy
},
popoverProps: POPOVER_PROPS
};
return (0, _element.createElement)(UIComponent, (0, _extends2.default)({
icon: setIcon(),
label: label,
controls: alignmentControls.map(control => {
const {
align
} = control;
const isActive = value === align;
return { ...control,
isActive,
role: isCollapsed ? 'menuitemradio' : undefined,
onClick: applyOrUnset(align)
};
})
}, extraProps));
}
var _default = AlignmentUI;
exports.default = _default;
//# sourceMappingURL=ui.js.map