@react-stately/menu 3.5.8-nightly.4322 → 3.6.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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import {useOverlayTriggerState as $9Xvoh$useOverlayTriggerState} from "@react-stately/overlays";
2
- import {useState as $9Xvoh$useState} from "react";
2
+ import {useState as $9Xvoh$useState, useMemo as $9Xvoh$useMemo, useCallback as $9Xvoh$useCallback} from "react";
3
3
 
4
4
  /*
5
5
  * Copyright 2020 Adobe. All rights reserved.
@@ -26,6 +26,27 @@ import {useState as $9Xvoh$useState} from "react";
26
26
  function $a28c903ee9ad8dc5$export$79fefeb1c2091ac3(props) {
27
27
  let overlayTriggerState = (0, $9Xvoh$useOverlayTriggerState)(props);
28
28
  let [focusStrategy, setFocusStrategy] = (0, $9Xvoh$useState)(null);
29
+ let [expandedKeysStack, setExpandedKeysStack] = (0, $9Xvoh$useState)([]);
30
+ let closeAll = ()=>{
31
+ setExpandedKeysStack([]);
32
+ overlayTriggerState.close();
33
+ };
34
+ let openSubmenu = (triggerKey, level)=>{
35
+ setExpandedKeysStack((oldStack)=>{
36
+ if (level > oldStack.length) return oldStack;
37
+ return [
38
+ ...oldStack.slice(0, level),
39
+ triggerKey
40
+ ];
41
+ });
42
+ };
43
+ let closeSubmenu = (triggerKey, level)=>{
44
+ setExpandedKeysStack((oldStack)=>{
45
+ let key = oldStack[level];
46
+ if (key === triggerKey) return oldStack.slice(0, level);
47
+ else return oldStack;
48
+ });
49
+ };
29
50
  return {
30
51
  focusStrategy: focusStrategy,
31
52
  ...overlayTriggerState,
@@ -36,12 +57,87 @@ function $a28c903ee9ad8dc5$export$79fefeb1c2091ac3(props) {
36
57
  toggle (focusStrategy = null) {
37
58
  setFocusStrategy(focusStrategy);
38
59
  overlayTriggerState.toggle();
39
- }
60
+ },
61
+ close () {
62
+ closeAll();
63
+ },
64
+ UNSTABLE_expandedKeysStack: expandedKeysStack,
65
+ UNSTABLE_openSubmenu: openSubmenu,
66
+ UNSTABLE_closeSubmenu: closeSubmenu
40
67
  };
41
68
  }
42
69
 
43
70
 
71
+ /*
72
+ * Copyright 2023 Adobe. All rights reserved.
73
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
74
+ * you may not use this file except in compliance with the License. You may obtain a copy
75
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
76
+ *
77
+ * Unless required by applicable law or agreed to in writing, software distributed under
78
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
79
+ * OF ANY KIND, either express or implied. See the License for the specific language
80
+ * governing permissions and limitations under the License.
81
+ */
82
+ function $e5614764aa47eb35$export$d39fdbfa42246ac4(props, state) {
83
+ let { triggerKey: triggerKey } = props;
84
+ let { UNSTABLE_expandedKeysStack: UNSTABLE_expandedKeysStack, UNSTABLE_openSubmenu: UNSTABLE_openSubmenu, UNSTABLE_closeSubmenu: UNSTABLE_closeSubmenu, close: closeAll } = state;
85
+ let [submenuLevel] = (0, $9Xvoh$useState)(UNSTABLE_expandedKeysStack === null || UNSTABLE_expandedKeysStack === void 0 ? void 0 : UNSTABLE_expandedKeysStack.length);
86
+ let isOpen = (0, $9Xvoh$useMemo)(()=>UNSTABLE_expandedKeysStack[submenuLevel] === triggerKey, [
87
+ UNSTABLE_expandedKeysStack,
88
+ triggerKey,
89
+ submenuLevel
90
+ ]);
91
+ let [focusStrategy, setFocusStrategy] = (0, $9Xvoh$useState)(null);
92
+ let open = (0, $9Xvoh$useCallback)((focusStrategy = null)=>{
93
+ setFocusStrategy(focusStrategy);
94
+ UNSTABLE_openSubmenu(triggerKey, submenuLevel);
95
+ }, [
96
+ UNSTABLE_openSubmenu,
97
+ submenuLevel,
98
+ triggerKey
99
+ ]);
100
+ let close = (0, $9Xvoh$useCallback)(()=>{
101
+ setFocusStrategy(null);
102
+ UNSTABLE_closeSubmenu(triggerKey, submenuLevel);
103
+ }, [
104
+ UNSTABLE_closeSubmenu,
105
+ submenuLevel,
106
+ triggerKey
107
+ ]);
108
+ let toggle = (0, $9Xvoh$useCallback)((focusStrategy = null)=>{
109
+ setFocusStrategy(focusStrategy);
110
+ if (isOpen) close();
111
+ else open(focusStrategy);
112
+ }, [
113
+ close,
114
+ open,
115
+ isOpen
116
+ ]);
117
+ return (0, $9Xvoh$useMemo)(()=>({
118
+ focusStrategy: focusStrategy,
119
+ isOpen: isOpen,
120
+ open: open,
121
+ close: close,
122
+ closeAll: closeAll,
123
+ submenuLevel: submenuLevel,
124
+ // TODO: Placeholders that aren't used but give us parity with OverlayTriggerState so we can use this in Popover. Refactor if we update Popover via
125
+ // https://github.com/adobe/react-spectrum/pull/4976#discussion_r1336472863
126
+ setOpen: ()=>{},
127
+ toggle: toggle
128
+ }), [
129
+ isOpen,
130
+ open,
131
+ close,
132
+ closeAll,
133
+ focusStrategy,
134
+ toggle,
135
+ submenuLevel
136
+ ]);
137
+ }
138
+
139
+
44
140
 
45
141
 
46
- export {$a28c903ee9ad8dc5$export$79fefeb1c2091ac3 as useMenuTriggerState};
142
+ export {$a28c903ee9ad8dc5$export$79fefeb1c2091ac3 as useMenuTriggerState, $e5614764aa47eb35$export$d39fdbfa42246ac4 as UNSTABLE_useSubmenuTriggerState};
47
143
  //# sourceMappingURL=module.js.map
package/dist/main.js CHANGED
@@ -7,6 +7,7 @@ function $parcel$export(e, n, v, s) {
7
7
  }
8
8
 
9
9
  $parcel$export(module.exports, "useMenuTriggerState", () => $f39cdb649cd48930$export$79fefeb1c2091ac3);
10
+ $parcel$export(module.exports, "UNSTABLE_useSubmenuTriggerState", () => $38ab7fb105c54ad2$export$d39fdbfa42246ac4);
10
11
  /*
11
12
  * Copyright 2020 Adobe. All rights reserved.
12
13
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
@@ -32,6 +33,27 @@ $parcel$export(module.exports, "useMenuTriggerState", () => $f39cdb649cd48930$ex
32
33
  function $f39cdb649cd48930$export$79fefeb1c2091ac3(props) {
33
34
  let overlayTriggerState = (0, $cAWze$reactstatelyoverlays.useOverlayTriggerState)(props);
34
35
  let [focusStrategy, setFocusStrategy] = (0, $cAWze$react.useState)(null);
36
+ let [expandedKeysStack, setExpandedKeysStack] = (0, $cAWze$react.useState)([]);
37
+ let closeAll = ()=>{
38
+ setExpandedKeysStack([]);
39
+ overlayTriggerState.close();
40
+ };
41
+ let openSubmenu = (triggerKey, level)=>{
42
+ setExpandedKeysStack((oldStack)=>{
43
+ if (level > oldStack.length) return oldStack;
44
+ return [
45
+ ...oldStack.slice(0, level),
46
+ triggerKey
47
+ ];
48
+ });
49
+ };
50
+ let closeSubmenu = (triggerKey, level)=>{
51
+ setExpandedKeysStack((oldStack)=>{
52
+ let key = oldStack[level];
53
+ if (key === triggerKey) return oldStack.slice(0, level);
54
+ else return oldStack;
55
+ });
56
+ };
35
57
  return {
36
58
  focusStrategy: focusStrategy,
37
59
  ...overlayTriggerState,
@@ -42,11 +64,86 @@ function $f39cdb649cd48930$export$79fefeb1c2091ac3(props) {
42
64
  toggle (focusStrategy = null) {
43
65
  setFocusStrategy(focusStrategy);
44
66
  overlayTriggerState.toggle();
45
- }
67
+ },
68
+ close () {
69
+ closeAll();
70
+ },
71
+ UNSTABLE_expandedKeysStack: expandedKeysStack,
72
+ UNSTABLE_openSubmenu: openSubmenu,
73
+ UNSTABLE_closeSubmenu: closeSubmenu
46
74
  };
47
75
  }
48
76
 
49
77
 
78
+ /*
79
+ * Copyright 2023 Adobe. All rights reserved.
80
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
81
+ * you may not use this file except in compliance with the License. You may obtain a copy
82
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
83
+ *
84
+ * Unless required by applicable law or agreed to in writing, software distributed under
85
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
86
+ * OF ANY KIND, either express or implied. See the License for the specific language
87
+ * governing permissions and limitations under the License.
88
+ */
89
+ function $38ab7fb105c54ad2$export$d39fdbfa42246ac4(props, state) {
90
+ let { triggerKey: triggerKey } = props;
91
+ let { UNSTABLE_expandedKeysStack: UNSTABLE_expandedKeysStack, UNSTABLE_openSubmenu: UNSTABLE_openSubmenu, UNSTABLE_closeSubmenu: UNSTABLE_closeSubmenu, close: closeAll } = state;
92
+ let [submenuLevel] = (0, $cAWze$react.useState)(UNSTABLE_expandedKeysStack === null || UNSTABLE_expandedKeysStack === void 0 ? void 0 : UNSTABLE_expandedKeysStack.length);
93
+ let isOpen = (0, $cAWze$react.useMemo)(()=>UNSTABLE_expandedKeysStack[submenuLevel] === triggerKey, [
94
+ UNSTABLE_expandedKeysStack,
95
+ triggerKey,
96
+ submenuLevel
97
+ ]);
98
+ let [focusStrategy, setFocusStrategy] = (0, $cAWze$react.useState)(null);
99
+ let open = (0, $cAWze$react.useCallback)((focusStrategy = null)=>{
100
+ setFocusStrategy(focusStrategy);
101
+ UNSTABLE_openSubmenu(triggerKey, submenuLevel);
102
+ }, [
103
+ UNSTABLE_openSubmenu,
104
+ submenuLevel,
105
+ triggerKey
106
+ ]);
107
+ let close = (0, $cAWze$react.useCallback)(()=>{
108
+ setFocusStrategy(null);
109
+ UNSTABLE_closeSubmenu(triggerKey, submenuLevel);
110
+ }, [
111
+ UNSTABLE_closeSubmenu,
112
+ submenuLevel,
113
+ triggerKey
114
+ ]);
115
+ let toggle = (0, $cAWze$react.useCallback)((focusStrategy = null)=>{
116
+ setFocusStrategy(focusStrategy);
117
+ if (isOpen) close();
118
+ else open(focusStrategy);
119
+ }, [
120
+ close,
121
+ open,
122
+ isOpen
123
+ ]);
124
+ return (0, $cAWze$react.useMemo)(()=>({
125
+ focusStrategy: focusStrategy,
126
+ isOpen: isOpen,
127
+ open: open,
128
+ close: close,
129
+ closeAll: closeAll,
130
+ submenuLevel: submenuLevel,
131
+ // TODO: Placeholders that aren't used but give us parity with OverlayTriggerState so we can use this in Popover. Refactor if we update Popover via
132
+ // https://github.com/adobe/react-spectrum/pull/4976#discussion_r1336472863
133
+ setOpen: ()=>{},
134
+ toggle: toggle
135
+ }), [
136
+ isOpen,
137
+ open,
138
+ close,
139
+ closeAll,
140
+ focusStrategy,
141
+ toggle,
142
+ submenuLevel
143
+ ]);
144
+ }
145
+
146
+
50
147
 
51
148
 
52
149
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;AAsBM,SAAS,0CAAoB,KAAuB;IACzD,IAAI,sBAAsB,CAAA,GAAA,kDAAqB,EAAE;IACjD,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,qBAAO,EAAiB;IAEhE,OAAO;uBACL;QACA,GAAG,mBAAmB;QACtB,MAAK,gBAA+B,IAAI;YACtC,iBAAiB;YACjB,oBAAoB,IAAI;QAC1B;QACA,QAAO,gBAA+B,IAAI;YACxC,iBAAiB;YACjB,oBAAoB,MAAM;QAC5B;IACF;AACF;;CDtCC","sources":["packages/@react-stately/menu/src/index.ts","packages/@react-stately/menu/src/useMenuTriggerState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useMenuTriggerState} from './useMenuTriggerState';\n\nexport type {MenuTriggerProps} from '@react-types/menu';\nexport type {MenuTriggerState} from './useMenuTriggerState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FocusStrategy} from '@react-types/shared';\nimport {MenuTriggerProps} from '@react-types/menu';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {useState} from 'react';\n\nexport interface MenuTriggerState extends OverlayTriggerState {\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy,\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void,\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void\n}\n\n/**\n * Manages state for a menu trigger. Tracks whether the menu is currently open,\n * and controls which item will receive focus when it opens.\n */\nexport function useMenuTriggerState(props: MenuTriggerProps): MenuTriggerState {\n let overlayTriggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);\n\n return {\n focusStrategy,\n ...overlayTriggerState,\n open(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.open();\n },\n toggle(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.toggle();\n }\n };\n}\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;AAuCM,SAAS,0CAAoB,KAAuB;IACzD,IAAI,sBAAsB,CAAA,GAAA,kDAAqB,EAAE;IACjD,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,qBAAO,EAAiB;IAChE,IAAI,CAAC,mBAAmB,qBAAqB,GAAG,CAAA,GAAA,qBAAO,EAAS,EAAE;IAElE,IAAI,WAAW;QACb,qBAAqB,EAAE;QACvB,oBAAoB,KAAK;IAC3B;IAEA,IAAI,cAAc,CAAC,YAAiB;QAClC,qBAAqB,C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;KAAW;IAEpD,IAAI,SAAS,CAAA,GAAA,wBAAU,EAAE,CAAC,gBAA+B,IAAI;QAC3D,iBAAiB;QACjB,IAAI,QACF;aAEA,KAAK;IAET,GAAG;QAAC;QAAO;QAAM;KAAO;IAExB,OAAO,CAAA,GAAA,oBAAM,EAAE,IAAO,CAAA;2BACpB;oBACA;kBACA;mBACA;sBACA;0BACA;YACA,mJAAmJ;YACnJ,2EAA2E;YAC3E,SAAS,KAAO;oBAChB;QACF,CAAA,GAAI;QAAC;QAAQ;QAAM;QAAO;QAAU;QAAe;QAAQ;KAAa;AAC1E;","sources":["packages/@react-stately/menu/src/index.ts","packages/@react-stately/menu/src/useMenuTriggerState.ts","packages/@react-stately/menu/src/useSubmenuTriggerState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useMenuTriggerState} from './useMenuTriggerState';\nexport {UNSTABLE_useSubmenuTriggerState} from './useSubmenuTriggerState';\n\nexport type {MenuTriggerProps} from '@react-types/menu';\nexport type {MenuTriggerState, RootMenuTriggerState} from './useMenuTriggerState';\nexport type {SubmenuTriggerProps, SubmenuTriggerState} from './useSubmenuTriggerState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FocusStrategy, Key} from '@react-types/shared';\nimport {MenuTriggerProps} from '@react-types/menu';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {useState} from 'react';\n\nexport interface MenuTriggerState extends OverlayTriggerState {\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy,\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void,\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void\n}\n\nexport interface RootMenuTriggerState extends MenuTriggerState {\n /** Opens a specific submenu tied to a specific menu item at a specific level. */\n UNSTABLE_openSubmenu: (triggerKey: Key, level: number) => void,\n\n /** Closes a specific submenu tied to a specific menu item at a specific level. */\n UNSTABLE_closeSubmenu: (triggerKey: Key, level: number) => void,\n\n /** An array of open submenu trigger keys within the menu tree.\n * The index of key within array matches the submenu level in the tree.\n */\n UNSTABLE_expandedKeysStack: Key[],\n\n /** Closes the menu and all submenus in the menu tree. */\n close: () => void\n}\n\n/**\n * Manages state for a menu trigger. Tracks whether the menu is currently open,\n * and controls which item will receive focus when it opens. Also tracks the open submenus within\n * the menu tree via their trigger keys.\n */\nexport function useMenuTriggerState(props: MenuTriggerProps): RootMenuTriggerState {\n let overlayTriggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);\n let [expandedKeysStack, setExpandedKeysStack] = useState<Key[]>([]);\n\n let closeAll = () => {\n setExpandedKeysStack([]);\n overlayTriggerState.close();\n };\n\n let openSubmenu = (triggerKey: Key, level: number) => {\n setExpandedKeysStack(oldStack => {\n if (level > oldStack.length) {\n return oldStack;\n }\n\n return [...oldStack.slice(0, level), triggerKey];\n });\n };\n\n let closeSubmenu = (triggerKey: Key, level: number) => {\n setExpandedKeysStack(oldStack => {\n let key = oldStack[level];\n if (key === triggerKey) {\n return oldStack.slice(0, level);\n } else {\n return oldStack;\n }\n });\n };\n\n return {\n focusStrategy,\n ...overlayTriggerState,\n open(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.open();\n },\n toggle(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.toggle();\n },\n close() {\n closeAll();\n },\n UNSTABLE_expandedKeysStack: expandedKeysStack,\n UNSTABLE_openSubmenu: openSubmenu,\n UNSTABLE_closeSubmenu: closeSubmenu\n };\n}\n","/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FocusStrategy, Key} from '@react-types/shared';\nimport type {OverlayTriggerState} from '@react-stately/overlays';\nimport {RootMenuTriggerState} from './useMenuTriggerState';\nimport {useCallback, useMemo, useState} from 'react';\n\nexport interface SubmenuTriggerProps {\n /** Key of the trigger item. */\n triggerKey: Key\n}\n\nexport interface SubmenuTriggerState extends OverlayTriggerState {\n /** Whether the submenu is currently open. */\n isOpen: boolean,\n /** Controls which item will be auto focused when the submenu opens. */\n focusStrategy: FocusStrategy | null,\n /** Opens the submenu. */\n open: (focusStrategy?: FocusStrategy | null) => void,\n /** Closes the submenu. */\n close: () => void,\n /** Closes all menus and submenus in the menu tree. */\n closeAll: () => void,\n /** The level of the submenu. */\n submenuLevel: number,\n /** Toggles the submenu. */\n toggle: (focusStrategy?: FocusStrategy | null) => void,\n /** @private */\n setOpen: () => void\n}\n\n/**\n * Manages state for a submenu trigger. Tracks whether the submenu is currently open, the level of the submenu, and\n * controls which item will receive focus when it opens.\n */\nexport function UNSTABLE_useSubmenuTriggerState(props: SubmenuTriggerProps, state: RootMenuTriggerState): SubmenuTriggerState {\n let {triggerKey} = props;\n let {UNSTABLE_expandedKeysStack, UNSTABLE_openSubmenu, UNSTABLE_closeSubmenu, close: closeAll} = state;\n let [submenuLevel] = useState(UNSTABLE_expandedKeysStack?.length);\n let isOpen = useMemo(() => UNSTABLE_expandedKeysStack[submenuLevel] === triggerKey, [UNSTABLE_expandedKeysStack, triggerKey, submenuLevel]);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);\n\n let open = useCallback((focusStrategy: FocusStrategy = null) => {\n setFocusStrategy(focusStrategy);\n UNSTABLE_openSubmenu(triggerKey, submenuLevel);\n }, [UNSTABLE_openSubmenu, submenuLevel, triggerKey]);\n\n let close = useCallback(() => {\n setFocusStrategy(null);\n UNSTABLE_closeSubmenu(triggerKey, submenuLevel);\n }, [UNSTABLE_closeSubmenu, submenuLevel, triggerKey]);\n\n let toggle = useCallback((focusStrategy: FocusStrategy = null) => {\n setFocusStrategy(focusStrategy);\n if (isOpen) {\n close();\n } else {\n open(focusStrategy);\n }\n }, [close, open, isOpen]);\n\n return useMemo(() => ({\n focusStrategy,\n isOpen,\n open,\n close,\n closeAll,\n submenuLevel,\n // TODO: Placeholders that aren't used but give us parity with OverlayTriggerState so we can use this in Popover. Refactor if we update Popover via\n // https://github.com/adobe/react-spectrum/pull/4976#discussion_r1336472863\n setOpen: () => {},\n toggle\n }), [isOpen, open, close, closeAll, focusStrategy, toggle, submenuLevel]);\n}\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import {useOverlayTriggerState as $9Xvoh$useOverlayTriggerState} from "@react-stately/overlays";
2
- import {useState as $9Xvoh$useState} from "react";
2
+ import {useState as $9Xvoh$useState, useMemo as $9Xvoh$useMemo, useCallback as $9Xvoh$useCallback} from "react";
3
3
 
4
4
  /*
5
5
  * Copyright 2020 Adobe. All rights reserved.
@@ -26,6 +26,27 @@ import {useState as $9Xvoh$useState} from "react";
26
26
  function $a28c903ee9ad8dc5$export$79fefeb1c2091ac3(props) {
27
27
  let overlayTriggerState = (0, $9Xvoh$useOverlayTriggerState)(props);
28
28
  let [focusStrategy, setFocusStrategy] = (0, $9Xvoh$useState)(null);
29
+ let [expandedKeysStack, setExpandedKeysStack] = (0, $9Xvoh$useState)([]);
30
+ let closeAll = ()=>{
31
+ setExpandedKeysStack([]);
32
+ overlayTriggerState.close();
33
+ };
34
+ let openSubmenu = (triggerKey, level)=>{
35
+ setExpandedKeysStack((oldStack)=>{
36
+ if (level > oldStack.length) return oldStack;
37
+ return [
38
+ ...oldStack.slice(0, level),
39
+ triggerKey
40
+ ];
41
+ });
42
+ };
43
+ let closeSubmenu = (triggerKey, level)=>{
44
+ setExpandedKeysStack((oldStack)=>{
45
+ let key = oldStack[level];
46
+ if (key === triggerKey) return oldStack.slice(0, level);
47
+ else return oldStack;
48
+ });
49
+ };
29
50
  return {
30
51
  focusStrategy: focusStrategy,
31
52
  ...overlayTriggerState,
@@ -36,12 +57,87 @@ function $a28c903ee9ad8dc5$export$79fefeb1c2091ac3(props) {
36
57
  toggle (focusStrategy = null) {
37
58
  setFocusStrategy(focusStrategy);
38
59
  overlayTriggerState.toggle();
39
- }
60
+ },
61
+ close () {
62
+ closeAll();
63
+ },
64
+ UNSTABLE_expandedKeysStack: expandedKeysStack,
65
+ UNSTABLE_openSubmenu: openSubmenu,
66
+ UNSTABLE_closeSubmenu: closeSubmenu
40
67
  };
41
68
  }
42
69
 
43
70
 
71
+ /*
72
+ * Copyright 2023 Adobe. All rights reserved.
73
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
74
+ * you may not use this file except in compliance with the License. You may obtain a copy
75
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
76
+ *
77
+ * Unless required by applicable law or agreed to in writing, software distributed under
78
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
79
+ * OF ANY KIND, either express or implied. See the License for the specific language
80
+ * governing permissions and limitations under the License.
81
+ */
82
+ function $e5614764aa47eb35$export$d39fdbfa42246ac4(props, state) {
83
+ let { triggerKey: triggerKey } = props;
84
+ let { UNSTABLE_expandedKeysStack: UNSTABLE_expandedKeysStack, UNSTABLE_openSubmenu: UNSTABLE_openSubmenu, UNSTABLE_closeSubmenu: UNSTABLE_closeSubmenu, close: closeAll } = state;
85
+ let [submenuLevel] = (0, $9Xvoh$useState)(UNSTABLE_expandedKeysStack === null || UNSTABLE_expandedKeysStack === void 0 ? void 0 : UNSTABLE_expandedKeysStack.length);
86
+ let isOpen = (0, $9Xvoh$useMemo)(()=>UNSTABLE_expandedKeysStack[submenuLevel] === triggerKey, [
87
+ UNSTABLE_expandedKeysStack,
88
+ triggerKey,
89
+ submenuLevel
90
+ ]);
91
+ let [focusStrategy, setFocusStrategy] = (0, $9Xvoh$useState)(null);
92
+ let open = (0, $9Xvoh$useCallback)((focusStrategy = null)=>{
93
+ setFocusStrategy(focusStrategy);
94
+ UNSTABLE_openSubmenu(triggerKey, submenuLevel);
95
+ }, [
96
+ UNSTABLE_openSubmenu,
97
+ submenuLevel,
98
+ triggerKey
99
+ ]);
100
+ let close = (0, $9Xvoh$useCallback)(()=>{
101
+ setFocusStrategy(null);
102
+ UNSTABLE_closeSubmenu(triggerKey, submenuLevel);
103
+ }, [
104
+ UNSTABLE_closeSubmenu,
105
+ submenuLevel,
106
+ triggerKey
107
+ ]);
108
+ let toggle = (0, $9Xvoh$useCallback)((focusStrategy = null)=>{
109
+ setFocusStrategy(focusStrategy);
110
+ if (isOpen) close();
111
+ else open(focusStrategy);
112
+ }, [
113
+ close,
114
+ open,
115
+ isOpen
116
+ ]);
117
+ return (0, $9Xvoh$useMemo)(()=>({
118
+ focusStrategy: focusStrategy,
119
+ isOpen: isOpen,
120
+ open: open,
121
+ close: close,
122
+ closeAll: closeAll,
123
+ submenuLevel: submenuLevel,
124
+ // TODO: Placeholders that aren't used but give us parity with OverlayTriggerState so we can use this in Popover. Refactor if we update Popover via
125
+ // https://github.com/adobe/react-spectrum/pull/4976#discussion_r1336472863
126
+ setOpen: ()=>{},
127
+ toggle: toggle
128
+ }), [
129
+ isOpen,
130
+ open,
131
+ close,
132
+ closeAll,
133
+ focusStrategy,
134
+ toggle,
135
+ submenuLevel
136
+ ]);
137
+ }
138
+
139
+
44
140
 
45
141
 
46
- export {$a28c903ee9ad8dc5$export$79fefeb1c2091ac3 as useMenuTriggerState};
142
+ export {$a28c903ee9ad8dc5$export$79fefeb1c2091ac3 as useMenuTriggerState, $e5614764aa47eb35$export$d39fdbfa42246ac4 as UNSTABLE_useSubmenuTriggerState};
47
143
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;AAsBM,SAAS,0CAAoB,KAAuB;IACzD,IAAI,sBAAsB,CAAA,GAAA,6BAAqB,EAAE;IACjD,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,eAAO,EAAiB;IAEhE,OAAO;uBACL;QACA,GAAG,mBAAmB;QACtB,MAAK,gBAA+B,IAAI;YACtC,iBAAiB;YACjB,oBAAoB,IAAI;QAC1B;QACA,QAAO,gBAA+B,IAAI;YACxC,iBAAiB;YACjB,oBAAoB,MAAM;QAC5B;IACF;AACF;;CDtCC","sources":["packages/@react-stately/menu/src/index.ts","packages/@react-stately/menu/src/useMenuTriggerState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useMenuTriggerState} from './useMenuTriggerState';\n\nexport type {MenuTriggerProps} from '@react-types/menu';\nexport type {MenuTriggerState} from './useMenuTriggerState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FocusStrategy} from '@react-types/shared';\nimport {MenuTriggerProps} from '@react-types/menu';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {useState} from 'react';\n\nexport interface MenuTriggerState extends OverlayTriggerState {\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy,\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void,\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void\n}\n\n/**\n * Manages state for a menu trigger. Tracks whether the menu is currently open,\n * and controls which item will receive focus when it opens.\n */\nexport function useMenuTriggerState(props: MenuTriggerProps): MenuTriggerState {\n let overlayTriggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);\n\n return {\n focusStrategy,\n ...overlayTriggerState,\n open(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.open();\n },\n toggle(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.toggle();\n }\n };\n}\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;AAuCM,SAAS,0CAAoB,KAAuB;IACzD,IAAI,sBAAsB,CAAA,GAAA,6BAAqB,EAAE;IACjD,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,eAAO,EAAiB;IAChE,IAAI,CAAC,mBAAmB,qBAAqB,G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,GAAG;QAAC;QAAsB;QAAc;KAAW;IAEnD,IAAI,QAAQ,CAAA,GAAA,kBAAU,EAAE;QACtB,iBAAiB;QACjB,sBAAsB,YAAY;IACpC,GAAG;QAAC;QAAuB;QAAc;KAAW;IAEpD,IAAI,SAAS,CAAA,GAAA,kBAAU,EAAE,CAAC,gBAA+B,IAAI;QAC3D,iBAAiB;QACjB,IAAI,QACF;aAEA,KAAK;IAET,GAAG;QAAC;QAAO;QAAM;KAAO;IAExB,OAAO,CAAA,GAAA,cAAM,EAAE,IAAO,CAAA;2BACpB;oBACA;kBACA;mBACA;sBACA;0BACA;YACA,mJAAmJ;YACnJ,2EAA2E;YAC3E,SAAS,KAAO;oBAChB;QACF,CAAA,GAAI;QAAC;QAAQ;QAAM;QAAO;QAAU;QAAe;QAAQ;KAAa;AAC1E;","sources":["packages/@react-stately/menu/src/index.ts","packages/@react-stately/menu/src/useMenuTriggerState.ts","packages/@react-stately/menu/src/useSubmenuTriggerState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useMenuTriggerState} from './useMenuTriggerState';\nexport {UNSTABLE_useSubmenuTriggerState} from './useSubmenuTriggerState';\n\nexport type {MenuTriggerProps} from '@react-types/menu';\nexport type {MenuTriggerState, RootMenuTriggerState} from './useMenuTriggerState';\nexport type {SubmenuTriggerProps, SubmenuTriggerState} from './useSubmenuTriggerState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FocusStrategy, Key} from '@react-types/shared';\nimport {MenuTriggerProps} from '@react-types/menu';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {useState} from 'react';\n\nexport interface MenuTriggerState extends OverlayTriggerState {\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy,\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void,\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void\n}\n\nexport interface RootMenuTriggerState extends MenuTriggerState {\n /** Opens a specific submenu tied to a specific menu item at a specific level. */\n UNSTABLE_openSubmenu: (triggerKey: Key, level: number) => void,\n\n /** Closes a specific submenu tied to a specific menu item at a specific level. */\n UNSTABLE_closeSubmenu: (triggerKey: Key, level: number) => void,\n\n /** An array of open submenu trigger keys within the menu tree.\n * The index of key within array matches the submenu level in the tree.\n */\n UNSTABLE_expandedKeysStack: Key[],\n\n /** Closes the menu and all submenus in the menu tree. */\n close: () => void\n}\n\n/**\n * Manages state for a menu trigger. Tracks whether the menu is currently open,\n * and controls which item will receive focus when it opens. Also tracks the open submenus within\n * the menu tree via their trigger keys.\n */\nexport function useMenuTriggerState(props: MenuTriggerProps): RootMenuTriggerState {\n let overlayTriggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);\n let [expandedKeysStack, setExpandedKeysStack] = useState<Key[]>([]);\n\n let closeAll = () => {\n setExpandedKeysStack([]);\n overlayTriggerState.close();\n };\n\n let openSubmenu = (triggerKey: Key, level: number) => {\n setExpandedKeysStack(oldStack => {\n if (level > oldStack.length) {\n return oldStack;\n }\n\n return [...oldStack.slice(0, level), triggerKey];\n });\n };\n\n let closeSubmenu = (triggerKey: Key, level: number) => {\n setExpandedKeysStack(oldStack => {\n let key = oldStack[level];\n if (key === triggerKey) {\n return oldStack.slice(0, level);\n } else {\n return oldStack;\n }\n });\n };\n\n return {\n focusStrategy,\n ...overlayTriggerState,\n open(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.open();\n },\n toggle(focusStrategy: FocusStrategy = null) {\n setFocusStrategy(focusStrategy);\n overlayTriggerState.toggle();\n },\n close() {\n closeAll();\n },\n UNSTABLE_expandedKeysStack: expandedKeysStack,\n UNSTABLE_openSubmenu: openSubmenu,\n UNSTABLE_closeSubmenu: closeSubmenu\n };\n}\n","/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FocusStrategy, Key} from '@react-types/shared';\nimport type {OverlayTriggerState} from '@react-stately/overlays';\nimport {RootMenuTriggerState} from './useMenuTriggerState';\nimport {useCallback, useMemo, useState} from 'react';\n\nexport interface SubmenuTriggerProps {\n /** Key of the trigger item. */\n triggerKey: Key\n}\n\nexport interface SubmenuTriggerState extends OverlayTriggerState {\n /** Whether the submenu is currently open. */\n isOpen: boolean,\n /** Controls which item will be auto focused when the submenu opens. */\n focusStrategy: FocusStrategy | null,\n /** Opens the submenu. */\n open: (focusStrategy?: FocusStrategy | null) => void,\n /** Closes the submenu. */\n close: () => void,\n /** Closes all menus and submenus in the menu tree. */\n closeAll: () => void,\n /** The level of the submenu. */\n submenuLevel: number,\n /** Toggles the submenu. */\n toggle: (focusStrategy?: FocusStrategy | null) => void,\n /** @private */\n setOpen: () => void\n}\n\n/**\n * Manages state for a submenu trigger. Tracks whether the submenu is currently open, the level of the submenu, and\n * controls which item will receive focus when it opens.\n */\nexport function UNSTABLE_useSubmenuTriggerState(props: SubmenuTriggerProps, state: RootMenuTriggerState): SubmenuTriggerState {\n let {triggerKey} = props;\n let {UNSTABLE_expandedKeysStack, UNSTABLE_openSubmenu, UNSTABLE_closeSubmenu, close: closeAll} = state;\n let [submenuLevel] = useState(UNSTABLE_expandedKeysStack?.length);\n let isOpen = useMemo(() => UNSTABLE_expandedKeysStack[submenuLevel] === triggerKey, [UNSTABLE_expandedKeysStack, triggerKey, submenuLevel]);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);\n\n let open = useCallback((focusStrategy: FocusStrategy = null) => {\n setFocusStrategy(focusStrategy);\n UNSTABLE_openSubmenu(triggerKey, submenuLevel);\n }, [UNSTABLE_openSubmenu, submenuLevel, triggerKey]);\n\n let close = useCallback(() => {\n setFocusStrategy(null);\n UNSTABLE_closeSubmenu(triggerKey, submenuLevel);\n }, [UNSTABLE_closeSubmenu, submenuLevel, triggerKey]);\n\n let toggle = useCallback((focusStrategy: FocusStrategy = null) => {\n setFocusStrategy(focusStrategy);\n if (isOpen) {\n close();\n } else {\n open(focusStrategy);\n }\n }, [close, open, isOpen]);\n\n return useMemo(() => ({\n focusStrategy,\n isOpen,\n open,\n close,\n closeAll,\n submenuLevel,\n // TODO: Placeholders that aren't used but give us parity with OverlayTriggerState so we can use this in Popover. Refactor if we update Popover via\n // https://github.com/adobe/react-spectrum/pull/4976#discussion_r1336472863\n setOpen: () => {},\n toggle\n }), [isOpen, open, close, closeAll, focusStrategy, toggle, submenuLevel]);\n}\n"],"names":[],"version":3,"file":"module.js.map"}
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { FocusStrategy } from "@react-types/shared";
1
+ import { FocusStrategy, Key } from "@react-types/shared";
2
2
  import { MenuTriggerProps } from "@react-types/menu";
3
3
  import { OverlayTriggerState } from "@react-stately/overlays";
4
4
  export interface MenuTriggerState extends OverlayTriggerState {
@@ -9,11 +9,51 @@ export interface MenuTriggerState extends OverlayTriggerState {
9
9
  /** Toggles the menu. */
10
10
  toggle(focusStrategy?: FocusStrategy | null): void;
11
11
  }
12
+ export interface RootMenuTriggerState extends MenuTriggerState {
13
+ /** Opens a specific submenu tied to a specific menu item at a specific level. */
14
+ UNSTABLE_openSubmenu: (triggerKey: Key, level: number) => void;
15
+ /** Closes a specific submenu tied to a specific menu item at a specific level. */
16
+ UNSTABLE_closeSubmenu: (triggerKey: Key, level: number) => void;
17
+ /** An array of open submenu trigger keys within the menu tree.
18
+ * The index of key within array matches the submenu level in the tree.
19
+ */
20
+ UNSTABLE_expandedKeysStack: Key[];
21
+ /** Closes the menu and all submenus in the menu tree. */
22
+ close: () => void;
23
+ }
12
24
  /**
13
25
  * Manages state for a menu trigger. Tracks whether the menu is currently open,
14
- * and controls which item will receive focus when it opens.
26
+ * and controls which item will receive focus when it opens. Also tracks the open submenus within
27
+ * the menu tree via their trigger keys.
28
+ */
29
+ export function useMenuTriggerState(props: MenuTriggerProps): RootMenuTriggerState;
30
+ export interface SubmenuTriggerProps {
31
+ /** Key of the trigger item. */
32
+ triggerKey: Key;
33
+ }
34
+ export interface SubmenuTriggerState extends OverlayTriggerState {
35
+ /** Whether the submenu is currently open. */
36
+ isOpen: boolean;
37
+ /** Controls which item will be auto focused when the submenu opens. */
38
+ focusStrategy: FocusStrategy | null;
39
+ /** Opens the submenu. */
40
+ open: (focusStrategy?: FocusStrategy | null) => void;
41
+ /** Closes the submenu. */
42
+ close: () => void;
43
+ /** Closes all menus and submenus in the menu tree. */
44
+ closeAll: () => void;
45
+ /** The level of the submenu. */
46
+ submenuLevel: number;
47
+ /** Toggles the submenu. */
48
+ toggle: (focusStrategy?: FocusStrategy | null) => void;
49
+ /** @private */
50
+ setOpen: () => void;
51
+ }
52
+ /**
53
+ * Manages state for a submenu trigger. Tracks whether the submenu is currently open, the level of the submenu, and
54
+ * controls which item will receive focus when it opens.
15
55
  */
16
- export function useMenuTriggerState(props: MenuTriggerProps): MenuTriggerState;
56
+ export function UNSTABLE_useSubmenuTriggerState(props: SubmenuTriggerProps, state: RootMenuTriggerState): SubmenuTriggerState;
17
57
  export type { MenuTriggerProps } from '@react-types/menu';
18
58
 
19
59
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"mappings":";;;AAiBA,iCAAkC,SAAQ,mBAAmB;IAC3D,oEAAoE;IACpE,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IAEtC,sBAAsB;IACtB,IAAI,CAAC,aAAa,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI,CAAC;IAEjD,wBAAwB;IACxB,MAAM,CAAC,aAAa,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI,CAAA;CACnD;AAED;;;GAGG;AACH,oCAAoC,KAAK,EAAE,gBAAgB,GAAG,gBAAgB,CAgB7E;AClCD,YAAY,EAAC,gBAAgB,EAAC,MAAM,mBAAmB,CAAC","sources":["packages/@react-stately/menu/src/packages/@react-stately/menu/src/useMenuTriggerState.ts","packages/@react-stately/menu/src/packages/@react-stately/menu/src/index.ts","packages/@react-stately/menu/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useMenuTriggerState} from './useMenuTriggerState';\n\nexport type {MenuTriggerProps} from '@react-types/menu';\nexport type {MenuTriggerState} from './useMenuTriggerState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":";;;AAiBA,iCAAkC,SAAQ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AA;CACpB;AAED;;;GAGG;AACH,gDAAgD,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,oBAAoB,GAAG,mBAAmB,CAsC5H;ACpED,YAAY,EAAC,gBAAgB,EAAC,MAAM,mBAAmB,CAAC","sources":["packages/@react-stately/menu/src/packages/@react-stately/menu/src/useMenuTriggerState.ts","packages/@react-stately/menu/src/packages/@react-stately/menu/src/useSubmenuTriggerState.ts","packages/@react-stately/menu/src/packages/@react-stately/menu/src/index.ts","packages/@react-stately/menu/src/index.ts"],"sourcesContent":[null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useMenuTriggerState} from './useMenuTriggerState';\nexport {UNSTABLE_useSubmenuTriggerState} from './useSubmenuTriggerState';\n\nexport type {MenuTriggerProps} from '@react-types/menu';\nexport type {MenuTriggerState, RootMenuTriggerState} from './useMenuTriggerState';\nexport type {SubmenuTriggerProps, SubmenuTriggerState} from './useSubmenuTriggerState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/menu",
3
- "version": "3.5.8-nightly.4322+1cba01d3e",
3
+ "version": "3.6.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -22,9 +22,9 @@
22
22
  "url": "https://github.com/adobe/react-spectrum"
23
23
  },
24
24
  "dependencies": {
25
- "@react-stately/overlays": "3.6.5-nightly.4322+1cba01d3e",
26
- "@react-types/menu": "3.9.7-nightly.4322+1cba01d3e",
27
- "@react-types/shared": "3.0.0-nightly.2611+1cba01d3e",
25
+ "@react-stately/overlays": "^3.6.4",
26
+ "@react-types/menu": "^3.9.6",
27
+ "@react-types/shared": "^3.22.0",
28
28
  "@swc/helpers": "^0.5.0"
29
29
  },
30
30
  "peerDependencies": {
@@ -33,5 +33,5 @@
33
33
  "publishConfig": {
34
34
  "access": "public"
35
35
  },
36
- "gitHead": "1cba01d3edd5160ccc6788fd54f9cd34b28f9699"
36
+ "gitHead": "86b38c87868ce7f262e0df905e5ac4eb2653791d"
37
37
  }
package/src/index.ts CHANGED
@@ -11,6 +11,8 @@
11
11
  */
12
12
 
13
13
  export {useMenuTriggerState} from './useMenuTriggerState';
14
+ export {UNSTABLE_useSubmenuTriggerState} from './useSubmenuTriggerState';
14
15
 
15
16
  export type {MenuTriggerProps} from '@react-types/menu';
16
- export type {MenuTriggerState} from './useMenuTriggerState';
17
+ export type {MenuTriggerState, RootMenuTriggerState} from './useMenuTriggerState';
18
+ export type {SubmenuTriggerProps, SubmenuTriggerState} from './useSubmenuTriggerState';
@@ -10,7 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {FocusStrategy} from '@react-types/shared';
13
+ import {FocusStrategy, Key} from '@react-types/shared';
14
14
  import {MenuTriggerProps} from '@react-types/menu';
15
15
  import {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';
16
16
  import {useState} from 'react';
@@ -26,13 +26,57 @@ export interface MenuTriggerState extends OverlayTriggerState {
26
26
  toggle(focusStrategy?: FocusStrategy | null): void
27
27
  }
28
28
 
29
+ export interface RootMenuTriggerState extends MenuTriggerState {
30
+ /** Opens a specific submenu tied to a specific menu item at a specific level. */
31
+ UNSTABLE_openSubmenu: (triggerKey: Key, level: number) => void,
32
+
33
+ /** Closes a specific submenu tied to a specific menu item at a specific level. */
34
+ UNSTABLE_closeSubmenu: (triggerKey: Key, level: number) => void,
35
+
36
+ /** An array of open submenu trigger keys within the menu tree.
37
+ * The index of key within array matches the submenu level in the tree.
38
+ */
39
+ UNSTABLE_expandedKeysStack: Key[],
40
+
41
+ /** Closes the menu and all submenus in the menu tree. */
42
+ close: () => void
43
+ }
44
+
29
45
  /**
30
46
  * Manages state for a menu trigger. Tracks whether the menu is currently open,
31
- * and controls which item will receive focus when it opens.
47
+ * and controls which item will receive focus when it opens. Also tracks the open submenus within
48
+ * the menu tree via their trigger keys.
32
49
  */
33
- export function useMenuTriggerState(props: MenuTriggerProps): MenuTriggerState {
50
+ export function useMenuTriggerState(props: MenuTriggerProps): RootMenuTriggerState {
34
51
  let overlayTriggerState = useOverlayTriggerState(props);
35
52
  let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);
53
+ let [expandedKeysStack, setExpandedKeysStack] = useState<Key[]>([]);
54
+
55
+ let closeAll = () => {
56
+ setExpandedKeysStack([]);
57
+ overlayTriggerState.close();
58
+ };
59
+
60
+ let openSubmenu = (triggerKey: Key, level: number) => {
61
+ setExpandedKeysStack(oldStack => {
62
+ if (level > oldStack.length) {
63
+ return oldStack;
64
+ }
65
+
66
+ return [...oldStack.slice(0, level), triggerKey];
67
+ });
68
+ };
69
+
70
+ let closeSubmenu = (triggerKey: Key, level: number) => {
71
+ setExpandedKeysStack(oldStack => {
72
+ let key = oldStack[level];
73
+ if (key === triggerKey) {
74
+ return oldStack.slice(0, level);
75
+ } else {
76
+ return oldStack;
77
+ }
78
+ });
79
+ };
36
80
 
37
81
  return {
38
82
  focusStrategy,
@@ -44,6 +88,12 @@ export function useMenuTriggerState(props: MenuTriggerProps): MenuTriggerState
44
88
  toggle(focusStrategy: FocusStrategy = null) {
45
89
  setFocusStrategy(focusStrategy);
46
90
  overlayTriggerState.toggle();
47
- }
91
+ },
92
+ close() {
93
+ closeAll();
94
+ },
95
+ UNSTABLE_expandedKeysStack: expandedKeysStack,
96
+ UNSTABLE_openSubmenu: openSubmenu,
97
+ UNSTABLE_closeSubmenu: closeSubmenu
48
98
  };
49
99
  }
@@ -0,0 +1,84 @@
1
+ /*
2
+ * Copyright 2023 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 {FocusStrategy, Key} from '@react-types/shared';
14
+ import type {OverlayTriggerState} from '@react-stately/overlays';
15
+ import {RootMenuTriggerState} from './useMenuTriggerState';
16
+ import {useCallback, useMemo, useState} from 'react';
17
+
18
+ export interface SubmenuTriggerProps {
19
+ /** Key of the trigger item. */
20
+ triggerKey: Key
21
+ }
22
+
23
+ export interface SubmenuTriggerState extends OverlayTriggerState {
24
+ /** Whether the submenu is currently open. */
25
+ isOpen: boolean,
26
+ /** Controls which item will be auto focused when the submenu opens. */
27
+ focusStrategy: FocusStrategy | null,
28
+ /** Opens the submenu. */
29
+ open: (focusStrategy?: FocusStrategy | null) => void,
30
+ /** Closes the submenu. */
31
+ close: () => void,
32
+ /** Closes all menus and submenus in the menu tree. */
33
+ closeAll: () => void,
34
+ /** The level of the submenu. */
35
+ submenuLevel: number,
36
+ /** Toggles the submenu. */
37
+ toggle: (focusStrategy?: FocusStrategy | null) => void,
38
+ /** @private */
39
+ setOpen: () => void
40
+ }
41
+
42
+ /**
43
+ * Manages state for a submenu trigger. Tracks whether the submenu is currently open, the level of the submenu, and
44
+ * controls which item will receive focus when it opens.
45
+ */
46
+ export function UNSTABLE_useSubmenuTriggerState(props: SubmenuTriggerProps, state: RootMenuTriggerState): SubmenuTriggerState {
47
+ let {triggerKey} = props;
48
+ let {UNSTABLE_expandedKeysStack, UNSTABLE_openSubmenu, UNSTABLE_closeSubmenu, close: closeAll} = state;
49
+ let [submenuLevel] = useState(UNSTABLE_expandedKeysStack?.length);
50
+ let isOpen = useMemo(() => UNSTABLE_expandedKeysStack[submenuLevel] === triggerKey, [UNSTABLE_expandedKeysStack, triggerKey, submenuLevel]);
51
+ let [focusStrategy, setFocusStrategy] = useState<FocusStrategy>(null);
52
+
53
+ let open = useCallback((focusStrategy: FocusStrategy = null) => {
54
+ setFocusStrategy(focusStrategy);
55
+ UNSTABLE_openSubmenu(triggerKey, submenuLevel);
56
+ }, [UNSTABLE_openSubmenu, submenuLevel, triggerKey]);
57
+
58
+ let close = useCallback(() => {
59
+ setFocusStrategy(null);
60
+ UNSTABLE_closeSubmenu(triggerKey, submenuLevel);
61
+ }, [UNSTABLE_closeSubmenu, submenuLevel, triggerKey]);
62
+
63
+ let toggle = useCallback((focusStrategy: FocusStrategy = null) => {
64
+ setFocusStrategy(focusStrategy);
65
+ if (isOpen) {
66
+ close();
67
+ } else {
68
+ open(focusStrategy);
69
+ }
70
+ }, [close, open, isOpen]);
71
+
72
+ return useMemo(() => ({
73
+ focusStrategy,
74
+ isOpen,
75
+ open,
76
+ close,
77
+ closeAll,
78
+ submenuLevel,
79
+ // TODO: Placeholders that aren't used but give us parity with OverlayTriggerState so we can use this in Popover. Refactor if we update Popover via
80
+ // https://github.com/adobe/react-spectrum/pull/4976#discussion_r1336472863
81
+ setOpen: () => {},
82
+ toggle
83
+ }), [isOpen, open, close, closeAll, focusStrategy, toggle, submenuLevel]);
84
+ }