UNPKG

@wordpress/block-library

Version:
1 lines • 3.02 kB
{"version":3,"file":"use-tab-list-items-sync.mjs","names":["blockEditorStore"],"sources":["../../src/tabs/use-tab-list-items-sync.js"],"sourcesContent":["import { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { useEffect } from '@wordpress/element';\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Keep the tab-list block's `tabs` attribute in sync with the tab-panel blocks.\n *\n * Whenever the list of core/tab-panel blocks changes (add, remove, reorder, or\n * label edit), this hook updates the `tabs` attribute on the core/tab-list\n * block so that save.jsx can render the correct buttons.\n *\n * @param {string} tabsClientId Client ID of the core/tabs block.\n */\nexport default function useTabListItemsSync( tabsClientId ) {\n\tconst { tabPanels, tabListClientId } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlocks } = select( blockEditorStore );\n\t\t\tconst innerBlocks = getBlocks( tabsClientId );\n\n\t\t\tconst tabPanelsBlock = innerBlocks.find(\n\t\t\t\t( block ) => block.name === 'core/tab-panels'\n\t\t\t);\n\t\t\tconst tabList = innerBlocks.find(\n\t\t\t\t( block ) => block.name === 'core/tab-list'\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\ttabPanels: tabPanelsBlock?.innerBlocks ?? EMPTY_ARRAY,\n\t\t\t\ttabListClientId: tabList?.clientId ?? null,\n\t\t\t};\n\t\t},\n\t\t[ tabsClientId ]\n\t);\n\n\tconst { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } =\n\t\tuseDispatch( blockEditorStore );\n\tconst { getBlockAttributes } = useSelect( blockEditorStore );\n\n\tuseEffect( () => {\n\t\tif ( ! tabListClientId ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newTabs = tabPanels.map( ( tab ) => ( {\n\t\t\tlabel: tab.attributes.label || '',\n\t\t} ) );\n\n\t\t// Skip the update when the stored tabs already match the derived ones.\n\t\tconst currentTabs = getBlockAttributes( tabListClientId )?.tabs ?? [];\n\t\tif ( JSON.stringify( newTabs ) === JSON.stringify( currentTabs ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\tupdateBlockAttributes( tabListClientId, { tabs: newTabs } );\n\t}, [\n\t\ttabPanels,\n\t\ttabListClientId,\n\t\tgetBlockAttributes,\n\t\tupdateBlockAttributes,\n\t\t__unstableMarkNextChangeAsNotPersistent,\n\t] );\n}\n"],"mappings":";;;;AAIA,MAAM,cAAc,CAAC;;;;;;;;;;AAWrB,SAAwB,oBAAqB,cAAe;CAC3D,MAAM,EAAE,WAAW,oBAAoB,WACpC,WAAY;EACb,MAAM,EAAE,cAAc,OAAQA,KAAiB;EAC/C,MAAM,cAAc,UAAW,YAAa;EAE5C,MAAM,iBAAiB,YAAY,MAChC,UAAW,MAAM,SAAS,iBAC7B;EACA,MAAM,UAAU,YAAY,MACzB,UAAW,MAAM,SAAS,eAC7B;EAEA,OAAO;GACN,WAAW,gBAAgB,eAAe;GAC1C,iBAAiB,SAAS,YAAY;EACvC;CACD,GACA,CAAE,YAAa,CAChB;CAEA,MAAM,EAAE,uBAAuB,4CAC9B,YAAaA,KAAiB;CAC/B,MAAM,EAAE,uBAAuB,UAAWA,KAAiB;CAE3D,gBAAiB;EAChB,IAAK,CAAE,iBACN;EAGD,MAAM,UAAU,UAAU,KAAO,SAAW,EAC3C,OAAO,IAAI,WAAW,SAAS,GAChC,EAAI;EAGJ,MAAM,cAAc,mBAAoB,eAAgB,CAAC,EAAE,QAAQ,CAAC;EACpE,IAAK,KAAK,UAAW,OAAQ,MAAM,KAAK,UAAW,WAAY,GAC9D;EAGD,wCAAwC;EACxC,sBAAuB,iBAAiB,EAAE,MAAM,QAAQ,CAAE;CAC3D,GAAG;EACF;EACA;EACA;EACA;EACA;CACD,CAAE;AACH"}