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