@gechiui/block-editor
Version:
86 lines (72 loc) • 2.84 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getBorderClassesAndStyles = getBorderClassesAndStyles;
exports.useBorderProps = useBorderProps;
var _classnames = _interopRequireDefault(require("classnames"));
var _style = require("./style");
var _colors = require("../components/colors");
var _useSetting = _interopRequireDefault(require("../components/use-setting"));
/**
* External dependencies
*/
/**
* Internal dependencies
*/
// This utility is intended to assist where the serialization of the border
// block support is being skipped for a block but the border related CSS classes
// & styles still need to be generated so they can be applied to inner elements.
const EMPTY_ARRAY = [];
/**
* Provides the CSS class names and inline styles for a block's border support
* attributes.
*
* @param {Object} attributes Block attributes.
* @param {string} attributes.borderColor Selected named border color.
* @param {Object} attributes.style Block's styles attribute.
*
* @return {Object} Border block support derived CSS classes & styles.
*/
function getBorderClassesAndStyles(_ref) {
var _style$border;
let {
borderColor,
style
} = _ref;
const borderStyles = (style === null || style === void 0 ? void 0 : style.border) || {};
const borderClass = (0, _colors.getColorClassName)('border-color', borderColor);
const className = (0, _classnames.default)({
[borderClass]: !!borderClass,
'has-border-color': borderColor || (style === null || style === void 0 ? void 0 : (_style$border = style.border) === null || _style$border === void 0 ? void 0 : _style$border.color)
});
return {
className: className || undefined,
style: (0, _style.getInlineStyles)({
border: borderStyles
})
};
}
/**
* Derives the border related props for a block from its border block support
* attributes.
*
* Inline styles are forced for named colors to ensure these selections are
* reflected when themes do not load their color stylesheets in the editor.
*
* @param {Object} attributes Block attributes.
*
* @return {Object} ClassName & style props from border block support.
*/
function useBorderProps(attributes) {
const colors = (0, _useSetting.default)('color.palette') || EMPTY_ARRAY;
const borderProps = getBorderClassesAndStyles(attributes); // Force inline style to apply border color when themes do not load their
// color stylesheets in the editor.
if (attributes.borderColor) {
const borderColorObject = (0, _colors.getColorObjectByAttributeValues)(colors, attributes.borderColor);
borderProps.style.borderColor = borderColorObject.color;
}
return borderProps;
}
//# sourceMappingURL=use-border-props.js.map