UNPKG

@wordpress/block-library

Version:
33 lines (32 loc) 1.09 kB
// packages/block-library/src/tabs/use-tab-list-items-sync.js import { store as blockEditorStore } from "@wordpress/block-editor"; import { useDispatch } from "@wordpress/data"; import { useEffect, useRef } from "@wordpress/element"; function useTabListItemsSync({ tabPanels, tabListClientId }) { const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } = useDispatch(blockEditorStore); const prevTabsRef = useRef(null); useEffect(() => { if (!tabListClientId) { return; } const newTabs = tabPanels.map((tab) => ({ label: tab.attributes.label || "" })); const serialized = JSON.stringify(newTabs); if (serialized === prevTabsRef.current) { return; } prevTabsRef.current = serialized; __unstableMarkNextChangeAsNotPersistent(); updateBlockAttributes(tabListClientId, { tabs: newTabs }); }, [ tabPanels, tabListClientId, updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent ]); } export { useTabListItemsSync as default }; //# sourceMappingURL=use-tab-list-items-sync.mjs.map