@wordpress/block-library
Version:
Block library for the WordPress editor.
115 lines (114 loc) • 3.06 kB
JavaScript
// packages/block-library/src/tabs/edit.js
import {
useBlockProps,
useInnerBlocksProps,
BlockContextProvider,
store as blockEditorStore
} from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { useMemo, useEffect } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import Controls from "./controls.mjs";
import useTabListSync from "./use-tab-list-sync.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var EMPTY_ARRAY = [];
var TABS_TEMPLATE = [
[
"core/tab-list",
{},
[
["core/tab", {}],
["core/tab", {}]
]
],
[
"core/tab-panels",
{},
[
[
"core/tab-panel",
{
label: __("Tab")
},
[["core/paragraph"]]
],
[
"core/tab-panel",
{
label: __("Tab")
},
[["core/paragraph"]]
]
]
]
];
function Edit({ clientId, attributes, setAttributes }) {
const { anchor, activeTabIndex, editorActiveTabIndex } = attributes;
useEffect(() => {
if (editorActiveTabIndex === void 0) {
setAttributes({ editorActiveTabIndex: activeTabIndex });
}
}, []);
const { tabPanels, tabPanelsClientId, tabs, tabListClientId } = useSelect(
(select) => {
const { getBlocks } = select(blockEditorStore);
const innerBlocks = getBlocks(clientId);
const tabPanelBlocks = innerBlocks.find(
(block) => block.name === "core/tab-panels"
);
const tabList = innerBlocks.find(
(block) => block.name === "core/tab-list"
);
return {
tabPanels: tabPanelBlocks?.innerBlocks ?? EMPTY_ARRAY,
tabPanelsClientId: tabPanelBlocks?.clientId ?? null,
tabs: tabList?.innerBlocks ?? EMPTY_ARRAY,
tabListClientId: tabList?.clientId ?? null
};
},
[clientId]
);
useTabListSync({
tabPanels,
tabs,
tabPanelsClientId,
tabListClientId
});
const contextValue = useMemo(() => {
const tabList = tabPanels.map((tab, index) => ({
id: tab.attributes.anchor || `tab-${index}`,
label: tab.attributes.label || "",
clientId: tab.clientId,
index
}));
return {
"core/tabs-list": tabList,
"core/tabs-id": anchor,
"core/tabs-activeTabIndex": activeTabIndex,
"core/tabs-editorActiveTabIndex": editorActiveTabIndex
};
}, [tabPanels, anchor, activeTabIndex, editorActiveTabIndex]);
const blockProps = useBlockProps();
const innerBlockProps = useInnerBlocksProps(blockProps, {
__experimentalCaptureToolbars: true,
template: TABS_TEMPLATE,
templateLock: "all",
renderAppender: false
});
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