@react-aria/menu 3.20.0 → 3.22.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.
- package/dist/import.mjs +2 -10
- package/dist/main.js +6 -14
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -10
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/package.json +15 -24
- package/src/index.ts +2 -11
- package/dist/ar-AE.main.js +0 -6
- package/dist/ar-AE.main.js.map +0 -1
- package/dist/ar-AE.mjs +0 -8
- package/dist/ar-AE.module.js +0 -8
- package/dist/ar-AE.module.js.map +0 -1
- package/dist/bg-BG.main.js +0 -6
- package/dist/bg-BG.main.js.map +0 -1
- package/dist/bg-BG.mjs +0 -8
- package/dist/bg-BG.module.js +0 -8
- package/dist/bg-BG.module.js.map +0 -1
- package/dist/cs-CZ.main.js +0 -6
- package/dist/cs-CZ.main.js.map +0 -1
- package/dist/cs-CZ.mjs +0 -8
- package/dist/cs-CZ.module.js +0 -8
- package/dist/cs-CZ.module.js.map +0 -1
- package/dist/da-DK.main.js +0 -6
- package/dist/da-DK.main.js.map +0 -1
- package/dist/da-DK.mjs +0 -8
- package/dist/da-DK.module.js +0 -8
- package/dist/da-DK.module.js.map +0 -1
- package/dist/de-DE.main.js +0 -6
- package/dist/de-DE.main.js.map +0 -1
- package/dist/de-DE.mjs +0 -8
- package/dist/de-DE.module.js +0 -8
- package/dist/de-DE.module.js.map +0 -1
- package/dist/el-GR.main.js +0 -6
- package/dist/el-GR.main.js.map +0 -1
- package/dist/el-GR.mjs +0 -8
- package/dist/el-GR.module.js +0 -8
- package/dist/el-GR.module.js.map +0 -1
- package/dist/en-US.main.js +0 -6
- package/dist/en-US.main.js.map +0 -1
- package/dist/en-US.mjs +0 -8
- package/dist/en-US.module.js +0 -8
- package/dist/en-US.module.js.map +0 -1
- package/dist/es-ES.main.js +0 -6
- package/dist/es-ES.main.js.map +0 -1
- package/dist/es-ES.mjs +0 -8
- package/dist/es-ES.module.js +0 -8
- package/dist/es-ES.module.js.map +0 -1
- package/dist/et-EE.main.js +0 -6
- package/dist/et-EE.main.js.map +0 -1
- package/dist/et-EE.mjs +0 -8
- package/dist/et-EE.module.js +0 -8
- package/dist/et-EE.module.js.map +0 -1
- package/dist/fi-FI.main.js +0 -6
- package/dist/fi-FI.main.js.map +0 -1
- package/dist/fi-FI.mjs +0 -8
- package/dist/fi-FI.module.js +0 -8
- package/dist/fi-FI.module.js.map +0 -1
- package/dist/fr-FR.main.js +0 -6
- package/dist/fr-FR.main.js.map +0 -1
- package/dist/fr-FR.mjs +0 -8
- package/dist/fr-FR.module.js +0 -8
- package/dist/fr-FR.module.js.map +0 -1
- package/dist/he-IL.main.js +0 -6
- package/dist/he-IL.main.js.map +0 -1
- package/dist/he-IL.mjs +0 -8
- package/dist/he-IL.module.js +0 -8
- package/dist/he-IL.module.js.map +0 -1
- package/dist/hr-HR.main.js +0 -6
- package/dist/hr-HR.main.js.map +0 -1
- package/dist/hr-HR.mjs +0 -8
- package/dist/hr-HR.module.js +0 -8
- package/dist/hr-HR.module.js.map +0 -1
- package/dist/hu-HU.main.js +0 -6
- package/dist/hu-HU.main.js.map +0 -1
- package/dist/hu-HU.mjs +0 -8
- package/dist/hu-HU.module.js +0 -8
- package/dist/hu-HU.module.js.map +0 -1
- package/dist/intlStrings.main.js +0 -108
- package/dist/intlStrings.main.js.map +0 -1
- package/dist/intlStrings.mjs +0 -110
- package/dist/intlStrings.module.js +0 -110
- package/dist/intlStrings.module.js.map +0 -1
- package/dist/it-IT.main.js +0 -6
- package/dist/it-IT.main.js.map +0 -1
- package/dist/it-IT.mjs +0 -8
- package/dist/it-IT.module.js +0 -8
- package/dist/it-IT.module.js.map +0 -1
- package/dist/ja-JP.main.js +0 -6
- package/dist/ja-JP.main.js.map +0 -1
- package/dist/ja-JP.mjs +0 -8
- package/dist/ja-JP.module.js +0 -8
- package/dist/ja-JP.module.js.map +0 -1
- package/dist/ko-KR.main.js +0 -6
- package/dist/ko-KR.main.js.map +0 -1
- package/dist/ko-KR.mjs +0 -8
- package/dist/ko-KR.module.js +0 -8
- package/dist/ko-KR.module.js.map +0 -1
- package/dist/lt-LT.main.js +0 -6
- package/dist/lt-LT.main.js.map +0 -1
- package/dist/lt-LT.mjs +0 -8
- package/dist/lt-LT.module.js +0 -8
- package/dist/lt-LT.module.js.map +0 -1
- package/dist/lv-LV.main.js +0 -6
- package/dist/lv-LV.main.js.map +0 -1
- package/dist/lv-LV.mjs +0 -8
- package/dist/lv-LV.module.js +0 -8
- package/dist/lv-LV.module.js.map +0 -1
- package/dist/nb-NO.main.js +0 -6
- package/dist/nb-NO.main.js.map +0 -1
- package/dist/nb-NO.mjs +0 -8
- package/dist/nb-NO.module.js +0 -8
- package/dist/nb-NO.module.js.map +0 -1
- package/dist/nl-NL.main.js +0 -6
- package/dist/nl-NL.main.js.map +0 -1
- package/dist/nl-NL.mjs +0 -8
- package/dist/nl-NL.module.js +0 -8
- package/dist/nl-NL.module.js.map +0 -1
- package/dist/pl-PL.main.js +0 -6
- package/dist/pl-PL.main.js.map +0 -1
- package/dist/pl-PL.mjs +0 -8
- package/dist/pl-PL.module.js +0 -8
- package/dist/pl-PL.module.js.map +0 -1
- package/dist/pt-BR.main.js +0 -6
- package/dist/pt-BR.main.js.map +0 -1
- package/dist/pt-BR.mjs +0 -8
- package/dist/pt-BR.module.js +0 -8
- package/dist/pt-BR.module.js.map +0 -1
- package/dist/pt-PT.main.js +0 -6
- package/dist/pt-PT.main.js.map +0 -1
- package/dist/pt-PT.mjs +0 -8
- package/dist/pt-PT.module.js +0 -8
- package/dist/pt-PT.module.js.map +0 -1
- package/dist/ro-RO.main.js +0 -6
- package/dist/ro-RO.main.js.map +0 -1
- package/dist/ro-RO.mjs +0 -8
- package/dist/ro-RO.module.js +0 -8
- package/dist/ro-RO.module.js.map +0 -1
- package/dist/ru-RU.main.js +0 -6
- package/dist/ru-RU.main.js.map +0 -1
- package/dist/ru-RU.mjs +0 -8
- package/dist/ru-RU.module.js +0 -8
- package/dist/ru-RU.module.js.map +0 -1
- package/dist/sk-SK.main.js +0 -6
- package/dist/sk-SK.main.js.map +0 -1
- package/dist/sk-SK.mjs +0 -8
- package/dist/sk-SK.module.js +0 -8
- package/dist/sk-SK.module.js.map +0 -1
- package/dist/sl-SI.main.js +0 -6
- package/dist/sl-SI.main.js.map +0 -1
- package/dist/sl-SI.mjs +0 -8
- package/dist/sl-SI.module.js +0 -8
- package/dist/sl-SI.module.js.map +0 -1
- package/dist/sr-SP.main.js +0 -6
- package/dist/sr-SP.main.js.map +0 -1
- package/dist/sr-SP.mjs +0 -8
- package/dist/sr-SP.module.js +0 -8
- package/dist/sr-SP.module.js.map +0 -1
- package/dist/sv-SE.main.js +0 -6
- package/dist/sv-SE.main.js.map +0 -1
- package/dist/sv-SE.mjs +0 -8
- package/dist/sv-SE.module.js +0 -8
- package/dist/sv-SE.module.js.map +0 -1
- package/dist/tr-TR.main.js +0 -6
- package/dist/tr-TR.main.js.map +0 -1
- package/dist/tr-TR.mjs +0 -8
- package/dist/tr-TR.module.js +0 -8
- package/dist/tr-TR.module.js.map +0 -1
- package/dist/types.d.ts +0 -191
- package/dist/types.d.ts.map +0 -1
- package/dist/uk-UA.main.js +0 -6
- package/dist/uk-UA.main.js.map +0 -1
- package/dist/uk-UA.mjs +0 -8
- package/dist/uk-UA.module.js +0 -8
- package/dist/uk-UA.module.js.map +0 -1
- package/dist/useMenu.main.js +0 -61
- package/dist/useMenu.main.js.map +0 -1
- package/dist/useMenu.mjs +0 -56
- package/dist/useMenu.module.js +0 -56
- package/dist/useMenu.module.js.map +0 -1
- package/dist/useMenuItem.main.js +0 -217
- package/dist/useMenuItem.main.js.map +0 -1
- package/dist/useMenuItem.mjs +0 -212
- package/dist/useMenuItem.module.js +0 -212
- package/dist/useMenuItem.module.js.map +0 -1
- package/dist/useMenuSection.main.js +0 -43
- package/dist/useMenuSection.main.js.map +0 -1
- package/dist/useMenuSection.mjs +0 -38
- package/dist/useMenuSection.module.js +0 -38
- package/dist/useMenuSection.module.js.map +0 -1
- package/dist/useMenuTrigger.main.js +0 -117
- package/dist/useMenuTrigger.main.js.map +0 -1
- package/dist/useMenuTrigger.mjs +0 -112
- package/dist/useMenuTrigger.module.js +0 -112
- package/dist/useMenuTrigger.module.js.map +0 -1
- package/dist/useSafelyMouseToSubmenu.main.js +0 -143
- package/dist/useSafelyMouseToSubmenu.main.js.map +0 -1
- package/dist/useSafelyMouseToSubmenu.mjs +0 -138
- package/dist/useSafelyMouseToSubmenu.module.js +0 -138
- package/dist/useSafelyMouseToSubmenu.module.js.map +0 -1
- package/dist/useSubmenuTrigger.main.js +0 -184
- package/dist/useSubmenuTrigger.main.js.map +0 -1
- package/dist/useSubmenuTrigger.mjs +0 -179
- package/dist/useSubmenuTrigger.module.js +0 -179
- package/dist/useSubmenuTrigger.module.js.map +0 -1
- package/dist/utils.main.js +0 -20
- package/dist/utils.main.js.map +0 -1
- package/dist/utils.mjs +0 -15
- package/dist/utils.module.js +0 -15
- package/dist/utils.module.js.map +0 -1
- package/dist/zh-CN.main.js +0 -6
- package/dist/zh-CN.main.js.map +0 -1
- package/dist/zh-CN.mjs +0 -8
- package/dist/zh-CN.module.js +0 -8
- package/dist/zh-CN.module.js.map +0 -1
- package/dist/zh-TW.main.js +0 -6
- package/dist/zh-TW.main.js.map +0 -1
- package/dist/zh-TW.mjs +0 -8
- package/dist/zh-TW.module.js +0 -8
- package/dist/zh-TW.module.js.map +0 -1
- package/src/useMenu.ts +0 -86
- package/src/useMenuItem.ts +0 -350
- package/src/useMenuSection.ts +0 -61
- package/src/useMenuTrigger.ts +0 -156
- package/src/useSafelyMouseToSubmenu.ts +0 -178
- package/src/useSubmenuTrigger.ts +0 -262
- package/src/utils.ts +0 -22
package/src/useMenuItem.ts
DELETED
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {DOMAttributes, DOMProps, FocusableElement, FocusEvents, HoverEvents, Key, KeyboardEvents, PressEvent, PressEvents, RefObject} from '@react-types/shared';
|
|
14
|
-
import {filterDOMProps, handleLinkClick, mergeProps, useLinkProps, useRouter, useSlotId} from '@react-aria/utils';
|
|
15
|
-
import {getItemCount} from '@react-stately/collections';
|
|
16
|
-
import {isFocusVisible, useFocusable, useHover, useKeyboard, usePress} from '@react-aria/interactions';
|
|
17
|
-
import {menuData} from './utils';
|
|
18
|
-
import {MouseEvent, useRef} from 'react';
|
|
19
|
-
import {SelectionManager} from '@react-stately/selection';
|
|
20
|
-
import {TreeState} from '@react-stately/tree';
|
|
21
|
-
import {useSelectableItem} from '@react-aria/selection';
|
|
22
|
-
|
|
23
|
-
export interface MenuItemAria {
|
|
24
|
-
/** Props for the menu item element. */
|
|
25
|
-
menuItemProps: DOMAttributes,
|
|
26
|
-
|
|
27
|
-
/** Props for the main text element inside the menu item. */
|
|
28
|
-
labelProps: DOMAttributes,
|
|
29
|
-
|
|
30
|
-
/** Props for the description text element inside the menu item, if any. */
|
|
31
|
-
descriptionProps: DOMAttributes,
|
|
32
|
-
|
|
33
|
-
/** Props for the keyboard shortcut text element inside the item, if any. */
|
|
34
|
-
keyboardShortcutProps: DOMAttributes,
|
|
35
|
-
|
|
36
|
-
/** Whether the item is currently focused. */
|
|
37
|
-
isFocused: boolean,
|
|
38
|
-
/** Whether the item is keyboard focused. */
|
|
39
|
-
isFocusVisible: boolean,
|
|
40
|
-
/** Whether the item is currently selected. */
|
|
41
|
-
isSelected: boolean,
|
|
42
|
-
/** Whether the item is currently in a pressed state. */
|
|
43
|
-
isPressed: boolean,
|
|
44
|
-
/** Whether the item is disabled. */
|
|
45
|
-
isDisabled: boolean
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface AriaMenuItemProps extends DOMProps, PressEvents, HoverEvents, KeyboardEvents, FocusEvents {
|
|
49
|
-
/**
|
|
50
|
-
* Whether the menu item is disabled.
|
|
51
|
-
* @deprecated - pass disabledKeys to useTreeState instead.
|
|
52
|
-
*/
|
|
53
|
-
isDisabled?: boolean,
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Whether the menu item is selected.
|
|
57
|
-
* @deprecated - pass selectedKeys to useTreeState instead.
|
|
58
|
-
*/
|
|
59
|
-
isSelected?: boolean,
|
|
60
|
-
|
|
61
|
-
/** A screen reader only label for the menu item. */
|
|
62
|
-
'aria-label'?: string,
|
|
63
|
-
|
|
64
|
-
/** The unique key for the menu item. */
|
|
65
|
-
key: Key,
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Handler that is called when the menu should close after selecting an item.
|
|
69
|
-
* @deprecated - pass to the menu instead.
|
|
70
|
-
*/
|
|
71
|
-
onClose?: () => void,
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Whether the menu should close when the menu item is selected.
|
|
75
|
-
* @deprecated - use shouldCloseOnSelect instead.
|
|
76
|
-
*/
|
|
77
|
-
closeOnSelect?: boolean,
|
|
78
|
-
|
|
79
|
-
/** Whether the menu should close when the menu item is selected. */
|
|
80
|
-
shouldCloseOnSelect?: boolean,
|
|
81
|
-
|
|
82
|
-
/** Whether the menu item is contained in a virtual scrolling menu. */
|
|
83
|
-
isVirtualized?: boolean,
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Handler that is called when the user activates the item.
|
|
87
|
-
* @deprecated - pass to the menu instead.
|
|
88
|
-
*/
|
|
89
|
-
onAction?: (key: Key) => void,
|
|
90
|
-
|
|
91
|
-
/** What kind of popup the item opens. */
|
|
92
|
-
'aria-haspopup'?: 'menu' | 'dialog',
|
|
93
|
-
|
|
94
|
-
/** Indicates whether the menu item's popup element is expanded or collapsed. */
|
|
95
|
-
'aria-expanded'?: boolean | 'true' | 'false',
|
|
96
|
-
|
|
97
|
-
/** Identifies the menu item's popup element whose contents or presence is controlled by the menu item. */
|
|
98
|
-
'aria-controls'?: string,
|
|
99
|
-
|
|
100
|
-
/** Override of the selection manager. By default, `state.selectionManager` is used. */
|
|
101
|
-
selectionManager?: SelectionManager
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Provides the behavior and accessibility implementation for an item in a menu.
|
|
106
|
-
* See `useMenu` for more details about menus.
|
|
107
|
-
* @param props - Props for the item.
|
|
108
|
-
* @param state - State for the menu, as returned by `useTreeState`.
|
|
109
|
-
*/
|
|
110
|
-
export function useMenuItem<T>(props: AriaMenuItemProps, state: TreeState<T>, ref: RefObject<FocusableElement | null>): MenuItemAria {
|
|
111
|
-
let {
|
|
112
|
-
id,
|
|
113
|
-
key,
|
|
114
|
-
closeOnSelect,
|
|
115
|
-
shouldCloseOnSelect,
|
|
116
|
-
isVirtualized,
|
|
117
|
-
'aria-haspopup': hasPopup,
|
|
118
|
-
onPressStart,
|
|
119
|
-
onPressUp: pressUpProp,
|
|
120
|
-
onPress,
|
|
121
|
-
onPressChange: pressChangeProp,
|
|
122
|
-
onPressEnd,
|
|
123
|
-
onClick: onClickProp,
|
|
124
|
-
onHoverStart: hoverStartProp,
|
|
125
|
-
onHoverChange,
|
|
126
|
-
onHoverEnd,
|
|
127
|
-
onKeyDown,
|
|
128
|
-
onKeyUp,
|
|
129
|
-
onFocus,
|
|
130
|
-
onFocusChange,
|
|
131
|
-
onBlur,
|
|
132
|
-
selectionManager = state.selectionManager
|
|
133
|
-
} = props;
|
|
134
|
-
|
|
135
|
-
let isTrigger = !!hasPopup;
|
|
136
|
-
let isTriggerExpanded = isTrigger && props['aria-expanded'] === 'true';
|
|
137
|
-
let isDisabled = props.isDisabled ?? selectionManager.isDisabled(key);
|
|
138
|
-
let isSelected = props.isSelected ?? selectionManager.isSelected(key);
|
|
139
|
-
let data = menuData.get(state)!;
|
|
140
|
-
let item = state.collection.getItem(key);
|
|
141
|
-
let onClose = props.onClose || data.onClose;
|
|
142
|
-
let router = useRouter();
|
|
143
|
-
let performAction = () => {
|
|
144
|
-
if (isTrigger) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (item?.props?.onAction) {
|
|
149
|
-
item.props.onAction();
|
|
150
|
-
} else if (props.onAction) {
|
|
151
|
-
props.onAction(key);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (data.onAction) {
|
|
155
|
-
// Must reassign to variable otherwise `this` binding gets messed up. Something to do with WeakMap.
|
|
156
|
-
let onAction = data.onAction;
|
|
157
|
-
onAction(key);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
let role = 'menuitem';
|
|
162
|
-
if (!isTrigger) {
|
|
163
|
-
if (selectionManager.selectionMode === 'single') {
|
|
164
|
-
role = 'menuitemradio';
|
|
165
|
-
} else if (selectionManager.selectionMode === 'multiple') {
|
|
166
|
-
role = 'menuitemcheckbox';
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
let labelId = useSlotId();
|
|
171
|
-
let descriptionId = useSlotId();
|
|
172
|
-
let keyboardId = useSlotId();
|
|
173
|
-
|
|
174
|
-
let ariaProps = {
|
|
175
|
-
id,
|
|
176
|
-
'aria-disabled': isDisabled || undefined,
|
|
177
|
-
role,
|
|
178
|
-
'aria-label': props['aria-label'],
|
|
179
|
-
'aria-labelledby': labelId,
|
|
180
|
-
'aria-describedby': [descriptionId, keyboardId].filter(Boolean).join(' ') || undefined,
|
|
181
|
-
'aria-controls': props['aria-controls'],
|
|
182
|
-
'aria-haspopup': hasPopup,
|
|
183
|
-
'aria-expanded': props['aria-expanded']
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
if (selectionManager.selectionMode !== 'none' && !isTrigger) {
|
|
187
|
-
ariaProps['aria-checked'] = isSelected;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (isVirtualized) {
|
|
191
|
-
ariaProps['aria-posinset'] = item?.index;
|
|
192
|
-
ariaProps['aria-setsize'] = getItemCount(state.collection);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
let isPressedRef = useRef(false);
|
|
196
|
-
let onPressChange = (isPressed: boolean) => {
|
|
197
|
-
pressChangeProp?.(isPressed);
|
|
198
|
-
isPressedRef.current = isPressed;
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
let interaction = useRef<{pointerType: string, key?: string} | null>(null);
|
|
202
|
-
let onPressUp = (e: PressEvent) => {
|
|
203
|
-
if (e.pointerType !== 'keyboard') {
|
|
204
|
-
interaction.current = {pointerType: e.pointerType};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// If interacting with mouse, allow the user to mouse down on the trigger button,
|
|
208
|
-
// drag, and release over an item (matching native behavior).
|
|
209
|
-
if (e.pointerType === 'mouse') {
|
|
210
|
-
if (!isPressedRef.current) {
|
|
211
|
-
(e.target as HTMLElement).click();
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
pressUpProp?.(e);
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
let onClick = (e: MouseEvent<FocusableElement>) => {
|
|
219
|
-
onClickProp?.(e);
|
|
220
|
-
performAction();
|
|
221
|
-
handleLinkClick(e, router, item!.props.href, item?.props.routerOptions);
|
|
222
|
-
|
|
223
|
-
let shouldClose = interaction.current?.pointerType === 'keyboard'
|
|
224
|
-
// Always close when pressing Enter key, or if item is not selectable.
|
|
225
|
-
? interaction.current?.key === 'Enter' || selectionManager.selectionMode === 'none' || selectionManager.isLink(key)
|
|
226
|
-
// Close except if multi-select is enabled.
|
|
227
|
-
: selectionManager.selectionMode !== 'multiple' || selectionManager.isLink(key);
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
shouldClose = shouldCloseOnSelect ?? closeOnSelect ?? shouldClose;
|
|
231
|
-
|
|
232
|
-
if (onClose && !isTrigger && shouldClose) {
|
|
233
|
-
onClose();
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
interaction.current = null;
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
let {itemProps, isFocused} = useSelectableItem({
|
|
240
|
-
id,
|
|
241
|
-
selectionManager: selectionManager,
|
|
242
|
-
key,
|
|
243
|
-
ref,
|
|
244
|
-
shouldSelectOnPressUp: true,
|
|
245
|
-
allowsDifferentPressOrigin: true,
|
|
246
|
-
// Disable all handling of links in useSelectable item
|
|
247
|
-
// because we handle it ourselves. The behavior of menus
|
|
248
|
-
// is slightly different from other collections because
|
|
249
|
-
// actions are performed on key down rather than key up.
|
|
250
|
-
linkBehavior: 'none',
|
|
251
|
-
shouldUseVirtualFocus: data.shouldUseVirtualFocus
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
let {pressProps, isPressed} = usePress({
|
|
255
|
-
onPressStart,
|
|
256
|
-
onPress,
|
|
257
|
-
onPressUp,
|
|
258
|
-
onPressChange,
|
|
259
|
-
onPressEnd,
|
|
260
|
-
isDisabled
|
|
261
|
-
});
|
|
262
|
-
let {hoverProps} = useHover({
|
|
263
|
-
isDisabled,
|
|
264
|
-
onHoverStart(e) {
|
|
265
|
-
// Hovering over an already expanded sub dialog trigger should keep focus in the dialog.
|
|
266
|
-
if (!isFocusVisible() && !(isTriggerExpanded && hasPopup)) {
|
|
267
|
-
selectionManager.setFocused(true);
|
|
268
|
-
selectionManager.setFocusedKey(key);
|
|
269
|
-
}
|
|
270
|
-
hoverStartProp?.(e);
|
|
271
|
-
},
|
|
272
|
-
onHoverChange,
|
|
273
|
-
onHoverEnd
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
let {keyboardProps} = useKeyboard({
|
|
277
|
-
onKeyDown: (e) => {
|
|
278
|
-
// Ignore repeating events, which may have started on the menu trigger before moving
|
|
279
|
-
// focus to the menu item. We want to wait for a second complete key press sequence.
|
|
280
|
-
if (e.repeat) {
|
|
281
|
-
e.continuePropagation();
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
switch (e.key) {
|
|
286
|
-
case ' ':
|
|
287
|
-
interaction.current = {pointerType: 'keyboard', key: ' '};
|
|
288
|
-
(e.target as HTMLElement).click();
|
|
289
|
-
break;
|
|
290
|
-
case 'Enter':
|
|
291
|
-
interaction.current = {pointerType: 'keyboard', key: 'Enter'};
|
|
292
|
-
|
|
293
|
-
// Trigger click unless this is a link. Links trigger click natively.
|
|
294
|
-
if ((e.target as HTMLElement).tagName !== 'A') {
|
|
295
|
-
(e.target as HTMLElement).click();
|
|
296
|
-
}
|
|
297
|
-
break;
|
|
298
|
-
default:
|
|
299
|
-
if (!isTrigger) {
|
|
300
|
-
e.continuePropagation();
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
onKeyDown?.(e);
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
},
|
|
307
|
-
onKeyUp
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
let {focusableProps} = useFocusable({onBlur, onFocus, onFocusChange}, ref);
|
|
311
|
-
let domProps = filterDOMProps(item?.props);
|
|
312
|
-
delete domProps.id;
|
|
313
|
-
let linkProps = useLinkProps(item?.props);
|
|
314
|
-
|
|
315
|
-
return {
|
|
316
|
-
menuItemProps: {
|
|
317
|
-
...ariaProps,
|
|
318
|
-
...mergeProps(
|
|
319
|
-
domProps,
|
|
320
|
-
linkProps,
|
|
321
|
-
isTrigger
|
|
322
|
-
? {onFocus: itemProps.onFocus, 'data-collection': itemProps['data-collection'], 'data-key': itemProps['data-key']}
|
|
323
|
-
: itemProps,
|
|
324
|
-
pressProps,
|
|
325
|
-
hoverProps,
|
|
326
|
-
keyboardProps,
|
|
327
|
-
focusableProps,
|
|
328
|
-
// Prevent DOM focus from moving on mouse down when using virtual focus or this is a submenu/subdialog trigger.
|
|
329
|
-
data.shouldUseVirtualFocus || isTrigger ? {onMouseDown: e => e.preventDefault()} : undefined,
|
|
330
|
-
isDisabled ? undefined : {onClick}
|
|
331
|
-
),
|
|
332
|
-
// If a submenu is expanded, set the tabIndex to -1 so that shift tabbing goes out of the menu instead of the parent menu item.
|
|
333
|
-
tabIndex: itemProps.tabIndex != null && isTriggerExpanded && !data.shouldUseVirtualFocus ? -1 : itemProps.tabIndex
|
|
334
|
-
},
|
|
335
|
-
labelProps: {
|
|
336
|
-
id: labelId
|
|
337
|
-
},
|
|
338
|
-
descriptionProps: {
|
|
339
|
-
id: descriptionId
|
|
340
|
-
},
|
|
341
|
-
keyboardShortcutProps: {
|
|
342
|
-
id: keyboardId
|
|
343
|
-
},
|
|
344
|
-
isFocused,
|
|
345
|
-
isFocusVisible: isFocused && selectionManager.isFocused && isFocusVisible() && !isTriggerExpanded,
|
|
346
|
-
isSelected,
|
|
347
|
-
isPressed,
|
|
348
|
-
isDisabled
|
|
349
|
-
};
|
|
350
|
-
}
|
package/src/useMenuSection.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {DOMAttributes} from '@react-types/shared';
|
|
14
|
-
import {ReactNode} from 'react';
|
|
15
|
-
import {useId} from '@react-aria/utils';
|
|
16
|
-
|
|
17
|
-
export interface AriaMenuSectionProps {
|
|
18
|
-
/** The heading for the section. */
|
|
19
|
-
heading?: ReactNode,
|
|
20
|
-
/** An accessibility label for the section. Required if `heading` is not present. */
|
|
21
|
-
'aria-label'?: string
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface MenuSectionAria {
|
|
25
|
-
/** Props for the wrapper list item. */
|
|
26
|
-
itemProps: DOMAttributes,
|
|
27
|
-
|
|
28
|
-
/** Props for the heading element, if any. */
|
|
29
|
-
headingProps: DOMAttributes,
|
|
30
|
-
|
|
31
|
-
/** Props for the group element. */
|
|
32
|
-
groupProps: DOMAttributes
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Provides the behavior and accessibility implementation for a section in a menu.
|
|
37
|
-
* See `useMenu` for more details about menus.
|
|
38
|
-
* @param props - Props for the section.
|
|
39
|
-
*/
|
|
40
|
-
export function useMenuSection(props: AriaMenuSectionProps): MenuSectionAria {
|
|
41
|
-
let {heading, 'aria-label': ariaLabel} = props;
|
|
42
|
-
let headingId = useId();
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
itemProps: {
|
|
46
|
-
role: 'presentation'
|
|
47
|
-
},
|
|
48
|
-
headingProps: heading ? {
|
|
49
|
-
// Techincally, menus cannot contain headings according to ARIA.
|
|
50
|
-
// We hide the heading from assistive technology, using role="presentation",
|
|
51
|
-
// and only use it as a label for the nested group.
|
|
52
|
-
id: headingId,
|
|
53
|
-
role: 'presentation'
|
|
54
|
-
} : {},
|
|
55
|
-
groupProps: {
|
|
56
|
-
role: 'group',
|
|
57
|
-
'aria-label': ariaLabel,
|
|
58
|
-
'aria-labelledby': heading ? headingId : undefined
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
package/src/useMenuTrigger.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {AriaButtonProps} from '@react-types/button';
|
|
14
|
-
import {AriaMenuOptions} from './useMenu';
|
|
15
|
-
import {FocusableElement, RefObject} from '@react-types/shared';
|
|
16
|
-
import {focusWithoutScrolling, useId} from '@react-aria/utils';
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
import intlMessages from '../intl/*.json';
|
|
19
|
-
import {MenuTriggerState} from '@react-stately/menu';
|
|
20
|
-
import {MenuTriggerType} from '@react-types/menu';
|
|
21
|
-
import {PressProps, useLongPress} from '@react-aria/interactions';
|
|
22
|
-
import {useLocalizedStringFormatter} from '@react-aria/i18n';
|
|
23
|
-
import {useOverlayTrigger} from '@react-aria/overlays';
|
|
24
|
-
|
|
25
|
-
export interface AriaMenuTriggerProps {
|
|
26
|
-
/** The type of menu that the menu trigger opens. */
|
|
27
|
-
type?: 'menu' | 'listbox',
|
|
28
|
-
/** Whether menu trigger is disabled. */
|
|
29
|
-
isDisabled?: boolean,
|
|
30
|
-
/** How menu is triggered. */
|
|
31
|
-
trigger?: MenuTriggerType
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface MenuTriggerAria<T> {
|
|
35
|
-
/** Props for the menu trigger element. */
|
|
36
|
-
menuTriggerProps: AriaButtonProps,
|
|
37
|
-
|
|
38
|
-
/** Props for the menu. */
|
|
39
|
-
menuProps: AriaMenuOptions<T>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Provides the behavior and accessibility implementation for a menu trigger.
|
|
44
|
-
* @param props - Props for the menu trigger.
|
|
45
|
-
* @param state - State for the menu trigger.
|
|
46
|
-
* @param ref - Ref to the HTML element trigger for the menu.
|
|
47
|
-
*/
|
|
48
|
-
export function useMenuTrigger<T>(props: AriaMenuTriggerProps, state: MenuTriggerState, ref: RefObject<Element | null>): MenuTriggerAria<T> {
|
|
49
|
-
let {
|
|
50
|
-
type = 'menu',
|
|
51
|
-
isDisabled,
|
|
52
|
-
trigger = 'press'
|
|
53
|
-
} = props;
|
|
54
|
-
|
|
55
|
-
let menuTriggerId = useId();
|
|
56
|
-
let {triggerProps, overlayProps} = useOverlayTrigger({type}, state, ref);
|
|
57
|
-
|
|
58
|
-
let onKeyDown = (e) => {
|
|
59
|
-
if (isDisabled) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (trigger === 'longPress' && !e.altKey) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (ref && ref.current) {
|
|
68
|
-
switch (e.key) {
|
|
69
|
-
case 'Enter':
|
|
70
|
-
case ' ':
|
|
71
|
-
// React puts listeners on the same root, so even if propagation was stopped, immediate propagation is still possible.
|
|
72
|
-
// useTypeSelect will handle the spacebar first if it's running, so we don't want to open if it's handled it already.
|
|
73
|
-
// We use isDefaultPrevented() instead of isPropagationStopped() because createEventHandler stops propagation by default.
|
|
74
|
-
// And default prevented means that the event was handled by something else (typeahead), so we don't want to open the menu.
|
|
75
|
-
if (trigger === 'longPress' || e.isDefaultPrevented()) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
// fallthrough
|
|
79
|
-
case 'ArrowDown':
|
|
80
|
-
// Stop propagation, unless it would already be handled by useKeyboard.
|
|
81
|
-
if (!('continuePropagation' in e)) {
|
|
82
|
-
e.stopPropagation();
|
|
83
|
-
}
|
|
84
|
-
e.preventDefault();
|
|
85
|
-
state.toggle('first');
|
|
86
|
-
break;
|
|
87
|
-
case 'ArrowUp':
|
|
88
|
-
if (!('continuePropagation' in e)) {
|
|
89
|
-
e.stopPropagation();
|
|
90
|
-
}
|
|
91
|
-
e.preventDefault();
|
|
92
|
-
state.toggle('last');
|
|
93
|
-
break;
|
|
94
|
-
default:
|
|
95
|
-
// Allow other keys.
|
|
96
|
-
if ('continuePropagation' in e) {
|
|
97
|
-
e.continuePropagation();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/menu');
|
|
104
|
-
let {longPressProps} = useLongPress({
|
|
105
|
-
isDisabled: isDisabled || trigger !== 'longPress',
|
|
106
|
-
accessibilityDescription: stringFormatter.format('longPressMessage'),
|
|
107
|
-
onLongPressStart() {
|
|
108
|
-
state.close();
|
|
109
|
-
},
|
|
110
|
-
onLongPress() {
|
|
111
|
-
state.open('first');
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
let pressProps: PressProps = {
|
|
116
|
-
preventFocusOnPress: true,
|
|
117
|
-
onPressStart(e) {
|
|
118
|
-
// For consistency with native, open the menu on mouse/key down, but touch up.
|
|
119
|
-
if (e.pointerType !== 'touch' && e.pointerType !== 'keyboard' && !isDisabled) {
|
|
120
|
-
// Ensure trigger has focus before opening the menu so it can be restored by FocusScope on close.
|
|
121
|
-
focusWithoutScrolling(e.target as FocusableElement);
|
|
122
|
-
|
|
123
|
-
// If opened with a screen reader, auto focus the first item.
|
|
124
|
-
// Otherwise, the menu itself will be focused.
|
|
125
|
-
state.open(e.pointerType === 'virtual' ? 'first' : null);
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
onPress(e) {
|
|
129
|
-
if (e.pointerType === 'touch' && !isDisabled) {
|
|
130
|
-
// Ensure trigger has focus before opening the menu so it can be restored by FocusScope on close.
|
|
131
|
-
focusWithoutScrolling(e.target as FocusableElement);
|
|
132
|
-
|
|
133
|
-
state.toggle();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// omit onPress from triggerProps since we override it above.
|
|
139
|
-
delete triggerProps.onPress;
|
|
140
|
-
|
|
141
|
-
return {
|
|
142
|
-
// @ts-ignore - TODO we pass out both DOMAttributes AND AriaButtonProps, but useButton will discard the longPress event handlers, it's only through PressResponder magic that this works for RSP and RAC. it does not work in aria examples
|
|
143
|
-
menuTriggerProps: {
|
|
144
|
-
...triggerProps,
|
|
145
|
-
...(trigger === 'press' ? pressProps : longPressProps),
|
|
146
|
-
id: menuTriggerId,
|
|
147
|
-
onKeyDown
|
|
148
|
-
},
|
|
149
|
-
menuProps: {
|
|
150
|
-
...overlayProps,
|
|
151
|
-
'aria-labelledby': menuTriggerId,
|
|
152
|
-
autoFocus: state.focusStrategy || true,
|
|
153
|
-
onClose: state.close
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
}
|