@mediaradar/design-components 0.33.0 → 0.34.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/dist/index.cjs CHANGED
@@ -727,6 +727,7 @@ var CREATIVE_INTEL_SHORTCUTS = [
727
727
  "All available history",
728
728
  "Custom"
729
729
  ];
730
+ var HIDDEN_SHORTCUTS = ["Last 30 days"];
730
731
  var WEEKDAYS = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];
731
732
  var MONTHS = [
732
733
  "January",
@@ -757,6 +758,20 @@ var fmt = (d) => `${MONTHS[d.getMonth()].slice(0, 3)} ${d.getDate()}, ${d.getFul
757
758
  var fmtFull = (d) => `${MONTHS[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()}`;
758
759
  var fullRangeLabel = (s, e) => `${fmtFull(s)} - ${fmtFull(e)}`;
759
760
  var presetLabel = (sc) => sc === "Custom" ? "Custom range" : sc;
761
+ var MON = (d) => MONTHS[d.getMonth()].slice(0, 3);
762
+ function spanLabel(s, e) {
763
+ if (s.getFullYear() !== e.getFullYear()) return `${fmt(s)} \u2013 ${fmt(e)}`;
764
+ if (s.getMonth() !== e.getMonth()) return `${MON(s)} ${s.getDate()} \u2013 ${MON(e)} ${e.getDate()}, ${e.getFullYear()}`;
765
+ return s.getDate() === e.getDate() ? fmt(s) : `${MON(s)} ${s.getDate()} \u2013 ${e.getDate()}, ${e.getFullYear()}`;
766
+ }
767
+ function compareSpan(s, e, mode) {
768
+ if (mode === "year") {
769
+ const back = (d) => new Date(d.getFullYear() - 1, d.getMonth(), d.getDate());
770
+ return [back(s), back(e)];
771
+ }
772
+ const days = Math.round((startOfDay(e).getTime() - startOfDay(s).getTime()) / 864e5) + 1;
773
+ return [addDays(s, -days), addDays(s, -1)];
774
+ }
760
775
  function presetRange(key, today) {
761
776
  const t = startOfDay(today);
762
777
  const qMonth = Math.floor(t.getMonth() / 3) * 3;
@@ -793,6 +808,8 @@ function presetRange(key, today) {
793
808
  return [addDays(t, -6), t];
794
809
  case "Last 14 days":
795
810
  return [addDays(t, -13), t];
811
+ case "Last 30 days":
812
+ return [addDays(t, -29), t];
796
813
  case "Last month": {
797
814
  const s = new Date(t.getFullYear(), t.getMonth() - 1, 1);
798
815
  const e = new Date(t.getFullYear(), t.getMonth(), 0);
@@ -814,7 +831,7 @@ function monthGrid(year, month) {
814
831
  }
815
832
  function matchShortcut(shortcuts, label) {
816
833
  if (!label) return "Rolling 12 months";
817
- const hit = shortcuts.find((s) => s.toLowerCase() === label.toLowerCase());
834
+ const hit = [...shortcuts, ...HIDDEN_SHORTCUTS].find((s) => s.toLowerCase() === label.toLowerCase());
818
835
  return hit || "Custom";
819
836
  }
820
837
  function slug2(value) {
@@ -833,23 +850,43 @@ function DateRangeFilter({
833
850
  shortDates = false,
834
851
  icon,
835
852
  presetSet = "default",
853
+ excludePresets = [],
854
+ emptyLabel,
855
+ today: todayProp,
856
+ initialRange,
857
+ compare,
858
+ onApply,
836
859
  testIdPrefix,
837
860
  granularity = "day"
838
861
  }) {
839
862
  const monthly = granularity === "month";
840
863
  const snap = (s, e) => monthly ? [firstOfMonth(s), lastOfMonth(e)] : [s, e];
841
864
  const controlled = controlledValue !== void 0 && controlledRange !== void 0;
842
- const SHORTCUTS = presetSet === "creativeIntel" ? CREATIVE_INTEL_SHORTCUTS : DEFAULT_SHORTCUTS;
843
- const today = (0, import_react3.useMemo)(() => /* @__PURE__ */ new Date(), []);
865
+ const startsEmpty = !controlled && !!emptyLabel;
866
+ const SHORTCUTS = (presetSet === "creativeIntel" ? CREATIVE_INTEL_SHORTCUTS : DEFAULT_SHORTCUTS).filter(
867
+ (s) => !excludePresets.includes(s)
868
+ );
869
+ const today = (0, import_react3.useMemo)(() => todayProp ?? /* @__PURE__ */ new Date(), []);
844
870
  const initShortcut = matchShortcut(SHORTCUTS, controlled ? controlledValue : defaultValue);
845
- const initRange = controlled ? controlledRange : snap(...presetRange(initShortcut === "Custom" ? "Rolling 12 months" : initShortcut, today));
871
+ const initRange = controlled ? controlledRange : initShortcut === "Custom" && initialRange ? snap(...initialRange) : snap(...presetRange(initShortcut === "Custom" ? "Rolling 12 months" : initShortcut, today));
872
+ const compareLabel = (id) => compare?.options.find((o) => o.id === id)?.label;
846
873
  const dateLine = (s, e) => monthly ? `${fmtMonth(s)} \u2013 ${fmtMonth(e)}` : shortDates ? `${fmt(s)} \u2013 ${fmt(e)}` : fullRangeLabel(s, e);
847
874
  const [open, setOpen] = (0, import_react3.useState)(false);
848
875
  const [uncontrolledValue, setUncontrolledValue] = (0, import_react3.useState)(
849
- showDates ? presetLabel(initShortcut) : defaultValue || "Last 7 days"
876
+ startsEmpty ? emptyLabel : showDates ? presetLabel(initShortcut) : defaultValue || "Last 7 days"
850
877
  );
851
- const [uncontrolledDatesLine, setUncontrolledDatesLine] = (0, import_react3.useState)(dateLine(initRange[0], initRange[1]));
852
- const committed = (0, import_react3.useRef)({ shortcut: initShortcut, start: initRange[0], end: initRange[1] });
878
+ const [uncontrolledDatesLine, setUncontrolledDatesLine] = (0, import_react3.useState)(
879
+ startsEmpty ? null : dateLine(initRange[0], initRange[1])
880
+ );
881
+ const committed = (0, import_react3.useRef)({ shortcut: initShortcut, start: initRange[0], end: initRange[1], set: !startsEmpty });
882
+ const [cmp, setCmp] = (0, import_react3.useState)(compare?.value);
883
+ const committedCmp = (0, import_react3.useRef)(compare?.value);
884
+ const [appliedCmp, setAppliedCmp] = (0, import_react3.useState)(compare?.value);
885
+ (0, import_react3.useEffect)(() => {
886
+ committedCmp.current = compare?.value;
887
+ setAppliedCmp(compare?.value);
888
+ }, [compare?.value]);
889
+ const compareName = `${(0, import_react3.useId)()}-compare`;
853
890
  const displayValue = controlled ? showDates ? presetLabel(initShortcut) : controlledValue : uncontrolledValue;
854
891
  const displayDatesLine = controlled ? dateLine(initRange[0], initRange[1]) : uncontrolledDatesLine;
855
892
  const [shortcut, setShortcut] = (0, import_react3.useState)(initShortcut);
@@ -858,7 +895,7 @@ function DateRangeFilter({
858
895
  const [leftMonth, setLeftMonth] = (0, import_react3.useState)(new Date(initRange[0].getFullYear(), initRange[0].getMonth(), 1));
859
896
  const ref = (0, import_react3.useRef)(null);
860
897
  (0, import_react3.useEffect)(() => {
861
- if (controlled) return;
898
+ if (controlled || startsEmpty) return;
862
899
  onRangeChange?.(committed.current.start, committed.current.end);
863
900
  }, []);
864
901
  (0, import_react3.useEffect)(() => {
@@ -866,10 +903,12 @@ function DateRangeFilter({
866
903
  const committedShortcut = controlled ? initShortcut : committed.current.shortcut;
867
904
  const committedStart = controlled ? initRange[0] : committed.current.start;
868
905
  const committedEnd = controlled ? initRange[1] : committed.current.end;
906
+ const hasRange = controlled || committed.current.set;
869
907
  setShortcut(committedShortcut);
870
- setStart(committedStart);
871
- setEnd(committedEnd);
872
- setLeftMonth(new Date(committedStart.getFullYear(), committedStart.getMonth(), 1));
908
+ setStart(hasRange ? committedStart : null);
909
+ setEnd(hasRange ? committedEnd : null);
910
+ setLeftMonth(hasRange ? new Date(committedStart.getFullYear(), committedStart.getMonth(), 1) : addMonths(today, -1));
911
+ setCmp(committedCmp.current);
873
912
  const onKey = (e) => e.key === "Escape" && setOpen(false);
874
913
  const onClick = (e) => {
875
914
  if (ref.current && !ref.current.contains(e.target)) setOpen(false);
@@ -903,17 +942,22 @@ function DateRangeFilter({
903
942
  setEnd(e);
904
943
  } else setEnd(monthly ? lastOfMonth(d) : d);
905
944
  };
945
+ const canApply = !!start || controlled || committed.current.set;
906
946
  const apply = () => {
947
+ if (!canApply) return;
907
948
  const [s, e] = snap(start || initRange[0], end || start || initRange[0]);
908
949
  const custom = monthly ? `${fmtMonth(s)} \u2014 ${fmtMonth(e)}` : `${fmt(s)} \u2014 ${fmt(e)}`;
909
950
  const v = showDates ? presetLabel(shortcut) : shortcut === "Custom" ? custom : shortcut;
910
951
  if (!controlled) {
911
- committed.current = { shortcut, start: s, end: e };
952
+ committed.current = { shortcut, start: s, end: e, set: true };
912
953
  setUncontrolledValue(v);
913
954
  setUncontrolledDatesLine(dateLine(s, e));
914
955
  }
956
+ committedCmp.current = cmp;
957
+ setAppliedCmp(cmp);
915
958
  onChange?.(v);
916
959
  onRangeChange?.(s, e);
960
+ onApply?.(shortcut === "Custom" ? `${fmt(s)} \u2013 ${fmt(e)}` : shortcut, s, e, cmp);
917
961
  setOpen(false);
918
962
  };
919
963
  const popRef = (0, import_react3.useRef)(null);
@@ -935,8 +979,14 @@ function DateRangeFilter({
935
979
  children: [
936
980
  icon && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "shrink-0 text-text-tertiary", children: icon }),
937
981
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("span", { className: "flex flex-col gap-1 text-left leading-none", children: [
938
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: showDates ? "text-[11px] text-text-tertiary" : "text-control-caption uppercase tracking-wide text-text-tertiary", children: showDates ? displayDatesLine : caption }),
939
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "text-[12px] font-medium text-text-primary", children: displayValue })
982
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: showDates && displayDatesLine ? "text-[11px] text-text-tertiary" : "text-control-caption uppercase tracking-wide text-text-tertiary", children: showDates && displayDatesLine ? displayDatesLine : caption }),
983
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("span", { className: "text-[12px] font-medium text-text-primary", children: [
984
+ displayValue,
985
+ compareLabel(appliedCmp) && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("span", { className: "font-normal text-text-secondary", children: [
986
+ " \xB7 vs ",
987
+ compareLabel(appliedCmp).toLowerCase()
988
+ ] })
989
+ ] })
940
990
  ] }),
941
991
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_lucide_react6.ChevronDown, { size: 14, "aria-hidden": "true", className: `shrink-0 text-text-secondary transition-transform ${open ? "rotate-180" : ""}` })
942
992
  ]
@@ -1012,10 +1062,39 @@ function DateRangeFilter({
1012
1062
  ] })
1013
1063
  ] }, offset);
1014
1064
  }) }),
1065
+ compare && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("fieldset", { className: "mt-4 border-t border-border-default pt-4", children: [
1066
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("legend", { className: "float-left mb-2 w-full text-xs font-medium text-text-secondary", children: "Compare to" }),
1067
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "clear-left flex flex-col gap-1", children: compare.options.map((o) => {
1068
+ const on = (cmp ?? compare.value) === o.id;
1069
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
1070
+ "label",
1071
+ {
1072
+ className: `flex cursor-pointer items-center gap-2.5 rounded-control px-2 py-1.5 text-dense transition-colors ${on ? "bg-subtle" : "hover:bg-subtle"}`,
1073
+ children: [
1074
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1075
+ "input",
1076
+ {
1077
+ type: "radio",
1078
+ name: compareName,
1079
+ value: o.id,
1080
+ checked: on,
1081
+ onChange: () => setCmp(o.id),
1082
+ "data-testid": testIdPrefix && `${testIdPrefix}-compare-${slug2(o.id)}`,
1083
+ className: "h-4 w-4 accent-brand-primaryAction"
1084
+ }
1085
+ ),
1086
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: `w-44 shrink-0 ${on ? "font-semibold text-text-primary" : "text-text-secondary"}`, children: o.label }),
1087
+ start && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "tabular-nums text-text-tertiary", children: spanLabel(...compareSpan(start, end ?? start, o.id)) })
1088
+ ]
1089
+ },
1090
+ o.id
1091
+ );
1092
+ }) })
1093
+ ] }),
1015
1094
  /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "mt-4 flex items-center justify-end gap-2", children: [
1016
1095
  monthly && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "mr-auto text-xs text-text-tertiary", children: "Data is monthly, so ranges cover whole months." }),
1017
1096
  /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("button", { type: "button", "data-testid": testIdPrefix && `${testIdPrefix}-cancel`, onClick: () => setOpen(false), className: "rounded-control border border-border-default bg-surface px-5 py-2 text-[13px] font-semibold text-text-primary transition-colors hover:bg-subtle", children: "Cancel" }),
1018
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("button", { type: "button", "data-testid": testIdPrefix && `${testIdPrefix}-apply`, onClick: apply, className: "rounded-control bg-brand-primaryAction px-5 py-2 text-[13px] font-semibold text-brand-onprimary transition-[filter] hover:brightness-110", children: "Apply" })
1097
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("button", { type: "button", "data-testid": testIdPrefix && `${testIdPrefix}-apply`, onClick: apply, disabled: !canApply, className: "rounded-control bg-brand-primaryAction px-5 py-2 text-[13px] font-semibold text-brand-onprimary transition-[filter] hover:brightness-110 disabled:cursor-not-allowed disabled:opacity-50", children: "Apply" })
1019
1098
  ] })
1020
1099
  ] })
1021
1100
  ]
@@ -3676,7 +3755,7 @@ function resolveRange(label, today = startOfToday()) {
3676
3755
  }
3677
3756
  return [new Date(end.getFullYear(), end.getMonth() - 6, 1), end];
3678
3757
  }
3679
- function spanLabel(label, today) {
3758
+ function spanLabel2(label, today) {
3680
3759
  const [s, e] = resolveRange(label, today);
3681
3760
  return `${fmtDate(s)} \u2013 ${fmtDate(e)}`;
3682
3761
  }
@@ -3762,7 +3841,7 @@ function DateRangePicker({
3762
3841
  className: chipTriggerClass(),
3763
3842
  children: [
3764
3843
  /* @__PURE__ */ (0, import_jsx_runtime38.jsxs)("span", { className: "flex flex-col gap-1 text-left leading-none", children: [
3765
- /* @__PURE__ */ (0, import_jsx_runtime38.jsx)("span", { className: "text-control-caption text-text-tertiary", children: spanLabel(value, end) }),
3844
+ /* @__PURE__ */ (0, import_jsx_runtime38.jsx)("span", { className: "text-control-caption text-text-tertiary", children: spanLabel2(value, end) }),
3766
3845
  /* @__PURE__ */ (0, import_jsx_runtime38.jsx)("span", { className: "text-[12px] font-medium text-text-primary", children: value })
3767
3846
  ] }),
3768
3847
  /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(import_lucide_react22.ChevronDown, { size: 14, "aria-hidden": "true", className: cn("shrink-0 text-text-secondary transition-transform", open && "rotate-180") })