@wordpress/block-library
Version:
Block library for the WordPress editor.
114 lines (113 loc) • 2.76 kB
JavaScript
// packages/block-library/src/tabs/edit.js
import { __ } from "@wordpress/i18n";
import {
useBlockProps,
useInnerBlocksProps,
BlockContextProvider,
store as blockEditorStore
} from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { useMemo, useEffect } from "@wordpress/element";
import Controls from "./controls.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var TABS_TEMPLATE = [
[
"core/tabs-menu",
{
lock: {
remove: true
}
}
],
[
"core/tab-panels",
{
lock: {
remove: true
}
},
[
[
"core/tab",
{
anchor: "tab-1",
label: "Tab 1"
},
[
[
"core/paragraph",
{
placeholder: __("Type / to add a block to tab")
}
]
]
]
]
]
];
function Edit({
clientId,
attributes,
setAttributes,
__unstableLayoutClassNames: layoutClassNames
}) {
const { anchor, activeTabIndex, editorActiveTabIndex } = attributes;
useEffect(() => {
if (editorActiveTabIndex === void 0) {
setAttributes({ editorActiveTabIndex: activeTabIndex });
}
}, []);
const tabsList = useSelect(
(select) => {
const { getBlocks } = select(blockEditorStore);
const innerBlocks = getBlocks(clientId);
const tabPanels = innerBlocks.find(
(block) => block.name === "core/tab-panels"
);
if (!tabPanels) {
return [];
}
return tabPanels.innerBlocks.filter((block) => block.name === "core/tab").map((tab, index) => ({
id: tab.attributes.anchor || `tab-${index}`,
label: tab.attributes.label || "",
clientId: tab.clientId,
index
}));
},
[clientId]
);
const contextValue = useMemo(
() => ({
"core/tabs-list": tabsList,
"core/tabs-id": anchor,
"core/tabs-activeTabIndex": activeTabIndex,
"core/tabs-editorActiveTabIndex": editorActiveTabIndex
}),
[tabsList, anchor, activeTabIndex, editorActiveTabIndex]
);
const blockProps = useBlockProps({
className: layoutClassNames
});
const innerBlockProps = useInnerBlocksProps(blockProps, {
template: TABS_TEMPLATE,
templateLock: false,
renderAppender: false,
__experimentalCaptureToolbars: true
});
return /* @__PURE__ */ jsx(BlockContextProvider, { value: contextValue, children: /* @__PURE__ */ jsxs("div", { ...innerBlockProps, children: [
/* @__PURE__ */ jsx(
Controls,
{
clientId,
attributes,
setAttributes
}
),
innerBlockProps.children
] }) });
}
var edit_default = Edit;
export {
edit_default as default
};
//# sourceMappingURL=edit.mjs.map