UNPKG

@wordpress/block-library

Version:
47 lines (46 loc) • 2.27 kB
import clsx from "clsx"; import { useCallback, useState } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import { BlockControls, RichText, __experimentalGetElementClassName } from "@wordpress/block-editor"; import { ToolbarButton } from "@wordpress/components"; import { caption } from "@wordpress/icons"; import { createBlock, getDefaultBlockName } from "@wordpress/blocks"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/utils/caption.jsx function Caption({ attributeKey = "caption", attributes, setAttributes, isSelected, insertBlocksAfter, placeholder = __("Add caption"), label = __("Caption text"), showToolbarButton = true, excludeElementClassName, className, readOnly, tagName = "figcaption", addLabel = __("Add caption"), removeLabel = __("Remove caption"), icon = caption, ...props }) { const caption$1 = attributes[attributeKey]; const isCaptionEmpty = RichText.isEmpty(caption$1); const [showCaption, setShowCaption] = useState(!isCaptionEmpty); if (!isCaptionEmpty && !showCaption) setShowCaption(true); if (!isSelected && isCaptionEmpty && showCaption) setShowCaption(false); const ref = useCallback((node) => { if (node && isCaptionEmpty) node.focus(); }, [isCaptionEmpty]); return /* @__PURE__ */ jsxs(Fragment, { children: [showToolbarButton && /* @__PURE__ */ jsx(BlockControls, { group: "block", children: /* @__PURE__ */ jsx(ToolbarButton, { onClick: () => { setShowCaption(!showCaption); if (showCaption && caption$1) setAttributes({ [attributeKey]: void 0 }); }, icon, isPressed: showCaption, label: showCaption ? removeLabel : addLabel }) }), showCaption && (!RichText.isEmpty(caption$1) || isSelected) && /* @__PURE__ */ jsx(RichText, { identifier: attributeKey, tagName, className: clsx(className, excludeElementClassName ? "" : __experimentalGetElementClassName("caption")), ref, "aria-label": label, placeholder, value: caption$1, onChange: (value) => setAttributes({ [attributeKey]: value }), inlineToolbar: true, __unstableOnSplitAtEnd: insertBlocksAfter ? () => insertBlocksAfter(createBlock(getDefaultBlockName())) : void 0, readOnly, ...props })] }); } export { Caption }; //# sourceMappingURL=caption.mjs.map