@svar-ui/react-menu 2.4.3 → 2.5.1

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/index.es.js CHANGED
@@ -1,294 +1,317 @@
1
- import { jsxs as N, jsx as o, Fragment as L } from "react/jsx-runtime";
2
- import { useRef as I, useCallback as v, useMemo as Z, useState as p, useEffect as $, Fragment as Y, forwardRef as S, useImperativeHandle as E } from "react";
3
- import { calculatePosition as j, clickOutside as B, id as T } from "@svar-ui/lib-dom";
4
- import { Portal as F } from "@svar-ui/react-core";
5
- function K(r, n) {
6
- return r.map((t) => {
7
- const e = n(t);
8
- return t.data && t.data.length && (e.data = K(t.data, n)), e;
1
+ import { jsxs as N, jsx as o, Fragment as R } from "react/jsx-runtime";
2
+ import { useRef as D, useCallback as k, useMemo as E, useState as C, useEffect as A, Fragment as B, forwardRef as X, useImperativeHandle as F } from "react";
3
+ import { uid as T, setID as W, calculatePosition as J, clickOutside as _, locateID as nn } from "@svar-ui/lib-dom";
4
+ import { Portal as G } from "@svar-ui/react-core";
5
+ function K(c, t) {
6
+ return c.map((n) => {
7
+ const l = t(n);
8
+ return n.data && n.data.length && (l.data = K(n.data, t)), l;
9
9
  });
10
10
  }
11
- function q(r, n) {
12
- const t = [];
13
- return r.forEach((e) => {
14
- if (e.data) {
15
- const a = q(e.data, n);
16
- a.length && t.push({ ...e, data: a });
11
+ function U(c, t) {
12
+ const n = [];
13
+ return c.forEach((l) => {
14
+ if (l.data) {
15
+ const a = U(l.data, t);
16
+ a.length && n.push({ ...l, data: a });
17
17
  } else
18
- n(e) && t.push(e);
19
- }), t;
18
+ t(l) && n.push(l);
19
+ }), n;
20
20
  }
21
- let W = 1;
22
- function G(r) {
23
- return K(r, (n) => {
24
- const t = { ...n, id: n.id || W++ };
25
- return t.type && (t.comp = t.type), t;
21
+ function V(c) {
22
+ return K(c, (t) => {
23
+ const n = { ...t, id: t.id || T() };
24
+ return n.type && (n.comp = n.type), n;
26
25
  });
27
26
  }
28
- const U = {};
29
- function J(r) {
30
- return U[r] || r;
27
+ const Y = {};
28
+ function en(c) {
29
+ return Y[c] || c;
31
30
  }
32
- function st(r, n) {
33
- U[r] = n;
31
+ function sn(c, t) {
32
+ Y[c] = t;
34
33
  }
35
- function _({ onClick: r, onShow: n, option: t }) {
36
- const e = I(null), a = v(() => {
37
- n(t.data ? t.id : !1, e.current);
38
- }, [n, t]), f = Z(() => t && t.comp ? J(t.comp) : null, [t]);
34
+ function tn({ onClick: c, onShow: t, option: n }) {
35
+ const l = D(null), a = k(() => {
36
+ t(n.data ? n.id : !1, l.current);
37
+ }, [t, n]), w = k((u) => {
38
+ if (n.data) {
39
+ u.stopPropagation(), t(n.id, l.current);
40
+ return;
41
+ }
42
+ c(u);
43
+ }, [c, t, n]), f = E(() => n && n.comp ? en(n.comp) : null, [n]);
39
44
  return /* @__PURE__ */ N(
40
45
  "div",
41
46
  {
42
- ref: e,
43
- className: `wx-cDCz9rZQ wx-option ${t.css || ""} ${t.disabled ? "wx-disabled" : ""}`,
44
- "data-id": t.id,
47
+ ref: l,
48
+ className: `wx-cDCz9rZQ wx-option ${n.css || ""} ${n.disabled ? "wx-disabled" : ""}`,
49
+ "data-id": W(n.id),
45
50
  onMouseEnter: a,
46
- onClick: r,
51
+ onClick: w,
47
52
  children: [
48
- t.icon ? /* @__PURE__ */ o("i", { className: `wx-cDCz9rZQ wx-icon ${t.icon}` }) : null,
49
- t.comp ? f ? /* @__PURE__ */ o(f, { item: t, option: t }) : null : /* @__PURE__ */ N("span", { className: "wx-cDCz9rZQ wx-value", children: [
53
+ n.icon ? /* @__PURE__ */ o("i", { className: `wx-cDCz9rZQ wx-icon ${n.icon}` }) : null,
54
+ n.comp ? f ? /* @__PURE__ */ o(f, { item: n, option: n }) : null : /* @__PURE__ */ N("span", { className: "wx-cDCz9rZQ wx-value", children: [
50
55
  " ",
51
- t.text,
56
+ n.text,
52
57
  " "
53
58
  ] }),
54
- t.subtext ? /* @__PURE__ */ o("span", { className: "wx-cDCz9rZQ wx-subtext", children: t.subtext }) : null,
55
- t.data ? /* @__PURE__ */ o("i", { className: "wx-cDCz9rZQ wx-sub-icon wxi-angle-right" }) : null
59
+ n.subtext ? /* @__PURE__ */ o("span", { className: "wx-cDCz9rZQ wx-subtext", children: n.subtext }) : null,
60
+ n.data ? /* @__PURE__ */ o("i", { className: "wx-cDCz9rZQ wx-sub-icon wxi-angle-right" }) : null
56
61
  ]
57
62
  }
58
63
  );
59
64
  }
60
- function H({
61
- options: r,
62
- left: n = 0,
63
- top: t = 0,
64
- at: e = "bottom",
65
+ function S({
66
+ options: c,
67
+ left: t = 0,
68
+ top: n = 0,
69
+ at: l = "bottom",
65
70
  parent: a = null,
66
- mount: f = null,
67
- context: x = null,
68
- css: d = "",
69
- onClick: c
71
+ mount: w = null,
72
+ context: f = null,
73
+ css: u = "",
74
+ onClick: h,
75
+ onCancel: i
70
76
  }) {
71
- const [m, w] = p(-1e4), [h, C] = p(-1e4), [g, s] = p(20), [l, b] = p(), P = I(null), [Q, z] = p(!1), [R, A] = p(null), D = v(() => {
72
- const i = j(P.current, a, e, n, t);
73
- i && (w(i.x), C(i.y), s(i.z), b(i.width));
74
- }, [a, e, n, t]);
75
- $(() => {
76
- f && f(D);
77
+ const [m, p] = C(-1e4), [g, x] = C(-1e4), [r, e] = C(20), [M, Z] = C(), $ = D(null), [P, y] = C(!1), [Q, H] = C(null), z = k(() => {
78
+ const s = J($.current, a, l, t, n);
79
+ s && (p(s.x), x(s.y), e(s.z), Z(s.width));
80
+ }, [a, l, t, n]);
81
+ A(() => {
82
+ w && w(z);
77
83
  }, []);
78
- const u = v(() => {
79
- z(!1);
80
- }, []), k = v(() => {
81
- c && c({ action: null, option: null });
82
- }, [c]), M = v((i, O) => {
83
- z(i), A(O);
84
- }, []), y = Z(() => G(r), [r]);
85
- return $(() => {
86
- D();
87
- }, [a, D]), $(() => {
88
- if (P.current)
89
- return B(P.current, { callback: k, modal: !0 }).destroy;
90
- }, [k]), /* @__PURE__ */ o(
84
+ const O = k(() => {
85
+ y(!1);
86
+ }, []), d = k((s, I) => {
87
+ y(s), H(I);
88
+ }, []), L = E(() => V(c), [c]);
89
+ A(() => {
90
+ z();
91
+ }, [a, z]);
92
+ const b = D(h), v = D(i);
93
+ return A(() => void (b.current = h), [h]), A(() => void (v.current = i), [i]), A(() => {
94
+ if (!$.current) return;
95
+ function s(I) {
96
+ v.current ? v.current(I) : b.current && b.current({ action: null, option: null });
97
+ }
98
+ return _($.current, { callback: s, modal: !0 }).destroy;
99
+ }, []), /* @__PURE__ */ o(
91
100
  "div",
92
101
  {
93
- ref: P,
102
+ ref: $,
94
103
  "data-wx-menu": "true",
95
- className: `wx-XMmAGqVx wx-menu ${d}`,
104
+ className: `wx-XMmAGqVx wx-menu ${u}`,
96
105
  style: {
97
106
  position: "absolute",
98
- top: h + "px",
107
+ top: g + "px",
99
108
  left: m + "px",
100
- width: l,
101
- zIndex: g
109
+ width: M,
110
+ zIndex: r
102
111
  },
103
- onMouseLeave: u,
104
- children: y.map((i) => /* @__PURE__ */ N(Y, { children: [
105
- i.comp === "separator" ? /* @__PURE__ */ o("div", { className: "wx-XMmAGqVx wx-separator" }) : /* @__PURE__ */ o(
106
- _,
112
+ onMouseLeave: O,
113
+ children: L.map((s) => /* @__PURE__ */ N(B, { children: [
114
+ s.comp === "separator" ? /* @__PURE__ */ o("div", { className: "wx-XMmAGqVx wx-separator" }) : /* @__PURE__ */ o(
115
+ tn,
107
116
  {
108
- option: i,
109
- onShow: M,
110
- onClick: (O) => {
111
- if (!i.data && !O.defaultPrevented) {
112
- const X = { context: x, action: i, option: i, event: O };
113
- i.handler && i.handler(X), c && c(X), O.stopPropagation();
117
+ option: s,
118
+ onShow: d,
119
+ onClick: (I) => {
120
+ if (!s.data && !I.defaultPrevented) {
121
+ const q = { context: f, action: s, option: s, event: I };
122
+ s.handler && s.handler(q), h && h(q), I.stopPropagation();
114
123
  }
115
124
  }
116
125
  }
117
126
  ),
118
- i.data && Q === i.id ? /* @__PURE__ */ o(
119
- H,
127
+ s.data && P === s.id ? /* @__PURE__ */ o(
128
+ S,
120
129
  {
121
- css: d,
122
- options: i.data,
130
+ css: u,
131
+ options: s.data,
123
132
  at: "right-overlap",
124
- parent: R,
125
- context: x,
126
- onClick: c
133
+ parent: Q,
134
+ context: f,
135
+ onClick: h,
136
+ onCancel: i
127
137
  }
128
138
  ) : null
129
- ] }, i.id))
139
+ ] }, s.id))
130
140
  }
131
141
  );
132
142
  }
133
- const V = S(function(n, t) {
143
+ const j = X(function(t, n) {
134
144
  const {
135
- options: e,
145
+ options: l,
136
146
  at: a = "bottom",
137
- resolver: f = null,
138
- dataKey: x = "contextId",
139
- filter: d = null,
140
- css: c = "",
141
- children: m,
142
- onClick: w
143
- } = n, [h, C] = p(null), [g, s] = p(null), [l, b] = p(0), [P, Q] = p(0), z = Z(() => h !== null && d ? q(e, (u) => d(u, h)) : e, [h, d, e]), R = v(
144
- (u) => {
145
- s(null), w && w(u);
146
- },
147
- [w]
148
- ), A = v((u, k) => {
149
- let M = null;
150
- for (; u && u.dataset && !M; )
151
- M = u.dataset[k], u = u.parentNode;
152
- return M ? T(M) : null;
153
- }, []), D = v(
154
- (u, k) => {
155
- if (!u) {
156
- s(null);
147
+ resolver: w = null,
148
+ dataKey: f = "contextId",
149
+ filter: u = null,
150
+ css: h = "",
151
+ children: i,
152
+ onClick: m
153
+ } = t, [p, g] = C(null), [x, r] = C(null), [e, M] = C(0), [Z, $] = C(0), P = D(null), y = E(
154
+ () => `data-${f.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase()}`,
155
+ [f]
156
+ ), Q = E(() => p !== null && u ? U(l, (d) => u(d, p)) : l, [p, u, l]);
157
+ function H(d) {
158
+ r(null), m && m(d);
159
+ }
160
+ function z(d) {
161
+ if (P.current === d) {
162
+ P.current = null;
163
+ return;
164
+ }
165
+ r(null), m && m({ action: null, option: null });
166
+ }
167
+ const O = k(
168
+ (d, L) => {
169
+ if (!d) {
170
+ r(null);
157
171
  return;
158
172
  }
159
- if (u.defaultPrevented) return;
160
- const M = u.target;
161
- if (M && M.dataset && M.dataset.menuIgnore) return;
162
- b(u.clientX + 1), Q(u.clientY + 1);
163
- let y = typeof k < "u" ? k : A(M, x);
164
- f && (y = f(y, u), !y) || (C(y), s(M), u.preventDefault());
173
+ if (d.defaultPrevented) return;
174
+ const b = d.target;
175
+ if (b && b.dataset && b.dataset.menuIgnore) return;
176
+ if (x && x === b) {
177
+ r(null), d.preventDefault();
178
+ return;
179
+ }
180
+ M(d.clientX + 1), $(d.clientY + 1);
181
+ let v = typeof L < "u" ? L : nn(b, y);
182
+ w && (v = w(v, d), !v) || (g(v), r(b), P.current = d.nativeEvent || d, d.preventDefault());
165
183
  },
166
- [x, A, f]
184
+ [x, y, w]
167
185
  );
168
- return E(t, () => ({ show: D }), [D]), /* @__PURE__ */ N(L, { children: [
169
- m ? /* @__PURE__ */ o("span", { onClick: D, "data-menu-ignore": "true", children: typeof m == "function" ? m() : m }) : null,
170
- g ? /* @__PURE__ */ o(F, { children: /* @__PURE__ */ o(
171
- H,
186
+ return F(n, () => ({ show: O }), [O]), /* @__PURE__ */ N(R, { children: [
187
+ i ? /* @__PURE__ */ o("span", { onClick: O, "data-menu-ignore": "true", children: typeof i == "function" ? i() : i }) : null,
188
+ x ? /* @__PURE__ */ o(G, { children: /* @__PURE__ */ o(
189
+ S,
172
190
  {
173
- css: c,
191
+ css: h,
174
192
  at: a,
175
- top: P,
176
- left: l,
177
- parent: g,
178
- context: h,
179
- onClick: R,
180
- options: z
193
+ top: Z,
194
+ left: e,
195
+ parent: x,
196
+ context: p,
197
+ onClick: H,
198
+ onCancel: z,
199
+ options: Q
181
200
  },
182
- g
201
+ x
183
202
  ) }) : null
184
203
  ] });
185
204
  });
186
- function rt(r) {
187
- const { css: n = "", menuCss: t = "", options: e, onClick: a } = r, f = Z(() => G(e), [e]), [x, d] = p(!1), [c, m] = p([]), w = I(null);
188
- function h(s) {
189
- d(null), a && a(s);
205
+ function an(c) {
206
+ const { css: t = "", menuCss: n = "", options: l, onClick: a } = c, w = E(() => V(l), [l]), [f, u] = C(!1), [h, i] = C([]), m = D(null);
207
+ function p(r) {
208
+ u(null), a && a(r);
190
209
  }
191
- function C(s, l, b) {
192
- l.data && l.data.length ? x && b ? d(null) : (m(l.data), d(l.id), w.current.show(s, l)) : (w.current.show(null), b ? (a && a({ action: l, option: l }), d(null)) : d(-1));
210
+ function g(r, e, M) {
211
+ e.data && e.data.length ? f && M ? u(null) : (i(e.data), u(e.id), m.current.show(r, e)) : (m.current.show(null), M ? (a && a({ action: e, option: e }), u(null)) : u(-1));
193
212
  }
194
- function g(s, l) {
195
- x && C(s, l);
213
+ function x(r, e) {
214
+ f && g(r, e);
196
215
  }
197
- return /* @__PURE__ */ N(L, { children: [
198
- /* @__PURE__ */ o("div", { className: `wx-UfhPCLL4 wx-menubar ${n}`, children: f.map((s) => /* @__PURE__ */ o(
216
+ return /* @__PURE__ */ N(R, { children: [
217
+ /* @__PURE__ */ o("div", { className: `wx-UfhPCLL4 wx-menubar ${t}`, children: w.map((r) => /* @__PURE__ */ o(
199
218
  "button",
200
219
  {
201
- className: `wx-UfhPCLL4 wx-option ${x === s.id ? "wx-active" : ""} ${s.disabled ? "wx-disabled" : ""}`,
202
- onMouseEnter: (l) => g(l, s),
203
- onClick: (l) => C(l, s, !0),
204
- children: s.text
220
+ className: `wx-UfhPCLL4 wx-option ${f === r.id ? "wx-active" : ""} ${r.disabled ? "wx-disabled" : ""}`,
221
+ onMouseEnter: (e) => x(e, r),
222
+ onClick: (e) => g(e, r, !0),
223
+ children: r.text
205
224
  },
206
- s.id
225
+ r.id
207
226
  )) }),
208
227
  /* @__PURE__ */ o(
209
- V,
228
+ j,
210
229
  {
211
- css: t,
212
- onClick: h,
213
- options: c,
214
- ref: w
230
+ css: n,
231
+ onClick: p,
232
+ options: h,
233
+ ref: m
215
234
  }
216
235
  )
217
236
  ] });
218
237
  }
219
- const at = S(function(n, t) {
220
- const { options: e, at: a = "bottom", css: f = "", children: x, onClick: d } = n, [c, m] = p(null);
221
- function w(l) {
222
- m(null), d && d(l);
238
+ const on = X(function(t, n) {
239
+ const { options: l, at: a = "bottom", css: w = "", children: f, onClick: u } = t, [h, i] = C(null), m = D(null);
240
+ function p(e) {
241
+ i(null), u && u(e);
242
+ }
243
+ function g(e) {
244
+ if (m.current === e) {
245
+ m.current = null;
246
+ return;
247
+ }
248
+ i(null), u && u({ action: null, option: null });
223
249
  }
224
- const h = v((l) => {
225
- m(l.target), l.preventDefault();
250
+ const x = k((e) => {
251
+ i(e.target), m.current = e.nativeEvent || e, e.preventDefault();
226
252
  }, []);
227
- E(t, () => ({ show: h }), [h]);
228
- function C(l) {
229
- let b = l.target;
230
- for (; !b.dataset.menuIgnore; )
231
- m(b), b = b.parentNode;
253
+ F(n, () => ({ show: x }), [x]);
254
+ function r(e) {
255
+ let M = e.target;
256
+ for (; !M.dataset.menuIgnore; )
257
+ i(M), M = M.parentNode;
232
258
  }
233
- const g = I(0), s = I(c);
234
- return $(() => {
235
- s.current !== c && (g.current += 1, s.current = c);
236
- }, [c]), /* @__PURE__ */ N(L, { children: [
237
- /* @__PURE__ */ o("span", { onClick: C, "data-menu-ignore": "true", children: x }),
238
- c ? /* @__PURE__ */ o(F, { children: /* @__PURE__ */ o(
239
- H,
259
+ return /* @__PURE__ */ N(R, { children: [
260
+ /* @__PURE__ */ o("span", { onClick: r, "data-menu-ignore": "true", children: f }),
261
+ h ? /* @__PURE__ */ o(G, { children: /* @__PURE__ */ o(
262
+ S,
240
263
  {
241
- css: f,
264
+ css: w,
242
265
  at: a,
243
- parent: c,
244
- options: e,
245
- onClick: w
246
- },
247
- g.current
266
+ parent: h,
267
+ options: l,
268
+ onClick: p,
269
+ onCancel: g
270
+ }
248
271
  ) }) : null
249
272
  ] });
250
- }), ct = S(function(n, t) {
273
+ }), dn = X(function(t, n) {
251
274
  const {
252
- options: e,
275
+ options: l,
253
276
  at: a = "bottom",
254
- resolver: f = null,
255
- dataKey: x = "contextId",
256
- filter: d = null,
257
- css: c = "",
258
- children: m,
259
- onClick: w
260
- } = n, h = I(null), C = v((g, s) => {
261
- h.current.show(g, s);
277
+ resolver: w = null,
278
+ dataKey: f = "contextId",
279
+ filter: u = null,
280
+ css: h = "",
281
+ children: i,
282
+ onClick: m
283
+ } = t, p = D(null), g = k((x, r) => {
284
+ p.current.show(x, r);
262
285
  }, []);
263
- return E(
264
- t,
286
+ return F(
287
+ n,
265
288
  () => ({
266
- show: C
289
+ show: g
267
290
  }),
268
- [C]
269
- ), /* @__PURE__ */ N(L, { children: [
270
- m ? /* @__PURE__ */ o("span", { onContextMenu: C, "data-menu-ignore": "true", children: m }) : null,
291
+ [g]
292
+ ), /* @__PURE__ */ N(R, { children: [
293
+ i ? /* @__PURE__ */ o("span", { onContextMenu: g, "data-menu-ignore": "true", children: i }) : null,
271
294
  /* @__PURE__ */ o(
272
- V,
295
+ j,
273
296
  {
274
- css: c,
297
+ css: h,
275
298
  at: a,
276
- options: e,
277
- resolver: f,
278
- dataKey: x,
279
- filter: d,
280
- ref: h,
281
- onClick: w
299
+ options: l,
300
+ resolver: w,
301
+ dataKey: f,
302
+ filter: u,
303
+ ref: p,
304
+ onClick: m
282
305
  }
283
306
  )
284
307
  ] });
285
308
  });
286
309
  export {
287
- V as ActionMenu,
288
- ct as ContextMenu,
289
- at as DropDownMenu,
290
- H as Menu,
291
- rt as MenuBar,
292
- st as registerMenuItem
310
+ j as ActionMenu,
311
+ dn as ContextMenu,
312
+ on as DropDownMenu,
313
+ S as Menu,
314
+ an as MenuBar,
315
+ sn as registerMenuItem
293
316
  };
294
317
  //# sourceMappingURL=index.es.js.map