UNPKG

@wordpress/block-library

Version:
1 lines • 5.52 kB
{"version":3,"file":"use-tab-actions.cjs","names":["useRegistry","useDispatch","blockEditorStore","createBlock"],"sources":["../../src/tabs/use-tab-actions.js"],"sourcesContent":["import { createBlock } from '@wordpress/blocks';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useDispatch, useRegistry } from '@wordpress/data';\n\n/**\n * Provides callbacks to insert, remove and move tabs for a tabs block.\n *\n * The hook intentionally avoids subscribing to the store: all data is derived\n * lazily inside the callbacks via `registry.select`, so consumers don't\n * re-render when the tab structure changes.\n *\n * @param {string|null} tabsClientId The client ID of the parent tabs block.\n * @return {{ insertTab: Function, removeTab: Function, moveTab: Function }} Tab action callbacks.\n */\nexport default function useTabActions( tabsClientId ) {\n\tconst registry = useRegistry();\n\tconst {\n\t\tinsertBlock,\n\t\tremoveBlock,\n\t\tmoveBlocksToPosition,\n\t\tupdateBlockAttributes,\n\t\t__unstableMarkNextChangeAsNotPersistent,\n\t} = useDispatch( blockEditorStore );\n\n\t// Derive the current tabs state at call time without subscribing.\n\tconst getTabsState = () => {\n\t\tconst { getBlocks, getBlockAttributes } =\n\t\t\tregistry.select( blockEditorStore );\n\n\t\tconst tabsAttributes = tabsClientId\n\t\t\t? getBlockAttributes( tabsClientId )\n\t\t\t: undefined;\n\t\tconst tabPanels = tabsClientId\n\t\t\t? getBlocks( tabsClientId ).find(\n\t\t\t\t\t( block ) => block.name === 'core/tab-panels'\n\t\t\t\t)\n\t\t\t: undefined;\n\n\t\treturn {\n\t\t\ttabPanelsClientId: tabPanels?.clientId ?? null,\n\t\t\ttabPanelBlocks: tabPanels?.innerBlocks ?? [],\n\t\t\tactiveIndex:\n\t\t\t\ttabsAttributes?.editorActiveTabIndex ??\n\t\t\t\ttabsAttributes?.activeTabIndex ??\n\t\t\t\t0,\n\t\t};\n\t};\n\n\t// Insert a new tab and make it active. Defaults to appending at the end.\n\tconst insertTab = ( atIndex ) => {\n\t\tconst { tabPanelsClientId, tabPanelBlocks } = getTabsState();\n\t\tif ( ! tabPanelsClientId ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newIndex = atIndex ?? tabPanelBlocks.length;\n\t\tinsertBlock(\n\t\t\tcreateBlock( 'core/tab-panel' ),\n\t\t\tnewIndex,\n\t\t\ttabPanelsClientId,\n\t\t\tfalse\n\t\t);\n\n\t\t// Switch editor active tab to the new tab.\n\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\tupdateBlockAttributes( tabsClientId, {\n\t\t\teditorActiveTabIndex: newIndex,\n\t\t} );\n\t};\n\n\t// Remove a tab and move the active tab to an adjacent one. Defaults to the\n\t// currently active tab.\n\tconst removeTab = ( atIndex ) => {\n\t\tconst { tabPanelBlocks, activeIndex } = getTabsState();\n\t\tconst tabCount = tabPanelBlocks.length;\n\t\tif ( tabCount <= 1 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst removeIndex = atIndex ?? activeIndex;\n\t\tconst target = tabPanelBlocks[ removeIndex ];\n\t\tif ( ! target ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Calculate new active index after removal.\n\t\tconst newActiveIndex =\n\t\t\tremoveIndex >= tabCount - 1 ? tabCount - 2 : removeIndex;\n\n\t\t// Switch editor to the adjacent tab and remove the current one.\n\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\tupdateBlockAttributes( tabsClientId, {\n\t\t\teditorActiveTabIndex: newActiveIndex,\n\t\t} );\n\t\tremoveBlock( target.clientId, false );\n\t};\n\n\t// Move the active tab one position left or right by reordering the\n\t// underlying tab-panel. The tab-list labels follow their panel\n\t// automatically via useTabListItemsSync.\n\tconst moveTab = ( direction ) => {\n\t\tconst { tabPanelsClientId, tabPanelBlocks, activeIndex } =\n\t\t\tgetTabsState();\n\t\tif ( ! tabPanelsClientId ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst toIndex = activeIndex + direction;\n\t\tconst target = tabPanelBlocks[ activeIndex ];\n\t\tif ( ! target || toIndex < 0 || toIndex >= tabPanelBlocks.length ) {\n\t\t\treturn;\n\t\t}\n\n\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\tupdateBlockAttributes( tabsClientId, {\n\t\t\teditorActiveTabIndex: toIndex,\n\t\t} );\n\t\tmoveBlocksToPosition(\n\t\t\t[ target.clientId ],\n\t\t\ttabPanelsClientId,\n\t\t\ttabPanelsClientId,\n\t\t\ttoIndex\n\t\t);\n\t};\n\n\treturn { insertTab, removeTab, moveTab };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAcA,SAAwB,cAAe,cAAe;CACrD,MAAM,YAAA,GAAWA,gBAAAA,YAAAA,CAAY;CAC7B,MAAM,EACL,aACA,aACA,sBACA,uBACA,6CAAA,GACGC,gBAAAA,YAAAA,CAAaC,wBAAAA,KAAiB;CAGlC,MAAM,qBAAqB;EAC1B,MAAM,EAAE,WAAW,uBAClB,SAAS,OAAQA,wBAAAA,KAAiB;EAEnC,MAAM,iBAAiB,eACpB,mBAAoB,YAAa,IACjC,KAAA;EACH,MAAM,YAAY,eACf,UAAW,YAAa,CAAC,CAAC,MACxB,UAAW,MAAM,SAAS,iBAC7B,IACC,KAAA;EAEH,OAAO;GACN,mBAAmB,WAAW,YAAY;GAC1C,gBAAgB,WAAW,eAAe,CAAC;GAC3C,aACC,gBAAgB,wBAChB,gBAAgB,kBAChB;EACF;CACD;CAGA,MAAM,aAAc,YAAa;EAChC,MAAM,EAAE,mBAAmB,mBAAmB,aAAa;EAC3D,IAAK,CAAE,mBACN;EAGD,MAAM,WAAW,WAAW,eAAe;EAC3C,aAAA,GACCC,kBAAAA,YAAAA,CAAa,gBAAiB,GAC9B,UACA,mBACA,KACD;EAGA,wCAAwC;EACxC,sBAAuB,cAAc,EACpC,sBAAsB,SACvB,CAAE;CACH;CAIA,MAAM,aAAc,YAAa;EAChC,MAAM,EAAE,gBAAgB,gBAAgB,aAAa;EACrD,MAAM,WAAW,eAAe;EAChC,IAAK,YAAY,GAChB;EAGD,MAAM,cAAc,WAAW;EAC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