@atlaskit/editor-plugin-insert-block 3.1.7 → 3.2.1

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 (24) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/cjs/ui/ElementBrowser/InsertMenu.js +2 -1
  3. package/dist/cjs/ui/ToolbarInsertBlock/index.js +5 -4
  4. package/dist/es2019/ui/ElementBrowser/InsertMenu.js +2 -1
  5. package/dist/es2019/ui/ToolbarInsertBlock/index.js +3 -2
  6. package/dist/esm/ui/ElementBrowser/InsertMenu.js +2 -1
  7. package/dist/esm/ui/ToolbarInsertBlock/index.js +3 -2
  8. package/dist/types/index.d.ts +1 -1
  9. package/dist/types/insertBlockPluginType.d.ts +2 -2
  10. package/dist/types/types/index.d.ts +7 -1
  11. package/dist/types-ts4.5/index.d.ts +1 -1
  12. package/dist/types-ts4.5/insertBlockPluginType.d.ts +2 -2
  13. package/dist/types-ts4.5/types/index.d.ts +7 -1
  14. package/package.json +25 -22
  15. package/dist/cjs/ui/ToolbarInsertBlock/table-selector-popup-with-listeners.js +0 -258
  16. package/dist/cjs/ui/ToolbarInsertBlock/table-selector-popup.js +0 -158
  17. package/dist/es2019/ui/ToolbarInsertBlock/table-selector-popup-with-listeners.js +0 -250
  18. package/dist/es2019/ui/ToolbarInsertBlock/table-selector-popup.js +0 -150
  19. package/dist/esm/ui/ToolbarInsertBlock/table-selector-popup-with-listeners.js +0 -252
  20. package/dist/esm/ui/ToolbarInsertBlock/table-selector-popup.js +0 -150
  21. package/dist/types/ui/ToolbarInsertBlock/table-selector-popup-with-listeners.d.ts +0 -20
  22. package/dist/types/ui/ToolbarInsertBlock/table-selector-popup.d.ts +0 -31
  23. package/dist/types-ts4.5/ui/ToolbarInsertBlock/table-selector-popup-with-listeners.d.ts +0 -20
  24. package/dist/types-ts4.5/ui/ToolbarInsertBlock/table-selector-popup.d.ts +0 -31
@@ -1,158 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.TABLE_SELECTOR_BUTTON_SIZE = exports.TABLE_SELECTOR_BUTTON_GAP = void 0;
7
- var _react = require("react");
8
- var _react2 = require("@emotion/react");
9
- var _reactIntlNext = require("react-intl-next");
10
- var _messages = require("@atlaskit/editor-common/messages");
11
- var _uiReact = require("@atlaskit/editor-common/ui-react");
12
- var _primitives = require("@atlaskit/primitives");
13
- var _colors = require("@atlaskit/theme/colors");
14
- /**
15
- * @jsxRuntime classic
16
- * @jsx jsx
17
- */
18
-
19
- // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
20
-
21
- var TABLE_SELECTOR_BUTTON_GAP = exports.TABLE_SELECTOR_BUTTON_GAP = 2;
22
- var TABLE_SELECTOR_BUTTON_SIZE = exports.TABLE_SELECTOR_BUTTON_SIZE = 17;
23
- var selectedButtonStyles = (0, _react2.css)({
24
- backgroundColor: "var(--ds-background-accent-blue-subtlest, #579DFF)",
25
- border: "1px solid ".concat("var(--ds-background-accent-blue-subtle, #579DFF)")
26
- });
27
- var buttonStyles = (0, _react2.css)({
28
- height: "".concat(TABLE_SELECTOR_BUTTON_SIZE, "px"),
29
- width: "".concat(TABLE_SELECTOR_BUTTON_SIZE, "px"),
30
- border: "1px solid ".concat("var(--ds-border, #091e4224)"),
31
- backgroundColor: "var(--ds-background-input, #ffffff)",
32
- borderRadius: '3px',
33
- cursor: 'pointer',
34
- display: 'block',
35
- '&:focus': {
36
- outline: 'none',
37
- border: "1px solid ".concat("var(--ds-border-focused, ".concat(_colors.B100, ")")),
38
- boxShadow: "0 0 0 0.5px ".concat("var(--ds-border-focused, ".concat(_colors.B100, ")"))
39
- }
40
- });
41
- var selectionSizeTextStyles = (0, _react2.css)({
42
- // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
43
- lineHeight: '14px',
44
- display: 'flex',
45
- justifyContent: 'center',
46
- marginTop: "var(--ds-space-075, 5px)",
47
- padding: "var(--ds-space-075, 10px)"
48
- });
49
- var TableSelectorButton = function TableSelectorButton(_ref) {
50
- var row = _ref.row,
51
- col = _ref.col,
52
- isActive = _ref.isActive,
53
- _onClick = _ref.onClick,
54
- label = _ref.label,
55
- onKeyDown = _ref.onKeyDown,
56
- isFocused = _ref.isFocused,
57
- handleInitialButtonFocus = _ref.handleInitialButtonFocus;
58
- var btnRef = (0, _react.useRef)(null);
59
- (0, _react.useEffect)(function () {
60
- if (btnRef.current) {
61
- if (isFocused) {
62
- btnRef.current.focus();
63
- } else {
64
- btnRef.current.blur();
65
- }
66
- }
67
- }, [isFocused, btnRef]);
68
- var handleFocus = col === 1 && row === 1 ? function () {
69
- return handleInitialButtonFocus();
70
- } : undefined;
71
- return (0, _react2.jsx)("button", {
72
- type: "button",
73
- css: [buttonStyles, isActive ? selectedButtonStyles : undefined],
74
- onClick: function onClick() {
75
- return _onClick(row, col);
76
- },
77
- "aria-label": label,
78
- onKeyDown: onKeyDown,
79
- ref: btnRef,
80
- onFocus: handleFocus
81
- });
82
- };
83
- var createArray = function createArray(maxCols, maxRows) {
84
- var arr = [];
85
- for (var i = 1; i < maxRows + 1; i++) {
86
- for (var j = 1; j < maxCols + 1; j++) {
87
- arr.push({
88
- col: j,
89
- row: i
90
- });
91
- }
92
- }
93
- return arr;
94
- };
95
- var gridWrapperStyles = function gridWrapperStyles(_ref2) {
96
- var maxCols = _ref2.maxCols,
97
- maxRows = _ref2.maxRows;
98
- return (0, _react2.css)({
99
- display: 'grid',
100
- // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
101
- gridTemplateColumns: "repeat(".concat(maxCols, ", 1fr)"),
102
- // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
103
- gridTemplateRows: "repeat(".concat(maxRows, ", 1fr)"),
104
- gap: "".concat("var(--ds-space-025, ".concat("".concat(TABLE_SELECTOR_BUTTON_GAP, "px"), ")"))
105
- });
106
- };
107
- var TableSelectorPopup = function TableSelectorPopup(_ref3) {
108
- var maxCols = _ref3.maxCols,
109
- maxRows = _ref3.maxRows,
110
- onSelection = _ref3.onSelection,
111
- selectedCol = _ref3.selectedCol,
112
- selectedRow = _ref3.selectedRow,
113
- onKeyDown = _ref3.onKeyDown,
114
- isFocused = _ref3.isFocused,
115
- handleInitialButtonFocus = _ref3.handleInitialButtonFocus,
116
- formatMessage = _ref3.intl.formatMessage;
117
- var buttons = (0, _react.useMemo)(function () {
118
- return createArray(maxCols, maxRows);
119
- }, [maxCols, maxRows]);
120
- var setOutsideClickTargetRef = (0, _react.useContext)(_uiReact.OutsideClickTargetRefContext);
121
- return (0, _react2.jsx)(_primitives.Stack, {
122
- ref: setOutsideClickTargetRef
123
- }, (0, _react2.jsx)("div", {
124
- "aria-label": "".concat(formatMessage(_messages.toolbarInsertBlockMessages.tableSizeSelectorPopup)),
125
- css:
126
- // eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
127
- gridWrapperStyles({
128
- maxCols: maxCols,
129
- maxRows: maxRows
130
- })
131
- }, buttons.map(function (_ref4, index) {
132
- var col = _ref4.col,
133
- row = _ref4.row;
134
- var isCurrentFocused = isFocused && selectedCol === col && selectedRow === row;
135
- var isActive = selectedCol >= col && selectedRow >= row ? true : false;
136
- return (0, _react2.jsx)(TableSelectorButton
137
- // Ignored via go/ees005
138
- // eslint-disable-next-line react/no-array-index-key
139
- , {
140
- key: index,
141
- isActive: isActive,
142
- col: col,
143
- row: row,
144
- onClick: onSelection,
145
- label: "".concat(formatMessage(_messages.toolbarInsertBlockMessages.tableSizeSelectorButton, {
146
- numberOfColumns: col,
147
- numberOfRows: row
148
- })),
149
- onKeyDown: onKeyDown,
150
- isFocused: isCurrentFocused,
151
- handleInitialButtonFocus: handleInitialButtonFocus
152
- });
153
- })), (0, _react2.jsx)("span", {
154
- css: selectionSizeTextStyles,
155
- "aria-hidden": true
156
- }, "".concat(selectedCol, " x ").concat(selectedRow)));
157
- };
158
- var _default = exports.default = (0, _reactIntlNext.injectIntl)(TableSelectorPopup);
@@ -1,250 +0,0 @@
1
- /**
2
- * @jsxRuntime classic
3
- * @jsx jsx
4
- */
5
-
6
- import { useCallback, useEffect, useRef, useState } from 'react';
7
-
8
- // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
9
- import { css, jsx } from '@emotion/react';
10
- import { bind } from 'bind-event-listener';
11
- import { Popup } from '@atlaskit/editor-common/ui';
12
- import { withReactEditorViewOuterListeners as withOuterListeners } from '@atlaskit/editor-common/ui-react';
13
- import { akEditorMenuZIndex } from '@atlaskit/editor-shared-styles';
14
- import { N0, N30A, N60A } from '@atlaskit/theme/colors';
15
- import tableSelectorPopup, { TABLE_SELECTOR_BUTTON_GAP, TABLE_SELECTOR_BUTTON_SIZE } from './table-selector-popup';
16
- const TABLE_SELECTOR_PADDING_TOP = 8;
17
- const TABLE_SELECTOR_PADDING_SIDE = 10;
18
- const DEFAULT_TABLE_SELECTOR_ROWS = 5;
19
- const DEFAULT_TABLE_SELECTOR_COLS = 10;
20
- const DEFAULT_TABLE_SELECTOR_SELECTION_SIZE = 1;
21
- const DEFAULT_MAX_TABLE_SELECTOR_ROWS = 10;
22
- const TableSelectorWithListeners = withOuterListeners(tableSelectorPopup);
23
- const initialSizeState = {
24
- col: DEFAULT_TABLE_SELECTOR_SELECTION_SIZE,
25
- row: DEFAULT_TABLE_SELECTOR_SELECTION_SIZE,
26
- maxCol: DEFAULT_TABLE_SELECTOR_COLS,
27
- maxRow: DEFAULT_TABLE_SELECTOR_ROWS
28
- };
29
- const tableSelectorPopupWrapperStyles = css({
30
- borderRadius: "var(--ds-border-radius, 3px)",
31
- backgroundColor: `var(--ds-surface-overlay, ${N0})`,
32
- boxShadow: `var(--ds-shadow-overlay, ${`0 0 0 1px ${N30A}, 0 2px 1px ${N30A}, 0 0 20px -6px ${N60A}`})`,
33
- padding: `${`var(--ds-space-100, ${`${TABLE_SELECTOR_PADDING_TOP}px`})`} ${TABLE_SELECTOR_PADDING_SIDE}px`
34
- });
35
- export const TableSelectorPopup = props => {
36
- const [size, setSize] = useState(initialSizeState);
37
- const tablePopupRef = useRef(null);
38
- // If popup opened by keyboard enable keyboard mode
39
- const isKeyboardMode = useRef(props.isOpenedByKeyboard);
40
- const enableKeyboardMode = useCallback(() => {
41
- if (!isKeyboardMode.current) {
42
- isKeyboardMode.current = true;
43
- }
44
- }, [isKeyboardMode]);
45
- const disableKeyboardMode = useCallback(() => {
46
- if (isKeyboardMode.current) {
47
- isKeyboardMode.current = false;
48
- }
49
- }, [isKeyboardMode]);
50
-
51
- // Mouse move is used to allow selection changes outside of the popup and is more reactive to changes.
52
- const handleMouseMove = useCallback(e => {
53
- if (!tablePopupRef.current) {
54
- return;
55
- }
56
- disableKeyboardMode();
57
- const tablePopup = tablePopupRef.current;
58
- const {
59
- left,
60
- top
61
- } = tablePopup.getBoundingClientRect();
62
-
63
- // Mouse position on popup
64
- const selectedWidth = e.clientX - left;
65
- const selectedHeight = e.clientY - top;
66
-
67
- // Calculate number grid cells selected
68
- let selectedGridCols = Math.ceil((selectedWidth - TABLE_SELECTOR_PADDING_SIDE + TABLE_SELECTOR_BUTTON_GAP) / (TABLE_SELECTOR_BUTTON_GAP + TABLE_SELECTOR_BUTTON_SIZE));
69
- let selectedGridRows = Math.ceil((selectedHeight - TABLE_SELECTOR_PADDING_TOP + TABLE_SELECTOR_BUTTON_GAP) / (TABLE_SELECTOR_BUTTON_GAP + TABLE_SELECTOR_BUTTON_SIZE));
70
- // Keep the selected rows and columns within the defined bounds
71
- let gridRows = DEFAULT_TABLE_SELECTOR_ROWS;
72
- if (selectedGridCols < 1) {
73
- selectedGridCols = 1;
74
- }
75
- if (selectedGridCols > size.maxCol) {
76
- selectedGridCols = size.maxCol;
77
- }
78
- if (selectedGridRows < 1) {
79
- selectedGridRows = 1;
80
- }
81
-
82
- // Expand grid when row selection is greater than the default grid size
83
- if (selectedGridRows >= DEFAULT_TABLE_SELECTOR_ROWS && selectedGridRows < DEFAULT_MAX_TABLE_SELECTOR_ROWS) {
84
- gridRows = selectedGridRows + 1;
85
- }
86
- if (selectedGridRows >= DEFAULT_MAX_TABLE_SELECTOR_ROWS) {
87
- selectedGridRows = DEFAULT_MAX_TABLE_SELECTOR_ROWS;
88
- gridRows = DEFAULT_MAX_TABLE_SELECTOR_ROWS;
89
- }
90
- if (selectedGridCols !== size.col || selectedGridRows !== size.row) {
91
- setSize({
92
- col: selectedGridCols,
93
- row: selectedGridRows,
94
- maxCol: DEFAULT_TABLE_SELECTOR_COLS,
95
- maxRow: gridRows
96
- });
97
- }
98
- }, [disableKeyboardMode, size, setSize]);
99
- const decreasingSequence = (maxNumber, prevNumber) => {
100
- let nextNumber = prevNumber - 1;
101
- if (prevNumber === 1) {
102
- nextNumber = maxNumber;
103
- }
104
- return nextNumber;
105
- };
106
- const getMaxRow = (prevSize, eventKey) => {
107
- switch (eventKey) {
108
- case 'ArrowDown':
109
- // Expand the grid size when last row selected
110
- if (prevSize.maxRow < DEFAULT_MAX_TABLE_SELECTOR_ROWS && prevSize.row >= DEFAULT_TABLE_SELECTOR_ROWS - 1) {
111
- return prevSize.maxRow + 1;
112
- }
113
- if (prevSize.row === DEFAULT_MAX_TABLE_SELECTOR_ROWS) {
114
- return DEFAULT_TABLE_SELECTOR_ROWS;
115
- }
116
- return prevSize.maxRow;
117
- case 'ArrowLeft':
118
- const moveToPrevRow = prevSize.col === 1 && prevSize.row > 1;
119
- const moveToLastRow = prevSize.row === 1 && prevSize.col === 1;
120
- // Expand the popup to max size when selected row wraps around to last row
121
- if (moveToLastRow) {
122
- return DEFAULT_MAX_TABLE_SELECTOR_ROWS;
123
- }
124
- // Decrease the popup when decreased row selection
125
- if (prevSize.maxRow > DEFAULT_TABLE_SELECTOR_ROWS && moveToPrevRow) {
126
- return prevSize.row;
127
- }
128
- return prevSize.maxRow;
129
- case 'ArrowUp':
130
- if (prevSize.row === 1) {
131
- return DEFAULT_MAX_TABLE_SELECTOR_ROWS;
132
- // Decrease the popup size when decreased row selection
133
- } else if (prevSize.maxRow > DEFAULT_TABLE_SELECTOR_ROWS) {
134
- return prevSize.row;
135
- }
136
- return prevSize.maxRow;
137
- case 'ArrowRight':
138
- const moveToNextRow = prevSize.col === DEFAULT_TABLE_SELECTOR_COLS;
139
- const increaseMaxRow = prevSize.maxRow < DEFAULT_MAX_TABLE_SELECTOR_ROWS && moveToNextRow && prevSize.row + 1 === prevSize.maxRow;
140
-
141
- // Decrease popup size for wrap around to selection 1 x 1
142
- if (prevSize.row === DEFAULT_MAX_TABLE_SELECTOR_ROWS && prevSize.col === DEFAULT_TABLE_SELECTOR_COLS) {
143
- return DEFAULT_TABLE_SELECTOR_ROWS;
144
- // Decrease the popup size when decreased row selection
145
- } else if (increaseMaxRow) {
146
- return prevSize.maxRow + 1;
147
- }
148
- return prevSize.maxRow;
149
- default:
150
- return prevSize.maxRow;
151
- }
152
- };
153
- const handleInitialButtonFocus = useCallback(() => {
154
- if (isKeyboardMode.current !== true) {
155
- enableKeyboardMode();
156
- setSize(initialSizeState);
157
- }
158
- }, [enableKeyboardMode, setSize]);
159
- const handleKeyDown = useCallback(
160
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
161
- event => {
162
- if (event.key === 'ArrowDown') {
163
- enableKeyboardMode();
164
- setSize(prevSize => {
165
- return {
166
- ...prevSize,
167
- row: prevSize.row % prevSize.maxRow + 1,
168
- maxRow: getMaxRow(prevSize, event.key)
169
- };
170
- });
171
- }
172
- if (event.key === 'ArrowRight') {
173
- enableKeyboardMode();
174
- setSize(prevSize => {
175
- const moveToNextRow = prevSize.col === DEFAULT_TABLE_SELECTOR_COLS;
176
- return {
177
- ...prevSize,
178
- col: prevSize.col % DEFAULT_TABLE_SELECTOR_COLS + 1,
179
- row: moveToNextRow ? prevSize.row % prevSize.maxRow + 1 : prevSize.row,
180
- maxRow: getMaxRow(prevSize, event.key)
181
- };
182
- });
183
- }
184
- if (event.key === 'ArrowLeft') {
185
- enableKeyboardMode();
186
- setSize(prevSize => {
187
- const getRow = (prevRow, prevCol) => {
188
- const row = prevRow;
189
- // Move to previous row for wrap around
190
- if (prevSize.col === 1 && prevSize.row > 1) {
191
- return prevRow - 1;
192
- // Increase the selection to max size when selected row and column wraps around
193
- } else if (prevRow === 1 && prevCol === 1) {
194
- return DEFAULT_MAX_TABLE_SELECTOR_ROWS;
195
- }
196
- return row;
197
- };
198
- return {
199
- ...prevSize,
200
- col: decreasingSequence(prevSize.maxCol, prevSize.col),
201
- row: getRow(prevSize.row, prevSize.col),
202
- maxRow: getMaxRow(prevSize, event.key)
203
- };
204
- });
205
- }
206
- if (event.key === 'ArrowUp') {
207
- enableKeyboardMode();
208
- setSize(prevSize => {
209
- const moveToLastRow = prevSize.row === 1;
210
- return {
211
- ...prevSize,
212
- row: moveToLastRow ? DEFAULT_MAX_TABLE_SELECTOR_ROWS : decreasingSequence(prevSize.maxRow, prevSize.row),
213
- maxRow: getMaxRow(prevSize, event.key)
214
- };
215
- });
216
- }
217
- }, [enableKeyboardMode, setSize]);
218
- useEffect(() => {
219
- const unbind = bind(window, {
220
- type: 'mousemove',
221
- listener: handleMouseMove
222
- });
223
- return unbind;
224
- }, [handleMouseMove]);
225
- return jsx(Popup, {
226
- target: props.target,
227
- offset: [0, 3],
228
- mountTo: props.popupsMountPoint,
229
- boundariesElement: props.popupsBoundariesElement,
230
- scrollableElement: props.popupsScrollableElement,
231
- focusTrap: true,
232
- onUnmount: props.onUnmount,
233
- zIndex: akEditorMenuZIndex
234
- }, jsx("div", {
235
- css: tableSelectorPopupWrapperStyles,
236
- ref: tablePopupRef
237
- }, jsx(TableSelectorWithListeners, {
238
- handleClickOutside: props.handleClickOutside,
239
- handleEscapeKeydown: props.handleEscapeKeydown,
240
- maxCols: size.maxCol,
241
- maxRows: size.maxRow,
242
- onSelection: props.onSelection,
243
- selectedCol: size.col,
244
- selectedRow: size.row,
245
- onKeyDown: handleKeyDown,
246
- isFocused: isKeyboardMode.current,
247
- handleInitialButtonFocus: handleInitialButtonFocus
248
- })));
249
- };
250
- export default TableSelectorPopup;
@@ -1,150 +0,0 @@
1
- /**
2
- * @jsxRuntime classic
3
- * @jsx jsx
4
- */
5
- import { useContext, useEffect, useMemo, useRef } from 'react';
6
- // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
7
- import { css, jsx } from '@emotion/react';
8
- import { injectIntl } from 'react-intl-next';
9
- import { toolbarInsertBlockMessages as messages } from '@atlaskit/editor-common/messages';
10
- import { OutsideClickTargetRefContext } from '@atlaskit/editor-common/ui-react';
11
- import { Stack } from '@atlaskit/primitives';
12
- import { B100 } from '@atlaskit/theme/colors';
13
- export const TABLE_SELECTOR_BUTTON_GAP = 2;
14
- export const TABLE_SELECTOR_BUTTON_SIZE = 17;
15
- const selectedButtonStyles = css({
16
- backgroundColor: "var(--ds-background-accent-blue-subtlest, #579DFF)",
17
- border: `1px solid ${"var(--ds-background-accent-blue-subtle, #579DFF)"}`
18
- });
19
- const buttonStyles = css({
20
- height: `${TABLE_SELECTOR_BUTTON_SIZE}px`,
21
- width: `${TABLE_SELECTOR_BUTTON_SIZE}px`,
22
- border: `1px solid ${"var(--ds-border, #091e4224)"}`,
23
- backgroundColor: "var(--ds-background-input, #ffffff)",
24
- borderRadius: '3px',
25
- cursor: 'pointer',
26
- display: 'block',
27
- '&:focus': {
28
- outline: 'none',
29
- border: `1px solid ${`var(--ds-border-focused, ${B100})`}`,
30
- boxShadow: `0 0 0 0.5px ${`var(--ds-border-focused, ${B100})`}`
31
- }
32
- });
33
- const selectionSizeTextStyles = css({
34
- // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
35
- lineHeight: '14px',
36
- display: 'flex',
37
- justifyContent: 'center',
38
- marginTop: "var(--ds-space-075, 5px)",
39
- padding: "var(--ds-space-075, 10px)"
40
- });
41
- const TableSelectorButton = ({
42
- row,
43
- col,
44
- isActive,
45
- onClick,
46
- label,
47
- onKeyDown,
48
- isFocused,
49
- handleInitialButtonFocus
50
- }) => {
51
- const btnRef = useRef(null);
52
- useEffect(() => {
53
- if (btnRef.current) {
54
- if (isFocused) {
55
- btnRef.current.focus();
56
- } else {
57
- btnRef.current.blur();
58
- }
59
- }
60
- }, [isFocused, btnRef]);
61
- const handleFocus = col === 1 && row === 1 ? () => handleInitialButtonFocus() : undefined;
62
- return jsx("button", {
63
- type: "button",
64
- css: [buttonStyles, isActive ? selectedButtonStyles : undefined],
65
- onClick: () => onClick(row, col),
66
- "aria-label": label,
67
- onKeyDown: onKeyDown,
68
- ref: btnRef,
69
- onFocus: handleFocus
70
- });
71
- };
72
- const createArray = (maxCols, maxRows) => {
73
- const arr = [];
74
- for (let i = 1; i < maxRows + 1; i++) {
75
- for (let j = 1; j < maxCols + 1; j++) {
76
- arr.push({
77
- col: j,
78
- row: i
79
- });
80
- }
81
- }
82
- return arr;
83
- };
84
- const gridWrapperStyles = ({
85
- maxCols,
86
- maxRows
87
- }) => css({
88
- display: 'grid',
89
- // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
90
- gridTemplateColumns: `repeat(${maxCols}, 1fr)`,
91
- // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
92
- gridTemplateRows: `repeat(${maxRows}, 1fr)`,
93
- gap: `${`var(--ds-space-025, ${`${TABLE_SELECTOR_BUTTON_GAP}px`})`}`
94
- });
95
- const TableSelectorPopup = ({
96
- maxCols,
97
- maxRows,
98
- onSelection,
99
- selectedCol,
100
- selectedRow,
101
- onKeyDown,
102
- isFocused,
103
- handleInitialButtonFocus,
104
- intl: {
105
- formatMessage
106
- }
107
- }) => {
108
- const buttons = useMemo(() => {
109
- return createArray(maxCols, maxRows);
110
- }, [maxCols, maxRows]);
111
- const setOutsideClickTargetRef = useContext(OutsideClickTargetRefContext);
112
- return jsx(Stack, {
113
- ref: setOutsideClickTargetRef
114
- }, jsx("div", {
115
- "aria-label": `${formatMessage(messages.tableSizeSelectorPopup)}`,
116
- css:
117
- // eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
118
- gridWrapperStyles({
119
- maxCols: maxCols,
120
- maxRows: maxRows
121
- })
122
- }, buttons.map(({
123
- col,
124
- row
125
- }, index) => {
126
- const isCurrentFocused = isFocused && selectedCol === col && selectedRow === row;
127
- const isActive = selectedCol >= col && selectedRow >= row ? true : false;
128
- return jsx(TableSelectorButton
129
- // Ignored via go/ees005
130
- // eslint-disable-next-line react/no-array-index-key
131
- , {
132
- key: index,
133
- isActive: isActive,
134
- col: col,
135
- row: row,
136
- onClick: onSelection,
137
- label: `${formatMessage(messages.tableSizeSelectorButton, {
138
- numberOfColumns: col,
139
- numberOfRows: row
140
- })}`,
141
- onKeyDown: onKeyDown,
142
- isFocused: isCurrentFocused,
143
- handleInitialButtonFocus: handleInitialButtonFocus
144
- });
145
- })), jsx("span", {
146
- css: selectionSizeTextStyles,
147
- "aria-hidden": true
148
- }, `${selectedCol} x ${selectedRow}`));
149
- };
150
- export default injectIntl(TableSelectorPopup);