UNPKG

@wordpress/block-library

Version:
60 lines (59 loc) • 2.52 kB
import { store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor"; import { useDispatch, useSelect } from "@wordpress/data"; import { useEffect } from "@wordpress/element"; import Controls from "./controls.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/tab-panel/edit.jsx function Edit({ clientId, isSelected }) { const { activeTabIndex, editorActiveTabIndex, blockIndex, hasInnerBlocksSelected, tabsClientId, tabCount } = useSelect((select) => { const { getBlockRootClientId, getBlockIndex, hasSelectedInnerBlock, getBlockAttributes, getBlockCount } = select(store); const tabPanelsClientId = getBlockRootClientId(clientId); const _tabsClientId = getBlockRootClientId(tabPanelsClientId); const tabsAttributes = getBlockAttributes(_tabsClientId) ?? {}; const _blockIndex = getBlockIndex(clientId); const _hasInnerBlocksSelected = hasSelectedInnerBlock(clientId, true); return { activeTabIndex: tabsAttributes.activeTabIndex, editorActiveTabIndex: tabsAttributes.editorActiveTabIndex, blockIndex: _blockIndex, hasInnerBlocksSelected: _hasInnerBlocksSelected, tabsClientId: _tabsClientId, tabCount: getBlockCount(tabPanelsClientId) }; }, [clientId]); const activeIndex = editorActiveTabIndex ?? activeTabIndex; const effectiveActiveIndex = activeIndex < tabCount ? activeIndex : 0; const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } = useDispatch(store); useEffect(() => { if ((isSelected || hasInnerBlocksSelected) && tabsClientId && effectiveActiveIndex !== blockIndex) { __unstableMarkNextChangeAsNotPersistent(); updateBlockAttributes(tabsClientId, { editorActiveTabIndex: blockIndex }); } }, [ isSelected, hasInnerBlocksSelected, tabsClientId, effectiveActiveIndex, blockIndex, updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent ]); const isActiveTab = effectiveActiveIndex === blockIndex; const isDefaultTab = activeTabIndex === blockIndex; const isSelectedTab = isSelected || hasInnerBlocksSelected || isActiveTab; const blockProps = useBlockProps({ hidden: !isSelectedTab, tabIndex: isSelectedTab ? 0 : -1 }); const innerBlocksProps = useInnerBlocksProps(blockProps, {}); return /* @__PURE__ */ jsxs("section", { ...innerBlocksProps, children: [/* @__PURE__ */ jsx(Controls, { tabsClientId, blockIndex, isDefaultTab }), isSelectedTab && innerBlocksProps.children] }); } export { Edit as default }; //# sourceMappingURL=edit.mjs.map