@wordpress/block-library
Version:
Block library for the WordPress editor.
109 lines (108 loc) • 3.61 kB
JavaScript
// packages/block-library/src/tab/edit.js
import clsx from "clsx";
import { __ } from "@wordpress/i18n";
import {
useBlockProps,
store as blockEditorStore,
RichText
} from "@wordpress/block-editor";
import { useSelect, useDispatch } from "@wordpress/data";
import { useMemo, useCallback } from "@wordpress/element";
import Controls from "./controls.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var EMPTY_ARRAY = [];
function Edit({ context, clientId }) {
const tabsList = context["core/tabs-list"] || EMPTY_ARRAY;
const activeTabIndex = context["core/tabs-activeTabIndex"];
const editorActiveTabIndex = context["core/tabs-editorActiveTabIndex"];
const effectiveActiveIndex = useMemo(() => {
return editorActiveTabIndex ?? activeTabIndex;
}, [editorActiveTabIndex, activeTabIndex]);
const { tabIndex, tabsClientId, selectedTabClientId } = useSelect(
(select) => {
const {
getBlockOrder,
getBlockRootClientId,
getSelectedBlockClientIds,
hasSelectedInnerBlock
} = select(blockEditorStore);
const _tabsListClientId = getBlockRootClientId(clientId);
const _tabsClientId = _tabsListClientId ? getBlockRootClientId(_tabsListClientId) : null;
const siblings = getBlockOrder(_tabsListClientId);
const _tabIndex = siblings.indexOf(clientId);
const selectedIds = getSelectedBlockClientIds();
let _selectedTabClientId = null;
for (const tab2 of tabsList) {
if (selectedIds.includes(tab2.clientId) || hasSelectedInnerBlock(tab2.clientId, true)) {
_selectedTabClientId = tab2.clientId;
break;
}
}
return {
tabIndex: _tabIndex,
tabsClientId: _tabsClientId,
selectedTabClientId: _selectedTabClientId
};
},
[clientId, tabsList]
);
const tab = tabsList[tabIndex] || {};
const tabListIndex = tab.index ?? tabIndex;
const tabClientId = tab.clientId || "";
const label = tab.label || "";
const isActive = tabListIndex === effectiveActiveIndex;
const isSelected = tabClientId === selectedTabClientId;
const { __unstableMarkNextChangeAsNotPersistent, updateBlockAttributes } = useDispatch(blockEditorStore);
const handleTabClick = useCallback(
(event) => {
event.preventDefault();
if (tabsClientId && tabListIndex !== effectiveActiveIndex) {
__unstableMarkNextChangeAsNotPersistent();
updateBlockAttributes(tabsClientId, {
editorActiveTabIndex: tabListIndex
});
}
},
[
tabsClientId,
tabListIndex,
effectiveActiveIndex,
updateBlockAttributes,
__unstableMarkNextChangeAsNotPersistent
]
);
const handleLabelChange = useCallback(
(newLabel) => {
if (tabClientId) {
updateBlockAttributes(tabClientId, { label: newLabel });
}
},
[tabClientId, updateBlockAttributes]
);
const blockProps = useBlockProps({
className: clsx({
"is-active": isActive,
"is-selected": isSelected
}),
tabIndex: -1,
onClick: handleTabClick
});
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(Controls, { tabsClientId }),
/* @__PURE__ */ jsx("button", { ...blockProps, type: "button", children: /* @__PURE__ */ jsx(
RichText,
{
tagName: "span",
withoutInteractiveFormatting: true,
placeholder: __("Tab title"),
value: label,
onChange: handleLabelChange
}
) })
] });
}
var edit_default = Edit;
export {
edit_default as default
};
//# sourceMappingURL=edit.mjs.map