@react-aria/menu 3.0.0-alpha.1 → 3.0.0-nightly-4980928d3-240906

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 (222) hide show
  1. package/README.md +1 -1
  2. package/dist/ar-AE.main.js +6 -0
  3. package/dist/ar-AE.main.js.map +1 -0
  4. package/dist/ar-AE.mjs +8 -0
  5. package/dist/ar-AE.module.js +8 -0
  6. package/dist/ar-AE.module.js.map +1 -0
  7. package/dist/bg-BG.main.js +6 -0
  8. package/dist/bg-BG.main.js.map +1 -0
  9. package/dist/bg-BG.mjs +8 -0
  10. package/dist/bg-BG.module.js +8 -0
  11. package/dist/bg-BG.module.js.map +1 -0
  12. package/dist/cs-CZ.main.js +6 -0
  13. package/dist/cs-CZ.main.js.map +1 -0
  14. package/dist/cs-CZ.mjs +8 -0
  15. package/dist/cs-CZ.module.js +8 -0
  16. package/dist/cs-CZ.module.js.map +1 -0
  17. package/dist/da-DK.main.js +6 -0
  18. package/dist/da-DK.main.js.map +1 -0
  19. package/dist/da-DK.mjs +8 -0
  20. package/dist/da-DK.module.js +8 -0
  21. package/dist/da-DK.module.js.map +1 -0
  22. package/dist/de-DE.main.js +6 -0
  23. package/dist/de-DE.main.js.map +1 -0
  24. package/dist/de-DE.mjs +8 -0
  25. package/dist/de-DE.module.js +8 -0
  26. package/dist/de-DE.module.js.map +1 -0
  27. package/dist/el-GR.main.js +6 -0
  28. package/dist/el-GR.main.js.map +1 -0
  29. package/dist/el-GR.mjs +8 -0
  30. package/dist/el-GR.module.js +8 -0
  31. package/dist/el-GR.module.js.map +1 -0
  32. package/dist/en-US.main.js +6 -0
  33. package/dist/en-US.main.js.map +1 -0
  34. package/dist/en-US.mjs +8 -0
  35. package/dist/en-US.module.js +8 -0
  36. package/dist/en-US.module.js.map +1 -0
  37. package/dist/es-ES.main.js +6 -0
  38. package/dist/es-ES.main.js.map +1 -0
  39. package/dist/es-ES.mjs +8 -0
  40. package/dist/es-ES.module.js +8 -0
  41. package/dist/es-ES.module.js.map +1 -0
  42. package/dist/et-EE.main.js +6 -0
  43. package/dist/et-EE.main.js.map +1 -0
  44. package/dist/et-EE.mjs +8 -0
  45. package/dist/et-EE.module.js +8 -0
  46. package/dist/et-EE.module.js.map +1 -0
  47. package/dist/fi-FI.main.js +6 -0
  48. package/dist/fi-FI.main.js.map +1 -0
  49. package/dist/fi-FI.mjs +8 -0
  50. package/dist/fi-FI.module.js +8 -0
  51. package/dist/fi-FI.module.js.map +1 -0
  52. package/dist/fr-FR.main.js +6 -0
  53. package/dist/fr-FR.main.js.map +1 -0
  54. package/dist/fr-FR.mjs +8 -0
  55. package/dist/fr-FR.module.js +8 -0
  56. package/dist/fr-FR.module.js.map +1 -0
  57. package/dist/he-IL.main.js +6 -0
  58. package/dist/he-IL.main.js.map +1 -0
  59. package/dist/he-IL.mjs +8 -0
  60. package/dist/he-IL.module.js +8 -0
  61. package/dist/he-IL.module.js.map +1 -0
  62. package/dist/hr-HR.main.js +6 -0
  63. package/dist/hr-HR.main.js.map +1 -0
  64. package/dist/hr-HR.mjs +8 -0
  65. package/dist/hr-HR.module.js +8 -0
  66. package/dist/hr-HR.module.js.map +1 -0
  67. package/dist/hu-HU.main.js +6 -0
  68. package/dist/hu-HU.main.js.map +1 -0
  69. package/dist/hu-HU.mjs +8 -0
  70. package/dist/hu-HU.module.js +8 -0
  71. package/dist/hu-HU.module.js.map +1 -0
  72. package/dist/import.mjs +25 -0
  73. package/dist/intlStrings.main.js +108 -0
  74. package/dist/intlStrings.main.js.map +1 -0
  75. package/dist/intlStrings.mjs +110 -0
  76. package/dist/intlStrings.module.js +110 -0
  77. package/dist/intlStrings.module.js.map +1 -0
  78. package/dist/it-IT.main.js +6 -0
  79. package/dist/it-IT.main.js.map +1 -0
  80. package/dist/it-IT.mjs +8 -0
  81. package/dist/it-IT.module.js +8 -0
  82. package/dist/it-IT.module.js.map +1 -0
  83. package/dist/ja-JP.main.js +6 -0
  84. package/dist/ja-JP.main.js.map +1 -0
  85. package/dist/ja-JP.mjs +8 -0
  86. package/dist/ja-JP.module.js +8 -0
  87. package/dist/ja-JP.module.js.map +1 -0
  88. package/dist/ko-KR.main.js +6 -0
  89. package/dist/ko-KR.main.js.map +1 -0
  90. package/dist/ko-KR.mjs +8 -0
  91. package/dist/ko-KR.module.js +8 -0
  92. package/dist/ko-KR.module.js.map +1 -0
  93. package/dist/lt-LT.main.js +6 -0
  94. package/dist/lt-LT.main.js.map +1 -0
  95. package/dist/lt-LT.mjs +8 -0
  96. package/dist/lt-LT.module.js +8 -0
  97. package/dist/lt-LT.module.js.map +1 -0
  98. package/dist/lv-LV.main.js +6 -0
  99. package/dist/lv-LV.main.js.map +1 -0
  100. package/dist/lv-LV.mjs +8 -0
  101. package/dist/lv-LV.module.js +8 -0
  102. package/dist/lv-LV.module.js.map +1 -0
  103. package/dist/main.js +24 -251
  104. package/dist/main.js.map +1 -0
  105. package/dist/module.js +18 -232
  106. package/dist/module.js.map +1 -0
  107. package/dist/nb-NO.main.js +6 -0
  108. package/dist/nb-NO.main.js.map +1 -0
  109. package/dist/nb-NO.mjs +8 -0
  110. package/dist/nb-NO.module.js +8 -0
  111. package/dist/nb-NO.module.js.map +1 -0
  112. package/dist/nl-NL.main.js +6 -0
  113. package/dist/nl-NL.main.js.map +1 -0
  114. package/dist/nl-NL.mjs +8 -0
  115. package/dist/nl-NL.module.js +8 -0
  116. package/dist/nl-NL.module.js.map +1 -0
  117. package/dist/pl-PL.main.js +6 -0
  118. package/dist/pl-PL.main.js.map +1 -0
  119. package/dist/pl-PL.mjs +8 -0
  120. package/dist/pl-PL.module.js +8 -0
  121. package/dist/pl-PL.module.js.map +1 -0
  122. package/dist/pt-BR.main.js +6 -0
  123. package/dist/pt-BR.main.js.map +1 -0
  124. package/dist/pt-BR.mjs +8 -0
  125. package/dist/pt-BR.module.js +8 -0
  126. package/dist/pt-BR.module.js.map +1 -0
  127. package/dist/pt-PT.main.js +6 -0
  128. package/dist/pt-PT.main.js.map +1 -0
  129. package/dist/pt-PT.mjs +8 -0
  130. package/dist/pt-PT.module.js +8 -0
  131. package/dist/pt-PT.module.js.map +1 -0
  132. package/dist/ro-RO.main.js +6 -0
  133. package/dist/ro-RO.main.js.map +1 -0
  134. package/dist/ro-RO.mjs +8 -0
  135. package/dist/ro-RO.module.js +8 -0
  136. package/dist/ro-RO.module.js.map +1 -0
  137. package/dist/ru-RU.main.js +6 -0
  138. package/dist/ru-RU.main.js.map +1 -0
  139. package/dist/ru-RU.mjs +8 -0
  140. package/dist/ru-RU.module.js +8 -0
  141. package/dist/ru-RU.module.js.map +1 -0
  142. package/dist/sk-SK.main.js +6 -0
  143. package/dist/sk-SK.main.js.map +1 -0
  144. package/dist/sk-SK.mjs +8 -0
  145. package/dist/sk-SK.module.js +8 -0
  146. package/dist/sk-SK.module.js.map +1 -0
  147. package/dist/sl-SI.main.js +6 -0
  148. package/dist/sl-SI.main.js.map +1 -0
  149. package/dist/sl-SI.mjs +8 -0
  150. package/dist/sl-SI.module.js +8 -0
  151. package/dist/sl-SI.module.js.map +1 -0
  152. package/dist/sr-SP.main.js +6 -0
  153. package/dist/sr-SP.main.js.map +1 -0
  154. package/dist/sr-SP.mjs +8 -0
  155. package/dist/sr-SP.module.js +8 -0
  156. package/dist/sr-SP.module.js.map +1 -0
  157. package/dist/sv-SE.main.js +6 -0
  158. package/dist/sv-SE.main.js.map +1 -0
  159. package/dist/sv-SE.mjs +8 -0
  160. package/dist/sv-SE.module.js +8 -0
  161. package/dist/sv-SE.module.js.map +1 -0
  162. package/dist/tr-TR.main.js +6 -0
  163. package/dist/tr-TR.main.js.map +1 -0
  164. package/dist/tr-TR.mjs +8 -0
  165. package/dist/tr-TR.module.js +8 -0
  166. package/dist/tr-TR.module.js.map +1 -0
  167. package/dist/types.d.ts +156 -36
  168. package/dist/types.d.ts.map +1 -1
  169. package/dist/uk-UA.main.js +6 -0
  170. package/dist/uk-UA.main.js.map +1 -0
  171. package/dist/uk-UA.mjs +8 -0
  172. package/dist/uk-UA.module.js +8 -0
  173. package/dist/uk-UA.module.js.map +1 -0
  174. package/dist/useMenu.main.js +59 -0
  175. package/dist/useMenu.main.js.map +1 -0
  176. package/dist/useMenu.mjs +53 -0
  177. package/dist/useMenu.module.js +53 -0
  178. package/dist/useMenu.module.js.map +1 -0
  179. package/dist/useMenuItem.main.js +181 -0
  180. package/dist/useMenuItem.main.js.map +1 -0
  181. package/dist/useMenuItem.mjs +176 -0
  182. package/dist/useMenuItem.module.js +176 -0
  183. package/dist/useMenuItem.module.js.map +1 -0
  184. package/dist/useMenuSection.main.js +43 -0
  185. package/dist/useMenuSection.main.js.map +1 -0
  186. package/dist/useMenuSection.mjs +38 -0
  187. package/dist/useMenuSection.module.js +38 -0
  188. package/dist/useMenuSection.module.js.map +1 -0
  189. package/dist/useMenuTrigger.main.js +103 -0
  190. package/dist/useMenuTrigger.main.js.map +1 -0
  191. package/dist/useMenuTrigger.mjs +98 -0
  192. package/dist/useMenuTrigger.module.js +98 -0
  193. package/dist/useMenuTrigger.module.js.map +1 -0
  194. package/dist/useSafelyMouseToSubmenu.main.js +134 -0
  195. package/dist/useSafelyMouseToSubmenu.main.js.map +1 -0
  196. package/dist/useSafelyMouseToSubmenu.mjs +129 -0
  197. package/dist/useSafelyMouseToSubmenu.module.js +129 -0
  198. package/dist/useSafelyMouseToSubmenu.module.js.map +1 -0
  199. package/dist/useSubmenuTrigger.main.js +169 -0
  200. package/dist/useSubmenuTrigger.main.js.map +1 -0
  201. package/dist/useSubmenuTrigger.mjs +164 -0
  202. package/dist/useSubmenuTrigger.module.js +164 -0
  203. package/dist/useSubmenuTrigger.module.js.map +1 -0
  204. package/dist/zh-CN.main.js +6 -0
  205. package/dist/zh-CN.main.js.map +1 -0
  206. package/dist/zh-CN.mjs +8 -0
  207. package/dist/zh-CN.module.js +8 -0
  208. package/dist/zh-CN.module.js.map +1 -0
  209. package/dist/zh-TW.main.js +6 -0
  210. package/dist/zh-TW.main.js.map +1 -0
  211. package/dist/zh-TW.mjs +8 -0
  212. package/dist/zh-TW.module.js +8 -0
  213. package/dist/zh-TW.module.js.map +1 -0
  214. package/package.json +26 -16
  215. package/src/index.ts +24 -0
  216. package/src/useMenu.ts +88 -0
  217. package/src/useMenuItem.ts +299 -0
  218. package/src/useMenuSection.ts +61 -0
  219. package/src/useMenuTrigger.ts +144 -0
  220. package/src/useSafelyMouseToSubmenu.ts +161 -0
  221. package/src/useSubmenuTrigger.ts +243 -0
  222. package/LICENSE +0 -201
package/package.json CHANGED
@@ -1,37 +1,47 @@
1
1
  {
2
2
  "name": "@react-aria/menu",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-nightly-4980928d3-240906",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/module.js",
8
+ "exports": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/import.mjs",
11
+ "require": "./dist/main.js"
12
+ },
8
13
  "types": "dist/types.d.ts",
9
14
  "source": "src/index.ts",
10
15
  "files": [
11
- "dist"
16
+ "dist",
17
+ "src"
12
18
  ],
13
19
  "sideEffects": false,
14
20
  "repository": {
15
21
  "type": "git",
16
- "url": "https://github.com/adobe-private/react-spectrum-v3"
22
+ "url": "https://github.com/adobe/react-spectrum"
17
23
  },
18
24
  "dependencies": {
19
- "@babel/runtime": "^7.6.2",
20
- "@react-aria/interactions": "^3.0.0-rc.2",
21
- "@react-aria/overlays": "^3.0.0-alpha.1",
22
- "@react-aria/selection": "^3.0.0-alpha.1",
23
- "@react-aria/utils": "^3.0.0-rc.2",
24
- "@react-stately/collections": "^3.0.0-alpha.1",
25
- "@react-stately/menu": "^3.0.0-alpha.1",
26
- "@react-stately/tree": "^3.0.0-alpha.1",
27
- "@react-types/menu": "^3.0.0-alpha.1",
28
- "@react-types/shared": "^3.0.0-rc.2"
25
+ "@react-aria/focus": "^3.0.0-nightly-4980928d3-240906",
26
+ "@react-aria/i18n": "^3.0.0-nightly-4980928d3-240906",
27
+ "@react-aria/interactions": "^3.0.0-nightly-4980928d3-240906",
28
+ "@react-aria/overlays": "^3.0.0-nightly-4980928d3-240906",
29
+ "@react-aria/selection": "^3.0.0-nightly-4980928d3-240906",
30
+ "@react-aria/utils": "^3.0.0-nightly-4980928d3-240906",
31
+ "@react-stately/collections": "^3.0.0-nightly-4980928d3-240906",
32
+ "@react-stately/menu": "^3.0.0-nightly-4980928d3-240906",
33
+ "@react-stately/tree": "^3.0.0-nightly-4980928d3-240906",
34
+ "@react-types/button": "^3.0.0-nightly-4980928d3-240906",
35
+ "@react-types/menu": "^3.0.0-nightly-4980928d3-240906",
36
+ "@react-types/shared": "^3.0.0-nightly-4980928d3-240906",
37
+ "@swc/helpers": "^0.5.0"
29
38
  },
30
39
  "peerDependencies": {
31
- "react": "^16.8.0"
40
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0",
41
+ "react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
32
42
  },
33
43
  "publishConfig": {
34
44
  "access": "public"
35
45
  },
36
- "gitHead": "207e6ee9076905c96638a7f81a367758872e1410"
37
- }
46
+ "stableVersion": "3.15.3"
47
+ }
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
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
+ export {useMenuTrigger} from './useMenuTrigger';
14
+ export {useMenu} from './useMenu';
15
+ export {useMenuItem} from './useMenuItem';
16
+ export {useMenuSection} from './useMenuSection';
17
+ export {useSubmenuTrigger} from './useSubmenuTrigger';
18
+
19
+ export type {AriaMenuProps} from '@react-types/menu';
20
+ export type {AriaMenuTriggerProps, MenuTriggerAria} from './useMenuTrigger';
21
+ export type {AriaMenuOptions, MenuAria} from './useMenu';
22
+ export type {AriaMenuItemProps, MenuItemAria} from './useMenuItem';
23
+ export type {AriaMenuSectionProps, MenuSectionAria} from './useMenuSection';
24
+ export type {AriaSubmenuTriggerProps, SubmenuTriggerAria} from './useSubmenuTrigger';
package/src/useMenu.ts ADDED
@@ -0,0 +1,88 @@
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 {AriaMenuProps} from '@react-types/menu';
14
+ import {DOMAttributes, Key, KeyboardDelegate, KeyboardEvents, RefObject} from '@react-types/shared';
15
+ import {filterDOMProps, mergeProps} from '@react-aria/utils';
16
+ import {TreeState} from '@react-stately/tree';
17
+ import {useSelectableList} from '@react-aria/selection';
18
+
19
+ export interface MenuAria {
20
+ /** Props for the menu element. */
21
+ menuProps: DOMAttributes
22
+ }
23
+
24
+ export interface AriaMenuOptions<T> extends Omit<AriaMenuProps<T>, 'children'>, KeyboardEvents {
25
+ /** Whether the menu uses virtual scrolling. */
26
+ isVirtualized?: boolean,
27
+
28
+ /**
29
+ * An optional keyboard delegate implementation for type to select,
30
+ * to override the default.
31
+ */
32
+ keyboardDelegate?: KeyboardDelegate
33
+ }
34
+
35
+ interface MenuData {
36
+ onClose?: () => void,
37
+ onAction?: (key: Key) => void
38
+ }
39
+
40
+ export const menuData = new WeakMap<TreeState<unknown>, MenuData>();
41
+
42
+ /**
43
+ * Provides the behavior and accessibility implementation for a menu component.
44
+ * A menu displays a list of actions or options that a user can choose.
45
+ * @param props - Props for the menu.
46
+ * @param state - State for the menu, as returned by `useListState`.
47
+ */
48
+ export function useMenu<T>(props: AriaMenuOptions<T>, state: TreeState<T>, ref: RefObject<HTMLElement | null>): MenuAria {
49
+ let {
50
+ shouldFocusWrap = true,
51
+ onKeyDown,
52
+ onKeyUp,
53
+ ...otherProps
54
+ } = props;
55
+
56
+ if (!props['aria-label'] && !props['aria-labelledby']) {
57
+ console.warn('An aria-label or aria-labelledby prop is required for accessibility.');
58
+ }
59
+
60
+ let domProps = filterDOMProps(props, {labelable: true});
61
+ let {listProps} = useSelectableList({
62
+ ...otherProps,
63
+ ref,
64
+ selectionManager: state.selectionManager,
65
+ collection: state.collection,
66
+ disabledKeys: state.disabledKeys,
67
+ shouldFocusWrap,
68
+ linkBehavior: 'override'
69
+ });
70
+
71
+ menuData.set(state, {
72
+ onClose: props.onClose,
73
+ onAction: props.onAction
74
+ });
75
+
76
+ return {
77
+ menuProps: mergeProps(domProps, {onKeyDown, onKeyUp}, {
78
+ role: 'menu',
79
+ ...listProps,
80
+ onKeyDown: (e) => {
81
+ // don't clear the menu selected keys if the user is presses escape since escape closes the menu
82
+ if (e.key !== 'Escape') {
83
+ listProps.onKeyDown(e);
84
+ }
85
+ }
86
+ })
87
+ };
88
+ }
@@ -0,0 +1,299 @@
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, RouterOptions} from '@react-types/shared';
14
+ import {filterDOMProps, mergeProps, useLinkProps, useRouter, useSlotId} from '@react-aria/utils';
15
+ import {getItemCount} from '@react-stately/collections';
16
+ import {isFocusVisible, useFocus, useHover, useKeyboard, usePress} from '@react-aria/interactions';
17
+ import {menuData} from './useMenu';
18
+ import {TreeState} from '@react-stately/tree';
19
+ import {useSelectableItem} from '@react-aria/selection';
20
+
21
+ export interface MenuItemAria {
22
+ /** Props for the menu item element. */
23
+ menuItemProps: DOMAttributes,
24
+
25
+ /** Props for the main text element inside the menu item. */
26
+ labelProps: DOMAttributes,
27
+
28
+ /** Props for the description text element inside the menu item, if any. */
29
+ descriptionProps: DOMAttributes,
30
+
31
+ /** Props for the keyboard shortcut text element inside the item, if any. */
32
+ keyboardShortcutProps: DOMAttributes,
33
+
34
+ /** Whether the item is currently focused. */
35
+ isFocused: boolean,
36
+ /** Whether the item is currently selected. */
37
+ isSelected: boolean,
38
+ /** Whether the item is currently in a pressed state. */
39
+ isPressed: boolean,
40
+ /** Whether the item is disabled. */
41
+ isDisabled: boolean
42
+ }
43
+
44
+ export interface AriaMenuItemProps extends DOMProps, PressEvents, HoverEvents, KeyboardEvents, FocusEvents {
45
+ /**
46
+ * Whether the menu item is disabled.
47
+ * @deprecated - pass disabledKeys to useTreeState instead.
48
+ */
49
+ isDisabled?: boolean,
50
+
51
+ /**
52
+ * Whether the menu item is selected.
53
+ * @deprecated - pass selectedKeys to useTreeState instead.
54
+ */
55
+ isSelected?: boolean,
56
+
57
+ /** A screen reader only label for the menu item. */
58
+ 'aria-label'?: string,
59
+
60
+ /** The unique key for the menu item. */
61
+ key?: Key,
62
+
63
+ /**
64
+ * Handler that is called when the menu should close after selecting an item.
65
+ * @deprecated - pass to the menu instead.
66
+ */
67
+ onClose?: () => void,
68
+
69
+ /**
70
+ * Whether the menu should close when the menu item is selected.
71
+ * @default true
72
+ */
73
+ closeOnSelect?: boolean,
74
+
75
+ /** Whether the menu item is contained in a virtual scrolling menu. */
76
+ isVirtualized?: boolean,
77
+
78
+ /**
79
+ * Handler that is called when the user activates the item.
80
+ * @deprecated - pass to the menu instead.
81
+ */
82
+ onAction?: (key: Key) => void,
83
+
84
+ /** What kind of popup the item opens. */
85
+ 'aria-haspopup'?: 'menu' | 'dialog',
86
+
87
+ /** Indicates whether the menu item's popup element is expanded or collapsed. */
88
+ 'aria-expanded'?: boolean | 'true' | 'false',
89
+
90
+ /** Identifies the menu item's popup element whose contents or presence is controlled by the menu item. */
91
+ 'aria-controls'?: string
92
+ }
93
+
94
+ /**
95
+ * Provides the behavior and accessibility implementation for an item in a menu.
96
+ * See `useMenu` for more details about menus.
97
+ * @param props - Props for the item.
98
+ * @param state - State for the menu, as returned by `useTreeState`.
99
+ */
100
+ export function useMenuItem<T>(props: AriaMenuItemProps, state: TreeState<T>, ref: RefObject<FocusableElement | null>): MenuItemAria {
101
+ let {
102
+ id,
103
+ key,
104
+ closeOnSelect,
105
+ isVirtualized,
106
+ 'aria-haspopup': hasPopup,
107
+ onPressStart: pressStartProp,
108
+ onPressUp: pressUpProp,
109
+ onPress,
110
+ onPressChange,
111
+ onPressEnd,
112
+ onHoverStart: hoverStartProp,
113
+ onHoverChange,
114
+ onHoverEnd,
115
+ onKeyDown,
116
+ onKeyUp,
117
+ onFocus,
118
+ onFocusChange,
119
+ onBlur
120
+ } = props;
121
+
122
+ let isTrigger = !!hasPopup;
123
+ let isDisabled = props.isDisabled ?? state.selectionManager.isDisabled(key);
124
+ let isSelected = props.isSelected ?? state.selectionManager.isSelected(key);
125
+ let data = menuData.get(state);
126
+ let item = state.collection.getItem(key);
127
+ let onClose = props.onClose || data.onClose;
128
+ let router = useRouter();
129
+ let performAction = (e: PressEvent) => {
130
+ if (isTrigger) {
131
+ return;
132
+ }
133
+
134
+ if (item?.props?.onAction) {
135
+ item.props.onAction();
136
+ } else if (props.onAction) {
137
+ props.onAction(key);
138
+ }
139
+
140
+ if (data.onAction) {
141
+ // Must reassign to variable otherwise `this` binding gets messed up. Something to do with WeakMap.
142
+ let onAction = data.onAction;
143
+ onAction(key);
144
+ }
145
+
146
+ if (e.target instanceof HTMLAnchorElement) {
147
+ router.open(e.target, e, item.props.href, item.props.routerOptions as RouterOptions);
148
+ }
149
+ };
150
+
151
+ let role = 'menuitem';
152
+ if (!isTrigger) {
153
+ if (state.selectionManager.selectionMode === 'single') {
154
+ role = 'menuitemradio';
155
+ } else if (state.selectionManager.selectionMode === 'multiple') {
156
+ role = 'menuitemcheckbox';
157
+ }
158
+ }
159
+
160
+ let labelId = useSlotId();
161
+ let descriptionId = useSlotId();
162
+ let keyboardId = useSlotId();
163
+
164
+ let ariaProps = {
165
+ id,
166
+ 'aria-disabled': isDisabled || undefined,
167
+ role,
168
+ 'aria-label': props['aria-label'],
169
+ 'aria-labelledby': labelId,
170
+ 'aria-describedby': [descriptionId, keyboardId].filter(Boolean).join(' ') || undefined,
171
+ 'aria-controls': props['aria-controls'],
172
+ 'aria-haspopup': hasPopup,
173
+ 'aria-expanded': props['aria-expanded']
174
+ };
175
+
176
+ if (state.selectionManager.selectionMode !== 'none' && !isTrigger) {
177
+ ariaProps['aria-checked'] = isSelected;
178
+ }
179
+
180
+ if (isVirtualized) {
181
+ ariaProps['aria-posinset'] = item?.index;
182
+ ariaProps['aria-setsize'] = getItemCount(state.collection);
183
+ }
184
+
185
+ let onPressStart = (e: PressEvent) => {
186
+ if (e.pointerType === 'keyboard') {
187
+ performAction(e);
188
+ }
189
+
190
+ pressStartProp?.(e);
191
+ };
192
+
193
+ let onPressUp = (e: PressEvent) => {
194
+ if (e.pointerType !== 'keyboard') {
195
+ performAction(e);
196
+
197
+ // Pressing a menu item should close by default in single selection mode but not multiple
198
+ // selection mode, except if overridden by the closeOnSelect prop.
199
+ if (!isTrigger && onClose && (closeOnSelect ?? (state.selectionManager.selectionMode !== 'multiple' || state.selectionManager.isLink(key)))) {
200
+ onClose();
201
+ }
202
+ }
203
+
204
+ pressUpProp?.(e);
205
+ };
206
+
207
+ let {itemProps, isFocused} = useSelectableItem({
208
+ selectionManager: state.selectionManager,
209
+ key,
210
+ ref,
211
+ shouldSelectOnPressUp: true,
212
+ allowsDifferentPressOrigin: true,
213
+ // Disable all handling of links in useSelectable item
214
+ // because we handle it ourselves. The behavior of menus
215
+ // is slightly different from other collections because
216
+ // actions are performed on key down rather than key up.
217
+ linkBehavior: 'none'
218
+ });
219
+
220
+ let {pressProps, isPressed} = usePress({
221
+ onPressStart,
222
+ onPress,
223
+ onPressUp,
224
+ onPressChange,
225
+ onPressEnd,
226
+ isDisabled
227
+ });
228
+ let {hoverProps} = useHover({
229
+ isDisabled,
230
+ onHoverStart(e) {
231
+ if (!isFocusVisible()) {
232
+ state.selectionManager.setFocused(true);
233
+ state.selectionManager.setFocusedKey(key);
234
+ }
235
+ hoverStartProp?.(e);
236
+ },
237
+ onHoverChange,
238
+ onHoverEnd
239
+ });
240
+
241
+ let {keyboardProps} = useKeyboard({
242
+ onKeyDown: (e) => {
243
+ // Ignore repeating events, which may have started on the menu trigger before moving
244
+ // focus to the menu item. We want to wait for a second complete key press sequence.
245
+ if (e.repeat) {
246
+ e.continuePropagation();
247
+ return;
248
+ }
249
+
250
+ switch (e.key) {
251
+ case ' ':
252
+ if (!isDisabled && state.selectionManager.selectionMode === 'none' && !isTrigger && closeOnSelect !== false && onClose) {
253
+ onClose();
254
+ }
255
+ break;
256
+ case 'Enter':
257
+ // The Enter key should always close on select, except if overridden.
258
+ if (!isDisabled && closeOnSelect !== false && !isTrigger && onClose) {
259
+ onClose();
260
+ }
261
+ break;
262
+ default:
263
+ if (!isTrigger) {
264
+ e.continuePropagation();
265
+ }
266
+
267
+ onKeyDown?.(e);
268
+ break;
269
+ }
270
+ },
271
+ onKeyUp
272
+ });
273
+
274
+ let {focusProps} = useFocus({onBlur, onFocus, onFocusChange});
275
+ let domProps = filterDOMProps(item.props);
276
+ delete domProps.id;
277
+ let linkProps = useLinkProps(item.props);
278
+
279
+ return {
280
+ menuItemProps: {
281
+ ...ariaProps,
282
+ ...mergeProps(domProps, linkProps, isTrigger ? {onFocus: itemProps.onFocus, 'data-key': itemProps['data-key']} : itemProps, pressProps, hoverProps, keyboardProps, focusProps),
283
+ tabIndex: itemProps.tabIndex != null ? -1 : undefined
284
+ },
285
+ labelProps: {
286
+ id: labelId
287
+ },
288
+ descriptionProps: {
289
+ id: descriptionId
290
+ },
291
+ keyboardShortcutProps: {
292
+ id: keyboardId
293
+ },
294
+ isFocused,
295
+ isSelected,
296
+ isPressed,
297
+ isDisabled
298
+ };
299
+ }
@@ -0,0 +1,61 @@
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
+ }
@@ -0,0 +1,144 @@
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
+ // @ts-ignore
16
+ import intlMessages from '../intl/*.json';
17
+ import {MenuTriggerState} from '@react-stately/menu';
18
+ import {MenuTriggerType} from '@react-types/menu';
19
+ import {RefObject} from '@react-types/shared';
20
+ import {useId} from '@react-aria/utils';
21
+ import {useLocalizedStringFormatter} from '@react-aria/i18n';
22
+ import {useLongPress} from '@react-aria/interactions';
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' as AriaMenuTriggerProps['type'],
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
+ if (trigger === 'longPress') {
72
+ return;
73
+ }
74
+ // fallthrough
75
+ case 'ArrowDown':
76
+ // Stop propagation, unless it would already be handled by useKeyboard.
77
+ if (!('continuePropagation' in e)) {
78
+ e.stopPropagation();
79
+ }
80
+ e.preventDefault();
81
+ state.toggle('first');
82
+ break;
83
+ case 'ArrowUp':
84
+ if (!('continuePropagation' in e)) {
85
+ e.stopPropagation();
86
+ }
87
+ e.preventDefault();
88
+ state.toggle('last');
89
+ break;
90
+ default:
91
+ // Allow other keys.
92
+ if ('continuePropagation' in e) {
93
+ e.continuePropagation();
94
+ }
95
+ }
96
+ }
97
+ };
98
+
99
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/menu');
100
+ let {longPressProps} = useLongPress({
101
+ isDisabled: isDisabled || trigger !== 'longPress',
102
+ accessibilityDescription: stringFormatter.format('longPressMessage'),
103
+ onLongPressStart() {
104
+ state.close();
105
+ },
106
+ onLongPress() {
107
+ state.open('first');
108
+ }
109
+ });
110
+
111
+ let pressProps = {
112
+ onPressStart(e) {
113
+ // For consistency with native, open the menu on mouse/key down, but touch up.
114
+ if (e.pointerType !== 'touch' && e.pointerType !== 'keyboard' && !isDisabled) {
115
+ // If opened with a screen reader, auto focus the first item.
116
+ // Otherwise, the menu itself will be focused.
117
+ state.open(e.pointerType === 'virtual' ? 'first' : null);
118
+ }
119
+ },
120
+ onPress(e) {
121
+ if (e.pointerType === 'touch' && !isDisabled) {
122
+ state.toggle();
123
+ }
124
+ }
125
+ };
126
+
127
+ // omit onPress from triggerProps since we override it above.
128
+ delete triggerProps.onPress;
129
+
130
+ return {
131
+ menuTriggerProps: {
132
+ ...triggerProps,
133
+ ...(trigger === 'press' ? pressProps : longPressProps),
134
+ id: menuTriggerId,
135
+ onKeyDown
136
+ },
137
+ menuProps: {
138
+ ...overlayProps,
139
+ 'aria-labelledby': menuTriggerId,
140
+ autoFocus: state.focusStrategy || true,
141
+ onClose: state.close
142
+ }
143
+ };
144
+ }