@atlaskit/editor-common 134.14.0 → 134.15.0

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.
Files changed (26) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/cjs/registered-menu/PreviewItemIndexContext.js +12 -0
  3. package/dist/cjs/registered-menu/RegisteredMenuList.js +8 -12
  4. package/dist/cjs/registered-menu/RegisteredMenuListItemRow.js +11 -11
  5. package/dist/cjs/registered-menu/useRegisteredMenu.js +59 -39
  6. package/dist/cjs/registered-menu/useRegisteredMenuSelection.js +4 -0
  7. package/dist/es2019/registered-menu/PreviewItemIndexContext.js +6 -0
  8. package/dist/es2019/registered-menu/RegisteredMenuList.js +7 -10
  9. package/dist/es2019/registered-menu/RegisteredMenuListItemRow.js +11 -11
  10. package/dist/es2019/registered-menu/useRegisteredMenu.js +51 -32
  11. package/dist/es2019/registered-menu/useRegisteredMenuSelection.js +2 -0
  12. package/dist/esm/registered-menu/PreviewItemIndexContext.js +6 -0
  13. package/dist/esm/registered-menu/RegisteredMenuList.js +8 -12
  14. package/dist/esm/registered-menu/RegisteredMenuListItemRow.js +11 -11
  15. package/dist/esm/registered-menu/useRegisteredMenu.js +60 -39
  16. package/dist/esm/registered-menu/useRegisteredMenuSelection.js +4 -0
  17. package/dist/types/registered-menu/PreviewItemIndexContext.d.ts +2 -0
  18. package/dist/types/registered-menu/RegisteredMenuList.d.ts +8 -7
  19. package/dist/types/registered-menu/RegisteredMenuListItemRow.d.ts +5 -5
  20. package/dist/types/registered-menu/useRegisteredMenu.d.ts +7 -6
  21. package/dist/types/registered-menu/useRegisteredMenuSelection.d.ts +2 -0
  22. package/package.json +1 -1
  23. package/dist/cjs/registered-menu/ActiveMenuItemKeyContext.js +0 -11
  24. package/dist/es2019/registered-menu/ActiveMenuItemKeyContext.js +0 -5
  25. package/dist/esm/registered-menu/ActiveMenuItemKeyContext.js +0 -5
  26. package/dist/types/registered-menu/ActiveMenuItemKeyContext.d.ts +0 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @atlaskit/editor-common
2
2
 
3
+ ## 134.15.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`5dc6e8c217a9a`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/5dc6e8c217a9a) -
8
+ [EDITOR-10045] Under the `platform_editor_slash_command` experiment, `useRegisteredMenu` and
9
+ `RegisteredMenuList` show a preview only on the selected row, so hovering one copy of an item
10
+ listed in two sections previews only that copy. `useRegisteredMenu` returns `isPreviewOpen` and
11
+ `RegisteredMenuList` accepts an `isPreviewOpen` prop, replacing `activeItemKey`; the item
12
+ callbacks take the item index instead of its key.
13
+
3
14
  ## 134.14.0
4
15
 
5
16
  ### Minor Changes
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.PreviewItemIndexContext = void 0;
7
+ var _react = require("react");
8
+ // The list caches rendered rows while scrolling, so those rows may not receive
9
+ // updated props. We pass the index of the item showing its preview through context
10
+ // so their styling updates without rebuilding the wrappers that measure row height.
11
+ // Each row compares it with its own index, which a cached row still has right.
12
+ var PreviewItemIndexContext = exports.PreviewItemIndexContext = /*#__PURE__*/(0, _react.createContext)(undefined);
@@ -11,8 +11,8 @@ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/sli
11
11
  var _react = _interopRequireWildcard(require("react"));
12
12
  var _CellMeasurer = require("react-virtualized/dist/commonjs/CellMeasurer");
13
13
  var _List = require("react-virtualized/dist/commonjs/List");
14
- var _ActiveMenuItemKeyContext = require("./ActiveMenuItemKeyContext");
15
14
  var _MeasuredRow = require("./MeasuredRow");
15
+ var _PreviewItemIndexContext = require("./PreviewItemIndexContext");
16
16
  var _RegisteredMenuListItemRow = require("./RegisteredMenuListItemRow");
17
17
  var _useMenuListHeight2 = require("./useMenuListHeight");
18
18
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
@@ -24,7 +24,8 @@ var RegisteredMenuList = exports.RegisteredMenuList = function RegisteredMenuLis
24
24
  var rows = _ref.rows,
25
25
  Item = _ref.Item,
26
26
  selectedItemIndex = _ref.selectedItemIndex,
27
- activeItemKey = _ref.activeItemKey,
27
+ _ref$isPreviewOpen = _ref.isPreviewOpen,
28
+ isPreviewOpen = _ref$isPreviewOpen === void 0 ? false : _ref$isPreviewOpen,
28
29
  maxHeight = _ref.maxHeight,
29
30
  width = _ref.width,
30
31
  getItemId = _ref.getItemId,
@@ -126,13 +127,8 @@ var RegisteredMenuList = exports.RegisteredMenuList = function RegisteredMenuLis
126
127
  overscanStopIndex = _ref3.overscanStopIndex,
127
128
  startIndex = _ref3.startIndex,
128
129
  stopIndex = _ref3.stopIndex;
129
- if (activeItemKey) {
130
- var activeRowIndex = rows.findIndex(function (row) {
131
- return row.type === 'menu-item' && row.key === activeItemKey;
132
- });
133
- if (activeRowIndex >= 0 && (activeRowIndex < startIndex || activeRowIndex > stopIndex)) {
134
- onItemVisibilityChange(activeItemKey, false);
135
- }
130
+ if (isPreviewOpen && selectedRowIndex >= 0 && (selectedRowIndex < startIndex || selectedRowIndex > stopIndex)) {
131
+ onItemVisibilityChange(selectedItemIndex, false);
136
132
  }
137
133
  updateHeight();
138
134
  var indexes = (0, _toConsumableArray2.default)(itemIndexByRowIndex).filter(function (_ref4) {
@@ -148,7 +144,7 @@ var RegisteredMenuList = exports.RegisteredMenuList = function RegisteredMenuLis
148
144
  startIndex: (_indexes$ = indexes[0]) !== null && _indexes$ !== void 0 ? _indexes$ : -1,
149
145
  stopIndex: (_indexes = indexes[indexes.length - 1]) !== null && _indexes !== void 0 ? _indexes : -1
150
146
  });
151
- }, [activeItemKey, itemIndexByRowIndex, onItemVisibilityChange, onRenderedItemsChange, rows, updateHeight]);
147
+ }, [isPreviewOpen, itemIndexByRowIndex, onItemVisibilityChange, onRenderedItemsChange, selectedItemIndex, selectedRowIndex, updateHeight]);
152
148
  var renderRow = (0, _react.useCallback)(function (_ref8) {
153
149
  var index = _ref8.index,
154
150
  key = _ref8.key,
@@ -208,8 +204,8 @@ var RegisteredMenuList = exports.RegisteredMenuList = function RegisteredMenuLis
208
204
  if (rows.length === 0) {
209
205
  return null;
210
206
  }
211
- return /*#__PURE__*/_react.default.createElement(_ActiveMenuItemKeyContext.ActiveMenuItemKeyContext.Provider, {
212
- value: activeItemKey
207
+ return /*#__PURE__*/_react.default.createElement(_PreviewItemIndexContext.PreviewItemIndexContext.Provider, {
208
+ value: isPreviewOpen ? selectedItemIndex : undefined
213
209
  }, /*#__PURE__*/_react.default.createElement(_List.List, {
214
210
  "aria-readonly": ariaReadonly,
215
211
  containerRole: containerRole,
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.RegisteredMenuListItemRow = void 0;
8
8
  var _react = _interopRequireWildcard(require("react"));
9
- var _ActiveMenuItemKeyContext = require("./ActiveMenuItemKeyContext");
9
+ var _PreviewItemIndexContext = require("./PreviewItemIndexContext");
10
10
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
11
11
  /**
12
12
  * Connects a rendered item to the list's active state and interaction callbacks.
@@ -27,35 +27,35 @@ var RegisteredMenuListItemRow = exports.RegisteredMenuListItemRow = function Reg
27
27
  registration = _ref.registration,
28
28
  rowIndex = _ref.rowIndex,
29
29
  selectedItemIndex = _ref.selectedItemIndex;
30
- var activeItemKey = (0, _react.useContext)(_ActiveMenuItemKeyContext.ActiveMenuItemKeyContext);
30
+ var previewItemIndex = (0, _react.useContext)(_PreviewItemIndexContext.PreviewItemIndexContext);
31
31
  (0, _react.useLayoutEffect)(function () {
32
- onItemVisibilityChange(registration.key, isVisible);
32
+ onItemVisibilityChange(itemIndex, isVisible);
33
33
  return function () {
34
- return onItemVisibilityChange(registration.key, false);
34
+ return onItemVisibilityChange(itemIndex, false);
35
35
  };
36
- }, [isVisible, onItemVisibilityChange, registration.key]);
36
+ }, [isVisible, itemIndex, onItemVisibilityChange]);
37
37
  var onBlur = (0, _react.useCallback)(function (event) {
38
38
  if (!event.currentTarget.contains(event.relatedTarget)) {
39
- onItemBlur(registration.key);
39
+ onItemBlur(itemIndex);
40
40
  }
41
- }, [onItemBlur, registration.key]);
41
+ }, [itemIndex, onItemBlur]);
42
42
  return /*#__PURE__*/_react.default.createElement("div", {
43
43
  onBlur: onBlur,
44
44
  onFocus: function onFocus() {
45
- return onItemFocus(itemIndex, registration.key);
45
+ return onItemFocus(itemIndex);
46
46
  },
47
47
  onMouseLeave: function onMouseLeave() {
48
- return onItemLeave(registration.key);
48
+ return onItemLeave(itemIndex);
49
49
  },
50
50
  onMouseMove: function onMouseMove() {
51
- return onItemHover(itemIndex, registration.key);
51
+ return onItemHover(itemIndex);
52
52
  }
53
53
  }, /*#__PURE__*/_react.default.createElement(Item, {
54
54
  id: getItemId({
55
55
  itemIndex: itemIndex,
56
56
  rowIndex: rowIndex
57
57
  }),
58
- isActive: activeItemKey === registration.key,
58
+ isActive: previewItemIndex === itemIndex,
59
59
  isSelected: itemIndex === selectedItemIndex,
60
60
  registration: registration
61
61
  }));
@@ -11,6 +11,8 @@ var _react = require("react");
11
11
  var _surfaceRenderer = require("@atlaskit/editor-ui-control-model/surface-renderer");
12
12
  var _useRegisteredMenuSelection = require("./useRegisteredMenuSelection");
13
13
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
14
+ /** An open preview always belongs to the selected item, so only whether it's open is stored. */
15
+
14
16
  var getSelectableItemIdentity = function getSelectableItemIdentity(item, scope) {
15
17
  return JSON.stringify([scope.type, scope.type === 'section' ? scope.sectionKey : undefined, (0, _surfaceRenderer.getComponentIdentity)(item)]);
16
18
  };
@@ -69,78 +71,96 @@ var useRegisteredMenu = exports.useRegisteredMenu = function useRegisteredMenu(_
69
71
  identities: selectableItemIdentities,
70
72
  resetKey: resetKey
71
73
  });
72
- var selectedItemIndex = selection.selectedIndex,
74
+ var getSelectedIndex = selection.getSelectedIndex,
75
+ selectedItemIndex = selection.selectedIndex,
73
76
  selectionSource = selection.selectionSource,
74
77
  select = selection.select;
75
78
  var _useState = (0, _react.useState)(),
76
79
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
77
80
  activation = _useState2[0],
78
- setItemActivation = _useState2[1];
79
- var activeItemKey = (activation === null || activation === void 0 ? void 0 : activation.model) === model && activation.resetKey === resetKey ? activation.itemKey : undefined;
81
+ setActivation = _useState2[1];
82
+ var isPreviewOpen = (activation === null || activation === void 0 ? void 0 : activation.model) === model && activation.resetKey === resetKey;
80
83
  (0, _react.useLayoutEffect)(function () {
81
- setItemActivation(undefined);
84
+ setActivation(undefined);
82
85
  }, [model, resetKey]);
86
+
87
+ /**
88
+ * Closes the preview when the event comes from the selected item, optionally only for one source.
89
+ * Rows report leaving, blur and visibility from effects, including cleanups. Reading the latest
90
+ * selection keeps this callback stable, so a selection change doesn't re-run those effects and
91
+ * a row's cleanup can't close the preview that just moved to another row.
92
+ */
93
+ var closePreviewFor = (0, _react.useCallback)(function (index, source) {
94
+ if (index !== getSelectedIndex()) {
95
+ return;
96
+ }
97
+ setActivation(function (current) {
98
+ return current && (source === undefined || current.source === source) ? undefined : current;
99
+ });
100
+ }, [getSelectedIndex]);
101
+
102
+ // Only listed items show a preview; the footer comes after them.
103
+ var hasPreview = (0, _react.useCallback)(function (index) {
104
+ return index < selectableItems.length;
105
+ }, [selectableItems]);
83
106
  var activateKeyboardItem = (0, _react.useCallback)(function (index) {
84
- var item = selectableItems[index];
85
- setItemActivation(item ? {
86
- itemKey: item.key,
107
+ setActivation(hasPreview(index) ? {
87
108
  model: model,
88
109
  resetKey: resetKey,
89
110
  source: 'keyboard'
90
111
  } : undefined);
91
- }, [model, resetKey, selectableItems]);
112
+ }, [hasPreview, model, resetKey]);
92
113
  var selectNext = (0, _react.useCallback)(function () {
93
114
  activateKeyboardItem(selection.selectNext());
94
115
  }, [selection.selectNext, activateKeyboardItem]);
95
116
  var selectPrevious = (0, _react.useCallback)(function () {
96
117
  activateKeyboardItem(selection.selectPrevious());
97
118
  }, [selection.selectPrevious, activateKeyboardItem]);
98
- var onItemHover = (0, _react.useCallback)(function (index, itemKey) {
119
+ var onItemHover = (0, _react.useCallback)(function (index) {
99
120
  select(index, 'pointer');
100
- setItemActivation(function (current) {
101
- if (itemKey && (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey && current.model === model && current.resetKey === resetKey && current.source === 'pointer') {
121
+ setActivation(function (current) {
122
+ if (!hasPreview(index)) {
123
+ return undefined;
124
+ }
125
+ if ((current === null || current === void 0 ? void 0 : current.model) === model && current.resetKey === resetKey && current.source === 'pointer') {
102
126
  return current;
103
127
  }
104
- return itemKey ? {
105
- itemKey: itemKey,
128
+ return {
106
129
  model: model,
107
130
  resetKey: resetKey,
108
131
  source: 'pointer'
109
- } : undefined;
132
+ };
110
133
  });
111
- }, [model, resetKey, select]);
112
- var onItemFocus = (0, _react.useCallback)(function (index, itemKey) {
134
+ }, [hasPreview, model, resetKey, select]);
135
+ var onItemFocus = (0, _react.useCallback)(function (index) {
136
+ var isSelectedItem = index === selectedItemIndex;
113
137
  // A press focuses the item the pointer already selected. Scrolling it then would move it before the click lands.
114
- select(index, index === selectedItemIndex && selectionSource === 'pointer' ? 'pointer' : 'keyboard');
115
- setItemActivation(function (current) {
116
- if (itemKey && (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey && current.model === model && current.resetKey === resetKey) {
138
+ select(index, isSelectedItem && selectionSource === 'pointer' ? 'pointer' : 'keyboard');
139
+ setActivation(function (current) {
140
+ if (!hasPreview(index)) {
141
+ return undefined;
142
+ }
143
+ if (isSelectedItem && (current === null || current === void 0 ? void 0 : current.model) === model && current.resetKey === resetKey) {
117
144
  return current;
118
145
  }
119
- return itemKey ? {
120
- itemKey: itemKey,
146
+ return {
121
147
  model: model,
122
148
  resetKey: resetKey,
123
149
  source: 'keyboard'
124
- } : undefined;
125
- });
126
- }, [model, resetKey, select, selectedItemIndex, selectionSource]);
127
- var onItemLeave = (0, _react.useCallback)(function (itemKey) {
128
- setItemActivation(function (current) {
129
- return (current === null || current === void 0 ? void 0 : current.source) === 'pointer' && current.itemKey === itemKey ? undefined : current;
130
- });
131
- }, []);
132
- var onItemBlur = (0, _react.useCallback)(function (itemKey) {
133
- setItemActivation(function (current) {
134
- return (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey ? undefined : current;
150
+ };
135
151
  });
136
- }, []);
137
- var onItemVisibilityChange = (0, _react.useCallback)(function (itemKey, isVisible) {
152
+ }, [hasPreview, model, resetKey, select, selectedItemIndex, selectionSource]);
153
+ var onItemLeave = (0, _react.useCallback)(function (index) {
154
+ return closePreviewFor(index, 'pointer');
155
+ }, [closePreviewFor]);
156
+ var onItemBlur = (0, _react.useCallback)(function (index) {
157
+ return closePreviewFor(index);
158
+ }, [closePreviewFor]);
159
+ var onItemVisibilityChange = (0, _react.useCallback)(function (index, isVisible) {
138
160
  if (!isVisible) {
139
- setItemActivation(function (current) {
140
- return (current === null || current === void 0 ? void 0 : current.source) === 'pointer' && current.itemKey === itemKey ? undefined : current;
141
- });
161
+ closePreviewFor(index, 'pointer');
142
162
  }
143
- }, []);
163
+ }, [closePreviewFor]);
144
164
  return {
145
165
  rows: rows,
146
166
  selectableItems: selectableItems,
@@ -149,7 +169,7 @@ var useRegisteredMenu = exports.useRegisteredMenu = function useRegisteredMenu(_
149
169
  select: select,
150
170
  selectNext: selectNext,
151
171
  selectPrevious: selectPrevious,
152
- activeItemKey: activeItemKey,
172
+ isPreviewOpen: isPreviewOpen,
153
173
  onItemHover: onItemHover,
154
174
  onItemFocus: onItemFocus,
155
175
  onItemLeave: onItemLeave,
@@ -73,7 +73,11 @@ var useRegisteredMenuSelection = exports.useRegisteredMenuSelection = function u
73
73
  select(previousIndex, 'keyboard');
74
74
  return previousIndex;
75
75
  }, [itemCount, select]);
76
+ var getSelectedIndex = (0, _react.useCallback)(function () {
77
+ return selectedIndexRef.current;
78
+ }, []);
76
79
  return {
80
+ getSelectedIndex: getSelectedIndex,
77
81
  selectedIndex: selectedIndex,
78
82
  selectionSource: selectionSource,
79
83
  select: select,
@@ -0,0 +1,6 @@
1
+ import { createContext } from 'react';
2
+ // The list caches rendered rows while scrolling, so those rows may not receive
3
+ // updated props. We pass the index of the item showing its preview through context
4
+ // so their styling updates without rebuilding the wrappers that measure row height.
5
+ // Each row compares it with its own index, which a cached row still has right.
6
+ export const PreviewItemIndexContext = /*#__PURE__*/createContext(undefined);
@@ -1,8 +1,8 @@
1
1
  import React, { useCallback, useLayoutEffect, useMemo, useRef } from 'react';
2
2
  import { CellMeasurer, CellMeasurerCache } from 'react-virtualized/dist/commonjs/CellMeasurer';
3
3
  import { List } from 'react-virtualized/dist/commonjs/List';
4
- import { ActiveMenuItemKeyContext } from './ActiveMenuItemKeyContext';
5
4
  import { MeasuredRow } from './MeasuredRow';
5
+ import { PreviewItemIndexContext } from './PreviewItemIndexContext';
6
6
  import { RegisteredMenuListItemRow } from './RegisteredMenuListItemRow';
7
7
  import { useMenuListHeight } from './useMenuListHeight';
8
8
  const ESTIMATED_ROW_HEIGHT = 44;
@@ -10,7 +10,7 @@ export const RegisteredMenuList = ({
10
10
  rows,
11
11
  Item,
12
12
  selectedItemIndex,
13
- activeItemKey,
13
+ isPreviewOpen = false,
14
14
  maxHeight,
15
15
  width,
16
16
  getItemId,
@@ -92,11 +92,8 @@ export const RegisteredMenuList = ({
92
92
  stopIndex
93
93
  }) => {
94
94
  var _indexes$, _indexes;
95
- if (activeItemKey) {
96
- const activeRowIndex = rows.findIndex(row => row.type === 'menu-item' && row.key === activeItemKey);
97
- if (activeRowIndex >= 0 && (activeRowIndex < startIndex || activeRowIndex > stopIndex)) {
98
- onItemVisibilityChange(activeItemKey, false);
99
- }
95
+ if (isPreviewOpen && selectedRowIndex >= 0 && (selectedRowIndex < startIndex || selectedRowIndex > stopIndex)) {
96
+ onItemVisibilityChange(selectedItemIndex, false);
100
97
  }
101
98
  updateHeight();
102
99
  const indexes = [...itemIndexByRowIndex].filter(([rowIndex]) => rowIndex >= overscanStartIndex && rowIndex <= overscanStopIndex).map(([, itemIndex]) => itemIndex);
@@ -104,7 +101,7 @@ export const RegisteredMenuList = ({
104
101
  startIndex: (_indexes$ = indexes[0]) !== null && _indexes$ !== void 0 ? _indexes$ : -1,
105
102
  stopIndex: (_indexes = indexes[indexes.length - 1]) !== null && _indexes !== void 0 ? _indexes : -1
106
103
  });
107
- }, [activeItemKey, itemIndexByRowIndex, onItemVisibilityChange, onRenderedItemsChange, rows, updateHeight]);
104
+ }, [isPreviewOpen, itemIndexByRowIndex, onItemVisibilityChange, onRenderedItemsChange, selectedItemIndex, selectedRowIndex, updateHeight]);
108
105
  const renderRow = useCallback(({
109
106
  index,
110
107
  key,
@@ -160,8 +157,8 @@ export const RegisteredMenuList = ({
160
157
  if (rows.length === 0) {
161
158
  return null;
162
159
  }
163
- return /*#__PURE__*/React.createElement(ActiveMenuItemKeyContext.Provider, {
164
- value: activeItemKey
160
+ return /*#__PURE__*/React.createElement(PreviewItemIndexContext.Provider, {
161
+ value: isPreviewOpen ? selectedItemIndex : undefined
165
162
  }, /*#__PURE__*/React.createElement(List, {
166
163
  "aria-readonly": ariaReadonly,
167
164
  containerRole: containerRole,
@@ -1,5 +1,5 @@
1
1
  import React, { useCallback, useContext, useLayoutEffect } from 'react';
2
- import { ActiveMenuItemKeyContext } from './ActiveMenuItemKeyContext';
2
+ import { PreviewItemIndexContext } from './PreviewItemIndexContext';
3
3
  /**
4
4
  * Connects a rendered item to the list's active state and interaction callbacks.
5
5
  * The list may reuse this row while scrolling, so context keeps its active styling
@@ -20,27 +20,27 @@ export const RegisteredMenuListItemRow = ({
20
20
  rowIndex,
21
21
  selectedItemIndex
22
22
  }) => {
23
- const activeItemKey = useContext(ActiveMenuItemKeyContext);
23
+ const previewItemIndex = useContext(PreviewItemIndexContext);
24
24
  useLayoutEffect(() => {
25
- onItemVisibilityChange(registration.key, isVisible);
26
- return () => onItemVisibilityChange(registration.key, false);
27
- }, [isVisible, onItemVisibilityChange, registration.key]);
25
+ onItemVisibilityChange(itemIndex, isVisible);
26
+ return () => onItemVisibilityChange(itemIndex, false);
27
+ }, [isVisible, itemIndex, onItemVisibilityChange]);
28
28
  const onBlur = useCallback(event => {
29
29
  if (!event.currentTarget.contains(event.relatedTarget)) {
30
- onItemBlur(registration.key);
30
+ onItemBlur(itemIndex);
31
31
  }
32
- }, [onItemBlur, registration.key]);
32
+ }, [itemIndex, onItemBlur]);
33
33
  return /*#__PURE__*/React.createElement("div", {
34
34
  onBlur: onBlur,
35
- onFocus: () => onItemFocus(itemIndex, registration.key),
36
- onMouseLeave: () => onItemLeave(registration.key),
37
- onMouseMove: () => onItemHover(itemIndex, registration.key)
35
+ onFocus: () => onItemFocus(itemIndex),
36
+ onMouseLeave: () => onItemLeave(itemIndex),
37
+ onMouseMove: () => onItemHover(itemIndex)
38
38
  }, /*#__PURE__*/React.createElement(Item, {
39
39
  id: getItemId({
40
40
  itemIndex,
41
41
  rowIndex
42
42
  }),
43
- isActive: activeItemKey === registration.key,
43
+ isActive: previewItemIndex === itemIndex,
44
44
  isSelected: itemIndex === selectedItemIndex,
45
45
  registration: registration
46
46
  }));
@@ -1,6 +1,9 @@
1
1
  import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
2
2
  import { getComponentIdentity } from '@atlaskit/editor-ui-control-model/surface-renderer';
3
3
  import { useRegisteredMenuSelection } from './useRegisteredMenuSelection';
4
+
5
+ /** An open preview always belongs to the selected item, so only whether it's open is stored. */
6
+
4
7
  const getSelectableItemIdentity = (item, scope) => JSON.stringify([scope.type, scope.type === 'section' ? scope.sectionKey : undefined, getComponentIdentity(item)]);
5
8
  /**
6
9
  * Connects menu rows to keyboard selection and tracks which item is active through
@@ -53,70 +56,86 @@ export const useRegisteredMenu = ({
53
56
  resetKey
54
57
  });
55
58
  const {
59
+ getSelectedIndex,
56
60
  selectedIndex: selectedItemIndex,
57
61
  selectionSource,
58
62
  select
59
63
  } = selection;
60
- const [activation, setItemActivation] = useState();
61
- const activeItemKey = (activation === null || activation === void 0 ? void 0 : activation.model) === model && activation.resetKey === resetKey ? activation.itemKey : undefined;
64
+ const [activation, setActivation] = useState();
65
+ const isPreviewOpen = (activation === null || activation === void 0 ? void 0 : activation.model) === model && activation.resetKey === resetKey;
62
66
  useLayoutEffect(() => {
63
- setItemActivation(undefined);
67
+ setActivation(undefined);
64
68
  }, [model, resetKey]);
69
+
70
+ /**
71
+ * Closes the preview when the event comes from the selected item, optionally only for one source.
72
+ * Rows report leaving, blur and visibility from effects, including cleanups. Reading the latest
73
+ * selection keeps this callback stable, so a selection change doesn't re-run those effects and
74
+ * a row's cleanup can't close the preview that just moved to another row.
75
+ */
76
+ const closePreviewFor = useCallback((index, source) => {
77
+ if (index !== getSelectedIndex()) {
78
+ return;
79
+ }
80
+ setActivation(current => current && (source === undefined || current.source === source) ? undefined : current);
81
+ }, [getSelectedIndex]);
82
+
83
+ // Only listed items show a preview; the footer comes after them.
84
+ const hasPreview = useCallback(index => index < selectableItems.length, [selectableItems]);
65
85
  const activateKeyboardItem = useCallback(index => {
66
- const item = selectableItems[index];
67
- setItemActivation(item ? {
68
- itemKey: item.key,
86
+ setActivation(hasPreview(index) ? {
69
87
  model,
70
88
  resetKey,
71
89
  source: 'keyboard'
72
90
  } : undefined);
73
- }, [model, resetKey, selectableItems]);
91
+ }, [hasPreview, model, resetKey]);
74
92
  const selectNext = useCallback(() => {
75
93
  activateKeyboardItem(selection.selectNext());
76
94
  }, [selection.selectNext, activateKeyboardItem]);
77
95
  const selectPrevious = useCallback(() => {
78
96
  activateKeyboardItem(selection.selectPrevious());
79
97
  }, [selection.selectPrevious, activateKeyboardItem]);
80
- const onItemHover = useCallback((index, itemKey) => {
98
+ const onItemHover = useCallback(index => {
81
99
  select(index, 'pointer');
82
- setItemActivation(current => {
83
- if (itemKey && (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey && current.model === model && current.resetKey === resetKey && current.source === 'pointer') {
100
+ setActivation(current => {
101
+ if (!hasPreview(index)) {
102
+ return undefined;
103
+ }
104
+ if ((current === null || current === void 0 ? void 0 : current.model) === model && current.resetKey === resetKey && current.source === 'pointer') {
84
105
  return current;
85
106
  }
86
- return itemKey ? {
87
- itemKey,
107
+ return {
88
108
  model,
89
109
  resetKey,
90
110
  source: 'pointer'
91
- } : undefined;
111
+ };
92
112
  });
93
- }, [model, resetKey, select]);
94
- const onItemFocus = useCallback((index, itemKey) => {
113
+ }, [hasPreview, model, resetKey, select]);
114
+ const onItemFocus = useCallback(index => {
115
+ const isSelectedItem = index === selectedItemIndex;
95
116
  // A press focuses the item the pointer already selected. Scrolling it then would move it before the click lands.
96
- select(index, index === selectedItemIndex && selectionSource === 'pointer' ? 'pointer' : 'keyboard');
97
- setItemActivation(current => {
98
- if (itemKey && (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey && current.model === model && current.resetKey === resetKey) {
117
+ select(index, isSelectedItem && selectionSource === 'pointer' ? 'pointer' : 'keyboard');
118
+ setActivation(current => {
119
+ if (!hasPreview(index)) {
120
+ return undefined;
121
+ }
122
+ if (isSelectedItem && (current === null || current === void 0 ? void 0 : current.model) === model && current.resetKey === resetKey) {
99
123
  return current;
100
124
  }
101
- return itemKey ? {
102
- itemKey,
125
+ return {
103
126
  model,
104
127
  resetKey,
105
128
  source: 'keyboard'
106
- } : undefined;
129
+ };
107
130
  });
108
- }, [model, resetKey, select, selectedItemIndex, selectionSource]);
109
- const onItemLeave = useCallback(itemKey => {
110
- setItemActivation(current => (current === null || current === void 0 ? void 0 : current.source) === 'pointer' && current.itemKey === itemKey ? undefined : current);
111
- }, []);
112
- const onItemBlur = useCallback(itemKey => {
113
- setItemActivation(current => (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey ? undefined : current);
114
- }, []);
115
- const onItemVisibilityChange = useCallback((itemKey, isVisible) => {
131
+ }, [hasPreview, model, resetKey, select, selectedItemIndex, selectionSource]);
132
+ const onItemLeave = useCallback(index => closePreviewFor(index, 'pointer'), [closePreviewFor]);
133
+ const onItemBlur = useCallback(index => closePreviewFor(index), [closePreviewFor]);
134
+ const onItemVisibilityChange = useCallback((index, isVisible) => {
116
135
  if (!isVisible) {
117
- setItemActivation(current => (current === null || current === void 0 ? void 0 : current.source) === 'pointer' && current.itemKey === itemKey ? undefined : current);
136
+ closePreviewFor(index, 'pointer');
118
137
  }
119
- }, []);
138
+ }, [closePreviewFor]);
120
139
  return {
121
140
  rows,
122
141
  selectableItems,
@@ -125,7 +144,7 @@ export const useRegisteredMenu = ({
125
144
  select,
126
145
  selectNext,
127
146
  selectPrevious,
128
- activeItemKey,
147
+ isPreviewOpen,
129
148
  onItemHover,
130
149
  onItemFocus,
131
150
  onItemLeave,
@@ -58,7 +58,9 @@ export const useRegisteredMenuSelection = ({
58
58
  select(previousIndex, 'keyboard');
59
59
  return previousIndex;
60
60
  }, [itemCount, select]);
61
+ const getSelectedIndex = useCallback(() => selectedIndexRef.current, []);
61
62
  return {
63
+ getSelectedIndex,
62
64
  selectedIndex,
63
65
  selectionSource,
64
66
  select,
@@ -0,0 +1,6 @@
1
+ import { createContext } from 'react';
2
+ // The list caches rendered rows while scrolling, so those rows may not receive
3
+ // updated props. We pass the index of the item showing its preview through context
4
+ // so their styling updates without rebuilding the wrappers that measure row height.
5
+ // Each row compares it with its own index, which a cached row still has right.
6
+ export var PreviewItemIndexContext = /*#__PURE__*/createContext(undefined);
@@ -6,8 +6,8 @@ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length)
6
6
  import React, { useCallback, useLayoutEffect, useMemo, useRef } from 'react';
7
7
  import { CellMeasurer, CellMeasurerCache } from 'react-virtualized/dist/commonjs/CellMeasurer';
8
8
  import { List } from 'react-virtualized/dist/commonjs/List';
9
- import { ActiveMenuItemKeyContext } from './ActiveMenuItemKeyContext';
10
9
  import { MeasuredRow } from './MeasuredRow';
10
+ import { PreviewItemIndexContext } from './PreviewItemIndexContext';
11
11
  import { RegisteredMenuListItemRow } from './RegisteredMenuListItemRow';
12
12
  import { useMenuListHeight } from './useMenuListHeight';
13
13
  var ESTIMATED_ROW_HEIGHT = 44;
@@ -15,7 +15,8 @@ export var RegisteredMenuList = function RegisteredMenuList(_ref) {
15
15
  var rows = _ref.rows,
16
16
  Item = _ref.Item,
17
17
  selectedItemIndex = _ref.selectedItemIndex,
18
- activeItemKey = _ref.activeItemKey,
18
+ _ref$isPreviewOpen = _ref.isPreviewOpen,
19
+ isPreviewOpen = _ref$isPreviewOpen === void 0 ? false : _ref$isPreviewOpen,
19
20
  maxHeight = _ref.maxHeight,
20
21
  width = _ref.width,
21
22
  getItemId = _ref.getItemId,
@@ -117,13 +118,8 @@ export var RegisteredMenuList = function RegisteredMenuList(_ref) {
117
118
  overscanStopIndex = _ref3.overscanStopIndex,
118
119
  startIndex = _ref3.startIndex,
119
120
  stopIndex = _ref3.stopIndex;
120
- if (activeItemKey) {
121
- var activeRowIndex = rows.findIndex(function (row) {
122
- return row.type === 'menu-item' && row.key === activeItemKey;
123
- });
124
- if (activeRowIndex >= 0 && (activeRowIndex < startIndex || activeRowIndex > stopIndex)) {
125
- onItemVisibilityChange(activeItemKey, false);
126
- }
121
+ if (isPreviewOpen && selectedRowIndex >= 0 && (selectedRowIndex < startIndex || selectedRowIndex > stopIndex)) {
122
+ onItemVisibilityChange(selectedItemIndex, false);
127
123
  }
128
124
  updateHeight();
129
125
  var indexes = _toConsumableArray(itemIndexByRowIndex).filter(function (_ref4) {
@@ -139,7 +135,7 @@ export var RegisteredMenuList = function RegisteredMenuList(_ref) {
139
135
  startIndex: (_indexes$ = indexes[0]) !== null && _indexes$ !== void 0 ? _indexes$ : -1,
140
136
  stopIndex: (_indexes = indexes[indexes.length - 1]) !== null && _indexes !== void 0 ? _indexes : -1
141
137
  });
142
- }, [activeItemKey, itemIndexByRowIndex, onItemVisibilityChange, onRenderedItemsChange, rows, updateHeight]);
138
+ }, [isPreviewOpen, itemIndexByRowIndex, onItemVisibilityChange, onRenderedItemsChange, selectedItemIndex, selectedRowIndex, updateHeight]);
143
139
  var renderRow = useCallback(function (_ref8) {
144
140
  var index = _ref8.index,
145
141
  key = _ref8.key,
@@ -199,8 +195,8 @@ export var RegisteredMenuList = function RegisteredMenuList(_ref) {
199
195
  if (rows.length === 0) {
200
196
  return null;
201
197
  }
202
- return /*#__PURE__*/React.createElement(ActiveMenuItemKeyContext.Provider, {
203
- value: activeItemKey
198
+ return /*#__PURE__*/React.createElement(PreviewItemIndexContext.Provider, {
199
+ value: isPreviewOpen ? selectedItemIndex : undefined
204
200
  }, /*#__PURE__*/React.createElement(List, {
205
201
  "aria-readonly": ariaReadonly,
206
202
  containerRole: containerRole,
@@ -1,5 +1,5 @@
1
1
  import React, { useCallback, useContext, useLayoutEffect } from 'react';
2
- import { ActiveMenuItemKeyContext } from './ActiveMenuItemKeyContext';
2
+ import { PreviewItemIndexContext } from './PreviewItemIndexContext';
3
3
  /**
4
4
  * Connects a rendered item to the list's active state and interaction callbacks.
5
5
  * The list may reuse this row while scrolling, so context keeps its active styling
@@ -19,35 +19,35 @@ export var RegisteredMenuListItemRow = function RegisteredMenuListItemRow(_ref)
19
19
  registration = _ref.registration,
20
20
  rowIndex = _ref.rowIndex,
21
21
  selectedItemIndex = _ref.selectedItemIndex;
22
- var activeItemKey = useContext(ActiveMenuItemKeyContext);
22
+ var previewItemIndex = useContext(PreviewItemIndexContext);
23
23
  useLayoutEffect(function () {
24
- onItemVisibilityChange(registration.key, isVisible);
24
+ onItemVisibilityChange(itemIndex, isVisible);
25
25
  return function () {
26
- return onItemVisibilityChange(registration.key, false);
26
+ return onItemVisibilityChange(itemIndex, false);
27
27
  };
28
- }, [isVisible, onItemVisibilityChange, registration.key]);
28
+ }, [isVisible, itemIndex, onItemVisibilityChange]);
29
29
  var onBlur = useCallback(function (event) {
30
30
  if (!event.currentTarget.contains(event.relatedTarget)) {
31
- onItemBlur(registration.key);
31
+ onItemBlur(itemIndex);
32
32
  }
33
- }, [onItemBlur, registration.key]);
33
+ }, [itemIndex, onItemBlur]);
34
34
  return /*#__PURE__*/React.createElement("div", {
35
35
  onBlur: onBlur,
36
36
  onFocus: function onFocus() {
37
- return onItemFocus(itemIndex, registration.key);
37
+ return onItemFocus(itemIndex);
38
38
  },
39
39
  onMouseLeave: function onMouseLeave() {
40
- return onItemLeave(registration.key);
40
+ return onItemLeave(itemIndex);
41
41
  },
42
42
  onMouseMove: function onMouseMove() {
43
- return onItemHover(itemIndex, registration.key);
43
+ return onItemHover(itemIndex);
44
44
  }
45
45
  }, /*#__PURE__*/React.createElement(Item, {
46
46
  id: getItemId({
47
47
  itemIndex: itemIndex,
48
48
  rowIndex: rowIndex
49
49
  }),
50
- isActive: activeItemKey === registration.key,
50
+ isActive: previewItemIndex === itemIndex,
51
51
  isSelected: itemIndex === selectedItemIndex,
52
52
  registration: registration
53
53
  }));
@@ -4,6 +4,9 @@ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length)
4
4
  import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
5
5
  import { getComponentIdentity } from '@atlaskit/editor-ui-control-model/surface-renderer';
6
6
  import { useRegisteredMenuSelection } from './useRegisteredMenuSelection';
7
+
8
+ /** An open preview always belongs to the selected item, so only whether it's open is stored. */
9
+
7
10
  var getSelectableItemIdentity = function getSelectableItemIdentity(item, scope) {
8
11
  return JSON.stringify([scope.type, scope.type === 'section' ? scope.sectionKey : undefined, getComponentIdentity(item)]);
9
12
  };
@@ -62,78 +65,96 @@ export var useRegisteredMenu = function useRegisteredMenu(_ref) {
62
65
  identities: selectableItemIdentities,
63
66
  resetKey: resetKey
64
67
  });
65
- var selectedItemIndex = selection.selectedIndex,
68
+ var getSelectedIndex = selection.getSelectedIndex,
69
+ selectedItemIndex = selection.selectedIndex,
66
70
  selectionSource = selection.selectionSource,
67
71
  select = selection.select;
68
72
  var _useState = useState(),
69
73
  _useState2 = _slicedToArray(_useState, 2),
70
74
  activation = _useState2[0],
71
- setItemActivation = _useState2[1];
72
- var activeItemKey = (activation === null || activation === void 0 ? void 0 : activation.model) === model && activation.resetKey === resetKey ? activation.itemKey : undefined;
75
+ setActivation = _useState2[1];
76
+ var isPreviewOpen = (activation === null || activation === void 0 ? void 0 : activation.model) === model && activation.resetKey === resetKey;
73
77
  useLayoutEffect(function () {
74
- setItemActivation(undefined);
78
+ setActivation(undefined);
75
79
  }, [model, resetKey]);
80
+
81
+ /**
82
+ * Closes the preview when the event comes from the selected item, optionally only for one source.
83
+ * Rows report leaving, blur and visibility from effects, including cleanups. Reading the latest
84
+ * selection keeps this callback stable, so a selection change doesn't re-run those effects and
85
+ * a row's cleanup can't close the preview that just moved to another row.
86
+ */
87
+ var closePreviewFor = useCallback(function (index, source) {
88
+ if (index !== getSelectedIndex()) {
89
+ return;
90
+ }
91
+ setActivation(function (current) {
92
+ return current && (source === undefined || current.source === source) ? undefined : current;
93
+ });
94
+ }, [getSelectedIndex]);
95
+
96
+ // Only listed items show a preview; the footer comes after them.
97
+ var hasPreview = useCallback(function (index) {
98
+ return index < selectableItems.length;
99
+ }, [selectableItems]);
76
100
  var activateKeyboardItem = useCallback(function (index) {
77
- var item = selectableItems[index];
78
- setItemActivation(item ? {
79
- itemKey: item.key,
101
+ setActivation(hasPreview(index) ? {
80
102
  model: model,
81
103
  resetKey: resetKey,
82
104
  source: 'keyboard'
83
105
  } : undefined);
84
- }, [model, resetKey, selectableItems]);
106
+ }, [hasPreview, model, resetKey]);
85
107
  var selectNext = useCallback(function () {
86
108
  activateKeyboardItem(selection.selectNext());
87
109
  }, [selection.selectNext, activateKeyboardItem]);
88
110
  var selectPrevious = useCallback(function () {
89
111
  activateKeyboardItem(selection.selectPrevious());
90
112
  }, [selection.selectPrevious, activateKeyboardItem]);
91
- var onItemHover = useCallback(function (index, itemKey) {
113
+ var onItemHover = useCallback(function (index) {
92
114
  select(index, 'pointer');
93
- setItemActivation(function (current) {
94
- if (itemKey && (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey && current.model === model && current.resetKey === resetKey && current.source === 'pointer') {
115
+ setActivation(function (current) {
116
+ if (!hasPreview(index)) {
117
+ return undefined;
118
+ }
119
+ if ((current === null || current === void 0 ? void 0 : current.model) === model && current.resetKey === resetKey && current.source === 'pointer') {
95
120
  return current;
96
121
  }
97
- return itemKey ? {
98
- itemKey: itemKey,
122
+ return {
99
123
  model: model,
100
124
  resetKey: resetKey,
101
125
  source: 'pointer'
102
- } : undefined;
126
+ };
103
127
  });
104
- }, [model, resetKey, select]);
105
- var onItemFocus = useCallback(function (index, itemKey) {
128
+ }, [hasPreview, model, resetKey, select]);
129
+ var onItemFocus = useCallback(function (index) {
130
+ var isSelectedItem = index === selectedItemIndex;
106
131
  // A press focuses the item the pointer already selected. Scrolling it then would move it before the click lands.
107
- select(index, index === selectedItemIndex && selectionSource === 'pointer' ? 'pointer' : 'keyboard');
108
- setItemActivation(function (current) {
109
- if (itemKey && (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey && current.model === model && current.resetKey === resetKey) {
132
+ select(index, isSelectedItem && selectionSource === 'pointer' ? 'pointer' : 'keyboard');
133
+ setActivation(function (current) {
134
+ if (!hasPreview(index)) {
135
+ return undefined;
136
+ }
137
+ if (isSelectedItem && (current === null || current === void 0 ? void 0 : current.model) === model && current.resetKey === resetKey) {
110
138
  return current;
111
139
  }
112
- return itemKey ? {
113
- itemKey: itemKey,
140
+ return {
114
141
  model: model,
115
142
  resetKey: resetKey,
116
143
  source: 'keyboard'
117
- } : undefined;
118
- });
119
- }, [model, resetKey, select, selectedItemIndex, selectionSource]);
120
- var onItemLeave = useCallback(function (itemKey) {
121
- setItemActivation(function (current) {
122
- return (current === null || current === void 0 ? void 0 : current.source) === 'pointer' && current.itemKey === itemKey ? undefined : current;
123
- });
124
- }, []);
125
- var onItemBlur = useCallback(function (itemKey) {
126
- setItemActivation(function (current) {
127
- return (current === null || current === void 0 ? void 0 : current.itemKey) === itemKey ? undefined : current;
144
+ };
128
145
  });
129
- }, []);
130
- var onItemVisibilityChange = useCallback(function (itemKey, isVisible) {
146
+ }, [hasPreview, model, resetKey, select, selectedItemIndex, selectionSource]);
147
+ var onItemLeave = useCallback(function (index) {
148
+ return closePreviewFor(index, 'pointer');
149
+ }, [closePreviewFor]);
150
+ var onItemBlur = useCallback(function (index) {
151
+ return closePreviewFor(index);
152
+ }, [closePreviewFor]);
153
+ var onItemVisibilityChange = useCallback(function (index, isVisible) {
131
154
  if (!isVisible) {
132
- setItemActivation(function (current) {
133
- return (current === null || current === void 0 ? void 0 : current.source) === 'pointer' && current.itemKey === itemKey ? undefined : current;
134
- });
155
+ closePreviewFor(index, 'pointer');
135
156
  }
136
- }, []);
157
+ }, [closePreviewFor]);
137
158
  return {
138
159
  rows: rows,
139
160
  selectableItems: selectableItems,
@@ -142,7 +163,7 @@ export var useRegisteredMenu = function useRegisteredMenu(_ref) {
142
163
  select: select,
143
164
  selectNext: selectNext,
144
165
  selectPrevious: selectPrevious,
145
- activeItemKey: activeItemKey,
166
+ isPreviewOpen: isPreviewOpen,
146
167
  onItemHover: onItemHover,
147
168
  onItemFocus: onItemFocus,
148
169
  onItemLeave: onItemLeave,
@@ -66,7 +66,11 @@ export var useRegisteredMenuSelection = function useRegisteredMenuSelection(_ref
66
66
  select(previousIndex, 'keyboard');
67
67
  return previousIndex;
68
68
  }, [itemCount, select]);
69
+ var getSelectedIndex = useCallback(function () {
70
+ return selectedIndexRef.current;
71
+ }, []);
69
72
  return {
73
+ getSelectedIndex: getSelectedIndex,
70
74
  selectedIndex: selectedIndex,
71
75
  selectionSource: selectionSource,
72
76
  select: select,
@@ -0,0 +1,2 @@
1
+ import type { Context } from 'react';
2
+ export declare const PreviewItemIndexContext: Context<number | undefined>;
@@ -3,19 +3,20 @@ import type { RegisterMenuItem, RegisterMenuSection } from '@atlaskit/editor-ui-
3
3
  import type { RegisteredMenuListItemIdParams } from './RegisteredMenuListItemRow';
4
4
  import type { RegisteredMenuItemProps, RegisteredMenuSelectionSource } from './types';
5
5
  export type RegisteredMenuListProps = {
6
- activeItemKey?: string;
7
6
  containerRole?: 'presentation';
8
7
  getItemId: (params: RegisteredMenuListItemIdParams) => string;
9
8
  hasScrollPadding?: boolean;
9
+ /** Whether the selected item shows its preview. */
10
+ isPreviewOpen?: boolean;
10
11
  Item: React.ComponentType<RegisteredMenuItemProps>;
11
12
  lastRowPadding?: 'none' | 'space.100';
12
13
  maxHeight: number;
13
14
  omitAriaReadonly?: boolean;
14
- onItemBlur: (itemKey: string) => void;
15
- onItemFocus: (itemIndex: number, itemKey?: string) => void;
16
- onItemHover: (itemIndex: number, itemKey?: string) => void;
17
- onItemLeave: (itemKey: string) => void;
18
- onItemVisibilityChange: (itemKey: string, isVisible: boolean) => void;
15
+ onItemBlur: (itemIndex: number) => void;
16
+ onItemFocus: (itemIndex: number) => void;
17
+ onItemHover: (itemIndex: number) => void;
18
+ onItemLeave: (itemIndex: number) => void;
19
+ onItemVisibilityChange: (itemIndex: number, isVisible: boolean) => void;
19
20
  onRenderedItemsChange?: (range: {
20
21
  startIndex: number;
21
22
  stopIndex: number;
@@ -30,4 +31,4 @@ export type RegisteredMenuListProps = {
30
31
  tabIndex?: number | null;
31
32
  width: number;
32
33
  };
33
- export declare const RegisteredMenuList: ({ rows, Item, selectedItemIndex, activeItemKey, maxHeight, width, getItemId, onItemHover, onItemFocus, onItemLeave, onItemBlur, onItemVisibilityChange, onRenderedItemsChange, scrollResetKey, role, containerRole, tabIndex, omitAriaReadonly, hasScrollPadding, lastRowPadding, scrollTo, selectionSource, }: RegisteredMenuListProps) => React.JSX.Element | null;
34
+ export declare const RegisteredMenuList: ({ rows, Item, selectedItemIndex, isPreviewOpen, maxHeight, width, getItemId, onItemHover, onItemFocus, onItemLeave, onItemBlur, onItemVisibilityChange, onRenderedItemsChange, scrollResetKey, role, containerRole, tabIndex, omitAriaReadonly, hasScrollPadding, lastRowPadding, scrollTo, selectionSource, }: RegisteredMenuListProps) => React.JSX.Element | null;
@@ -10,11 +10,11 @@ export type RegisteredMenuListItemRowProps = {
10
10
  isVisible: boolean;
11
11
  Item: React.ComponentType<RegisteredMenuItemProps>;
12
12
  itemIndex: number;
13
- onItemBlur: (itemKey: string) => void;
14
- onItemFocus: (itemIndex: number, itemKey?: string) => void;
15
- onItemHover: (itemIndex: number, itemKey?: string) => void;
16
- onItemLeave: (itemKey: string) => void;
17
- onItemVisibilityChange: (itemKey: string, isVisible: boolean) => void;
13
+ onItemBlur: (itemIndex: number) => void;
14
+ onItemFocus: (itemIndex: number) => void;
15
+ onItemHover: (itemIndex: number) => void;
16
+ onItemLeave: (itemIndex: number) => void;
17
+ onItemVisibilityChange: (itemIndex: number, isVisible: boolean) => void;
18
18
  registration: RegisterMenuItem;
19
19
  rowIndex: number;
20
20
  selectedItemIndex: number;
@@ -5,12 +5,13 @@ type Options = {
5
5
  resetKey?: string | number | symbol;
6
6
  };
7
7
  export type RegisteredMenuState = {
8
- activeItemKey?: string;
9
- onItemBlur: (itemKey: string) => void;
10
- onItemFocus: (index: number, itemKey?: string) => void;
11
- onItemHover: (index: number, itemKey?: string) => void;
12
- onItemLeave: (itemKey: string) => void;
13
- onItemVisibilityChange: (itemKey: string, isVisible: boolean) => void;
8
+ /** Whether the selected item shows item-specific feedback, such as a preview. */
9
+ isPreviewOpen: boolean;
10
+ onItemBlur: (index: number) => void;
11
+ onItemFocus: (index: number) => void;
12
+ onItemHover: (index: number) => void;
13
+ onItemLeave: (index: number) => void;
14
+ onItemVisibilityChange: (index: number, isVisible: boolean) => void;
14
15
  rows: Array<RegisterMenuItem | RegisterMenuSection>;
15
16
  select: (index: number, source: RegisteredMenuSelectionSource) => void;
16
17
  selectableItems: RegisterMenuItem[];
@@ -4,6 +4,8 @@ type Options = {
4
4
  resetKey?: string | number | symbol;
5
5
  };
6
6
  type RegisteredMenuSelectionState = {
7
+ /** The latest selection, for callbacks that must stay stable; render with `selectedIndex`. */
8
+ getSelectedIndex: () => number;
7
9
  select: (index: number, source: RegisteredMenuSelectionSource) => void;
8
10
  selectedIndex: number;
9
11
  selectionSource: RegisteredMenuSelectionSource;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-common",
3
- "version": "134.14.0",
3
+ "version": "134.15.0",
4
4
  "description": "A package that contains common classes and components for editor and renderer",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -1,11 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.ActiveMenuItemKeyContext = void 0;
7
- var _react = require("react");
8
- // The list caches rendered rows while scrolling, so those rows may not receive
9
- // updated props. We pass the active item key down through context so their styling
10
- // updates without rebuilding the wrappers that measure row height.
11
- var ActiveMenuItemKeyContext = exports.ActiveMenuItemKeyContext = /*#__PURE__*/(0, _react.createContext)(undefined);
@@ -1,5 +0,0 @@
1
- import { createContext } from 'react';
2
- // The list caches rendered rows while scrolling, so those rows may not receive
3
- // updated props. We pass the active item key down through context so their styling
4
- // updates without rebuilding the wrappers that measure row height.
5
- export const ActiveMenuItemKeyContext = /*#__PURE__*/createContext(undefined);
@@ -1,5 +0,0 @@
1
- import { createContext } from 'react';
2
- // The list caches rendered rows while scrolling, so those rows may not receive
3
- // updated props. We pass the active item key down through context so their styling
4
- // updates without rebuilding the wrappers that measure row height.
5
- export var ActiveMenuItemKeyContext = /*#__PURE__*/createContext(undefined);
@@ -1,2 +0,0 @@
1
- import type { Context } from 'react';
2
- export declare const ActiveMenuItemKeyContext: Context<string | undefined>;