@wordpress/block-library
Version:
Block library for the WordPress editor.
39 lines (38 loc) • 1.64 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { BlockControls, store } from "@wordpress/block-editor";
import { ToolbarButton, ToolbarGroup } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import useTabActions from "./use-tab-actions.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/tabs/tab-toolbar-controls.jsx
/**
* "Add tab" and "Remove tab" buttons in the block toolbar for the tabs block.
* Inserts or removes a core/tab-panel. The tab-list items attribute is kept
* in sync automatically via useTabListItemsSync.
*
* @param {Object} props
* @param {string} props.tabsClientId The client ID of the parent tabs block.
* @return {React.JSX.Element} The toolbar control element.
*/
function TabToolbarControls({ tabsClientId }) {
const { insertTab, removeTab } = useTabActions(tabsClientId);
const isRemoveDisabled = useSelect((select) => {
if (!tabsClientId) return true;
return (select(store).getBlocks(tabsClientId).find((block) => block.name === "core/tab-panels")?.innerBlocks.length ?? 0) <= 1;
}, [tabsClientId]);
return /* @__PURE__ */ jsx(BlockControls, {
group: "other",
children: /* @__PURE__ */ jsxs(ToolbarGroup, { children: [/* @__PURE__ */ jsx(ToolbarButton, {
className: "components-toolbar__control",
onClick: () => insertTab(),
text: __("Add tab")
}), /* @__PURE__ */ jsx(ToolbarButton, {
className: "components-toolbar__control",
onClick: () => removeTab(),
text: __("Remove tab"),
disabled: isRemoveDisabled
})] })
});
}
export { TabToolbarControls as default };
//# sourceMappingURL=tab-toolbar-controls.mjs.map