@wordpress/block-library
Version:
Block library for the WordPress editor.
57 lines (56 loc) • 2.34 kB
JavaScript
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 } = useSelect((select) => {
const { getBlockRootClientId, getBlockIndex, hasSelectedInnerBlock, getBlockAttributes } = select(store);
const _tabsClientId = getBlockRootClientId(getBlockRootClientId(clientId));
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(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