UNPKG

@wordpress/block-library

Version:
124 lines (123 loc) • 5.6 kB
import clsx from "clsx"; import { __ } from "@wordpress/i18n"; import { ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { BlockControls, BlockVerticalAlignmentToolbar, InspectorControls, __experimentalBlockVariationPicker, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor"; import { useDispatch, useRegistry, useSelect } from "@wordpress/data"; import { createBlocksFromInnerBlocksTemplate, store as store$1 } from "@wordpress/blocks"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/columns/edit.jsx const DEFAULT_BLOCK = { name: "core/column" }; function ColumnInspectorControls({ setAttributes, isStackedOnMobile }) { const dropdownMenuProps = useToolsPanelDropdownMenuProps(); return /* @__PURE__ */ jsx(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ isStackedOnMobile: true }); }, dropdownMenuProps, children: /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Stack on mobile"), isShownByDefault: true, hasValue: () => isStackedOnMobile !== true, onDeselect: () => setAttributes({ isStackedOnMobile: true }), children: /* @__PURE__ */ jsx(ToggleControl, { label: __("Stack on mobile"), checked: isStackedOnMobile, onChange: () => setAttributes({ isStackedOnMobile: !isStackedOnMobile }) }) }) }); } function ColumnsEditContainer({ attributes, setAttributes, clientId }) { const { isStackedOnMobile, verticalAlignment, templateLock } = attributes; const registry = useRegistry(); const { getBlockOrder } = useSelect(store); const { updateBlockAttributes } = useDispatch(store); const classes = clsx({ [`are-vertically-aligned-${verticalAlignment}`]: verticalAlignment, [`is-not-stacked-on-mobile`]: !isStackedOnMobile }); const blockProps = useBlockProps({ className: classes }); const innerBlocksProps = useInnerBlocksProps(blockProps, { defaultBlock: DEFAULT_BLOCK, directInsert: true, orientation: "horizontal", renderAppender: false, templateLock }); /** * Update all child Column blocks with a new vertical alignment setting * based on whatever alignment is passed in. This allows change to parent * to override anything set on a individual column basis. * * @param {string} newVerticalAlignment The vertical alignment setting. */ function updateAlignment(newVerticalAlignment) { const innerBlockClientIds = getBlockOrder(clientId); registry.batch(() => { setAttributes({ verticalAlignment: newVerticalAlignment }); updateBlockAttributes(innerBlockClientIds, { verticalAlignment: newVerticalAlignment }); }); } return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(BlockVerticalAlignmentToolbar, { onChange: updateAlignment, value: verticalAlignment }) }), /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(ColumnInspectorControls, { setAttributes, isStackedOnMobile }) }), /* @__PURE__ */ jsx("div", { ...innerBlocksProps }) ] }); } function Placeholder({ clientId, name, setAttributes }) { const { blockType, variations } = useSelect((select) => { const { getBlockVariations, getBlockType } = select(store$1); return { blockType: getBlockType(name), variations: getBlockVariations(name, "block") }; }, [name]); const { replaceInnerBlocks } = useDispatch(store); const blockProps = useBlockProps(); return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(__experimentalBlockVariationPicker, { icon: blockType?.icon?.src, label: blockType?.title, variations, instructions: __("Divide into columns. Select a layout:"), onSelect: (nextVariation) => { if (nextVariation.attributes) setAttributes(nextVariation.attributes); if (nextVariation.innerBlocks) replaceInnerBlocks(clientId, createBlocksFromInnerBlocksTemplate(nextVariation.innerBlocks), true); } }) }); } /** * Renders the `core/columns` block in the editor. * * Until the block has inner blocks it renders a variation picker; afterwards it * renders the columns container. Every prop is forwarded to whichever of the two * is rendered. * * @param {Object} props Component props. * @param {string} props.clientId Client ID of the block. * @param {string} props.name Block name, used to look up the variations offered by the picker. * @param {Object} props.attributes Block attributes. * @param {string} [props.attributes.verticalAlignment] Vertical alignment applied to the block and to every child Column block. * @param {boolean} [props.attributes.isStackedOnMobile] Whether the columns stack on small viewports. Defaults to `true`. * @param {string|boolean} [props.attributes.templateLock] Template lock applied to the inner blocks, one of `all`, `insert`, `contentOnly` or `false`. * @param {Function} props.setAttributes Callback for updating block attributes. * * @return {React.JSX.Element} React element. */ const ColumnsEdit = (props) => { const { clientId } = props; const Component = useSelect((select) => select(store).getBlocks(clientId).length > 0, [clientId]) ? ColumnsEditContainer : Placeholder; return /* @__PURE__ */ jsx(Component, { ...props }); }; export { ColumnsEdit as default }; //# sourceMappingURL=edit.mjs.map