@wordpress/block-library
Version:
Block library for the WordPress editor.
113 lines (112 loc) • 4.93 kB
JavaScript
import clsx from "clsx";
import { BlockControls, BlockVerticalAlignmentToolbar, InnerBlocks, InspectorControls, store, useBlockProps, useInnerBlocksProps, useSettings } from "@wordpress/block-editor";
import { __experimentalToolsPanel, __experimentalToolsPanelItem, __experimentalUnitControl, __experimentalUseCustomUnits } from "@wordpress/components";
import { useDispatch, useSelect } from "@wordpress/data";
import { useState } from "@wordpress/element";
import { __, sprintf } from "@wordpress/i18n";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/column/edit.jsx
function ColumnInspectorControls({ width, setAttributes }) {
const [availableUnits] = useSettings("spacing.units");
const units = __experimentalUseCustomUnits({ availableUnits: availableUnits || [
"%",
"px",
"em",
"rem",
"vw"
] });
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return /* @__PURE__ */ jsx(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({ width: void 0 });
},
dropdownMenuProps,
children: /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => width !== void 0,
label: __("Width"),
onDeselect: () => setAttributes({ width: void 0 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(__experimentalUnitControl, {
label: __("Width"),
__unstableInputWidth: "calc(50% - 8px)",
value: width || "",
onChange: (nextWidth) => {
nextWidth = 0 > parseFloat(nextWidth) ? "0" : nextWidth;
setAttributes({ width: nextWidth });
},
units
})
})
});
}
/**
* Renders the `core/column` block in the editor.
*
* @param {Object} props Component props.
* @param {Object} props.attributes Block attributes.
* @param {string} [props.attributes.verticalAlignment] Vertical alignment of the column, one of `top`, `center`, `bottom` or `stretch`. Setting it resets the alignment on the parent Columns block.
* @param {string|number} [props.attributes.width] Column width, used as its `flex-basis`. A number is treated as a percentage, for content saved before the attribute became a CSS length.
* @param {string|boolean} [props.attributes.templateLock] Template lock applied to the inner blocks, one of `all`, `insert`, `contentOnly` or `false`.
* @param {string[]} [props.attributes.allowedBlocks] Names of the blocks allowed as inner blocks, added by the `allowedBlocks` block support.
* @param {Function} props.setAttributes Callback for updating block attributes.
* @param {string} props.clientId Client ID of the block.
*
* @return {React.JSX.Element} React element.
*/
function ColumnEdit({ attributes: { verticalAlignment, width, templateLock, allowedBlocks }, setAttributes, clientId }) {
const classes = clsx("block-core-columns", { [`is-vertically-aligned-${verticalAlignment}`]: verticalAlignment });
const { columnsIds, hasChildBlocks, rootClientId } = useSelect((select) => {
const { getBlockOrder, getBlockRootClientId } = select(store);
const rootId = getBlockRootClientId(clientId);
return {
hasChildBlocks: getBlockOrder(clientId).length > 0,
rootClientId: rootId,
columnsIds: getBlockOrder(rootId)
};
}, [clientId]);
const { updateBlockAttributes } = useDispatch(store);
const updateAlignment = (value) => {
setAttributes({ verticalAlignment: value });
updateBlockAttributes(rootClientId, { verticalAlignment: null });
};
const widthWithUnit = Number.isFinite(width) ? width + "%" : width;
const [dropZoneElement, setDropZoneElement] = useState(null);
const blockProps = useBlockProps({
ref: setDropZoneElement,
className: classes,
style: widthWithUnit ? { flexBasis: widthWithUnit } : void 0
});
const columnsCount = columnsIds.length;
const currentColumnPosition = columnsIds.indexOf(clientId) + 1;
const label = sprintf(__("%1$s (%2$d of %3$d)"), blockProps["aria-label"], currentColumnPosition, columnsCount);
const innerBlocksProps = useInnerBlocksProps({
...blockProps,
"aria-label": label
}, {
dropZoneElement,
templateLock,
allowedBlocks,
renderAppender: hasChildBlocks ? false : InnerBlocks.ButtonBlockAppender
});
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(BlockVerticalAlignmentToolbar, {
onChange: updateAlignment,
value: verticalAlignment,
controls: [
"top",
"center",
"bottom",
"stretch"
]
}) }),
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(ColumnInspectorControls, {
width,
setAttributes
}) }),
/* @__PURE__ */ jsx("div", { ...innerBlocksProps })
] });
}
export { ColumnEdit as default };
//# sourceMappingURL=edit.mjs.map