@yamada-ui/menu 0.1.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/chunk-6ALPY7K4.mjs +111 -0
- package/dist/chunk-OFZGIB33.mjs +374 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +504 -0
- package/dist/index.mjs +28 -0
- package/dist/menu-button.d.ts +7 -0
- package/dist/menu-button.js +100 -0
- package/dist/menu-button.mjs +7 -0
- package/dist/menu-divider.d.ts +7 -0
- package/dist/menu-divider.js +58 -0
- package/dist/menu-divider.mjs +7 -0
- package/dist/menu-group.d.ts +7 -0
- package/dist/menu-group.js +73 -0
- package/dist/menu-group.mjs +7 -0
- package/dist/menu-item.d.ts +23 -0
- package/dist/menu-item.js +202 -0
- package/dist/menu-item.mjs +9 -0
- package/dist/menu-list.d.ts +7 -0
- package/dist/menu-list.js +110 -0
- package/dist/menu-list.mjs +7 -0
- package/dist/menu-option-group.d.ts +15 -0
- package/dist/menu-option-group.js +276 -0
- package/dist/menu-option-group.mjs +7 -0
- package/dist/menu.d.ts +2134 -0
- package/dist/menu.js +138 -0
- package/dist/menu.mjs +12 -0
- package/package.json +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Hirotomo Yamada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @yamada-ui/menu
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
$ pnpm add @yamada-ui/menu
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
$ yarn add @yamada-ui/menu
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
or
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
$ npm install @yamada-ui/menu
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Contribution
|
|
22
|
+
|
|
23
|
+
Wouldn't you like to contribute? That's amazing! We have prepared a [contribution guide](https://github.com/hirotomoyamada/yamada-ui/blob/main/CONTRIBUTING.md) to assist you.
|
|
24
|
+
|
|
25
|
+
## Licence
|
|
26
|
+
|
|
27
|
+
This package is licensed under the terms of the
|
|
28
|
+
[MIT license](https://github.com/hirotomoyamada/yamada-ui/blob/main/LICENSE).
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// src/menu.tsx
|
|
2
|
+
import { useMultiComponentStyle, omitThemeProps } from "@yamada-ui/core";
|
|
3
|
+
import { Popover } from "@yamada-ui/popover";
|
|
4
|
+
import { createDescendant } from "@yamada-ui/use-descendant";
|
|
5
|
+
import { useDisclosure } from "@yamada-ui/use-disclosure";
|
|
6
|
+
import { createContext, useUnmountEffect, useUpdateEffect } from "@yamada-ui/utils";
|
|
7
|
+
import { useCallback, useRef, useState } from "react";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
var {
|
|
10
|
+
DescendantsContextProvider,
|
|
11
|
+
useDescendantsContext: useMenuDescendantsContext,
|
|
12
|
+
useDescendants,
|
|
13
|
+
useDescendant: useMenuDescendant
|
|
14
|
+
} = createDescendant();
|
|
15
|
+
var [MenuProvider, useMenu] = createContext({
|
|
16
|
+
name: "MenuContext",
|
|
17
|
+
errorMessage: `useMenu returned is 'undefined'. Seems you forgot to wrap the components in "<Menu />"`
|
|
18
|
+
});
|
|
19
|
+
var Menu = (props) => {
|
|
20
|
+
const [styles, mergedProps] = useMultiComponentStyle("Menu", props);
|
|
21
|
+
const {
|
|
22
|
+
initialFocusRef,
|
|
23
|
+
closeOnSelect = true,
|
|
24
|
+
placement = "bottom-start",
|
|
25
|
+
duration = 0.2,
|
|
26
|
+
...rest
|
|
27
|
+
} = omitThemeProps(mergedProps);
|
|
28
|
+
const descendants = useDescendants();
|
|
29
|
+
const [focusedIndex, setFocusedIndex] = useState(-1);
|
|
30
|
+
const menuRef = useRef(null);
|
|
31
|
+
const timeoutIds = useRef(/* @__PURE__ */ new Set([]));
|
|
32
|
+
const onFocusMenu = useCallback(() => {
|
|
33
|
+
requestAnimationFrame(() => {
|
|
34
|
+
var _a;
|
|
35
|
+
return (_a = menuRef.current) == null ? void 0 : _a.focus({ preventScroll: false });
|
|
36
|
+
});
|
|
37
|
+
}, []);
|
|
38
|
+
const onFocusFirstItem = useCallback(() => {
|
|
39
|
+
const id = setTimeout(() => {
|
|
40
|
+
if (initialFocusRef)
|
|
41
|
+
return;
|
|
42
|
+
const first = descendants.enabledfirstValue();
|
|
43
|
+
if (first)
|
|
44
|
+
setFocusedIndex(first.index);
|
|
45
|
+
});
|
|
46
|
+
timeoutIds.current.add(id);
|
|
47
|
+
}, [descendants, initialFocusRef]);
|
|
48
|
+
const onFocusLastItem = useCallback(() => {
|
|
49
|
+
const id = setTimeout(() => {
|
|
50
|
+
if (initialFocusRef)
|
|
51
|
+
return;
|
|
52
|
+
const last = descendants.enabledlastValue();
|
|
53
|
+
if (last)
|
|
54
|
+
setFocusedIndex(last.index);
|
|
55
|
+
});
|
|
56
|
+
timeoutIds.current.add(id);
|
|
57
|
+
}, [descendants, initialFocusRef]);
|
|
58
|
+
const onOpenInternal = useCallback(() => {
|
|
59
|
+
var _a;
|
|
60
|
+
(_a = rest.onOpen) == null ? void 0 : _a.call(rest);
|
|
61
|
+
onFocusMenu();
|
|
62
|
+
}, [onFocusMenu, rest]);
|
|
63
|
+
const [isOpen, onOpen, onClose] = useDisclosure({ ...props, onOpen: onOpenInternal });
|
|
64
|
+
useUpdateEffect(() => {
|
|
65
|
+
if (!isOpen)
|
|
66
|
+
setFocusedIndex(-1);
|
|
67
|
+
}, [isOpen]);
|
|
68
|
+
useUnmountEffect(() => {
|
|
69
|
+
timeoutIds.current.forEach((id) => clearTimeout(id));
|
|
70
|
+
timeoutIds.current.clear();
|
|
71
|
+
});
|
|
72
|
+
return /* @__PURE__ */ jsx(DescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ jsx(
|
|
73
|
+
MenuProvider,
|
|
74
|
+
{
|
|
75
|
+
value: {
|
|
76
|
+
isOpen,
|
|
77
|
+
onOpen,
|
|
78
|
+
onClose,
|
|
79
|
+
onFocusFirstItem,
|
|
80
|
+
onFocusLastItem,
|
|
81
|
+
closeOnSelect,
|
|
82
|
+
focusedIndex,
|
|
83
|
+
setFocusedIndex,
|
|
84
|
+
menuRef,
|
|
85
|
+
styles
|
|
86
|
+
},
|
|
87
|
+
children: /* @__PURE__ */ jsx(
|
|
88
|
+
Popover,
|
|
89
|
+
{
|
|
90
|
+
...{
|
|
91
|
+
...rest,
|
|
92
|
+
isOpen,
|
|
93
|
+
onOpen,
|
|
94
|
+
onClose,
|
|
95
|
+
placement,
|
|
96
|
+
duration,
|
|
97
|
+
initialFocusRef,
|
|
98
|
+
closeOnButton: false
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
) });
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export {
|
|
107
|
+
useMenuDescendantsContext,
|
|
108
|
+
useMenuDescendant,
|
|
109
|
+
useMenu,
|
|
110
|
+
Menu
|
|
111
|
+
};
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useMenu,
|
|
3
|
+
useMenuDescendant,
|
|
4
|
+
useMenuDescendantsContext
|
|
5
|
+
} from "./chunk-6ALPY7K4.mjs";
|
|
6
|
+
|
|
7
|
+
// src/menu-button.tsx
|
|
8
|
+
import { ui as ui4, forwardRef as forwardRef6 } from "@yamada-ui/core";
|
|
9
|
+
import { PopoverTrigger } from "@yamada-ui/popover";
|
|
10
|
+
import { ariaAttr as ariaAttr2, cx as cx6, dataAttr, funcAll, handlerAll as handlerAll2 } from "@yamada-ui/utils";
|
|
11
|
+
import { useCallback as useCallback4 } from "react";
|
|
12
|
+
|
|
13
|
+
// src/menu-list.tsx
|
|
14
|
+
import { forwardRef } from "@yamada-ui/core";
|
|
15
|
+
import { PopoverContent } from "@yamada-ui/popover";
|
|
16
|
+
import { cx, handlerAll, mergeRefs } from "@yamada-ui/utils";
|
|
17
|
+
import { useCallback } from "react";
|
|
18
|
+
import { jsx } from "react/jsx-runtime";
|
|
19
|
+
var MenuList = forwardRef(({ className, ...rest }, ref) => {
|
|
20
|
+
const { menuRef, focusedIndex, setFocusedIndex, onClose, styles } = useMenu();
|
|
21
|
+
const descendants = useMenuDescendantsContext();
|
|
22
|
+
const onNext = useCallback(() => {
|
|
23
|
+
const next = descendants.enabledNextValue(focusedIndex);
|
|
24
|
+
if (next)
|
|
25
|
+
setFocusedIndex(next.index);
|
|
26
|
+
}, [descendants, focusedIndex, setFocusedIndex]);
|
|
27
|
+
const onPrev = useCallback(() => {
|
|
28
|
+
const prev = descendants.enabledPrevValue(focusedIndex);
|
|
29
|
+
if (prev)
|
|
30
|
+
setFocusedIndex(prev.index);
|
|
31
|
+
}, [descendants, focusedIndex, setFocusedIndex]);
|
|
32
|
+
const onFirst = useCallback(() => {
|
|
33
|
+
const first = descendants.enabledfirstValue();
|
|
34
|
+
if (first)
|
|
35
|
+
setFocusedIndex(first.index);
|
|
36
|
+
}, [descendants, setFocusedIndex]);
|
|
37
|
+
const onLast = useCallback(() => {
|
|
38
|
+
const last = descendants.enabledlastValue();
|
|
39
|
+
if (last)
|
|
40
|
+
setFocusedIndex(last.index);
|
|
41
|
+
}, [descendants, setFocusedIndex]);
|
|
42
|
+
const onKeyDown = useCallback(
|
|
43
|
+
(ev) => {
|
|
44
|
+
const actions = {
|
|
45
|
+
Tab: (ev2) => ev2.preventDefault(),
|
|
46
|
+
Escape: onClose,
|
|
47
|
+
ArrowDown: focusedIndex === -1 ? onFirst : onNext,
|
|
48
|
+
ArrowUp: focusedIndex === -1 ? onLast : onPrev,
|
|
49
|
+
Home: onFirst,
|
|
50
|
+
End: onLast
|
|
51
|
+
};
|
|
52
|
+
const action = actions[ev.key];
|
|
53
|
+
if (!action)
|
|
54
|
+
return;
|
|
55
|
+
ev.preventDefault();
|
|
56
|
+
action(ev);
|
|
57
|
+
},
|
|
58
|
+
[focusedIndex, onClose, onFirst, onLast, onNext, onPrev]
|
|
59
|
+
);
|
|
60
|
+
const css = { ...styles.list };
|
|
61
|
+
return /* @__PURE__ */ jsx(
|
|
62
|
+
PopoverContent,
|
|
63
|
+
{
|
|
64
|
+
ref: mergeRefs(menuRef, ref),
|
|
65
|
+
className: cx("ui-menu-list", className),
|
|
66
|
+
role: "menu",
|
|
67
|
+
tabIndex: -1,
|
|
68
|
+
__css: css,
|
|
69
|
+
...rest,
|
|
70
|
+
onKeyDown: handlerAll(rest.onKeyDown, onKeyDown)
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// src/menu-item.tsx
|
|
76
|
+
import { ui, forwardRef as forwardRef2 } from "@yamada-ui/core";
|
|
77
|
+
import { useClickable } from "@yamada-ui/use-clickable";
|
|
78
|
+
import {
|
|
79
|
+
ariaAttr,
|
|
80
|
+
cx as cx2,
|
|
81
|
+
isActiveElement,
|
|
82
|
+
isHTMLElement,
|
|
83
|
+
mergeRefs as mergeRefs2,
|
|
84
|
+
useUpdateEffect
|
|
85
|
+
} from "@yamada-ui/utils";
|
|
86
|
+
import { useCallback as useCallback2, useRef } from "react";
|
|
87
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
88
|
+
var isTargetMenuItem = (target) => {
|
|
89
|
+
var _a;
|
|
90
|
+
return isHTMLElement(target) && !!((_a = target == null ? void 0 : target.getAttribute("role")) == null ? void 0 : _a.startsWith("menu-item"));
|
|
91
|
+
};
|
|
92
|
+
var MenuItem = forwardRef2(
|
|
93
|
+
({
|
|
94
|
+
as,
|
|
95
|
+
className,
|
|
96
|
+
type,
|
|
97
|
+
isDisabled,
|
|
98
|
+
isFocusable,
|
|
99
|
+
closeOnSelect: customCloseOnSelect,
|
|
100
|
+
icon,
|
|
101
|
+
command,
|
|
102
|
+
children,
|
|
103
|
+
...props
|
|
104
|
+
}, ref) => {
|
|
105
|
+
const {
|
|
106
|
+
focusedIndex,
|
|
107
|
+
setFocusedIndex,
|
|
108
|
+
isOpen,
|
|
109
|
+
onClose,
|
|
110
|
+
closeOnSelect: generalCloseOnSelect,
|
|
111
|
+
menuRef,
|
|
112
|
+
styles
|
|
113
|
+
} = useMenu();
|
|
114
|
+
const trulyDisabled = isDisabled && !isFocusable;
|
|
115
|
+
const buttonRef = useRef(null);
|
|
116
|
+
const { index, register } = useMenuDescendant({ disabled: trulyDisabled });
|
|
117
|
+
const isFocused = index === focusedIndex;
|
|
118
|
+
const onClick = useCallback2(
|
|
119
|
+
(ev) => {
|
|
120
|
+
var _a;
|
|
121
|
+
(_a = props.onClick) == null ? void 0 : _a.call(props, ev);
|
|
122
|
+
if (!isTargetMenuItem(ev.currentTarget))
|
|
123
|
+
return;
|
|
124
|
+
if (customCloseOnSelect != null ? customCloseOnSelect : generalCloseOnSelect)
|
|
125
|
+
onClose();
|
|
126
|
+
},
|
|
127
|
+
[props, customCloseOnSelect, generalCloseOnSelect, onClose]
|
|
128
|
+
);
|
|
129
|
+
const onFocus = useCallback2(
|
|
130
|
+
(ev) => {
|
|
131
|
+
var _a;
|
|
132
|
+
(_a = props.onFocus) == null ? void 0 : _a.call(props, ev);
|
|
133
|
+
setFocusedIndex(index);
|
|
134
|
+
},
|
|
135
|
+
[props, setFocusedIndex, index]
|
|
136
|
+
);
|
|
137
|
+
const rest = useClickable({
|
|
138
|
+
onClick,
|
|
139
|
+
onFocus,
|
|
140
|
+
ref: mergeRefs2(register, buttonRef, ref),
|
|
141
|
+
isDisabled,
|
|
142
|
+
isFocusable
|
|
143
|
+
});
|
|
144
|
+
useUpdateEffect(() => {
|
|
145
|
+
if (!isOpen)
|
|
146
|
+
return;
|
|
147
|
+
if (isFocused && !trulyDisabled && buttonRef.current) {
|
|
148
|
+
requestAnimationFrame(() => {
|
|
149
|
+
var _a;
|
|
150
|
+
return (_a = buttonRef.current) == null ? void 0 : _a.focus();
|
|
151
|
+
});
|
|
152
|
+
} else if (menuRef.current && !isActiveElement(menuRef.current)) {
|
|
153
|
+
menuRef.current.focus();
|
|
154
|
+
}
|
|
155
|
+
}, [isFocused, trulyDisabled, menuRef, isOpen]);
|
|
156
|
+
type = as || type ? type != null ? type : void 0 : "button";
|
|
157
|
+
children = icon || command ? /* @__PURE__ */ jsx2(ui.span, { style: { pointerEvents: "none", flex: 1 }, children }) : children;
|
|
158
|
+
const css = {
|
|
159
|
+
textDecoration: "none",
|
|
160
|
+
color: "inherit",
|
|
161
|
+
userSelect: "none",
|
|
162
|
+
display: "flex",
|
|
163
|
+
width: "100%",
|
|
164
|
+
alignItems: "center",
|
|
165
|
+
textAlign: "start",
|
|
166
|
+
flex: "0 0 auto",
|
|
167
|
+
outline: 0,
|
|
168
|
+
gap: "0.75rem",
|
|
169
|
+
...styles.item
|
|
170
|
+
};
|
|
171
|
+
return /* @__PURE__ */ jsxs(
|
|
172
|
+
ui.button,
|
|
173
|
+
{
|
|
174
|
+
...props,
|
|
175
|
+
...rest,
|
|
176
|
+
as,
|
|
177
|
+
type,
|
|
178
|
+
role: "menu-item",
|
|
179
|
+
tabInde: isFocused ? 0 : -1,
|
|
180
|
+
className: cx2("ui-menu-item", className),
|
|
181
|
+
__css: css,
|
|
182
|
+
children: [
|
|
183
|
+
icon ? /* @__PURE__ */ jsx2(MenuIcon, { children: icon }) : null,
|
|
184
|
+
children,
|
|
185
|
+
command ? /* @__PURE__ */ jsx2(MenuCommand, { children: command }) : null
|
|
186
|
+
]
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
var MenuOptionItem = forwardRef2(
|
|
192
|
+
({ className, icon, isChecked, closeOnSelect = false, children, ...rest }, ref) => {
|
|
193
|
+
return /* @__PURE__ */ jsxs(
|
|
194
|
+
MenuItem,
|
|
195
|
+
{
|
|
196
|
+
ref,
|
|
197
|
+
className: cx2("ui-menu-option-item", className),
|
|
198
|
+
"aria-checked": ariaAttr(isChecked),
|
|
199
|
+
closeOnSelect,
|
|
200
|
+
...rest,
|
|
201
|
+
children: [
|
|
202
|
+
icon !== null ? /* @__PURE__ */ jsx2(MenuIcon, { opacity: isChecked ? 1 : 0, children: icon || /* @__PURE__ */ jsx2(CheckIcon, {}) }) : null,
|
|
203
|
+
children
|
|
204
|
+
]
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
var MenuIcon = forwardRef2(({ className, ...rest }, ref) => {
|
|
210
|
+
const { styles } = useMenu();
|
|
211
|
+
const css = {
|
|
212
|
+
flexShrink: 0,
|
|
213
|
+
display: "inline-flex",
|
|
214
|
+
justifyContent: "center",
|
|
215
|
+
alignItems: "center",
|
|
216
|
+
fontSize: "0.85em",
|
|
217
|
+
...styles.icon
|
|
218
|
+
};
|
|
219
|
+
return /* @__PURE__ */ jsx2(ui.span, { ref, className: cx2("ui-menu-icon", className), __css: css, ...rest });
|
|
220
|
+
});
|
|
221
|
+
var MenuCommand = forwardRef2(({ className, ...rest }, ref) => {
|
|
222
|
+
const { styles } = useMenu();
|
|
223
|
+
const css = { ...styles.command };
|
|
224
|
+
return /* @__PURE__ */ jsx2(ui.span, { ref, className: cx2("ui-menu-command", className), __css: css, ...rest });
|
|
225
|
+
});
|
|
226
|
+
var CheckIcon = () => /* @__PURE__ */ jsx2("svg", { viewBox: "0 0 14 14", width: "1em", height: "1em", children: /* @__PURE__ */ jsx2(
|
|
227
|
+
"polygon",
|
|
228
|
+
{
|
|
229
|
+
fill: "currentColor",
|
|
230
|
+
points: "5.5 11.9993304 14 3.49933039 12.5 2 5.5 8.99933039 1.5 4.9968652 0 6.49933039"
|
|
231
|
+
}
|
|
232
|
+
) });
|
|
233
|
+
|
|
234
|
+
// src/menu-group.tsx
|
|
235
|
+
import { ui as ui2, forwardRef as forwardRef3 } from "@yamada-ui/core";
|
|
236
|
+
import { cx as cx3 } from "@yamada-ui/utils";
|
|
237
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
238
|
+
var MenuGroup = forwardRef3(
|
|
239
|
+
({ className, title, children, ...rest }, ref) => {
|
|
240
|
+
const { styles } = useMenu();
|
|
241
|
+
const css = { ...styles.group };
|
|
242
|
+
return /* @__PURE__ */ jsxs2(
|
|
243
|
+
ui2.div,
|
|
244
|
+
{
|
|
245
|
+
ref,
|
|
246
|
+
className: cx3("ui-menu-group", className),
|
|
247
|
+
role: "group",
|
|
248
|
+
__css: css,
|
|
249
|
+
...rest,
|
|
250
|
+
children: [
|
|
251
|
+
title ? /* @__PURE__ */ jsx3(ui2.span, { className: cx3("ui-menu-group-title"), __css: styles.groupTitle, children: title }) : null,
|
|
252
|
+
children
|
|
253
|
+
]
|
|
254
|
+
}
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
// src/menu-option-group.tsx
|
|
260
|
+
import { useControllableState } from "@yamada-ui/use-controllable-state";
|
|
261
|
+
import { cx as cx4, getValidChildren, omitObject, isArray } from "@yamada-ui/utils";
|
|
262
|
+
import { cloneElement, forwardRef as forwardRef4, useCallback as useCallback3 } from "react";
|
|
263
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
264
|
+
var MenuOptionGroup = forwardRef4(
|
|
265
|
+
({ className, defaultValue, type, children, ...rest }, ref) => {
|
|
266
|
+
const isRadio = type === "radio";
|
|
267
|
+
defaultValue = isRadio ? "" : [];
|
|
268
|
+
const [value, setValue] = useControllableState({
|
|
269
|
+
...rest,
|
|
270
|
+
defaultValue
|
|
271
|
+
});
|
|
272
|
+
const onChange = useCallback3(
|
|
273
|
+
(selectedValue) => {
|
|
274
|
+
if (isRadio && typeof value === "string")
|
|
275
|
+
setValue(selectedValue);
|
|
276
|
+
if (!isRadio && isArray(value)) {
|
|
277
|
+
const nextValue = value.includes(selectedValue) ? value.filter((item) => item !== selectedValue) : value.concat(selectedValue);
|
|
278
|
+
setValue(nextValue);
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
[isRadio, value, setValue]
|
|
282
|
+
);
|
|
283
|
+
const validChildren = getValidChildren(children);
|
|
284
|
+
const cloneChildren = validChildren.map((child) => {
|
|
285
|
+
if (child.type !== MenuOptionItem)
|
|
286
|
+
return child;
|
|
287
|
+
const onClick = (ev) => {
|
|
288
|
+
var _a, _b;
|
|
289
|
+
onChange(child.props.value);
|
|
290
|
+
(_b = (_a = child.props).onClick) == null ? void 0 : _b.call(_a, ev);
|
|
291
|
+
};
|
|
292
|
+
const isChecked = !isRadio && isArray(value) ? value.includes(child.props.value) : child.props.value === value;
|
|
293
|
+
return cloneElement(child, { type, onClick, isChecked });
|
|
294
|
+
});
|
|
295
|
+
return /* @__PURE__ */ jsx4(
|
|
296
|
+
MenuGroup,
|
|
297
|
+
{
|
|
298
|
+
ref,
|
|
299
|
+
className: cx4("ui-menu-option-group", className),
|
|
300
|
+
...omitObject(rest, ["value", "onChange"]),
|
|
301
|
+
children: cloneChildren
|
|
302
|
+
}
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
);
|
|
306
|
+
MenuOptionGroup.displayName = "MenuOptionGroup";
|
|
307
|
+
|
|
308
|
+
// src/menu-divider.tsx
|
|
309
|
+
import { ui as ui3, forwardRef as forwardRef5 } from "@yamada-ui/core";
|
|
310
|
+
import { cx as cx5 } from "@yamada-ui/utils";
|
|
311
|
+
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
312
|
+
var MenuDivider = forwardRef5(({ className, ...rest }, ref) => {
|
|
313
|
+
const { styles } = useMenu();
|
|
314
|
+
const css = { ...styles.divider };
|
|
315
|
+
return /* @__PURE__ */ jsx5(ui3.hr, { ref, className: cx5("ui-menu-divider", className), __css: css, ...rest });
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// src/menu-button.tsx
|
|
319
|
+
import { jsx as jsx6 } from "react/jsx-runtime";
|
|
320
|
+
var MenuButton = forwardRef6(
|
|
321
|
+
({ className, children, as: As, ...rest }, ref) => {
|
|
322
|
+
const { isOpen, onOpen, onFocusFirstItem, onFocusLastItem } = useMenu();
|
|
323
|
+
const onKeyDown = useCallback4(
|
|
324
|
+
(ev) => {
|
|
325
|
+
const actions = {
|
|
326
|
+
Enter: funcAll(onOpen, onFocusFirstItem),
|
|
327
|
+
ArrowDown: funcAll(onOpen, onFocusFirstItem),
|
|
328
|
+
ArrowUp: funcAll(onOpen, onFocusLastItem)
|
|
329
|
+
};
|
|
330
|
+
const action = actions[ev.key];
|
|
331
|
+
if (!action)
|
|
332
|
+
return;
|
|
333
|
+
ev.preventDefault();
|
|
334
|
+
ev.stopPropagation();
|
|
335
|
+
action();
|
|
336
|
+
},
|
|
337
|
+
[onFocusFirstItem, onFocusLastItem, onOpen]
|
|
338
|
+
);
|
|
339
|
+
const Component = As || Button;
|
|
340
|
+
return /* @__PURE__ */ jsx6(PopoverTrigger, { children: /* @__PURE__ */ jsx6(
|
|
341
|
+
Component,
|
|
342
|
+
{
|
|
343
|
+
ref,
|
|
344
|
+
className: cx6("ui-menu-button", className),
|
|
345
|
+
...rest,
|
|
346
|
+
"data-active": dataAttr(isOpen),
|
|
347
|
+
"aria-expanded": ariaAttr2(isOpen),
|
|
348
|
+
onKeyDown: handlerAll2(rest.onKeyDown, onKeyDown),
|
|
349
|
+
children: /* @__PURE__ */ jsx6(ui4.span, { __css: { pointerEvents: "none", flex: "1 1 auto", minW: 0 }, children })
|
|
350
|
+
}
|
|
351
|
+
) });
|
|
352
|
+
}
|
|
353
|
+
);
|
|
354
|
+
var Button = forwardRef6((rest, ref) => {
|
|
355
|
+
const { styles } = useMenu();
|
|
356
|
+
const css = {
|
|
357
|
+
display: "inline-flex",
|
|
358
|
+
appearance: "none",
|
|
359
|
+
alignItems: "center",
|
|
360
|
+
outline: 0,
|
|
361
|
+
...styles.button
|
|
362
|
+
};
|
|
363
|
+
return /* @__PURE__ */ jsx6(ui4.button, { ref, __css: css, ...rest });
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
export {
|
|
367
|
+
MenuButton,
|
|
368
|
+
MenuList,
|
|
369
|
+
MenuItem,
|
|
370
|
+
MenuOptionItem,
|
|
371
|
+
MenuGroup,
|
|
372
|
+
MenuOptionGroup,
|
|
373
|
+
MenuDivider
|
|
374
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { Menu, MenuProps, useMenu, useMenuDescendant, useMenuDescendantsContext } from './menu.js';
|
|
2
|
+
export { MenuButton, MenuButtonProps } from './menu-button.js';
|
|
3
|
+
export { MenuList, MenuListProps } from './menu-list.js';
|
|
4
|
+
export { MenuItem, MenuItemProps, MenuOptionItem, MenuOptionItemProps } from './menu-item.js';
|
|
5
|
+
export { MenuGroup, MenuGroupProps } from './menu-group.js';
|
|
6
|
+
export { MenuOptionGroup, MenuOptionGroupProps } from './menu-option-group.js';
|
|
7
|
+
export { MenuDivider, MenuDividerProps } from './menu-divider.js';
|
|
8
|
+
import '@yamada-ui/core';
|
|
9
|
+
import '@yamada-ui/popover';
|
|
10
|
+
import 'react';
|