@elementor/editor-ui 4.5.0-1161 → 4.5.0-1163
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/dist/index.d.mts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +122 -26
- package/dist/index.mjs +111 -16
- package/package.json +2 -2
- package/src/components/popover/__tests__/menu-list.test.tsx +172 -8
- package/src/components/popover/menu-list.tsx +88 -4
- package/src/hooks/__tests__/use-roving-focus.test.tsx +87 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/use-roving-focus.ts +41 -0
- package/src/index.ts +1 -0
package/dist/index.d.mts
CHANGED
|
@@ -217,9 +217,10 @@ type PopoverMenuListProps<T, V extends string> = {
|
|
|
217
217
|
categoryItemContentTemplate?: (item: VirtualizedItem<T, V>) => React$1.ReactNode;
|
|
218
218
|
noResultsComponent?: React$1.ReactNode;
|
|
219
219
|
itemHeight?: number;
|
|
220
|
+
rovingFocus?: boolean;
|
|
220
221
|
};
|
|
221
222
|
declare const ITEM_HEIGHT = 32;
|
|
222
|
-
declare const PopoverMenuList: <T, V extends string>({ items, onSelect, onClose, selectedValue, itemStyle, onChange, "data-testid": dataTestId, menuItemContentTemplate, categoryItemContentTemplate, noResultsComponent, menuListTemplate: CustomMenuList, itemHeight, }: PopoverMenuListProps<T, V>) => React$1.JSX.Element;
|
|
223
|
+
declare const PopoverMenuList: <T, V extends string>({ items, onSelect, onClose, selectedValue, itemStyle, onChange, "data-testid": dataTestId, menuItemContentTemplate, categoryItemContentTemplate, noResultsComponent, menuListTemplate: CustomMenuList, itemHeight, rovingFocus, }: PopoverMenuListProps<T, V>) => React$1.JSX.Element;
|
|
223
224
|
declare const StyledMenuList: _emotion_styled.StyledComponent<any, {}, {
|
|
224
225
|
ref?: React$1.Ref<any> | undefined;
|
|
225
226
|
}>;
|
|
@@ -315,4 +316,13 @@ declare const useTextFieldAutoSelect: () => React$1.RefObject<HTMLInputElement>;
|
|
|
315
316
|
|
|
316
317
|
declare const useCanvasClickHandler: (isActive: boolean, onClickAway: (e: MouseEvent) => void) => void;
|
|
317
318
|
|
|
318
|
-
|
|
319
|
+
type UseRovingFocusProps = {
|
|
320
|
+
tabbableKey: string | undefined;
|
|
321
|
+
mountedSignature: string;
|
|
322
|
+
};
|
|
323
|
+
declare const useRovingFocus: <E extends HTMLElement>({ tabbableKey, mountedSignature }: UseRovingFocusProps) => {
|
|
324
|
+
registerRef: (key: string) => (node: E | null) => void;
|
|
325
|
+
requestFocus: () => void;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export { CollapseIcon, CollapsibleContent, type CollapsibleValue, ConfirmationDialog, CtaButton, EditableField, EllipsisWithTooltip, FileUploadDropzone, FileUploadRow, FloatingActionsBar, Form, GlobalDialog, ITEM_HEIGHT, InfoAlert, InfoTipCard, IntroductionModal, MenuItemInfotip, MenuListItem, PopoverAction, type PopoverActionProps, PopoverBody, PopoverHeader, PopoverMenuList, type PopoverMenuListProps, PromotionAlert, PromotionChip, PromotionInfotip, PromotionPopover, SaveChangesDialog, SearchField, SectionPopoverBody, SectionRefContext, StyledMenuList, ThemeProvider, type VirtualizedItem, WarningInfotip, closeDialog, getCollapsibleValue, openDialog, useCanvasClickHandler, useDialog, useEditable, useFloatingActionsBar, useRovingFocus, useSectionWidth, useTextFieldAutoSelect };
|
package/dist/index.d.ts
CHANGED
|
@@ -217,9 +217,10 @@ type PopoverMenuListProps<T, V extends string> = {
|
|
|
217
217
|
categoryItemContentTemplate?: (item: VirtualizedItem<T, V>) => React$1.ReactNode;
|
|
218
218
|
noResultsComponent?: React$1.ReactNode;
|
|
219
219
|
itemHeight?: number;
|
|
220
|
+
rovingFocus?: boolean;
|
|
220
221
|
};
|
|
221
222
|
declare const ITEM_HEIGHT = 32;
|
|
222
|
-
declare const PopoverMenuList: <T, V extends string>({ items, onSelect, onClose, selectedValue, itemStyle, onChange, "data-testid": dataTestId, menuItemContentTemplate, categoryItemContentTemplate, noResultsComponent, menuListTemplate: CustomMenuList, itemHeight, }: PopoverMenuListProps<T, V>) => React$1.JSX.Element;
|
|
223
|
+
declare const PopoverMenuList: <T, V extends string>({ items, onSelect, onClose, selectedValue, itemStyle, onChange, "data-testid": dataTestId, menuItemContentTemplate, categoryItemContentTemplate, noResultsComponent, menuListTemplate: CustomMenuList, itemHeight, rovingFocus, }: PopoverMenuListProps<T, V>) => React$1.JSX.Element;
|
|
223
224
|
declare const StyledMenuList: _emotion_styled.StyledComponent<any, {}, {
|
|
224
225
|
ref?: React$1.Ref<any> | undefined;
|
|
225
226
|
}>;
|
|
@@ -315,4 +316,13 @@ declare const useTextFieldAutoSelect: () => React$1.RefObject<HTMLInputElement>;
|
|
|
315
316
|
|
|
316
317
|
declare const useCanvasClickHandler: (isActive: boolean, onClickAway: (e: MouseEvent) => void) => void;
|
|
317
318
|
|
|
318
|
-
|
|
319
|
+
type UseRovingFocusProps = {
|
|
320
|
+
tabbableKey: string | undefined;
|
|
321
|
+
mountedSignature: string;
|
|
322
|
+
};
|
|
323
|
+
declare const useRovingFocus: <E extends HTMLElement>({ tabbableKey, mountedSignature }: UseRovingFocusProps) => {
|
|
324
|
+
registerRef: (key: string) => (node: E | null) => void;
|
|
325
|
+
requestFocus: () => void;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export { CollapseIcon, CollapsibleContent, type CollapsibleValue, ConfirmationDialog, CtaButton, EditableField, EllipsisWithTooltip, FileUploadDropzone, FileUploadRow, FloatingActionsBar, Form, GlobalDialog, ITEM_HEIGHT, InfoAlert, InfoTipCard, IntroductionModal, MenuItemInfotip, MenuListItem, PopoverAction, type PopoverActionProps, PopoverBody, PopoverHeader, PopoverMenuList, type PopoverMenuListProps, PromotionAlert, PromotionChip, PromotionInfotip, PromotionPopover, SaveChangesDialog, SearchField, SectionPopoverBody, SectionRefContext, StyledMenuList, ThemeProvider, type VirtualizedItem, WarningInfotip, closeDialog, getCollapsibleValue, openDialog, useCanvasClickHandler, useDialog, useEditable, useFloatingActionsBar, useRovingFocus, useSectionWidth, useTextFieldAutoSelect };
|
package/dist/index.js
CHANGED
|
@@ -69,6 +69,7 @@ __export(index_exports, {
|
|
|
69
69
|
useDialog: () => useDialog,
|
|
70
70
|
useEditable: () => useEditable,
|
|
71
71
|
useFloatingActionsBar: () => useFloatingActionsBar,
|
|
72
|
+
useRovingFocus: () => useRovingFocus,
|
|
72
73
|
useSectionWidth: () => useSectionWidth,
|
|
73
74
|
useTextFieldAutoSelect: () => useTextFieldAutoSelect
|
|
74
75
|
});
|
|
@@ -586,6 +587,37 @@ var useCanvasClickHandler = (isActive, onClickAway) => {
|
|
|
586
587
|
}, [isActive, onClickAway]);
|
|
587
588
|
};
|
|
588
589
|
|
|
590
|
+
// src/hooks/use-roving-focus.ts
|
|
591
|
+
var import_react15 = require("react");
|
|
592
|
+
var useRovingFocus = ({ tabbableKey, mountedSignature }) => {
|
|
593
|
+
const elements = (0, import_react15.useRef)(/* @__PURE__ */ new Map());
|
|
594
|
+
const isFocusPending = (0, import_react15.useRef)(false);
|
|
595
|
+
(0, import_react15.useLayoutEffect)(() => {
|
|
596
|
+
if (!isFocusPending.current || tabbableKey === void 0) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
const element = elements.current.get(tabbableKey);
|
|
600
|
+
if (element) {
|
|
601
|
+
element.focus();
|
|
602
|
+
isFocusPending.current = false;
|
|
603
|
+
}
|
|
604
|
+
}, [tabbableKey, mountedSignature]);
|
|
605
|
+
const registerRef = (0, import_react15.useCallback)(
|
|
606
|
+
(key) => (node) => {
|
|
607
|
+
if (node) {
|
|
608
|
+
elements.current.set(key, node);
|
|
609
|
+
} else {
|
|
610
|
+
elements.current.delete(key);
|
|
611
|
+
}
|
|
612
|
+
},
|
|
613
|
+
[]
|
|
614
|
+
);
|
|
615
|
+
const requestFocus = (0, import_react15.useCallback)(() => {
|
|
616
|
+
isFocusPending.current = true;
|
|
617
|
+
}, []);
|
|
618
|
+
return { registerRef, requestFocus };
|
|
619
|
+
};
|
|
620
|
+
|
|
589
621
|
// src/components/promotions/promotion-infotip.tsx
|
|
590
622
|
var PromotionInfotip = ({ children, open, onClose, onCtaClick, ...cardProps }) => {
|
|
591
623
|
useCanvasClickHandler(!!open, onClose);
|
|
@@ -756,7 +788,7 @@ var PromotionAlert = ({ message, upgradeUrl, onCtaClick }) => /* @__PURE__ */ Re
|
|
|
756
788
|
|
|
757
789
|
// src/components/floating-bar.tsx
|
|
758
790
|
var React18 = __toESM(require("react"));
|
|
759
|
-
var
|
|
791
|
+
var import_react16 = require("react");
|
|
760
792
|
var import_ui18 = require("@elementor/ui");
|
|
761
793
|
var FloatingBarContainer = (0, import_ui18.styled)("span")`
|
|
762
794
|
display: contents;
|
|
@@ -769,13 +801,13 @@ var FloatingBarContainer = (0, import_ui18.styled)("span")`
|
|
|
769
801
|
z-index: 1000;
|
|
770
802
|
}
|
|
771
803
|
`;
|
|
772
|
-
var FloatingActionsContext = (0,
|
|
804
|
+
var FloatingActionsContext = (0, import_react16.createContext)(null);
|
|
773
805
|
function FloatingActionsBar({ actions, children }) {
|
|
774
|
-
const [open, setOpen] = (0,
|
|
806
|
+
const [open, setOpen] = (0, import_react16.useState)(false);
|
|
775
807
|
return /* @__PURE__ */ React18.createElement(FloatingActionsContext.Provider, { value: { open, setOpen } }, /* @__PURE__ */ React18.createElement(FloatingBarContainer, null, /* @__PURE__ */ React18.createElement(import_ui18.UnstableFloatingActionBar, { actions, open: open || void 0 }, children)));
|
|
776
808
|
}
|
|
777
809
|
function useFloatingActionsBar() {
|
|
778
|
-
const context = (0,
|
|
810
|
+
const context = (0, import_react16.useContext)(FloatingActionsContext);
|
|
779
811
|
if (!context) {
|
|
780
812
|
throw new Error("useFloatingActions must be used within a FloatingActionsBar");
|
|
781
813
|
}
|
|
@@ -784,7 +816,7 @@ function useFloatingActionsBar() {
|
|
|
784
816
|
|
|
785
817
|
// src/components/file-upload/file-upload-dropzone.tsx
|
|
786
818
|
var React19 = __toESM(require("react"));
|
|
787
|
-
var
|
|
819
|
+
var import_react17 = require("react");
|
|
788
820
|
var import_icons8 = require("@elementor/icons");
|
|
789
821
|
var import_ui19 = require("@elementor/ui");
|
|
790
822
|
var import_i18n6 = require("@wordpress/i18n");
|
|
@@ -803,7 +835,7 @@ var FileUploadDropzone = ({
|
|
|
803
835
|
secondaryLabel,
|
|
804
836
|
helperText
|
|
805
837
|
}) => {
|
|
806
|
-
const fileInputRef = (0,
|
|
838
|
+
const fileInputRef = (0, import_react17.useRef)(null);
|
|
807
839
|
const { getDropZoneProps } = (0, import_ui19.useUnstableDropZone)({
|
|
808
840
|
allowedFileTypes,
|
|
809
841
|
onChange: ({ valid }) => {
|
|
@@ -914,10 +946,10 @@ function resolvePopoverWidth(width, fillWidth) {
|
|
|
914
946
|
var React22 = __toESM(require("react"));
|
|
915
947
|
|
|
916
948
|
// src/contexts/section-context.tsx
|
|
917
|
-
var
|
|
949
|
+
var import_react18 = require("react");
|
|
918
950
|
var FALLBACK_SECTION_WIDTH = 320;
|
|
919
|
-
var SectionRefContext = (0,
|
|
920
|
-
var useSectionRef = () => (0,
|
|
951
|
+
var SectionRefContext = (0, import_react18.createContext)(null);
|
|
952
|
+
var useSectionRef = () => (0, import_react18.useContext)(SectionRefContext);
|
|
921
953
|
var useSectionWidth = () => {
|
|
922
954
|
const sectionRef = useSectionRef();
|
|
923
955
|
return sectionRef?.current?.offsetWidth ?? FALLBACK_SECTION_WIDTH;
|
|
@@ -956,12 +988,13 @@ var PopoverHeader = ({ title, onClose, icon, actions, sx }) => {
|
|
|
956
988
|
|
|
957
989
|
// src/components/popover/menu-list.tsx
|
|
958
990
|
var React24 = __toESM(require("react"));
|
|
959
|
-
var
|
|
991
|
+
var import_react19 = require("react");
|
|
960
992
|
var import_ui23 = require("@elementor/ui");
|
|
961
993
|
var import_react_virtual = require("@tanstack/react-virtual");
|
|
962
994
|
var ITEM_HEIGHT = 32;
|
|
963
995
|
var LIST_ITEMS_BUFFER = 6;
|
|
964
996
|
var MENU_LIST_PADDING_TOP = 8;
|
|
997
|
+
var ROVING_FOCUS_KEYS = /* @__PURE__ */ new Set(["ArrowDown", "ArrowUp", "Home", "End"]);
|
|
965
998
|
var menuSubHeaderAbsoluteStyling = (start) => ({
|
|
966
999
|
position: "absolute",
|
|
967
1000
|
transform: `translateY(${start + MENU_LIST_PADDING_TOP}px)`
|
|
@@ -983,13 +1016,15 @@ var PopoverMenuList = ({
|
|
|
983
1016
|
categoryItemContentTemplate,
|
|
984
1017
|
noResultsComponent,
|
|
985
1018
|
menuListTemplate: CustomMenuList,
|
|
986
|
-
itemHeight = ITEM_HEIGHT
|
|
1019
|
+
itemHeight = ITEM_HEIGHT,
|
|
1020
|
+
rovingFocus = false
|
|
987
1021
|
}) => {
|
|
988
|
-
const containerRef = (0,
|
|
1022
|
+
const containerRef = (0, import_react19.useRef)(null);
|
|
989
1023
|
const scrollTop = useScrollTop({ containerRef });
|
|
990
1024
|
const theme = (0, import_ui23.useTheme)();
|
|
1025
|
+
const [focusedValue, setFocusedValue] = (0, import_react19.useState)();
|
|
991
1026
|
const MenuListComponent = CustomMenuList || StyledMenuList;
|
|
992
|
-
const stickyIndices = (0,
|
|
1027
|
+
const stickyIndices = (0, import_react19.useMemo)(
|
|
993
1028
|
() => items.reduce((categoryIndices, item, index) => {
|
|
994
1029
|
if (item.type === "category") {
|
|
995
1030
|
categoryIndices.push(index);
|
|
@@ -998,6 +1033,28 @@ var PopoverMenuList = ({
|
|
|
998
1033
|
}, []),
|
|
999
1034
|
[items]
|
|
1000
1035
|
);
|
|
1036
|
+
const enabledIndices = (0, import_react19.useMemo)(
|
|
1037
|
+
() => items.reduce((indices, item, index) => {
|
|
1038
|
+
if (item.type !== "category" && !item.disabled) {
|
|
1039
|
+
indices.push(index);
|
|
1040
|
+
}
|
|
1041
|
+
return indices;
|
|
1042
|
+
}, []),
|
|
1043
|
+
[items]
|
|
1044
|
+
);
|
|
1045
|
+
const tabbableIndex = (0, import_react19.useMemo)(() => {
|
|
1046
|
+
if (!rovingFocus) {
|
|
1047
|
+
return -1;
|
|
1048
|
+
}
|
|
1049
|
+
const candidates = [focusedValue, selectedValue];
|
|
1050
|
+
for (const candidate of candidates) {
|
|
1051
|
+
const index = items.findIndex((item) => item.value === candidate);
|
|
1052
|
+
if (enabledIndices.includes(index)) {
|
|
1053
|
+
return index;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
return enabledIndices[0] ?? -1;
|
|
1057
|
+
}, [rovingFocus, focusedValue, selectedValue, items, enabledIndices]);
|
|
1001
1058
|
const getActiveItemIndices = (range) => {
|
|
1002
1059
|
const visibleAndStickyIndexes = [];
|
|
1003
1060
|
for (let i = range.startIndex; i <= range.endIndex; i++) {
|
|
@@ -1009,6 +1066,9 @@ var PopoverMenuList = ({
|
|
|
1009
1066
|
visibleAndStickyIndexes.push(stickyIndex);
|
|
1010
1067
|
}
|
|
1011
1068
|
});
|
|
1069
|
+
if (tabbableIndex >= 0 && !visibleAndStickyIndexes.includes(tabbableIndex)) {
|
|
1070
|
+
visibleAndStickyIndexes.push(tabbableIndex);
|
|
1071
|
+
}
|
|
1012
1072
|
return visibleAndStickyIndexes.sort((a, b) => a - b);
|
|
1013
1073
|
};
|
|
1014
1074
|
const onChangeCallback = ({ getVirtualIndexes }) => {
|
|
@@ -1023,11 +1083,32 @@ var PopoverMenuList = ({
|
|
|
1023
1083
|
rangeExtractor: getActiveItemIndices,
|
|
1024
1084
|
onChange: onChangeCallback
|
|
1025
1085
|
});
|
|
1026
|
-
(0,
|
|
1086
|
+
(0, import_react19.useEffect)(() => {
|
|
1027
1087
|
onChangeCallback(virtualizer);
|
|
1028
1088
|
}, [items]);
|
|
1029
1089
|
useScrollToSelected({ selectedValue, items, virtualizer });
|
|
1030
1090
|
const virtualItems = virtualizer.getVirtualItems();
|
|
1091
|
+
const mountedSignature = virtualItems.map((row) => row.index).join(",");
|
|
1092
|
+
const { registerRef, requestFocus } = useRovingFocus({
|
|
1093
|
+
tabbableKey: items[tabbableIndex]?.value,
|
|
1094
|
+
mountedSignature
|
|
1095
|
+
});
|
|
1096
|
+
const moveFocus = (key, currentIndex) => {
|
|
1097
|
+
const position = enabledIndices.indexOf(currentIndex);
|
|
1098
|
+
const targets = {
|
|
1099
|
+
ArrowDown: enabledIndices[position + 1],
|
|
1100
|
+
ArrowUp: position > 0 ? enabledIndices[position - 1] : void 0,
|
|
1101
|
+
Home: enabledIndices[0],
|
|
1102
|
+
End: enabledIndices[enabledIndices.length - 1]
|
|
1103
|
+
};
|
|
1104
|
+
const targetIndex = targets[key];
|
|
1105
|
+
if (targetIndex === void 0 || targetIndex === currentIndex) {
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
requestFocus();
|
|
1109
|
+
setFocusedValue(items[targetIndex].value);
|
|
1110
|
+
virtualizer.scrollToIndex(targetIndex);
|
|
1111
|
+
};
|
|
1031
1112
|
return /* @__PURE__ */ React24.createElement(import_ui23.Box, { ref: containerRef, sx: { height: "100%", overflowY: "auto" } }, items.length === 0 && noResultsComponent ? noResultsComponent : /* @__PURE__ */ React24.createElement(
|
|
1032
1113
|
MenuListComponent,
|
|
1033
1114
|
{
|
|
@@ -1042,6 +1123,9 @@ var PopoverMenuList = ({
|
|
|
1042
1123
|
const isFirst = items[0]?.type === "category" ? virtualRow.index === 1 : virtualRow.index === 0;
|
|
1043
1124
|
const isSelected = selectedValue === item.value;
|
|
1044
1125
|
const tabIndexFallback = !selectedValue ? 0 : -1;
|
|
1126
|
+
const defaultTabIndex = isSelected ? 0 : tabIndexFallback;
|
|
1127
|
+
const rovingTabIndex = virtualRow.index === tabbableIndex ? 0 : -1;
|
|
1128
|
+
const optionTabIndex = rovingFocus ? rovingTabIndex : defaultTabIndex;
|
|
1045
1129
|
if (!item) {
|
|
1046
1130
|
return null;
|
|
1047
1131
|
}
|
|
@@ -1062,6 +1146,7 @@ var PopoverMenuList = ({
|
|
|
1062
1146
|
import_ui23.ListItem,
|
|
1063
1147
|
{
|
|
1064
1148
|
key: virtualRow.key,
|
|
1149
|
+
ref: registerRef(item.value),
|
|
1065
1150
|
role: "option",
|
|
1066
1151
|
"aria-selected": isSelected,
|
|
1067
1152
|
"aria-disabled": isDisabled,
|
|
@@ -1073,10 +1158,19 @@ var PopoverMenuList = ({
|
|
|
1073
1158
|
onClose();
|
|
1074
1159
|
},
|
|
1075
1160
|
onKeyDown: (event) => {
|
|
1076
|
-
if (event.key === "Enter" && !isDisabled) {
|
|
1161
|
+
if ((event.key === "Enter" || rovingFocus && event.key === " ") && !isDisabled) {
|
|
1162
|
+
if (rovingFocus) {
|
|
1163
|
+
event.preventDefault();
|
|
1164
|
+
}
|
|
1077
1165
|
onSelect(item.value);
|
|
1078
1166
|
onClose();
|
|
1079
1167
|
}
|
|
1168
|
+
if (rovingFocus && ROVING_FOCUS_KEYS.has(event.key)) {
|
|
1169
|
+
event.preventDefault();
|
|
1170
|
+
event.stopPropagation();
|
|
1171
|
+
moveFocus(event.key, virtualRow.index);
|
|
1172
|
+
return;
|
|
1173
|
+
}
|
|
1080
1174
|
if (event.key === "ArrowDown" && isLast) {
|
|
1081
1175
|
event.preventDefault();
|
|
1082
1176
|
event.stopPropagation();
|
|
@@ -1086,7 +1180,8 @@ var PopoverMenuList = ({
|
|
|
1086
1180
|
event.stopPropagation();
|
|
1087
1181
|
}
|
|
1088
1182
|
},
|
|
1089
|
-
|
|
1183
|
+
onFocus: rovingFocus ? () => setFocusedValue(item.value) : void 0,
|
|
1184
|
+
tabIndex: optionTabIndex,
|
|
1090
1185
|
sx: {
|
|
1091
1186
|
transform: `translateY(${virtualRow.start + MENU_LIST_PADDING_TOP}px)`,
|
|
1092
1187
|
...theme.typography.caption,
|
|
@@ -1182,7 +1277,7 @@ function useFloatingActionsPopover() {
|
|
|
1182
1277
|
|
|
1183
1278
|
// src/components/save-changes-dialog.tsx
|
|
1184
1279
|
var React26 = __toESM(require("react"));
|
|
1185
|
-
var
|
|
1280
|
+
var import_react20 = require("react");
|
|
1186
1281
|
var import_icons10 = require("@elementor/icons");
|
|
1187
1282
|
var import_ui25 = require("@elementor/ui");
|
|
1188
1283
|
var TITLE_ID = "save-changes-dialog";
|
|
@@ -1202,7 +1297,7 @@ var SaveChangesDialogTitle = ({ children, onClose }) => /* @__PURE__ */ React26.
|
|
|
1202
1297
|
var SaveChangesDialogContent = ({ children }) => /* @__PURE__ */ React26.createElement(import_ui25.DialogContent, null, children);
|
|
1203
1298
|
var SaveChangesDialogContentText = (props) => /* @__PURE__ */ React26.createElement(import_ui25.DialogContentText, { variant: "body2", color: "textPrimary", display: "flex", flexDirection: "column", ...props });
|
|
1204
1299
|
var SaveChangesDialogActions = ({ actions }) => {
|
|
1205
|
-
const [isConfirming, setIsConfirming] = (0,
|
|
1300
|
+
const [isConfirming, setIsConfirming] = (0, import_react20.useState)(false);
|
|
1206
1301
|
const { cancel, confirm, discard } = actions;
|
|
1207
1302
|
const onConfirm = async () => {
|
|
1208
1303
|
setIsConfirming(true);
|
|
@@ -1216,7 +1311,7 @@ SaveChangesDialog.Content = SaveChangesDialogContent;
|
|
|
1216
1311
|
SaveChangesDialog.ContentText = SaveChangesDialogContentText;
|
|
1217
1312
|
SaveChangesDialog.Actions = SaveChangesDialogActions;
|
|
1218
1313
|
var useDialog = () => {
|
|
1219
|
-
const [isOpen, setIsOpen] = (0,
|
|
1314
|
+
const [isOpen, setIsOpen] = (0, import_react20.useState)(false);
|
|
1220
1315
|
const open = () => setIsOpen(true);
|
|
1221
1316
|
const close = () => setIsOpen(false);
|
|
1222
1317
|
return { isOpen, open, close };
|
|
@@ -1224,7 +1319,7 @@ var useDialog = () => {
|
|
|
1224
1319
|
|
|
1225
1320
|
// src/components/confirmation-dialog.tsx
|
|
1226
1321
|
var React27 = __toESM(require("react"));
|
|
1227
|
-
var
|
|
1322
|
+
var import_react21 = require("react");
|
|
1228
1323
|
var import_icons11 = require("@elementor/icons");
|
|
1229
1324
|
var import_ui26 = require("@elementor/ui");
|
|
1230
1325
|
var import_i18n8 = require("@wordpress/i18n");
|
|
@@ -1246,7 +1341,7 @@ var ConfirmationDialogActions = ({
|
|
|
1246
1341
|
onSuppressMessage,
|
|
1247
1342
|
suppressLabel = (0, import_i18n8.__)("Don't show this again", "elementor")
|
|
1248
1343
|
}) => {
|
|
1249
|
-
const [dontShowAgain, setDontShowAgain] = (0,
|
|
1344
|
+
const [dontShowAgain, setDontShowAgain] = (0, import_react21.useState)(false);
|
|
1250
1345
|
const handleConfirm = () => {
|
|
1251
1346
|
if (dontShowAgain && onSuppressMessage) {
|
|
1252
1347
|
onSuppressMessage();
|
|
@@ -1275,10 +1370,10 @@ ConfirmationDialog.ContentText = ConfirmationDialogContentText;
|
|
|
1275
1370
|
ConfirmationDialog.Actions = ConfirmationDialogActions;
|
|
1276
1371
|
|
|
1277
1372
|
// src/hooks/use-editable.ts
|
|
1278
|
-
var
|
|
1373
|
+
var import_react22 = require("react");
|
|
1279
1374
|
var useEditable = ({ value, onSubmit, validation, onClick, onError }) => {
|
|
1280
|
-
const [isEditing, setIsEditing] = (0,
|
|
1281
|
-
const [error, setError] = (0,
|
|
1375
|
+
const [isEditing, setIsEditing] = (0, import_react22.useState)(false);
|
|
1376
|
+
const [error, setError] = (0, import_react22.useState)(null);
|
|
1282
1377
|
const ref = useSelection(isEditing);
|
|
1283
1378
|
const isDirty = (newValue) => newValue !== value;
|
|
1284
1379
|
const openEditMode = () => {
|
|
@@ -1360,8 +1455,8 @@ var useEditable = ({ value, onSubmit, validation, onClick, onError }) => {
|
|
|
1360
1455
|
};
|
|
1361
1456
|
};
|
|
1362
1457
|
var useSelection = (isEditing) => {
|
|
1363
|
-
const ref = (0,
|
|
1364
|
-
(0,
|
|
1458
|
+
const ref = (0, import_react22.useRef)(null);
|
|
1459
|
+
(0, import_react22.useEffect)(() => {
|
|
1365
1460
|
if (isEditing) {
|
|
1366
1461
|
selectAll(ref.current);
|
|
1367
1462
|
}
|
|
@@ -1419,6 +1514,7 @@ var selectAll = (el) => {
|
|
|
1419
1514
|
useDialog,
|
|
1420
1515
|
useEditable,
|
|
1421
1516
|
useFloatingActionsBar,
|
|
1517
|
+
useRovingFocus,
|
|
1422
1518
|
useSectionWidth,
|
|
1423
1519
|
useTextFieldAutoSelect
|
|
1424
1520
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -538,6 +538,37 @@ var useCanvasClickHandler = (isActive, onClickAway) => {
|
|
|
538
538
|
}, [isActive, onClickAway]);
|
|
539
539
|
};
|
|
540
540
|
|
|
541
|
+
// src/hooks/use-roving-focus.ts
|
|
542
|
+
import { useCallback, useLayoutEffect, useRef as useRef4 } from "react";
|
|
543
|
+
var useRovingFocus = ({ tabbableKey, mountedSignature }) => {
|
|
544
|
+
const elements = useRef4(/* @__PURE__ */ new Map());
|
|
545
|
+
const isFocusPending = useRef4(false);
|
|
546
|
+
useLayoutEffect(() => {
|
|
547
|
+
if (!isFocusPending.current || tabbableKey === void 0) {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
const element = elements.current.get(tabbableKey);
|
|
551
|
+
if (element) {
|
|
552
|
+
element.focus();
|
|
553
|
+
isFocusPending.current = false;
|
|
554
|
+
}
|
|
555
|
+
}, [tabbableKey, mountedSignature]);
|
|
556
|
+
const registerRef = useCallback(
|
|
557
|
+
(key) => (node) => {
|
|
558
|
+
if (node) {
|
|
559
|
+
elements.current.set(key, node);
|
|
560
|
+
} else {
|
|
561
|
+
elements.current.delete(key);
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
[]
|
|
565
|
+
);
|
|
566
|
+
const requestFocus = useCallback(() => {
|
|
567
|
+
isFocusPending.current = true;
|
|
568
|
+
}, []);
|
|
569
|
+
return { registerRef, requestFocus };
|
|
570
|
+
};
|
|
571
|
+
|
|
541
572
|
// src/components/promotions/promotion-infotip.tsx
|
|
542
573
|
var PromotionInfotip = ({ children, open, onClose, onCtaClick, ...cardProps }) => {
|
|
543
574
|
useCanvasClickHandler(!!open, onClose);
|
|
@@ -744,7 +775,7 @@ function useFloatingActionsBar() {
|
|
|
744
775
|
|
|
745
776
|
// src/components/file-upload/file-upload-dropzone.tsx
|
|
746
777
|
import * as React19 from "react";
|
|
747
|
-
import { useRef as
|
|
778
|
+
import { useRef as useRef5 } from "react";
|
|
748
779
|
import { UploadIcon } from "@elementor/icons";
|
|
749
780
|
import { Card as Card3, Link as Link2, Stack as Stack2, Typography as Typography5, useUnstableDropZone } from "@elementor/ui";
|
|
750
781
|
import { __ as __6 } from "@wordpress/i18n";
|
|
@@ -763,7 +794,7 @@ var FileUploadDropzone = ({
|
|
|
763
794
|
secondaryLabel,
|
|
764
795
|
helperText
|
|
765
796
|
}) => {
|
|
766
|
-
const fileInputRef =
|
|
797
|
+
const fileInputRef = useRef5(null);
|
|
767
798
|
const { getDropZoneProps } = useUnstableDropZone({
|
|
768
799
|
allowedFileTypes,
|
|
769
800
|
onChange: ({ valid }) => {
|
|
@@ -916,12 +947,13 @@ var PopoverHeader = ({ title, onClose, icon, actions, sx }) => {
|
|
|
916
947
|
|
|
917
948
|
// src/components/popover/menu-list.tsx
|
|
918
949
|
import * as React24 from "react";
|
|
919
|
-
import { useEffect as useEffect8, useMemo, useRef as
|
|
950
|
+
import { useEffect as useEffect8, useMemo, useRef as useRef6, useState as useState8 } from "react";
|
|
920
951
|
import { Box as Box7, ListItem, MenuList, MenuSubheader, styled as styled5, useTheme } from "@elementor/ui";
|
|
921
952
|
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
922
953
|
var ITEM_HEIGHT = 32;
|
|
923
954
|
var LIST_ITEMS_BUFFER = 6;
|
|
924
955
|
var MENU_LIST_PADDING_TOP = 8;
|
|
956
|
+
var ROVING_FOCUS_KEYS = /* @__PURE__ */ new Set(["ArrowDown", "ArrowUp", "Home", "End"]);
|
|
925
957
|
var menuSubHeaderAbsoluteStyling = (start) => ({
|
|
926
958
|
position: "absolute",
|
|
927
959
|
transform: `translateY(${start + MENU_LIST_PADDING_TOP}px)`
|
|
@@ -943,11 +975,13 @@ var PopoverMenuList = ({
|
|
|
943
975
|
categoryItemContentTemplate,
|
|
944
976
|
noResultsComponent,
|
|
945
977
|
menuListTemplate: CustomMenuList,
|
|
946
|
-
itemHeight = ITEM_HEIGHT
|
|
978
|
+
itemHeight = ITEM_HEIGHT,
|
|
979
|
+
rovingFocus = false
|
|
947
980
|
}) => {
|
|
948
|
-
const containerRef =
|
|
981
|
+
const containerRef = useRef6(null);
|
|
949
982
|
const scrollTop = useScrollTop({ containerRef });
|
|
950
983
|
const theme = useTheme();
|
|
984
|
+
const [focusedValue, setFocusedValue] = useState8();
|
|
951
985
|
const MenuListComponent = CustomMenuList || StyledMenuList;
|
|
952
986
|
const stickyIndices = useMemo(
|
|
953
987
|
() => items.reduce((categoryIndices, item, index) => {
|
|
@@ -958,6 +992,28 @@ var PopoverMenuList = ({
|
|
|
958
992
|
}, []),
|
|
959
993
|
[items]
|
|
960
994
|
);
|
|
995
|
+
const enabledIndices = useMemo(
|
|
996
|
+
() => items.reduce((indices, item, index) => {
|
|
997
|
+
if (item.type !== "category" && !item.disabled) {
|
|
998
|
+
indices.push(index);
|
|
999
|
+
}
|
|
1000
|
+
return indices;
|
|
1001
|
+
}, []),
|
|
1002
|
+
[items]
|
|
1003
|
+
);
|
|
1004
|
+
const tabbableIndex = useMemo(() => {
|
|
1005
|
+
if (!rovingFocus) {
|
|
1006
|
+
return -1;
|
|
1007
|
+
}
|
|
1008
|
+
const candidates = [focusedValue, selectedValue];
|
|
1009
|
+
for (const candidate of candidates) {
|
|
1010
|
+
const index = items.findIndex((item) => item.value === candidate);
|
|
1011
|
+
if (enabledIndices.includes(index)) {
|
|
1012
|
+
return index;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
return enabledIndices[0] ?? -1;
|
|
1016
|
+
}, [rovingFocus, focusedValue, selectedValue, items, enabledIndices]);
|
|
961
1017
|
const getActiveItemIndices = (range) => {
|
|
962
1018
|
const visibleAndStickyIndexes = [];
|
|
963
1019
|
for (let i = range.startIndex; i <= range.endIndex; i++) {
|
|
@@ -969,6 +1025,9 @@ var PopoverMenuList = ({
|
|
|
969
1025
|
visibleAndStickyIndexes.push(stickyIndex);
|
|
970
1026
|
}
|
|
971
1027
|
});
|
|
1028
|
+
if (tabbableIndex >= 0 && !visibleAndStickyIndexes.includes(tabbableIndex)) {
|
|
1029
|
+
visibleAndStickyIndexes.push(tabbableIndex);
|
|
1030
|
+
}
|
|
972
1031
|
return visibleAndStickyIndexes.sort((a, b) => a - b);
|
|
973
1032
|
};
|
|
974
1033
|
const onChangeCallback = ({ getVirtualIndexes }) => {
|
|
@@ -988,6 +1047,27 @@ var PopoverMenuList = ({
|
|
|
988
1047
|
}, [items]);
|
|
989
1048
|
useScrollToSelected({ selectedValue, items, virtualizer });
|
|
990
1049
|
const virtualItems = virtualizer.getVirtualItems();
|
|
1050
|
+
const mountedSignature = virtualItems.map((row) => row.index).join(",");
|
|
1051
|
+
const { registerRef, requestFocus } = useRovingFocus({
|
|
1052
|
+
tabbableKey: items[tabbableIndex]?.value,
|
|
1053
|
+
mountedSignature
|
|
1054
|
+
});
|
|
1055
|
+
const moveFocus = (key, currentIndex) => {
|
|
1056
|
+
const position = enabledIndices.indexOf(currentIndex);
|
|
1057
|
+
const targets = {
|
|
1058
|
+
ArrowDown: enabledIndices[position + 1],
|
|
1059
|
+
ArrowUp: position > 0 ? enabledIndices[position - 1] : void 0,
|
|
1060
|
+
Home: enabledIndices[0],
|
|
1061
|
+
End: enabledIndices[enabledIndices.length - 1]
|
|
1062
|
+
};
|
|
1063
|
+
const targetIndex = targets[key];
|
|
1064
|
+
if (targetIndex === void 0 || targetIndex === currentIndex) {
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
requestFocus();
|
|
1068
|
+
setFocusedValue(items[targetIndex].value);
|
|
1069
|
+
virtualizer.scrollToIndex(targetIndex);
|
|
1070
|
+
};
|
|
991
1071
|
return /* @__PURE__ */ React24.createElement(Box7, { ref: containerRef, sx: { height: "100%", overflowY: "auto" } }, items.length === 0 && noResultsComponent ? noResultsComponent : /* @__PURE__ */ React24.createElement(
|
|
992
1072
|
MenuListComponent,
|
|
993
1073
|
{
|
|
@@ -1002,6 +1082,9 @@ var PopoverMenuList = ({
|
|
|
1002
1082
|
const isFirst = items[0]?.type === "category" ? virtualRow.index === 1 : virtualRow.index === 0;
|
|
1003
1083
|
const isSelected = selectedValue === item.value;
|
|
1004
1084
|
const tabIndexFallback = !selectedValue ? 0 : -1;
|
|
1085
|
+
const defaultTabIndex = isSelected ? 0 : tabIndexFallback;
|
|
1086
|
+
const rovingTabIndex = virtualRow.index === tabbableIndex ? 0 : -1;
|
|
1087
|
+
const optionTabIndex = rovingFocus ? rovingTabIndex : defaultTabIndex;
|
|
1005
1088
|
if (!item) {
|
|
1006
1089
|
return null;
|
|
1007
1090
|
}
|
|
@@ -1022,6 +1105,7 @@ var PopoverMenuList = ({
|
|
|
1022
1105
|
ListItem,
|
|
1023
1106
|
{
|
|
1024
1107
|
key: virtualRow.key,
|
|
1108
|
+
ref: registerRef(item.value),
|
|
1025
1109
|
role: "option",
|
|
1026
1110
|
"aria-selected": isSelected,
|
|
1027
1111
|
"aria-disabled": isDisabled,
|
|
@@ -1033,10 +1117,19 @@ var PopoverMenuList = ({
|
|
|
1033
1117
|
onClose();
|
|
1034
1118
|
},
|
|
1035
1119
|
onKeyDown: (event) => {
|
|
1036
|
-
if (event.key === "Enter" && !isDisabled) {
|
|
1120
|
+
if ((event.key === "Enter" || rovingFocus && event.key === " ") && !isDisabled) {
|
|
1121
|
+
if (rovingFocus) {
|
|
1122
|
+
event.preventDefault();
|
|
1123
|
+
}
|
|
1037
1124
|
onSelect(item.value);
|
|
1038
1125
|
onClose();
|
|
1039
1126
|
}
|
|
1127
|
+
if (rovingFocus && ROVING_FOCUS_KEYS.has(event.key)) {
|
|
1128
|
+
event.preventDefault();
|
|
1129
|
+
event.stopPropagation();
|
|
1130
|
+
moveFocus(event.key, virtualRow.index);
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1040
1133
|
if (event.key === "ArrowDown" && isLast) {
|
|
1041
1134
|
event.preventDefault();
|
|
1042
1135
|
event.stopPropagation();
|
|
@@ -1046,7 +1139,8 @@ var PopoverMenuList = ({
|
|
|
1046
1139
|
event.stopPropagation();
|
|
1047
1140
|
}
|
|
1048
1141
|
},
|
|
1049
|
-
|
|
1142
|
+
onFocus: rovingFocus ? () => setFocusedValue(item.value) : void 0,
|
|
1143
|
+
tabIndex: optionTabIndex,
|
|
1050
1144
|
sx: {
|
|
1051
1145
|
transform: `translateY(${virtualRow.start + MENU_LIST_PADDING_TOP}px)`,
|
|
1052
1146
|
...theme.typography.caption,
|
|
@@ -1142,7 +1236,7 @@ function useFloatingActionsPopover() {
|
|
|
1142
1236
|
|
|
1143
1237
|
// src/components/save-changes-dialog.tsx
|
|
1144
1238
|
import * as React26 from "react";
|
|
1145
|
-
import { useState as
|
|
1239
|
+
import { useState as useState9 } from "react";
|
|
1146
1240
|
import { AlertTriangleFilledIcon, XIcon as XIcon3 } from "@elementor/icons";
|
|
1147
1241
|
import {
|
|
1148
1242
|
Button as Button6,
|
|
@@ -1171,7 +1265,7 @@ var SaveChangesDialogTitle = ({ children, onClose }) => /* @__PURE__ */ React26.
|
|
|
1171
1265
|
var SaveChangesDialogContent = ({ children }) => /* @__PURE__ */ React26.createElement(DialogContent, null, children);
|
|
1172
1266
|
var SaveChangesDialogContentText = (props) => /* @__PURE__ */ React26.createElement(DialogContentText, { variant: "body2", color: "textPrimary", display: "flex", flexDirection: "column", ...props });
|
|
1173
1267
|
var SaveChangesDialogActions = ({ actions }) => {
|
|
1174
|
-
const [isConfirming, setIsConfirming] =
|
|
1268
|
+
const [isConfirming, setIsConfirming] = useState9(false);
|
|
1175
1269
|
const { cancel, confirm, discard } = actions;
|
|
1176
1270
|
const onConfirm = async () => {
|
|
1177
1271
|
setIsConfirming(true);
|
|
@@ -1185,7 +1279,7 @@ SaveChangesDialog.Content = SaveChangesDialogContent;
|
|
|
1185
1279
|
SaveChangesDialog.ContentText = SaveChangesDialogContentText;
|
|
1186
1280
|
SaveChangesDialog.Actions = SaveChangesDialogActions;
|
|
1187
1281
|
var useDialog = () => {
|
|
1188
|
-
const [isOpen, setIsOpen] =
|
|
1282
|
+
const [isOpen, setIsOpen] = useState9(false);
|
|
1189
1283
|
const open = () => setIsOpen(true);
|
|
1190
1284
|
const close = () => setIsOpen(false);
|
|
1191
1285
|
return { isOpen, open, close };
|
|
@@ -1193,7 +1287,7 @@ var useDialog = () => {
|
|
|
1193
1287
|
|
|
1194
1288
|
// src/components/confirmation-dialog.tsx
|
|
1195
1289
|
import * as React27 from "react";
|
|
1196
|
-
import { useState as
|
|
1290
|
+
import { useState as useState10 } from "react";
|
|
1197
1291
|
import { AlertOctagonFilledIcon } from "@elementor/icons";
|
|
1198
1292
|
import {
|
|
1199
1293
|
Button as Button7,
|
|
@@ -1225,7 +1319,7 @@ var ConfirmationDialogActions = ({
|
|
|
1225
1319
|
onSuppressMessage,
|
|
1226
1320
|
suppressLabel = __8("Don't show this again", "elementor")
|
|
1227
1321
|
}) => {
|
|
1228
|
-
const [dontShowAgain, setDontShowAgain] =
|
|
1322
|
+
const [dontShowAgain, setDontShowAgain] = useState10(false);
|
|
1229
1323
|
const handleConfirm = () => {
|
|
1230
1324
|
if (dontShowAgain && onSuppressMessage) {
|
|
1231
1325
|
onSuppressMessage();
|
|
@@ -1254,10 +1348,10 @@ ConfirmationDialog.ContentText = ConfirmationDialogContentText;
|
|
|
1254
1348
|
ConfirmationDialog.Actions = ConfirmationDialogActions;
|
|
1255
1349
|
|
|
1256
1350
|
// src/hooks/use-editable.ts
|
|
1257
|
-
import { useEffect as useEffect9, useRef as
|
|
1351
|
+
import { useEffect as useEffect9, useRef as useRef7, useState as useState11 } from "react";
|
|
1258
1352
|
var useEditable = ({ value, onSubmit, validation, onClick, onError }) => {
|
|
1259
|
-
const [isEditing, setIsEditing] =
|
|
1260
|
-
const [error, setError] =
|
|
1353
|
+
const [isEditing, setIsEditing] = useState11(false);
|
|
1354
|
+
const [error, setError] = useState11(null);
|
|
1261
1355
|
const ref = useSelection(isEditing);
|
|
1262
1356
|
const isDirty = (newValue) => newValue !== value;
|
|
1263
1357
|
const openEditMode = () => {
|
|
@@ -1339,7 +1433,7 @@ var useEditable = ({ value, onSubmit, validation, onClick, onError }) => {
|
|
|
1339
1433
|
};
|
|
1340
1434
|
};
|
|
1341
1435
|
var useSelection = (isEditing) => {
|
|
1342
|
-
const ref =
|
|
1436
|
+
const ref = useRef7(null);
|
|
1343
1437
|
useEffect9(() => {
|
|
1344
1438
|
if (isEditing) {
|
|
1345
1439
|
selectAll(ref.current);
|
|
@@ -1397,6 +1491,7 @@ export {
|
|
|
1397
1491
|
useDialog,
|
|
1398
1492
|
useEditable,
|
|
1399
1493
|
useFloatingActionsBar,
|
|
1494
|
+
useRovingFocus,
|
|
1400
1495
|
useSectionWidth,
|
|
1401
1496
|
useTextFieldAutoSelect
|
|
1402
1497
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-ui",
|
|
3
3
|
"description": "Elementor Editor UI",
|
|
4
|
-
"version": "4.5.0-
|
|
4
|
+
"version": "4.5.0-1163",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"react-dom": "^18.3.1"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@elementor/editor-v1-adapters": "4.5.0-
|
|
40
|
+
"@elementor/editor-v1-adapters": "4.5.0-1163",
|
|
41
41
|
"@elementor/icons": "~1.78.1",
|
|
42
42
|
"@elementor/ui": "1.41.4",
|
|
43
43
|
"@tanstack/react-virtual": "^3.13.3",
|
|
@@ -14,15 +14,23 @@ const mockItems = [
|
|
|
14
14
|
{ value: 'Item 8', type: 'item', disabled: false },
|
|
15
15
|
];
|
|
16
16
|
|
|
17
|
+
let mockRangeExtractor: ( range: { startIndex: number; endIndex: number } ) => number[] = () => [];
|
|
18
|
+
|
|
17
19
|
jest.mock( '@tanstack/react-virtual', () => ( {
|
|
18
|
-
useVirtualizer: jest.fn().mockImplementation( () =>
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
useVirtualizer: jest.fn().mockImplementation( ( config ) => {
|
|
21
|
+
mockRangeExtractor = config.rangeExtractor;
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
getVirtualItems: jest
|
|
25
|
+
.fn()
|
|
26
|
+
.mockReturnValue(
|
|
27
|
+
mockItems.map( ( item, index ) => ( { key: item.value, index, start: index * 10 } ) )
|
|
28
|
+
),
|
|
29
|
+
getTotalSize: jest.fn().mockReturnValue( mockItems.length ),
|
|
30
|
+
scrollToIndex: jest.fn(),
|
|
31
|
+
getVirtualIndexes: jest.fn().mockReturnValue( mockItems.map( ( _, index ) => index ) ),
|
|
32
|
+
};
|
|
33
|
+
} ),
|
|
26
34
|
} ) );
|
|
27
35
|
|
|
28
36
|
describe( 'PopoverMenuList', () => {
|
|
@@ -104,4 +112,160 @@ describe( 'PopoverMenuList', () => {
|
|
|
104
112
|
fireEvent.click( item1 );
|
|
105
113
|
expect( onSelect ).toHaveBeenCalledWith( mockItems[ 0 ].value );
|
|
106
114
|
} );
|
|
115
|
+
describe( 'rovingFocus', () => {
|
|
116
|
+
const getOption = ( value: string ) =>
|
|
117
|
+
screen.getAllByRole( 'option' ).find( ( option ) => option.textContent === value ) as HTMLElement;
|
|
118
|
+
|
|
119
|
+
it( 'should make only the first enabled item tabbable when nothing is selected', () => {
|
|
120
|
+
// Arrange.
|
|
121
|
+
render( <PopoverMenuList items={ mockItems } onSelect={ jest.fn() } onClose={ jest.fn() } rovingFocus /> );
|
|
122
|
+
|
|
123
|
+
// Assert.
|
|
124
|
+
const tabbable = screen.getAllByRole( 'option' ).filter( ( option ) => option.tabIndex === 0 );
|
|
125
|
+
|
|
126
|
+
expect( tabbable ).toHaveLength( 1 );
|
|
127
|
+
expect( tabbable[ 0 ] ).toBe( getOption( 'Item 1' ) );
|
|
128
|
+
} );
|
|
129
|
+
|
|
130
|
+
it( 'should make only the selected item tabbable', () => {
|
|
131
|
+
// Arrange.
|
|
132
|
+
render(
|
|
133
|
+
<PopoverMenuList
|
|
134
|
+
items={ mockItems }
|
|
135
|
+
selectedValue="Item 4"
|
|
136
|
+
onSelect={ jest.fn() }
|
|
137
|
+
onClose={ jest.fn() }
|
|
138
|
+
rovingFocus
|
|
139
|
+
/>
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// Assert.
|
|
143
|
+
const tabbable = screen.getAllByRole( 'option' ).filter( ( option ) => option.tabIndex === 0 );
|
|
144
|
+
|
|
145
|
+
expect( tabbable ).toEqual( [ getOption( 'Item 4' ) ] );
|
|
146
|
+
} );
|
|
147
|
+
|
|
148
|
+
it( 'should move focus with the arrow keys and skip disabled items', () => {
|
|
149
|
+
// Arrange.
|
|
150
|
+
render( <PopoverMenuList items={ mockItems } onSelect={ jest.fn() } onClose={ jest.fn() } rovingFocus /> );
|
|
151
|
+
|
|
152
|
+
getOption( 'Item 1' ).focus();
|
|
153
|
+
|
|
154
|
+
// Act.
|
|
155
|
+
fireEvent.keyDown( getOption( 'Item 1' ), { key: 'ArrowDown' } );
|
|
156
|
+
|
|
157
|
+
// Assert.
|
|
158
|
+
expect( getOption( 'Item 3' ) ).toHaveFocus();
|
|
159
|
+
expect( getOption( 'Item 3' ).tabIndex ).toBe( 0 );
|
|
160
|
+
expect( getOption( 'Item 1' ).tabIndex ).toBe( -1 );
|
|
161
|
+
|
|
162
|
+
// Act.
|
|
163
|
+
fireEvent.keyDown( getOption( 'Item 3' ), { key: 'ArrowUp' } );
|
|
164
|
+
|
|
165
|
+
// Assert.
|
|
166
|
+
expect( getOption( 'Item 1' ) ).toHaveFocus();
|
|
167
|
+
} );
|
|
168
|
+
|
|
169
|
+
it( 'should move focus to the first and last enabled items with Home and End', () => {
|
|
170
|
+
// Arrange.
|
|
171
|
+
render( <PopoverMenuList items={ mockItems } onSelect={ jest.fn() } onClose={ jest.fn() } rovingFocus /> );
|
|
172
|
+
|
|
173
|
+
getOption( 'Item 1' ).focus();
|
|
174
|
+
|
|
175
|
+
// Act.
|
|
176
|
+
fireEvent.keyDown( getOption( 'Item 1' ), { key: 'End' } );
|
|
177
|
+
|
|
178
|
+
// Assert.
|
|
179
|
+
expect( getOption( 'Item 8' ) ).toHaveFocus();
|
|
180
|
+
|
|
181
|
+
// Act.
|
|
182
|
+
fireEvent.keyDown( getOption( 'Item 8' ), { key: 'Home' } );
|
|
183
|
+
|
|
184
|
+
// Assert.
|
|
185
|
+
expect( getOption( 'Item 1' ) ).toHaveFocus();
|
|
186
|
+
} );
|
|
187
|
+
|
|
188
|
+
it( 'should select and close with Enter on the focused item', () => {
|
|
189
|
+
// Arrange.
|
|
190
|
+
const onSelect = jest.fn();
|
|
191
|
+
const onClose = jest.fn();
|
|
192
|
+
|
|
193
|
+
render( <PopoverMenuList items={ mockItems } onSelect={ onSelect } onClose={ onClose } rovingFocus /> );
|
|
194
|
+
|
|
195
|
+
getOption( 'Item 1' ).focus();
|
|
196
|
+
fireEvent.keyDown( getOption( 'Item 1' ), { key: 'ArrowDown' } );
|
|
197
|
+
|
|
198
|
+
// Act.
|
|
199
|
+
fireEvent.keyDown( getOption( 'Item 3' ), { key: 'Enter' } );
|
|
200
|
+
|
|
201
|
+
// Assert.
|
|
202
|
+
expect( onSelect ).toHaveBeenCalledWith( 'Item 3' );
|
|
203
|
+
expect( onClose ).toHaveBeenCalledTimes( 1 );
|
|
204
|
+
} );
|
|
205
|
+
|
|
206
|
+
it( 'should prevent the default Enter action so it does not click the element focused after closing', () => {
|
|
207
|
+
// Arrange.
|
|
208
|
+
render( <PopoverMenuList items={ mockItems } onSelect={ jest.fn() } onClose={ jest.fn() } rovingFocus /> );
|
|
209
|
+
|
|
210
|
+
// Act.
|
|
211
|
+
const notPrevented = fireEvent.keyDown( getOption( 'Item 1' ), { key: 'Enter' } );
|
|
212
|
+
|
|
213
|
+
// Assert.
|
|
214
|
+
expect( notPrevented ).toBe( false );
|
|
215
|
+
} );
|
|
216
|
+
|
|
217
|
+
it( 'should select and close with Space on the focused item', () => {
|
|
218
|
+
// Arrange.
|
|
219
|
+
const onSelect = jest.fn();
|
|
220
|
+
const onClose = jest.fn();
|
|
221
|
+
|
|
222
|
+
render( <PopoverMenuList items={ mockItems } onSelect={ onSelect } onClose={ onClose } rovingFocus /> );
|
|
223
|
+
|
|
224
|
+
// Act.
|
|
225
|
+
const notPrevented = fireEvent.keyDown( getOption( 'Item 1' ), { key: ' ' } );
|
|
226
|
+
|
|
227
|
+
// Assert.
|
|
228
|
+
expect( notPrevented ).toBe( false );
|
|
229
|
+
expect( onSelect ).toHaveBeenCalledWith( 'Item 1' );
|
|
230
|
+
expect( onClose ).toHaveBeenCalledTimes( 1 );
|
|
231
|
+
} );
|
|
232
|
+
|
|
233
|
+
it( 'should keep the tab stop mounted when it is outside the visible range', () => {
|
|
234
|
+
// Arrange.
|
|
235
|
+
render(
|
|
236
|
+
<PopoverMenuList
|
|
237
|
+
items={ mockItems }
|
|
238
|
+
selectedValue="Item 8"
|
|
239
|
+
onSelect={ jest.fn() }
|
|
240
|
+
onClose={ jest.fn() }
|
|
241
|
+
rovingFocus
|
|
242
|
+
/>
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
// Act.
|
|
246
|
+
const indices = mockRangeExtractor( { startIndex: 0, endIndex: 2 } );
|
|
247
|
+
|
|
248
|
+
// Assert.
|
|
249
|
+
expect( indices ).toEqual( [ 0, 1, 2, 7 ] );
|
|
250
|
+
} );
|
|
251
|
+
|
|
252
|
+
it( 'should keep the previous tab behavior when rovingFocus is off', () => {
|
|
253
|
+
// Arrange.
|
|
254
|
+
render(
|
|
255
|
+
<PopoverMenuList
|
|
256
|
+
items={ mockItems }
|
|
257
|
+
selectedValue="Item 4"
|
|
258
|
+
onSelect={ jest.fn() }
|
|
259
|
+
onClose={ jest.fn() }
|
|
260
|
+
/>
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
// Act.
|
|
264
|
+
fireEvent.keyDown( getOption( 'Item 4' ), { key: 'ArrowDown' } );
|
|
265
|
+
|
|
266
|
+
// Assert.
|
|
267
|
+
expect( getOption( 'Item 6' ) ).not.toHaveFocus();
|
|
268
|
+
expect( getOption( 'Item 1' ).tabIndex ).toBe( -1 );
|
|
269
|
+
} );
|
|
270
|
+
} );
|
|
107
271
|
} );
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
3
|
import { Box, ListItem, MenuList, MenuSubheader, styled, useTheme } from '@elementor/ui';
|
|
4
4
|
import { useVirtualizer, type Virtualizer } from '@tanstack/react-virtual';
|
|
5
5
|
|
|
6
|
-
import { useScrollTop, useScrollToSelected } from '../../hooks';
|
|
6
|
+
import { useRovingFocus, useScrollTop, useScrollToSelected } from '../../hooks';
|
|
7
7
|
|
|
8
8
|
export type VirtualizedItem< T, V extends string > = {
|
|
9
9
|
type: T;
|
|
@@ -28,11 +28,13 @@ export type PopoverMenuListProps< T, V extends string > = {
|
|
|
28
28
|
categoryItemContentTemplate?: ( item: VirtualizedItem< T, V > ) => React.ReactNode;
|
|
29
29
|
noResultsComponent?: React.ReactNode;
|
|
30
30
|
itemHeight?: number;
|
|
31
|
+
rovingFocus?: boolean;
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
export const ITEM_HEIGHT = 32;
|
|
34
35
|
const LIST_ITEMS_BUFFER = 6;
|
|
35
36
|
const MENU_LIST_PADDING_TOP = 8;
|
|
37
|
+
const ROVING_FOCUS_KEYS = new Set( [ 'ArrowDown', 'ArrowUp', 'Home', 'End' ] );
|
|
36
38
|
|
|
37
39
|
const menuSubHeaderAbsoluteStyling = ( start: number ) => ( {
|
|
38
40
|
position: 'absolute',
|
|
@@ -65,10 +67,12 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
65
67
|
noResultsComponent,
|
|
66
68
|
menuListTemplate: CustomMenuList,
|
|
67
69
|
itemHeight = ITEM_HEIGHT,
|
|
70
|
+
rovingFocus = false,
|
|
68
71
|
}: PopoverMenuListProps< T, V > ) => {
|
|
69
72
|
const containerRef = useRef< HTMLDivElement >( null );
|
|
70
73
|
const scrollTop = useScrollTop( { containerRef } );
|
|
71
74
|
const theme = useTheme();
|
|
75
|
+
const [ focusedValue, setFocusedValue ] = useState< V | undefined >();
|
|
72
76
|
|
|
73
77
|
const MenuListComponent = CustomMenuList || StyledMenuList;
|
|
74
78
|
|
|
@@ -83,6 +87,36 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
83
87
|
[ items ]
|
|
84
88
|
);
|
|
85
89
|
|
|
90
|
+
const enabledIndices = useMemo(
|
|
91
|
+
() =>
|
|
92
|
+
items.reduce( ( indices, item, index ) => {
|
|
93
|
+
if ( item.type !== 'category' && ! item.disabled ) {
|
|
94
|
+
indices.push( index );
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return indices;
|
|
98
|
+
}, [] as number[] ),
|
|
99
|
+
[ items ]
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const tabbableIndex = useMemo( () => {
|
|
103
|
+
if ( ! rovingFocus ) {
|
|
104
|
+
return -1;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const candidates = [ focusedValue, selectedValue ];
|
|
108
|
+
|
|
109
|
+
for ( const candidate of candidates ) {
|
|
110
|
+
const index = items.findIndex( ( item ) => item.value === candidate );
|
|
111
|
+
|
|
112
|
+
if ( enabledIndices.includes( index ) ) {
|
|
113
|
+
return index;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return enabledIndices[ 0 ] ?? -1;
|
|
118
|
+
}, [ rovingFocus, focusedValue, selectedValue, items, enabledIndices ] );
|
|
119
|
+
|
|
86
120
|
const getActiveItemIndices = ( range: { startIndex: number; endIndex: number } ) => {
|
|
87
121
|
const visibleAndStickyIndexes: number[] = [];
|
|
88
122
|
|
|
@@ -98,6 +132,10 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
98
132
|
}
|
|
99
133
|
} );
|
|
100
134
|
|
|
135
|
+
if ( tabbableIndex >= 0 && ! visibleAndStickyIndexes.includes( tabbableIndex ) ) {
|
|
136
|
+
visibleAndStickyIndexes.push( tabbableIndex );
|
|
137
|
+
}
|
|
138
|
+
|
|
101
139
|
return visibleAndStickyIndexes.sort( ( a, b ) => a - b );
|
|
102
140
|
};
|
|
103
141
|
|
|
@@ -123,6 +161,32 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
123
161
|
useScrollToSelected( { selectedValue, items, virtualizer } );
|
|
124
162
|
const virtualItems = virtualizer.getVirtualItems();
|
|
125
163
|
|
|
164
|
+
const mountedSignature = virtualItems.map( ( row ) => row.index ).join( ',' );
|
|
165
|
+
|
|
166
|
+
const { registerRef, requestFocus } = useRovingFocus< HTMLLIElement >( {
|
|
167
|
+
tabbableKey: items[ tabbableIndex ]?.value,
|
|
168
|
+
mountedSignature,
|
|
169
|
+
} );
|
|
170
|
+
|
|
171
|
+
const moveFocus = ( key: string, currentIndex: number ) => {
|
|
172
|
+
const position = enabledIndices.indexOf( currentIndex );
|
|
173
|
+
const targets: Record< string, number | undefined > = {
|
|
174
|
+
ArrowDown: enabledIndices[ position + 1 ],
|
|
175
|
+
ArrowUp: position > 0 ? enabledIndices[ position - 1 ] : undefined,
|
|
176
|
+
Home: enabledIndices[ 0 ],
|
|
177
|
+
End: enabledIndices[ enabledIndices.length - 1 ],
|
|
178
|
+
};
|
|
179
|
+
const targetIndex = targets[ key ];
|
|
180
|
+
|
|
181
|
+
if ( targetIndex === undefined || targetIndex === currentIndex ) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
requestFocus();
|
|
186
|
+
setFocusedValue( items[ targetIndex ].value );
|
|
187
|
+
virtualizer.scrollToIndex( targetIndex );
|
|
188
|
+
};
|
|
189
|
+
|
|
126
190
|
return (
|
|
127
191
|
<Box ref={ containerRef } sx={ { height: '100%', overflowY: 'auto' } }>
|
|
128
192
|
{ items.length === 0 && noResultsComponent ? (
|
|
@@ -141,6 +205,9 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
141
205
|
items[ 0 ]?.type === 'category' ? virtualRow.index === 1 : virtualRow.index === 0;
|
|
142
206
|
const isSelected = selectedValue === item.value;
|
|
143
207
|
const tabIndexFallback = ! selectedValue ? 0 : -1;
|
|
208
|
+
const defaultTabIndex = isSelected ? 0 : tabIndexFallback;
|
|
209
|
+
const rovingTabIndex = virtualRow.index === tabbableIndex ? 0 : -1;
|
|
210
|
+
const optionTabIndex = rovingFocus ? rovingTabIndex : defaultTabIndex;
|
|
144
211
|
|
|
145
212
|
if ( ! item ) {
|
|
146
213
|
return null;
|
|
@@ -165,6 +232,7 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
165
232
|
return (
|
|
166
233
|
<ListItem
|
|
167
234
|
key={ virtualRow.key }
|
|
235
|
+
ref={ registerRef( item.value ) }
|
|
168
236
|
role="option"
|
|
169
237
|
aria-selected={ isSelected }
|
|
170
238
|
aria-disabled={ isDisabled }
|
|
@@ -180,11 +248,26 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
180
248
|
}
|
|
181
249
|
}
|
|
182
250
|
onKeyDown={ ( event: React.KeyboardEvent< HTMLLIElement > ) => {
|
|
183
|
-
if (
|
|
251
|
+
if (
|
|
252
|
+
( event.key === 'Enter' || ( rovingFocus && event.key === ' ' ) ) &&
|
|
253
|
+
! isDisabled
|
|
254
|
+
) {
|
|
255
|
+
if ( rovingFocus ) {
|
|
256
|
+
event.preventDefault();
|
|
257
|
+
}
|
|
258
|
+
|
|
184
259
|
onSelect( item.value );
|
|
185
260
|
onClose();
|
|
186
261
|
}
|
|
187
262
|
|
|
263
|
+
if ( rovingFocus && ROVING_FOCUS_KEYS.has( event.key ) ) {
|
|
264
|
+
event.preventDefault();
|
|
265
|
+
event.stopPropagation();
|
|
266
|
+
moveFocus( event.key, virtualRow.index );
|
|
267
|
+
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
188
271
|
if ( event.key === 'ArrowDown' && isLast ) {
|
|
189
272
|
event.preventDefault();
|
|
190
273
|
event.stopPropagation();
|
|
@@ -195,7 +278,8 @@ export const PopoverMenuList = < T, V extends string >( {
|
|
|
195
278
|
event.stopPropagation();
|
|
196
279
|
}
|
|
197
280
|
} }
|
|
198
|
-
|
|
281
|
+
onFocus={ rovingFocus ? () => setFocusedValue( item.value ) : undefined }
|
|
282
|
+
tabIndex={ optionTabIndex }
|
|
199
283
|
sx={ {
|
|
200
284
|
transform: `translateY(${ virtualRow.start + MENU_LIST_PADDING_TOP }px)`,
|
|
201
285
|
...theme.typography.caption,
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
|
|
4
|
+
import { useRovingFocus } from '../use-roving-focus';
|
|
5
|
+
|
|
6
|
+
type HarnessProps = {
|
|
7
|
+
tabbableKey?: string;
|
|
8
|
+
mountedSignature?: string;
|
|
9
|
+
mountedKeys: string[];
|
|
10
|
+
onReady: ( api: ReturnType< typeof useRovingFocus< HTMLButtonElement > > ) => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const Harness = ( { tabbableKey, mountedSignature = '', mountedKeys, onReady }: HarnessProps ) => {
|
|
14
|
+
const api = useRovingFocus< HTMLButtonElement >( { tabbableKey, mountedSignature } );
|
|
15
|
+
|
|
16
|
+
onReady( api );
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<>
|
|
20
|
+
{ mountedKeys.map( ( key ) => (
|
|
21
|
+
<button key={ key } ref={ api.registerRef( key ) }>
|
|
22
|
+
{ key }
|
|
23
|
+
</button>
|
|
24
|
+
) ) }
|
|
25
|
+
</>
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
describe( 'useRovingFocus', () => {
|
|
30
|
+
it( 'should not move focus until a focus is requested', () => {
|
|
31
|
+
// Arrange.
|
|
32
|
+
render( <Harness tabbableKey="a" mountedKeys={ [ 'a', 'b' ] } onReady={ jest.fn() } /> );
|
|
33
|
+
|
|
34
|
+
// Assert.
|
|
35
|
+
expect( screen.getByText( 'a' ) ).not.toHaveFocus();
|
|
36
|
+
} );
|
|
37
|
+
|
|
38
|
+
it( 'should focus the tabbable element after a request', () => {
|
|
39
|
+
// Arrange.
|
|
40
|
+
let api: ReturnType< typeof useRovingFocus< HTMLButtonElement > > | undefined;
|
|
41
|
+
const { rerender } = render(
|
|
42
|
+
<Harness tabbableKey="a" mountedKeys={ [ 'a', 'b' ] } onReady={ ( value ) => ( api = value ) } />
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// Act.
|
|
46
|
+
api?.requestFocus();
|
|
47
|
+
rerender( <Harness tabbableKey="b" mountedKeys={ [ 'a', 'b' ] } onReady={ ( value ) => ( api = value ) } /> );
|
|
48
|
+
|
|
49
|
+
// Assert.
|
|
50
|
+
expect( screen.getByText( 'b' ) ).toHaveFocus();
|
|
51
|
+
} );
|
|
52
|
+
|
|
53
|
+
it( 'should wait for the tabbable element to mount before focusing it', () => {
|
|
54
|
+
// Arrange.
|
|
55
|
+
let api: ReturnType< typeof useRovingFocus< HTMLButtonElement > > | undefined;
|
|
56
|
+
const { rerender } = render(
|
|
57
|
+
<Harness tabbableKey="a" mountedKeys={ [ 'a' ] } onReady={ ( value ) => ( api = value ) } />
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// Act.
|
|
61
|
+
api?.requestFocus();
|
|
62
|
+
rerender(
|
|
63
|
+
<Harness
|
|
64
|
+
tabbableKey="c"
|
|
65
|
+
mountedSignature="0"
|
|
66
|
+
mountedKeys={ [ 'a' ] }
|
|
67
|
+
onReady={ ( value ) => ( api = value ) }
|
|
68
|
+
/>
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// Assert.
|
|
72
|
+
expect( document.body ).toHaveFocus();
|
|
73
|
+
|
|
74
|
+
// Act.
|
|
75
|
+
rerender(
|
|
76
|
+
<Harness
|
|
77
|
+
tabbableKey="c"
|
|
78
|
+
mountedSignature="0,2"
|
|
79
|
+
mountedKeys={ [ 'a', 'c' ] }
|
|
80
|
+
onReady={ ( value ) => ( api = value ) }
|
|
81
|
+
/>
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// Assert.
|
|
85
|
+
expect( screen.getByText( 'c' ) ).toHaveFocus();
|
|
86
|
+
} );
|
|
87
|
+
} );
|
package/src/hooks/index.ts
CHANGED
|
@@ -2,3 +2,4 @@ export { useScrollToSelected } from './use-scroll-to-selected';
|
|
|
2
2
|
export { useScrollTop } from './use-scroll-top';
|
|
3
3
|
export { useTextFieldAutoSelect } from './use-text-field-auto-select';
|
|
4
4
|
export { useCanvasClickHandler } from './use-canvas-click-handler';
|
|
5
|
+
export { useRovingFocus } from './use-roving-focus';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
type UseRovingFocusProps = {
|
|
4
|
+
tabbableKey: string | undefined;
|
|
5
|
+
mountedSignature: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const useRovingFocus = < E extends HTMLElement >( { tabbableKey, mountedSignature }: UseRovingFocusProps ) => {
|
|
9
|
+
const elements = useRef( new Map< string, E >() );
|
|
10
|
+
const isFocusPending = useRef( false );
|
|
11
|
+
|
|
12
|
+
useLayoutEffect( () => {
|
|
13
|
+
if ( ! isFocusPending.current || tabbableKey === undefined ) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const element = elements.current.get( tabbableKey );
|
|
18
|
+
|
|
19
|
+
if ( element ) {
|
|
20
|
+
element.focus();
|
|
21
|
+
isFocusPending.current = false;
|
|
22
|
+
}
|
|
23
|
+
}, [ tabbableKey, mountedSignature ] );
|
|
24
|
+
|
|
25
|
+
const registerRef = useCallback(
|
|
26
|
+
( key: string ) => ( node: E | null ) => {
|
|
27
|
+
if ( node ) {
|
|
28
|
+
elements.current.set( key, node );
|
|
29
|
+
} else {
|
|
30
|
+
elements.current.delete( key );
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
[]
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const requestFocus = useCallback( () => {
|
|
37
|
+
isFocusPending.current = true;
|
|
38
|
+
}, [] );
|
|
39
|
+
|
|
40
|
+
return { registerRef, requestFocus };
|
|
41
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -31,3 +31,4 @@ export { SectionRefContext, useSectionWidth } from './contexts/section-context';
|
|
|
31
31
|
export { useEditable } from './hooks/use-editable';
|
|
32
32
|
export { useTextFieldAutoSelect } from './hooks/use-text-field-auto-select';
|
|
33
33
|
export { useCanvasClickHandler } from './hooks/use-canvas-click-handler';
|
|
34
|
+
export { useRovingFocus } from './hooks/use-roving-focus';
|