UNPKG

@wordpress/block-library

Version:
80 lines (79 loc) 2.52 kB
// 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 } from "@wordpress/element"; 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-panels"]]; function Edit({ clientId, attributes, setAttributes }) { const { anchor, activeTabIndex, editorActiveTabIndex } = attributes; 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