react-smart-date-compare 1.0.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,935 @@
1
+ import Re, { useMemo as De, useState as D, useEffect as G, useRef as ve } from "react";
2
+ import { createPortal as Te } from "react-dom";
3
+ import { differenceInCalendarDays as Ee, subDays as U, subYears as ye, startOfMonth as ae, endOfMonth as ue, startOfWeek as se, endOfWeek as fe, eachDayOfInterval as Pe, format as Z, isBefore as K, startOfDay as B, isAfter as be, isSameDay as Q, isSameMonth as Me, startOfToday as Ce, endOfToday as Oe, startOfYear as Ne, subMonths as de, addMonths as ge } from "date-fns";
4
+ var ie = { exports: {} }, re = {};
5
+ var ke;
6
+ function Ae() {
7
+ if (ke) return re;
8
+ ke = 1;
9
+ var r = /* @__PURE__ */ Symbol.for("react.transitional.element"), n = /* @__PURE__ */ Symbol.for("react.fragment");
10
+ function a(p, l, i) {
11
+ var m = null;
12
+ if (i !== void 0 && (m = "" + i), l.key !== void 0 && (m = "" + l.key), "key" in l) {
13
+ i = {};
14
+ for (var y in l)
15
+ y !== "key" && (i[y] = l[y]);
16
+ } else i = l;
17
+ return l = i.ref, {
18
+ $$typeof: r,
19
+ type: p,
20
+ key: m,
21
+ ref: l !== void 0 ? l : null,
22
+ props: i
23
+ };
24
+ }
25
+ return re.Fragment = n, re.jsx = a, re.jsxs = a, re;
26
+ }
27
+ var ne = {};
28
+ var we;
29
+ function $e() {
30
+ return we || (we = 1, process.env.NODE_ENV !== "production" && (function() {
31
+ function r(e) {
32
+ if (e == null) return null;
33
+ if (typeof e == "function")
34
+ return e.$$typeof === H ? null : e.displayName || e.name || null;
35
+ if (typeof e == "string") return e;
36
+ switch (e) {
37
+ case W:
38
+ return "Fragment";
39
+ case g:
40
+ return "Profiler";
41
+ case $:
42
+ return "StrictMode";
43
+ case V:
44
+ return "Suspense";
45
+ case J:
46
+ return "SuspenseList";
47
+ case b:
48
+ return "Activity";
49
+ }
50
+ if (typeof e == "object")
51
+ switch (typeof e.tag == "number" && console.error(
52
+ "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
53
+ ), e.$$typeof) {
54
+ case Y:
55
+ return "Portal";
56
+ case c:
57
+ return e.displayName || "Context";
58
+ case z:
59
+ return (e._context.displayName || "Context") + ".Consumer";
60
+ case w:
61
+ var s = e.render;
62
+ return e = e.displayName, e || (e = s.displayName || s.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
63
+ case u:
64
+ return s = e.displayName || null, s !== null ? s : r(e.type) || "Memo";
65
+ case k:
66
+ s = e._payload, e = e._init;
67
+ try {
68
+ return r(e(s));
69
+ } catch {
70
+ }
71
+ }
72
+ return null;
73
+ }
74
+ function n(e) {
75
+ return "" + e;
76
+ }
77
+ function a(e) {
78
+ try {
79
+ n(e);
80
+ var s = !1;
81
+ } catch {
82
+ s = !0;
83
+ }
84
+ if (s) {
85
+ s = console;
86
+ var o = s.error, f = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
87
+ return o.call(
88
+ s,
89
+ "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
90
+ f
91
+ ), n(e);
92
+ }
93
+ }
94
+ function p(e) {
95
+ if (e === W) return "<>";
96
+ if (typeof e == "object" && e !== null && e.$$typeof === k)
97
+ return "<...>";
98
+ try {
99
+ var s = r(e);
100
+ return s ? "<" + s + ">" : "<...>";
101
+ } catch {
102
+ return "<...>";
103
+ }
104
+ }
105
+ function l() {
106
+ var e = d.A;
107
+ return e === null ? null : e.getOwner();
108
+ }
109
+ function i() {
110
+ return Error("react-stack-top-frame");
111
+ }
112
+ function m(e) {
113
+ if (C.call(e, "key")) {
114
+ var s = Object.getOwnPropertyDescriptor(e, "key").get;
115
+ if (s && s.isReactWarning) return !1;
116
+ }
117
+ return e.key !== void 0;
118
+ }
119
+ function y(e, s) {
120
+ function o() {
121
+ R || (R = !0, console.error(
122
+ "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
123
+ s
124
+ ));
125
+ }
126
+ o.isReactWarning = !0, Object.defineProperty(e, "key", {
127
+ get: o,
128
+ configurable: !0
129
+ });
130
+ }
131
+ function P() {
132
+ var e = r(this.type);
133
+ return E[e] || (E[e] = !0, console.error(
134
+ "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
135
+ )), e = this.props.ref, e !== void 0 ? e : null;
136
+ }
137
+ function I(e, s, o, f, x, A) {
138
+ var v = o.ref;
139
+ return e = {
140
+ $$typeof: q,
141
+ type: e,
142
+ key: s,
143
+ props: o,
144
+ _owner: f
145
+ }, (v !== void 0 ? v : null) !== null ? Object.defineProperty(e, "ref", {
146
+ enumerable: !1,
147
+ get: P
148
+ }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
149
+ configurable: !1,
150
+ enumerable: !1,
151
+ writable: !0,
152
+ value: 0
153
+ }), Object.defineProperty(e, "_debugInfo", {
154
+ configurable: !1,
155
+ enumerable: !1,
156
+ writable: !0,
157
+ value: null
158
+ }), Object.defineProperty(e, "_debugStack", {
159
+ configurable: !1,
160
+ enumerable: !1,
161
+ writable: !0,
162
+ value: x
163
+ }), Object.defineProperty(e, "_debugTask", {
164
+ configurable: !1,
165
+ enumerable: !1,
166
+ writable: !0,
167
+ value: A
168
+ }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
169
+ }
170
+ function M(e, s, o, f, x, A) {
171
+ var v = s.children;
172
+ if (v !== void 0)
173
+ if (f)
174
+ if (T(v)) {
175
+ for (f = 0; f < v.length; f++)
176
+ O(v[f]);
177
+ Object.freeze && Object.freeze(v);
178
+ } else
179
+ console.error(
180
+ "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
181
+ );
182
+ else O(v);
183
+ if (C.call(s, "key")) {
184
+ v = r(e);
185
+ var X = Object.keys(s).filter(function(oe) {
186
+ return oe !== "key";
187
+ });
188
+ f = 0 < X.length ? "{key: someKey, " + X.join(": ..., ") + ": ...}" : "{key: someKey}", F[v + f] || (X = 0 < X.length ? "{" + X.join(": ..., ") + ": ...}" : "{}", console.error(
189
+ `A props object containing a "key" prop is being spread into JSX:
190
+ let props = %s;
191
+ <%s {...props} />
192
+ React keys must be passed directly to JSX without using spread:
193
+ let props = %s;
194
+ <%s key={someKey} {...props} />`,
195
+ f,
196
+ v,
197
+ X,
198
+ v
199
+ ), F[v + f] = !0);
200
+ }
201
+ if (v = null, o !== void 0 && (a(o), v = "" + o), m(s) && (a(s.key), v = "" + s.key), "key" in s) {
202
+ o = {};
203
+ for (var ee in s)
204
+ ee !== "key" && (o[ee] = s[ee]);
205
+ } else o = s;
206
+ return v && y(
207
+ o,
208
+ typeof e == "function" ? e.displayName || e.name || "Unknown" : e
209
+ ), I(
210
+ e,
211
+ v,
212
+ o,
213
+ l(),
214
+ x,
215
+ A
216
+ );
217
+ }
218
+ function O(e) {
219
+ N(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e !== null && e.$$typeof === k && (e._payload.status === "fulfilled" ? N(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
220
+ }
221
+ function N(e) {
222
+ return typeof e == "object" && e !== null && e.$$typeof === q;
223
+ }
224
+ var _ = Re, q = /* @__PURE__ */ Symbol.for("react.transitional.element"), Y = /* @__PURE__ */ Symbol.for("react.portal"), W = /* @__PURE__ */ Symbol.for("react.fragment"), $ = /* @__PURE__ */ Symbol.for("react.strict_mode"), g = /* @__PURE__ */ Symbol.for("react.profiler"), z = /* @__PURE__ */ Symbol.for("react.consumer"), c = /* @__PURE__ */ Symbol.for("react.context"), w = /* @__PURE__ */ Symbol.for("react.forward_ref"), V = /* @__PURE__ */ Symbol.for("react.suspense"), J = /* @__PURE__ */ Symbol.for("react.suspense_list"), u = /* @__PURE__ */ Symbol.for("react.memo"), k = /* @__PURE__ */ Symbol.for("react.lazy"), b = /* @__PURE__ */ Symbol.for("react.activity"), H = /* @__PURE__ */ Symbol.for("react.client.reference"), d = _.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, C = Object.prototype.hasOwnProperty, T = Array.isArray, j = console.createTask ? console.createTask : function() {
225
+ return null;
226
+ };
227
+ _ = {
228
+ react_stack_bottom_frame: function(e) {
229
+ return e();
230
+ }
231
+ };
232
+ var R, E = {}, S = _.react_stack_bottom_frame.bind(
233
+ _,
234
+ i
235
+ )(), L = j(p(i)), F = {};
236
+ ne.Fragment = W, ne.jsx = function(e, s, o) {
237
+ var f = 1e4 > d.recentlyCreatedOwnerStacks++;
238
+ return M(
239
+ e,
240
+ s,
241
+ o,
242
+ !1,
243
+ f ? Error("react-stack-top-frame") : S,
244
+ f ? j(p(e)) : L
245
+ );
246
+ }, ne.jsxs = function(e, s, o) {
247
+ var f = 1e4 > d.recentlyCreatedOwnerStacks++;
248
+ return M(
249
+ e,
250
+ s,
251
+ o,
252
+ !0,
253
+ f ? Error("react-stack-top-frame") : S,
254
+ f ? j(p(e)) : L
255
+ );
256
+ };
257
+ })()), ne;
258
+ }
259
+ var je;
260
+ function Le() {
261
+ return je || (je = 1, process.env.NODE_ENV === "production" ? ie.exports = Ae() : ie.exports = $e()), ie.exports;
262
+ }
263
+ var t = Le();
264
+ const Ie = (r, n) => {
265
+ const a = Ee(n, r) + 1;
266
+ return {
267
+ compareStart: U(r, a),
268
+ compareEnd: U(n, a)
269
+ };
270
+ }, We = (r, n) => {
271
+ const a = Ee(n, r) + 1, p = a % 7, l = p === 0 ? a : a + (7 - p);
272
+ return {
273
+ compareStart: U(r, l),
274
+ compareEnd: U(n, l)
275
+ };
276
+ }, Ye = (r, n) => ({
277
+ compareStart: ye(r, 1),
278
+ compareEnd: ye(n, 1)
279
+ }), ze = (r, n) => {
280
+ const a = ae(r), p = ue(a), l = se(a, n), i = fe(p, n);
281
+ return Pe({
282
+ start: l,
283
+ end: i
284
+ });
285
+ };
286
+ function ce(r, n) {
287
+ const a = r.replace("#", ""), p = parseInt(a.substring(0, 2), 16), l = parseInt(a.substring(2, 4), 16), i = parseInt(a.substring(4, 6), 16);
288
+ return `rgba(${p},${l},${i},${n})`;
289
+ }
290
+ const Fe = ({
291
+ month: r,
292
+ startDate: n,
293
+ endDate: a,
294
+ compareStartDate: p,
295
+ compareEndDate: l,
296
+ hoverDate: i,
297
+ onDateClick: m,
298
+ onDateHover: y,
299
+ selecting: P,
300
+ anchorDate: I,
301
+ selectionVariant: M = "primary",
302
+ primaryColor: O = "#3b82f6",
303
+ compareColor: N = "#f97316",
304
+ minDate: _,
305
+ maxDate: q,
306
+ weekStartsOn: Y = 0,
307
+ disabledDates: W,
308
+ disableFuture: $,
309
+ disablePast: g,
310
+ locale: z,
311
+ classNames: c
312
+ }) => {
313
+ const w = De(() => ze(r, { weekStartsOn: Y }), [r, Y]), V = w.slice(0, 7), J = (u) => {
314
+ let k = !1;
315
+ if (_ && K(u, B(_)) && (k = !0), q && be(u, B(q)) && (k = !0), $ && be(u, B(/* @__PURE__ */ new Date())) && (k = !0), g && K(u, B(/* @__PURE__ */ new Date())) && (k = !0), W?.some((o) => Q(u, o)) && (k = !0), k)
316
+ return { className: "h-8 w-8 flex items-center justify-center text-sm text-gray-300 cursor-not-allowed pointer-events-none" };
317
+ if (!Me(u, r))
318
+ return { className: "invisible pointer-events-none h-8 w-8" };
319
+ const b = u.getTime(), d = ["h-8 w-8 flex items-center justify-center text-sm cursor-pointer relative z-10 transition-colors duration-75"];
320
+ let C = {};
321
+ c?.day && d.push(c.day);
322
+ let T = !1, j = !1, R = !1;
323
+ if (p && l) {
324
+ const o = B(p).getTime(), f = B(l).getTime(), x = Math.min(o, f), A = Math.max(o, f);
325
+ T = Q(u, p), j = Q(u, l), b >= x && b <= A && (R = !0);
326
+ }
327
+ let E = !1, S = !1, L = !1;
328
+ if (n && (E = Q(u, n)), a && (S = Q(u, a)), n && a) {
329
+ const o = B(n).getTime(), f = B(a).getTime();
330
+ b >= Math.min(o, f) && b <= Math.max(o, f) && (L = !0);
331
+ }
332
+ const F = I ?? (M === "primary" ? n : p);
333
+ let e = !1;
334
+ if (P && F && i) {
335
+ const o = B(F).getTime(), f = B(i).getTime();
336
+ b >= Math.min(o, f) && b <= Math.max(o, f) && (e = !0);
337
+ }
338
+ c?.dayInRange && L && !E && !S ? d.push(c.dayInRange) : c?.dayCompare && R && !T && !j ? d.push(c.dayCompare) : e ? M === "compare" ? C = { backgroundColor: ce(N, 0.12), color: N } : C = { backgroundColor: ce(O, 0.12), color: O } : R && !T && !j ? c?.dayCompare ? d.push(c.dayCompare) : C = { backgroundColor: ce(N, 0.12), color: N, fontWeight: 500 } : L && !E && !S ? c?.dayInRange ? d.push(c.dayInRange) : C = { backgroundColor: ce(O, 0.12), color: O } : !T && !j && !E && !S && d.push("hover:bg-gray-100 text-gray-700 rounded-full");
339
+ const s = P && F && i && (Q(u, F) || Q(u, i));
340
+ return (T || j || s && M === "compare") && (c?.dayCompare ? d.push(c.dayCompare) : C = { backgroundColor: N, color: "#fff", fontWeight: 600, boxShadow: "0 1px 4px rgba(0,0,0,0.15)" }, T && d.push("rounded-l-full"), j && d.push("rounded-r-full"), T && j && d.push("rounded-full"), s && d.push("rounded-full")), (E || S || s && M === "primary") && (c?.daySelected ? (d.push(c.daySelected), d.push("z-20")) : C = { backgroundColor: O, color: "#fff", fontWeight: 600, boxShadow: "0 1px 4px rgba(0,0,0,0.15)", zIndex: 20 }, E && d.push("rounded-l-full"), S && d.push("rounded-r-full"), E && S && d.push("rounded-full"), s && d.push("rounded-full")), { className: d.join(" "), style: Object.keys(C).length ? C : void 0 };
341
+ };
342
+ return /* @__PURE__ */ t.jsxs("div", { className: `w-64 select-none mr-2 ${c?.calendar || ""}`, children: [
343
+ /* @__PURE__ */ t.jsx("div", { className: "text-center text-sm font-semibold text-gray-800 mb-2 h-5 first-letter:uppercase", children: Z(r, "MMMM yyyy", { locale: z }) }),
344
+ /* @__PURE__ */ t.jsx("div", { className: "grid grid-cols-7 mb-1", children: V.map((u) => /* @__PURE__ */ t.jsx("div", { className: "text-center text-[10px] text-gray-400 font-medium h-6 flex items-center justify-center uppercase tracking-wide", children: Z(u, "cccccc", { locale: z }) }, u.toString())) }),
345
+ /* @__PURE__ */ t.jsx("div", { className: "grid grid-cols-7 gap-y-0.5", children: w.map((u) => {
346
+ const { className: k, style: b } = J(u);
347
+ return /* @__PURE__ */ t.jsx(
348
+ "div",
349
+ {
350
+ className: k + " text-xs",
351
+ style: b,
352
+ onClick: () => {
353
+ k.includes("pointer-events-none") || m(u);
354
+ },
355
+ onMouseEnter: () => y(u),
356
+ children: Z(u, "d")
357
+ },
358
+ u.toISOString()
359
+ );
360
+ }) })
361
+ ] });
362
+ }, Se = ({ preset: r, onSelect: n, activePreset: a, primaryColor: p = "#3b82f6", classNames: l, depth: i = 0 }) => {
363
+ const m = r.children && r.children.length > 0, y = m ? r.children.some((_) => _.value === a) : !1, [P, I] = D(y), M = a === r.value, O = l?.presetActive ? void 0 : { backgroundColor: `${p}15`, color: p, fontWeight: 600 }, N = l?.presetActive || "";
364
+ return m ? /* @__PURE__ */ t.jsxs("div", { children: [
365
+ /* @__PURE__ */ t.jsxs(
366
+ "button",
367
+ {
368
+ className: `w-full flex items-center justify-between px-3 py-2 rounded-md text-xs transition-colors cursor-pointer
369
+ ${y ? N : "text-gray-600 hover:bg-gray-100"}`,
370
+ style: {
371
+ paddingLeft: `${(i + 1) * 12}px`,
372
+ ...y ? O : {}
373
+ },
374
+ onClick: () => I((_) => !_),
375
+ type: "button",
376
+ "aria-expanded": P,
377
+ children: [
378
+ /* @__PURE__ */ t.jsx("span", { children: r.label }),
379
+ /* @__PURE__ */ t.jsx(
380
+ "svg",
381
+ {
382
+ className: `w-3 h-3 shrink-0 ml-1 text-gray-400 transition-transform duration-200 ${P ? "rotate-90" : ""}`,
383
+ fill: "none",
384
+ stroke: "currentColor",
385
+ viewBox: "0 0 24 24",
386
+ children: /* @__PURE__ */ t.jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M9 5l7 7-7 7" })
387
+ }
388
+ )
389
+ ]
390
+ }
391
+ ),
392
+ P && /* @__PURE__ */ t.jsx("div", { className: "flex flex-col gap-0.5 mt-0.5 ml-2 pl-2 border-l border-gray-200", children: r.children.map((_) => /* @__PURE__ */ t.jsx(
393
+ Se,
394
+ {
395
+ preset: _,
396
+ onSelect: n,
397
+ activePreset: a,
398
+ primaryColor: p,
399
+ classNames: l,
400
+ depth: i + 1
401
+ },
402
+ _.value
403
+ )) })
404
+ ] }) : /* @__PURE__ */ t.jsx(
405
+ "button",
406
+ {
407
+ className: `w-full text-left px-3 py-2 rounded-md text-xs transition-colors cursor-pointer
408
+ ${M ? N : "text-gray-600 hover:bg-gray-100"}`,
409
+ style: {
410
+ paddingLeft: i > 0 ? `${(i + 1) * 12}px` : void 0,
411
+ ...M ? O : {}
412
+ },
413
+ onClick: () => n(r.value),
414
+ type: "button",
415
+ children: r.label
416
+ }
417
+ );
418
+ }, Ue = ({
419
+ presets: r,
420
+ onSelect: n,
421
+ activePreset: a,
422
+ primaryColor: p,
423
+ classNames: l
424
+ }) => /* @__PURE__ */ t.jsx("aside", { className: "flex flex-col gap-0.5 p-2", children: r.map((i) => /* @__PURE__ */ t.jsx(
425
+ Se,
426
+ {
427
+ preset: i,
428
+ onSelect: n,
429
+ activePreset: a,
430
+ primaryColor: p,
431
+ classNames: l
432
+ },
433
+ i.value
434
+ )) }), Ve = ({
435
+ enabled: r,
436
+ mode: n,
437
+ onToggle: a,
438
+ onModeChange: p,
439
+ primaryColor: l = "#3b82f6",
440
+ compareColor: i = "#f97316"
441
+ }) => /* @__PURE__ */ t.jsxs("div", { className: "flex flex-col gap-2 text-sm text-gray-700", children: [
442
+ /* @__PURE__ */ t.jsxs("label", { className: "flex items-center gap-2 cursor-pointer select-none", children: [
443
+ /* @__PURE__ */ t.jsxs(
444
+ "div",
445
+ {
446
+ className: "relative inline-flex h-5 w-9 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out",
447
+ style: { backgroundColor: r ? l : "#d1d5db" },
448
+ onClick: () => a(!r),
449
+ children: [
450
+ /* @__PURE__ */ t.jsx(
451
+ "input",
452
+ {
453
+ type: "checkbox",
454
+ className: "sr-only",
455
+ checked: r,
456
+ onChange: (m) => a(m.target.checked)
457
+ }
458
+ ),
459
+ /* @__PURE__ */ t.jsx(
460
+ "span",
461
+ {
462
+ "aria-hidden": "true",
463
+ className: `pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${r ? "translate-x-4" : "translate-x-0"}`
464
+ }
465
+ )
466
+ ]
467
+ }
468
+ ),
469
+ /* @__PURE__ */ t.jsx("span", { className: "font-medium", children: "Compare" })
470
+ ] }),
471
+ r && /* @__PURE__ */ t.jsx("div", { className: "flex flex-col gap-1 mt-2 pl-2", style: { borderLeft: `2px solid ${l}40` }, children: [
472
+ { value: "previousPeriodMatchDay", label: "Previous period (match day of week)" },
473
+ { value: "previousPeriod", label: "Previous period" },
474
+ { value: "samePeriodLastYear", label: "Previous year" },
475
+ { value: "custom", label: "Custom" }
476
+ ].map((m) => {
477
+ const y = n === m.value;
478
+ return /* @__PURE__ */ t.jsx(
479
+ "button",
480
+ {
481
+ className: "text-left px-3 py-2 rounded-md text-xs transition-colors cursor-pointer",
482
+ style: y ? { backgroundColor: `${i}15`, color: i, fontWeight: 600 } : { color: "#6b7280" },
483
+ onClick: () => p(m.value),
484
+ children: m.label
485
+ },
486
+ m.value
487
+ );
488
+ }) })
489
+ ] }), Be = (r) => {
490
+ const n = Ce(), a = Oe();
491
+ switch (r) {
492
+ case "today":
493
+ return {
494
+ startDate: n,
495
+ endDate: a
496
+ };
497
+ case "yesterday":
498
+ return {
499
+ startDate: U(n, 1),
500
+ endDate: U(a, 1)
501
+ };
502
+ case "last7days":
503
+ return {
504
+ startDate: U(n, 6),
505
+ endDate: a
506
+ };
507
+ case "thisWeekSunToday":
508
+ return {
509
+ startDate: se(n, { weekStartsOn: 0 }),
510
+ endDate: a
511
+ };
512
+ case "thisWeekMonToday":
513
+ return {
514
+ startDate: se(n, { weekStartsOn: 1 }),
515
+ endDate: a
516
+ };
517
+ case "lastWeekSunSat":
518
+ const l = U(se(n, { weekStartsOn: 0 }), 7);
519
+ return {
520
+ startDate: l,
521
+ endDate: fe(l, { weekStartsOn: 0 })
522
+ };
523
+ case "lastWeekMonSun":
524
+ const i = U(se(n, { weekStartsOn: 1 }), 7);
525
+ return {
526
+ startDate: i,
527
+ endDate: fe(i, { weekStartsOn: 1 })
528
+ };
529
+ case "last28Days":
530
+ return {
531
+ startDate: U(n, 27),
532
+ endDate: a
533
+ };
534
+ case "last30days":
535
+ return {
536
+ startDate: U(n, 29),
537
+ endDate: a
538
+ };
539
+ case "thisMonth":
540
+ return {
541
+ startDate: ae(n),
542
+ endDate: ue(n)
543
+ };
544
+ case "lastMonth":
545
+ const m = de(n, 1);
546
+ return {
547
+ startDate: ae(m),
548
+ endDate: ue(m)
549
+ };
550
+ case "ytd":
551
+ return {
552
+ startDate: Ne(n),
553
+ endDate: a
554
+ };
555
+ default:
556
+ return { startDate: n, endDate: a };
557
+ }
558
+ }, qe = [
559
+ { label: "Today", value: "today" },
560
+ { label: "Yesterday", value: "yesterday" },
561
+ {
562
+ // Group header — no range of its own, just expands to show sub-options
563
+ label: "This week",
564
+ value: "__group_thisWeek",
565
+ children: [
566
+ { label: "Sun – Today", value: "thisWeekSunToday" },
567
+ { label: "Mon – Today", value: "thisWeekMonToday" }
568
+ ]
569
+ },
570
+ { label: "Last 7 days", value: "last7days" },
571
+ {
572
+ label: "Last week",
573
+ value: "__group_lastWeek",
574
+ children: [
575
+ { label: "Sun – Sat", value: "lastWeekSunSat" },
576
+ { label: "Mon – Sun", value: "lastWeekMonSun" }
577
+ ]
578
+ },
579
+ { label: "Last 28 days", value: "last28Days" },
580
+ { label: "Last 30 days", value: "last30days" },
581
+ { label: "This month", value: "thisMonth" },
582
+ { label: "Last month", value: "lastMonth" },
583
+ { label: "Year to Date", value: "ytd" },
584
+ { label: "Custom", value: "custom" }
585
+ ], Je = ({
586
+ initialRange: r,
587
+ initialCompareRange: n,
588
+ presets: a = qe,
589
+ onApply: p,
590
+ onCancel: l,
591
+ enableCompare: i = !1,
592
+ activeCompareMode: m = "previousPeriod",
593
+ primaryColor: y = "#3b82f6",
594
+ compareColor: P = "#f97316",
595
+ minDate: I,
596
+ maxDate: M,
597
+ weekStartsOn: O,
598
+ disabledDates: N,
599
+ disableFuture: _,
600
+ disablePast: q,
601
+ locale: Y,
602
+ labels: W,
603
+ classNames: $
604
+ }) => {
605
+ const [g, z] = D(r?.startDate), [c, w] = D(r?.endDate), [V, J] = D(null), [u, k] = D(!1), [b, H] = D(i), [d, C] = D(m);
606
+ G(() => {
607
+ H(i);
608
+ }, [i]), G(() => {
609
+ C(m);
610
+ }, [m]);
611
+ const [T, j] = D("primary");
612
+ G(() => {
613
+ j(b && d === "custom" ? "compare" : "primary");
614
+ }, [b, d]);
615
+ const [R, E] = D(n?.startDate), [S, L] = D(n?.endDate), [F, e] = D(""), [s, o] = D(() => {
616
+ const h = r?.endDate || /* @__PURE__ */ new Date();
617
+ return de(ae(h), 1);
618
+ });
619
+ G(() => {
620
+ if (b && !u && g && c && d !== "custom") {
621
+ let h;
622
+ d === "previousPeriod" ? h = Ie(g, c) : d === "previousPeriodMatchDay" ? h = We(g, c) : d === "samePeriodLastYear" && (h = Ye(g, c)), h && (E(h.compareStart), L(h.compareEnd));
623
+ }
624
+ }, [g, c, b, d, u]);
625
+ const f = (h) => {
626
+ I && K(h, I) || M && K(M, h) || (u ? (T === "compare" ? R && K(h, R) ? (E(h), L(R)) : L(h) : g && K(h, g) ? (z(h), w(g)) : w(h), k(!1)) : (e("custom"), T === "compare" ? (E(h), L(void 0)) : (z(h), w(void 0)), k(!0)));
627
+ }, x = (h) => {
628
+ u && J(h);
629
+ }, A = (h) => {
630
+ if (h.startsWith("__group_")) return;
631
+ const pe = (_e, he) => {
632
+ for (const le of _e) {
633
+ if (le.value === he) return le;
634
+ if (le.children) {
635
+ const xe = pe(le.children, he);
636
+ if (xe) return xe;
637
+ }
638
+ }
639
+ }, me = pe(a, h);
640
+ e(h);
641
+ let te;
642
+ me?.range ? te = me.range() : te = Be(h), z(te.startDate), w(te.endDate), k(!1), j("primary"), o(de(ae(te.endDate), 1));
643
+ }, v = () => o(de(s, 1)), X = () => o(ge(s, 1)), ee = () => {
644
+ g && c && p({ startDate: g, endDate: c }, b && R && S ? { startDate: R, endDate: S } : void 0, b ? d : void 0);
645
+ }, oe = (h) => /* @__PURE__ */ t.jsx(
646
+ Fe,
647
+ {
648
+ month: h,
649
+ startDate: g,
650
+ endDate: c,
651
+ compareStartDate: b ? R : void 0,
652
+ compareEndDate: b ? S : void 0,
653
+ hoverDate: V,
654
+ onDateClick: f,
655
+ onDateHover: x,
656
+ selecting: u,
657
+ anchorDate: u ? T === "primary" ? g : R : void 0,
658
+ selectionVariant: T,
659
+ minDate: I,
660
+ maxDate: M,
661
+ weekStartsOn: O,
662
+ disabledDates: N,
663
+ disableFuture: _,
664
+ disablePast: q,
665
+ locale: Y,
666
+ classNames: $,
667
+ primaryColor: y,
668
+ compareColor: P
669
+ }
670
+ );
671
+ return /* @__PURE__ */ t.jsxs("div", { className: `flex bg-white h-[450px] ${$?.root || ""}`, children: [
672
+ /* @__PURE__ */ t.jsxs("div", { className: `w-48 bg-gray-50/50 border-r border-gray-100 flex flex-col h-full shrink-0 ${$?.sidebar || ""}`, children: [
673
+ /* @__PURE__ */ t.jsx("div", { className: "flex-1 overflow-y-auto preset-scrollbar", children: /* @__PURE__ */ t.jsx(
674
+ Ue,
675
+ {
676
+ presets: a,
677
+ onSelect: A,
678
+ activePreset: F,
679
+ primaryColor: y,
680
+ classNames: $
681
+ }
682
+ ) }),
683
+ /* @__PURE__ */ t.jsx("div", { className: "p-3 border-t border-gray-200 bg-gray-50/80 backdrop-blur-sm", children: /* @__PURE__ */ t.jsx(
684
+ Ve,
685
+ {
686
+ enabled: b,
687
+ mode: d,
688
+ onToggle: H,
689
+ onModeChange: C,
690
+ primaryColor: y,
691
+ compareColor: P
692
+ }
693
+ ) })
694
+ ] }),
695
+ /* @__PURE__ */ t.jsxs("div", { className: "flex flex-col flex-1 h-full overflow-hidden", children: [
696
+ /* @__PURE__ */ t.jsxs("div", { className: "flex justify-between items-center px-4 py-2 border-b border-gray-100 shrink-0", children: [
697
+ /* @__PURE__ */ t.jsx(
698
+ "button",
699
+ {
700
+ className: "p-1.5 rounded-full hover:bg-gray-100 text-gray-500 hover:text-gray-900 transition-colors cursor-pointer",
701
+ onClick: v,
702
+ children: /* @__PURE__ */ t.jsx("svg", { className: "w-5 h-5", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ t.jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M15 19l-7-7 7-7" }) })
703
+ }
704
+ ),
705
+ /* @__PURE__ */ t.jsxs("div", { className: "text-xs font-medium text-gray-500 flex flex-col items-center", children: [
706
+ g && c ? /* @__PURE__ */ t.jsxs("span", { children: [
707
+ Z(g, "MMM d, yyyy", { locale: Y }),
708
+ " - ",
709
+ Z(c, "MMM d, yyyy", { locale: Y })
710
+ ] }) : /* @__PURE__ */ t.jsx("span", { children: W?.selectDateRange || "Select date range" }),
711
+ b && R && S && /* @__PURE__ */ t.jsxs("span", { className: "text-[10px] font-medium", style: { color: P }, children: [
712
+ W?.vs || "vs",
713
+ " ",
714
+ Z(R, "MMM d, yyyy", { locale: Y }),
715
+ " - ",
716
+ Z(S, "MMM d, yyyy", { locale: Y })
717
+ ] })
718
+ ] }),
719
+ /* @__PURE__ */ t.jsx(
720
+ "button",
721
+ {
722
+ className: "p-1.5 rounded-full hover:bg-gray-100 text-gray-500 hover:text-gray-900 transition-colors cursor-pointer",
723
+ onClick: X,
724
+ children: /* @__PURE__ */ t.jsx("svg", { className: "w-5 h-5", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ t.jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M9 5l7 7-7 7" }) })
725
+ }
726
+ )
727
+ ] }),
728
+ /* @__PURE__ */ t.jsxs("div", { className: `flex flex-col gap-4 p-4 overflow-y-auto flex-1 items-center ${$?.container || ""}`, children: [
729
+ oe(s),
730
+ oe(ge(s, 1))
731
+ ] }),
732
+ /* @__PURE__ */ t.jsxs("div", { className: `px-4 py-3 border-t border-gray-100 flex items-center justify-between shrink-0 bg-white ${$?.footer || ""}`, children: [
733
+ /* @__PURE__ */ t.jsx(
734
+ "button",
735
+ {
736
+ className: "text-xs font-medium text-gray-500 hover:text-gray-700 underline decoration-gray-300 hover:decoration-gray-500 underline-offset-2 transition-colors cursor-pointer",
737
+ onClick: () => {
738
+ z(void 0), w(void 0), k(!1), e(""), H(!1);
739
+ },
740
+ children: W?.clear || "Reset"
741
+ }
742
+ ),
743
+ /* @__PURE__ */ t.jsxs("div", { className: "flex gap-3", children: [
744
+ /* @__PURE__ */ t.jsx(
745
+ "button",
746
+ {
747
+ className: `px-3 py-1.5 rounded-md border border-gray-300 bg-white text-xs font-medium text-gray-700 hover:bg-gray-50 transition-colors cursor-pointer ${$?.buttonCancel || ""}`,
748
+ onClick: l,
749
+ children: W?.cancel || "Cancel"
750
+ }
751
+ ),
752
+ /* @__PURE__ */ t.jsx(
753
+ "button",
754
+ {
755
+ className: `px-3 py-1.5 rounded-md border border-transparent text-xs font-medium text-white transition-colors disabled:opacity-50 disabled:cursor-not-allowed shadow-sm cursor-pointer ${$?.buttonApply || ""}`,
756
+ style: { backgroundColor: y },
757
+ onClick: ee,
758
+ disabled: !g || !c,
759
+ children: W?.apply || "Apply"
760
+ }
761
+ )
762
+ ] })
763
+ ] })
764
+ ] })
765
+ ] });
766
+ }, Ze = ({
767
+ value: r,
768
+ defaultValue: n,
769
+ onChange: a,
770
+ onApply: p,
771
+ onCancel: l,
772
+ presets: i,
773
+ primaryColor: m,
774
+ compareColor: y,
775
+ enableCompare: P = !1,
776
+ compareMode: I = "previousPeriod",
777
+ className: M,
778
+ style: O,
779
+ minDate: N,
780
+ maxDate: _,
781
+ weekStartsOn: q,
782
+ disabledDates: Y,
783
+ disableFuture: W,
784
+ disablePast: $,
785
+ locale: g,
786
+ labels: z,
787
+ classNames: c
788
+ }) => {
789
+ const [w, V] = D(!1), [J, u] = D(
790
+ r || n || { startDate: /* @__PURE__ */ new Date(), endDate: /* @__PURE__ */ new Date() }
791
+ ), [k, b] = D(), [H, d] = D(P), [C, T] = D(I), [j, R] = D({ top: 0, left: 0 }), E = ve(null), S = ve(null);
792
+ G(() => {
793
+ r && u(r);
794
+ }, [r]), G(() => {
795
+ d(P);
796
+ }, [P]), G(() => {
797
+ T(I);
798
+ }, [I]);
799
+ const L = () => {
800
+ if (!E.current) return;
801
+ const x = E.current.getBoundingClientRect();
802
+ R({
803
+ top: x.bottom + window.scrollY + 4,
804
+ left: x.left + window.scrollX
805
+ });
806
+ }, F = () => {
807
+ w || L(), V((x) => !x);
808
+ };
809
+ G(() => {
810
+ if (!w) return;
811
+ const x = () => L();
812
+ return window.addEventListener("scroll", x, !0), window.addEventListener("resize", x), () => {
813
+ window.removeEventListener("scroll", x, !0), window.removeEventListener("resize", x);
814
+ };
815
+ }, [w]), G(() => {
816
+ if (!w) return;
817
+ const x = (A) => {
818
+ const v = A.target;
819
+ E.current?.contains(v) || S.current?.contains(v) || (V(!1), l?.());
820
+ };
821
+ return document.addEventListener("mousedown", x), () => document.removeEventListener("mousedown", x);
822
+ }, [w, l]);
823
+ const e = (x, A, v) => {
824
+ u(x), b(A), d(!!A), v && T(v), V(!1), a?.({
825
+ startDate: x.startDate,
826
+ endDate: x.endDate,
827
+ compareStartDate: A?.startDate,
828
+ compareEndDate: A?.endDate
829
+ }), p?.(x, A);
830
+ }, s = () => {
831
+ V(!1), l?.();
832
+ }, o = (x) => Z(x, "MMM d, yyyy", { locale: g }), f = `${o(J.startDate)} - ${o(J.endDate)}`;
833
+ return /* @__PURE__ */ t.jsxs(
834
+ "div",
835
+ {
836
+ ref: E,
837
+ className: `relative inline-block text-left ${M || ""} ${c?.root || ""}`,
838
+ style: {
839
+ "--sdc-primary-color": m,
840
+ "--sdc-compare-color": y,
841
+ ...O
842
+ },
843
+ children: [
844
+ /* @__PURE__ */ t.jsxs(
845
+ "button",
846
+ {
847
+ className: `flex items-center justify-between min-w-[240px] px-3 py-2 bg-white border border-gray-300 rounded hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all shadow-sm ${c?.container || ""}`,
848
+ onClick: F,
849
+ type: "button",
850
+ "aria-haspopup": "dialog",
851
+ "aria-expanded": w,
852
+ children: [
853
+ /* @__PURE__ */ t.jsxs("div", { className: "flex items-center gap-2 text-gray-700", children: [
854
+ /* @__PURE__ */ t.jsx("span", { className: "text-gray-400", children: /* @__PURE__ */ t.jsx("svg", { className: "w-4 h-4", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ t.jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" }) }) }),
855
+ /* @__PURE__ */ t.jsx("span", { className: "text-sm font-medium", children: f })
856
+ ] }),
857
+ k && /* @__PURE__ */ t.jsxs(
858
+ "span",
859
+ {
860
+ className: `text-xs px-1.5 py-0.5 rounded ml-2 font-medium ${c?.dayCompare || ""}`,
861
+ style: { color: y, backgroundColor: `${y}18` },
862
+ children: [
863
+ "vs ",
864
+ o(k.startDate)
865
+ ]
866
+ }
867
+ ),
868
+ /* @__PURE__ */ t.jsx(
869
+ "svg",
870
+ {
871
+ className: `w-4 h-4 text-gray-400 transition-transform ${w ? "rotate-180" : ""}`,
872
+ fill: "none",
873
+ stroke: "currentColor",
874
+ viewBox: "0 0 24 24",
875
+ children: /* @__PURE__ */ t.jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M19 9l-7 7-7-7" })
876
+ }
877
+ )
878
+ ]
879
+ }
880
+ ),
881
+ w && Te(
882
+ /* @__PURE__ */ t.jsx(
883
+ "div",
884
+ {
885
+ ref: S,
886
+ style: {
887
+ position: "absolute",
888
+ top: j.top,
889
+ left: j.left,
890
+ zIndex: 99999
891
+ },
892
+ role: "dialog",
893
+ "aria-modal": "true",
894
+ children: /* @__PURE__ */ t.jsx(
895
+ "div",
896
+ {
897
+ className: `bg-white ring-1 ring-black ring-opacity-5 rounded-lg shadow-2xl overflow-hidden ${c?.calendar || ""}`,
898
+ style: { "--sdc-primary-color": m, "--sdc-compare-color": y },
899
+ children: /* @__PURE__ */ t.jsx(
900
+ Je,
901
+ {
902
+ initialRange: J,
903
+ initialCompareRange: k,
904
+ presets: i,
905
+ onApply: e,
906
+ onCancel: s,
907
+ enableCompare: H,
908
+ activeCompareMode: C,
909
+ primaryColor: m,
910
+ compareColor: y,
911
+ minDate: N,
912
+ maxDate: _,
913
+ weekStartsOn: q,
914
+ disabledDates: Y,
915
+ disableFuture: W,
916
+ disablePast: $,
917
+ locale: g,
918
+ labels: z,
919
+ classNames: c
920
+ }
921
+ )
922
+ }
923
+ )
924
+ }
925
+ ),
926
+ document.body
927
+ )
928
+ ]
929
+ }
930
+ );
931
+ };
932
+ export {
933
+ Ze as SmartDateCompare,
934
+ qe as defaultPresets
935
+ };