UNPKG

@wordpress/block-library

Version:
87 lines (86 loc) 2.73 kB
// packages/block-library/src/tab-panel/edit.js import { useBlockProps, useInnerBlocksProps, store as blockEditorStore } from "@wordpress/block-editor"; import { useSelect, useDispatch } from "@wordpress/data"; import { useEffect } from "@wordpress/element"; import Controls from "./controls.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; function Edit({ clientId, isSelected }) { const { activeTabIndex, editorActiveTabIndex, blockIndex, hasInnerBlocksSelected, tabsClientId } = useSelect( (select) => { const { getBlockRootClientId, getBlockIndex, hasSelectedInnerBlock, getBlockAttributes } = select(blockEditorStore); 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 }; }, [clientId] ); const effectiveActiveIndex = editorActiveTabIndex ?? activeTabIndex; const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } = useDispatch(blockEditorStore); useEffect(() => { const isTabSelected = isSelected || hasInnerBlocksSelected; if (isTabSelected && 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