@wordpress/block-library
Version:
Block library for the WordPress editor.
59 lines (58 loc) • 1.86 kB
JavaScript
// packages/block-library/src/tab/add-tab-toolbar-control.js
import { sprintf, __ } from "@wordpress/i18n";
import { createBlock } from "@wordpress/blocks";
import {
BlockControls,
store as blockEditorStore
} from "@wordpress/block-editor";
import { ToolbarGroup, ToolbarButton } from "@wordpress/components";
import { useDispatch, useSelect } from "@wordpress/data";
import { jsx } from "react/jsx-runtime";
function AddTabToolbarControl({ tabsClientId }) {
const { insertBlock } = useDispatch(blockEditorStore);
const { tabPanelsClientId, nextTabIndex } = useSelect(
(select) => {
if (!tabsClientId) {
return {
tabPanelsClientId: null,
nextTabIndex: 0
};
}
const { getBlocks } = select(blockEditorStore);
const innerBlocks = getBlocks(tabsClientId);
const tabPanels = innerBlocks.find(
(block) => block.name === "core/tab-panels"
);
return {
tabPanelsClientId: tabPanels?.clientId || null,
nextTabIndex: (tabPanels?.innerBlocks.length || 0) + 1
};
},
[tabsClientId]
);
const addTab = () => {
if (!tabPanelsClientId) {
return;
}
const newTabBlock = createBlock("core/tab", {
anchor: "tab-" + nextTabIndex,
/* translators: %d: tab number */
label: sprintf(__("Tab %d"), nextTabIndex)
});
insertBlock(newTabBlock, void 0, tabPanelsClientId);
};
return /* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(
ToolbarButton,
{
className: "components-toolbar__control",
label: __("Add a new tab"),
onClick: addTab,
showTooltip: true,
text: __("Add Tab")
}
) }) });
}
export {
AddTabToolbarControl as default
};
//# sourceMappingURL=add-tab-toolbar-control.mjs.map