@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.
@@ -0,0 +1,276 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/menu-option-group.tsx
21
+ var menu_option_group_exports = {};
22
+ __export(menu_option_group_exports, {
23
+ MenuOptionGroup: () => MenuOptionGroup
24
+ });
25
+ module.exports = __toCommonJS(menu_option_group_exports);
26
+ var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
27
+ var import_utils4 = require("@yamada-ui/utils");
28
+ var import_react3 = require("react");
29
+
30
+ // src/menu-group.tsx
31
+ var import_core3 = require("@yamada-ui/core");
32
+ var import_utils3 = require("@yamada-ui/utils");
33
+
34
+ // src/menu.tsx
35
+ var import_core = require("@yamada-ui/core");
36
+ var import_popover = require("@yamada-ui/popover");
37
+ var import_use_descendant = require("@yamada-ui/use-descendant");
38
+ var import_use_disclosure = require("@yamada-ui/use-disclosure");
39
+ var import_utils = require("@yamada-ui/utils");
40
+ var import_react = require("react");
41
+ var import_jsx_runtime = require("react/jsx-runtime");
42
+ var {
43
+ DescendantsContextProvider,
44
+ useDescendantsContext: useMenuDescendantsContext,
45
+ useDescendants,
46
+ useDescendant: useMenuDescendant
47
+ } = (0, import_use_descendant.createDescendant)();
48
+ var [MenuProvider, useMenu] = (0, import_utils.createContext)({
49
+ name: "MenuContext",
50
+ errorMessage: `useMenu returned is 'undefined'. Seems you forgot to wrap the components in "<Menu />"`
51
+ });
52
+
53
+ // src/menu-item.tsx
54
+ var import_core2 = require("@yamada-ui/core");
55
+ var import_use_clickable = require("@yamada-ui/use-clickable");
56
+ var import_utils2 = require("@yamada-ui/utils");
57
+ var import_react2 = require("react");
58
+ var import_jsx_runtime2 = require("react/jsx-runtime");
59
+ var isTargetMenuItem = (target) => {
60
+ var _a;
61
+ return (0, import_utils2.isHTMLElement)(target) && !!((_a = target == null ? void 0 : target.getAttribute("role")) == null ? void 0 : _a.startsWith("menu-item"));
62
+ };
63
+ var MenuItem = (0, import_core2.forwardRef)(
64
+ ({
65
+ as,
66
+ className,
67
+ type,
68
+ isDisabled,
69
+ isFocusable,
70
+ closeOnSelect: customCloseOnSelect,
71
+ icon,
72
+ command,
73
+ children,
74
+ ...props
75
+ }, ref) => {
76
+ const {
77
+ focusedIndex,
78
+ setFocusedIndex,
79
+ isOpen,
80
+ onClose,
81
+ closeOnSelect: generalCloseOnSelect,
82
+ menuRef,
83
+ styles
84
+ } = useMenu();
85
+ const trulyDisabled = isDisabled && !isFocusable;
86
+ const buttonRef = (0, import_react2.useRef)(null);
87
+ const { index, register } = useMenuDescendant({ disabled: trulyDisabled });
88
+ const isFocused = index === focusedIndex;
89
+ const onClick = (0, import_react2.useCallback)(
90
+ (ev) => {
91
+ var _a;
92
+ (_a = props.onClick) == null ? void 0 : _a.call(props, ev);
93
+ if (!isTargetMenuItem(ev.currentTarget))
94
+ return;
95
+ if (customCloseOnSelect != null ? customCloseOnSelect : generalCloseOnSelect)
96
+ onClose();
97
+ },
98
+ [props, customCloseOnSelect, generalCloseOnSelect, onClose]
99
+ );
100
+ const onFocus = (0, import_react2.useCallback)(
101
+ (ev) => {
102
+ var _a;
103
+ (_a = props.onFocus) == null ? void 0 : _a.call(props, ev);
104
+ setFocusedIndex(index);
105
+ },
106
+ [props, setFocusedIndex, index]
107
+ );
108
+ const rest = (0, import_use_clickable.useClickable)({
109
+ onClick,
110
+ onFocus,
111
+ ref: (0, import_utils2.mergeRefs)(register, buttonRef, ref),
112
+ isDisabled,
113
+ isFocusable
114
+ });
115
+ (0, import_utils2.useUpdateEffect)(() => {
116
+ if (!isOpen)
117
+ return;
118
+ if (isFocused && !trulyDisabled && buttonRef.current) {
119
+ requestAnimationFrame(() => {
120
+ var _a;
121
+ return (_a = buttonRef.current) == null ? void 0 : _a.focus();
122
+ });
123
+ } else if (menuRef.current && !(0, import_utils2.isActiveElement)(menuRef.current)) {
124
+ menuRef.current.focus();
125
+ }
126
+ }, [isFocused, trulyDisabled, menuRef, isOpen]);
127
+ type = as || type ? type != null ? type : void 0 : "button";
128
+ children = icon || command ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_core2.ui.span, { style: { pointerEvents: "none", flex: 1 }, children }) : children;
129
+ const css = {
130
+ textDecoration: "none",
131
+ color: "inherit",
132
+ userSelect: "none",
133
+ display: "flex",
134
+ width: "100%",
135
+ alignItems: "center",
136
+ textAlign: "start",
137
+ flex: "0 0 auto",
138
+ outline: 0,
139
+ gap: "0.75rem",
140
+ ...styles.item
141
+ };
142
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
143
+ import_core2.ui.button,
144
+ {
145
+ ...props,
146
+ ...rest,
147
+ as,
148
+ type,
149
+ role: "menu-item",
150
+ tabInde: isFocused ? 0 : -1,
151
+ className: (0, import_utils2.cx)("ui-menu-item", className),
152
+ __css: css,
153
+ children: [
154
+ icon ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MenuIcon, { children: icon }) : null,
155
+ children,
156
+ command ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MenuCommand, { children: command }) : null
157
+ ]
158
+ }
159
+ );
160
+ }
161
+ );
162
+ var MenuOptionItem = (0, import_core2.forwardRef)(
163
+ ({ className, icon, isChecked, closeOnSelect = false, children, ...rest }, ref) => {
164
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
165
+ MenuItem,
166
+ {
167
+ ref,
168
+ className: (0, import_utils2.cx)("ui-menu-option-item", className),
169
+ "aria-checked": (0, import_utils2.ariaAttr)(isChecked),
170
+ closeOnSelect,
171
+ ...rest,
172
+ children: [
173
+ icon !== null ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MenuIcon, { opacity: isChecked ? 1 : 0, children: icon || /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CheckIcon, {}) }) : null,
174
+ children
175
+ ]
176
+ }
177
+ );
178
+ }
179
+ );
180
+ var MenuIcon = (0, import_core2.forwardRef)(({ className, ...rest }, ref) => {
181
+ const { styles } = useMenu();
182
+ const css = {
183
+ flexShrink: 0,
184
+ display: "inline-flex",
185
+ justifyContent: "center",
186
+ alignItems: "center",
187
+ fontSize: "0.85em",
188
+ ...styles.icon
189
+ };
190
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_core2.ui.span, { ref, className: (0, import_utils2.cx)("ui-menu-icon", className), __css: css, ...rest });
191
+ });
192
+ var MenuCommand = (0, import_core2.forwardRef)(({ className, ...rest }, ref) => {
193
+ const { styles } = useMenu();
194
+ const css = { ...styles.command };
195
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_core2.ui.span, { ref, className: (0, import_utils2.cx)("ui-menu-command", className), __css: css, ...rest });
196
+ });
197
+ var CheckIcon = () => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("svg", { viewBox: "0 0 14 14", width: "1em", height: "1em", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
198
+ "polygon",
199
+ {
200
+ fill: "currentColor",
201
+ points: "5.5 11.9993304 14 3.49933039 12.5 2 5.5 8.99933039 1.5 4.9968652 0 6.49933039"
202
+ }
203
+ ) });
204
+
205
+ // src/menu-group.tsx
206
+ var import_jsx_runtime3 = require("react/jsx-runtime");
207
+ var MenuGroup = (0, import_core3.forwardRef)(
208
+ ({ className, title, children, ...rest }, ref) => {
209
+ const { styles } = useMenu();
210
+ const css = { ...styles.group };
211
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
212
+ import_core3.ui.div,
213
+ {
214
+ ref,
215
+ className: (0, import_utils3.cx)("ui-menu-group", className),
216
+ role: "group",
217
+ __css: css,
218
+ ...rest,
219
+ children: [
220
+ title ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core3.ui.span, { className: (0, import_utils3.cx)("ui-menu-group-title"), __css: styles.groupTitle, children: title }) : null,
221
+ children
222
+ ]
223
+ }
224
+ );
225
+ }
226
+ );
227
+
228
+ // src/menu-option-group.tsx
229
+ var import_jsx_runtime4 = require("react/jsx-runtime");
230
+ var MenuOptionGroup = (0, import_react3.forwardRef)(
231
+ ({ className, defaultValue, type, children, ...rest }, ref) => {
232
+ const isRadio = type === "radio";
233
+ defaultValue = isRadio ? "" : [];
234
+ const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
235
+ ...rest,
236
+ defaultValue
237
+ });
238
+ const onChange = (0, import_react3.useCallback)(
239
+ (selectedValue) => {
240
+ if (isRadio && typeof value === "string")
241
+ setValue(selectedValue);
242
+ if (!isRadio && (0, import_utils4.isArray)(value)) {
243
+ const nextValue = value.includes(selectedValue) ? value.filter((item) => item !== selectedValue) : value.concat(selectedValue);
244
+ setValue(nextValue);
245
+ }
246
+ },
247
+ [isRadio, value, setValue]
248
+ );
249
+ const validChildren = (0, import_utils4.getValidChildren)(children);
250
+ const cloneChildren = validChildren.map((child) => {
251
+ if (child.type !== MenuOptionItem)
252
+ return child;
253
+ const onClick = (ev) => {
254
+ var _a, _b;
255
+ onChange(child.props.value);
256
+ (_b = (_a = child.props).onClick) == null ? void 0 : _b.call(_a, ev);
257
+ };
258
+ const isChecked = !isRadio && (0, import_utils4.isArray)(value) ? value.includes(child.props.value) : child.props.value === value;
259
+ return (0, import_react3.cloneElement)(child, { type, onClick, isChecked });
260
+ });
261
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
262
+ MenuGroup,
263
+ {
264
+ ref,
265
+ className: (0, import_utils4.cx)("ui-menu-option-group", className),
266
+ ...(0, import_utils4.omitObject)(rest, ["value", "onChange"]),
267
+ children: cloneChildren
268
+ }
269
+ );
270
+ }
271
+ );
272
+ MenuOptionGroup.displayName = "MenuOptionGroup";
273
+ // Annotate the CommonJS export names for ESM import in node:
274
+ 0 && (module.exports = {
275
+ MenuOptionGroup
276
+ });
@@ -0,0 +1,7 @@
1
+ import {
2
+ MenuOptionGroup
3
+ } from "./chunk-OFZGIB33.mjs";
4
+ import "./chunk-6ALPY7K4.mjs";
5
+ export {
6
+ MenuOptionGroup
7
+ };