@wordpress/block-library
Version:
Block library for the WordPress editor.
81 lines (80 loc) • 3.26 kB
JavaScript
import { InspectorControls, RichText, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor";
import { TextControl, ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { __ } from "@wordpress/i18n";
import { useState } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import { withIgnoreIMEEvents } from "@wordpress/keycodes";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/details/edit.jsx
function DetailsEdit({ attributes, setAttributes, clientId }) {
const { name, showContent, summary, allowedBlocks, placeholder } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps(blockProps, {
__experimentalCaptureToolbars: true,
allowedBlocks
});
const [isOpen, setIsOpen] = useState(showContent);
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const hasSelectedInnerBlock = useSelect((select) => select(store).hasSelectedInnerBlock(clientId, true), [clientId]);
const handleSummaryKeyDown = (event) => {
if (event.key === "Enter" && !event.shiftKey) {
setIsOpen((prevIsOpen) => !prevIsOpen);
event.preventDefault();
}
};
const handleSummaryKeyUp = (event) => {
if (event.key === " ") event.preventDefault();
};
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({ showContent: false });
},
dropdownMenuProps,
children: /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
isShownByDefault: true,
label: __("Open by default"),
hasValue: () => showContent,
onDeselect: () => {
setAttributes({ showContent: false });
},
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Open by default"),
checked: showContent,
onChange: () => setAttributes({ showContent: !showContent })
})
})
}) }),
/* @__PURE__ */ jsx(InspectorControls, {
group: "advanced",
children: /* @__PURE__ */ jsx(TextControl, {
label: __("Name attribute"),
value: name || "",
onChange: (newName) => setAttributes({ name: newName }),
help: __("Enables multiple Details blocks with the same name attribute to be connected, with only one open at a time.")
})
}),
/* @__PURE__ */ jsxs("details", {
...innerBlocksProps,
open: isOpen || hasSelectedInnerBlock,
onToggle: (event) => setIsOpen(event.target.open),
name: name || "",
children: [/* @__PURE__ */ jsx("summary", {
onKeyDown: withIgnoreIMEEvents(handleSummaryKeyDown),
onKeyUp: handleSummaryKeyUp,
children: /* @__PURE__ */ jsx(RichText, {
identifier: "summary",
"aria-label": __("Write summary. Press Enter to expand or collapse the details."),
placeholder: placeholder || __("Write summary…"),
withoutInteractiveFormatting: true,
value: summary,
onChange: (newSummary) => setAttributes({ summary: newSummary })
})
}), innerBlocksProps.children]
})
] });
}
export { DetailsEdit as default };
//# sourceMappingURL=edit.mjs.map