@secretstache/wordpress-gutenberg 0.5.12 → 0.5.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.js +2 -2
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useParentBlock.js +1 -2
- package/src/hooks/useTabs.js +29 -1
package/package.json
CHANGED
@@ -20,8 +20,7 @@ export const useParentBlock = (
|
|
20
20
|
return null;
|
21
21
|
}
|
22
22
|
|
23
|
-
|
24
|
-
if (currentBlock?.name === parentBlockName) {
|
23
|
+
if (currentBlock.clientId === blockClientIdToLimitSearch && currentBlock.name === parentBlockName) {
|
25
24
|
return currentBlock;
|
26
25
|
}
|
27
26
|
|
package/src/hooks/useTabs.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { useSelect, useDispatch } from '@wordpress/data';
|
1
|
+
import { useSelect, useDispatch, select } from '@wordpress/data';
|
2
2
|
import { store as blockEditorStore } from '@wordpress/block-editor';
|
3
3
|
import { useLayoutEffect, useState } from '@wordpress/element';
|
4
4
|
import { Button } from '@wordpress/components';
|
@@ -50,12 +50,40 @@ export const useTabs = (tabsClientId, tabItemName) => {
|
|
50
50
|
|
51
51
|
const parentBlock = useParentBlock(tabItemName, tabsClientId);
|
52
52
|
|
53
|
+
/**
|
54
|
+
* Sets the active tab when focus/selection is inside any inner block of a tab item.
|
55
|
+
* For example, if the user selects text inside a tab, that tab should become active.
|
56
|
+
*/
|
53
57
|
useLayoutEffect(() => {
|
54
58
|
if (parentBlock) {
|
55
59
|
setActiveItemId(parentBlock.clientId);
|
56
60
|
}
|
57
61
|
}, [ parentBlock ]);
|
58
62
|
|
63
|
+
/**
|
64
|
+
* Sets the active tab when the tab-item block itself is selected in the editor sidebar.
|
65
|
+
* Ensures the selected tab-item belongs directly to this Tabs block (avoids conflicts if there are multiple Tabs on the page).
|
66
|
+
*/
|
67
|
+
useLayoutEffect(() => {
|
68
|
+
// Early return if there is no selected block, or if the selected block is not a tab-item.
|
69
|
+
const isTabItemBlockSelected = selectedBlock && selectedBlock.name === tabItemName;
|
70
|
+
|
71
|
+
if (!isTabItemBlockSelected) {
|
72
|
+
return;
|
73
|
+
}
|
74
|
+
|
75
|
+
// Get the parent block clientId for this selected tab-item block.
|
76
|
+
const selectedTabItemBlock = selectedBlock;
|
77
|
+
const tabItemParentId = select('core/block-editor').getBlockRootClientId(selectedTabItemBlock.clientId);
|
78
|
+
|
79
|
+
// Only activate the tab if it is a direct child of this Tabs instance.
|
80
|
+
const isDirectChildOfCurrentTabs = tabItemParentId === tabsClientId;
|
81
|
+
|
82
|
+
if (isDirectChildOfCurrentTabs) {
|
83
|
+
setActiveItemId(selectedTabItemBlock.clientId);
|
84
|
+
}
|
85
|
+
}, [ selectedBlock, tabItemName, tabsClientId ]);
|
86
|
+
|
59
87
|
const createTab = (blockName, attributes = {}, position = tabsCount) => {
|
60
88
|
const newTab = createBlock(blockName, attributes);
|
61
89
|
insertBlock(newTab, position, tabsClientId);
|