@wordpress/block-library
Version:
Block library for the WordPress editor.
8 lines (7 loc) • 2.31 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/tabs/use-tab-list-items-sync.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useDispatch } from '@wordpress/data';\nimport { useEffect, useRef } from '@wordpress/element';\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.js can render the correct buttons.\n *\n * @param {Object} props\n * @param {Array} props.tabPanels Raw core/tab-panel block objects.\n * @param {string|null} props.tabListClientId Client ID of the core/tab-list block.\n */\nexport default function useTabListItemsSync( { tabPanels, tabListClientId } ) {\n\tconst { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } =\n\t\tuseDispatch( blockEditorStore );\n\n\tconst prevTabsRef = useRef( null );\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// Only update if tabs actually changed to avoid unnecessary re-renders.\n\t\tconst serialized = JSON.stringify( newTabs );\n\t\tif ( serialized === prevTabsRef.current ) {\n\t\t\treturn;\n\t\t}\n\t\tprevTabsRef.current = serialized;\n\n\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\tupdateBlockAttributes( tabListClientId, { tabs: newTabs } );\n\t}, [\n\t\ttabPanels,\n\t\ttabListClientId,\n\t\tupdateBlockAttributes,\n\t\t__unstableMarkNextChangeAsNotPersistent,\n\t] );\n}\n"],
"mappings": ";AAGA,SAAS,SAAS,wBAAwB;AAC1C,SAAS,mBAAmB;AAC5B,SAAS,WAAW,cAAc;AAanB,SAAR,oBAAsC,EAAE,WAAW,gBAAgB,GAAI;AAC7E,QAAM,EAAE,uBAAuB,wCAAwC,IACtE,YAAa,gBAAiB;AAE/B,QAAM,cAAc,OAAQ,IAAK;AAEjC,YAAW,MAAM;AAChB,QAAK,CAAE,iBAAkB;AACxB;AAAA,IACD;AAEA,UAAM,UAAU,UAAU,IAAK,CAAE,SAAW;AAAA,MAC3C,OAAO,IAAI,WAAW,SAAS;AAAA,IAChC,EAAI;AAGJ,UAAM,aAAa,KAAK,UAAW,OAAQ;AAC3C,QAAK,eAAe,YAAY,SAAU;AACzC;AAAA,IACD;AACA,gBAAY,UAAU;AAEtB,4CAAwC;AACxC,0BAAuB,iBAAiB,EAAE,MAAM,QAAQ,CAAE;AAAA,EAC3D,GAAG;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AACH;",
"names": []
}