react-sharesheet 1.7.0 → 1.9.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.js CHANGED
@@ -304,6 +304,8 @@ function useShareSheet({
304
304
  downloadUrl,
305
305
  downloadFilename,
306
306
  emailSubject = "Share",
307
+ shareFile,
308
+ shareFilename = "share.png",
307
309
  onNativeShare,
308
310
  onCopy,
309
311
  onDownload
@@ -332,10 +334,47 @@ function useShareSheet({
332
334
  }
333
335
  }, [safeUrl, onCopy]);
334
336
  const nativeShare = (0, import_react.useCallback)(async () => {
335
- if (!safeUrl) return;
336
337
  const nav = navigator;
337
338
  if (!("share" in nav) || typeof nav.share !== "function") return;
338
339
  try {
340
+ if (shareFile) {
341
+ let blob;
342
+ if (typeof shareFile === "string" && shareFile.startsWith("data:")) {
343
+ const response = await fetch(shareFile);
344
+ blob = await response.blob();
345
+ } else if (shareFile instanceof Blob) {
346
+ blob = shareFile;
347
+ } else {
348
+ if (!safeUrl) return;
349
+ await nav.share({
350
+ title: shareText,
351
+ text: shareText,
352
+ url: safeUrl
353
+ });
354
+ onNativeShare?.();
355
+ return;
356
+ }
357
+ const file = new File([blob], shareFilename, { type: blob.type || "image/png" });
358
+ const shareData = {
359
+ files: [file],
360
+ title: shareText,
361
+ text: shareText
362
+ };
363
+ if (nav.canShare && !nav.canShare(shareData)) {
364
+ if (!safeUrl) return;
365
+ await nav.share({
366
+ title: shareText,
367
+ text: shareText,
368
+ url: safeUrl
369
+ });
370
+ onNativeShare?.();
371
+ return;
372
+ }
373
+ await nav.share(shareData);
374
+ onNativeShare?.();
375
+ return;
376
+ }
377
+ if (!safeUrl) return;
339
378
  await nav.share({
340
379
  title: shareText,
341
380
  text: shareText,
@@ -344,7 +383,7 @@ function useShareSheet({
344
383
  onNativeShare?.();
345
384
  } catch {
346
385
  }
347
- }, [safeUrl, shareText, onNativeShare]);
386
+ }, [safeUrl, shareText, shareFile, shareFilename, onNativeShare]);
348
387
  const downloadFile = (0, import_react.useCallback)(async () => {
349
388
  const url = (downloadUrl ?? "").trim();
350
389
  if (!url) return;
@@ -604,7 +643,7 @@ var CSS_VAR_UI_DEFAULTS = {
604
643
  [CSS_VARS_UI.overlayBg]: "rgba(0, 0, 0, 0.7)",
605
644
  [CSS_VARS_UI.drawerBg]: "#09090b",
606
645
  [CSS_VARS_UI.drawerBorder]: "#27272a",
607
- [CSS_VARS_UI.handleBg]: "#27272a",
646
+ [CSS_VARS_UI.handleBg]: "rgba(255, 255, 255, 0.2)",
608
647
  [CSS_VARS_UI.titleColor]: "#ffffff",
609
648
  [CSS_VARS_UI.subtitleColor]: "#a1a1aa",
610
649
  [CSS_VARS_UI.buttonLabelColor]: "#ffffff",
@@ -623,9 +662,9 @@ var defaultClasses = {
623
662
  header: "text-center mb-2",
624
663
  title: "text-2xl font-black",
625
664
  subtitle: "mt-1 text-sm",
626
- preview: "flex justify-center mb-4 px-4",
665
+ preview: "flex justify-center my-4 px-4",
627
666
  previewSkeleton: "rounded-xl overflow-hidden",
628
- previewImage: "border border-white/10",
667
+ previewImage: "border border-white/10 rounded-xl",
629
668
  previewMeta: "",
630
669
  grid: "px-2 py-6 flex flex-row items-start gap-4 gap-y-6 flex-wrap justify-center",
631
670
  button: "flex flex-col items-center gap-0 text-xs w-[60px] outline-none cursor-pointer group",
@@ -637,6 +676,13 @@ var shimmerKeyframes = `
637
676
  0% { transform: translateX(-100%); }
638
677
  100% { transform: translateX(100%); }
639
678
  }
679
+ @keyframes sharesheet-fadein {
680
+ 0% { opacity: 0; }
681
+ 100% { opacity: 1; }
682
+ }
683
+ .sharesheet-fadein {
684
+ animation: sharesheet-fadein 0.3s ease-out forwards;
685
+ }
640
686
  `;
641
687
  function cssVar(name, fallback) {
642
688
  return `var(${name}, ${fallback})`;
@@ -647,12 +693,16 @@ function ShareSheetContent({
647
693
  shareText,
648
694
  downloadUrl,
649
695
  downloadFilename,
696
+ previewImage,
697
+ shareFile,
698
+ shareFilename,
650
699
  className,
651
700
  classNames = {},
652
701
  buttonSize = DEFAULT_BUTTON_SIZE,
653
702
  iconSize = DEFAULT_ICON_SIZE,
654
703
  onNativeShare,
655
704
  onCopy,
705
+ onShare,
656
706
  onDownload,
657
707
  hide = [],
658
708
  show,
@@ -661,7 +711,7 @@ function ShareSheetContent({
661
711
  }) {
662
712
  const [imageLoaded, setImageLoaded] = (0, import_react2.useState)(false);
663
713
  const [imageError, setImageError] = (0, import_react2.useState)(false);
664
- const { ogData, loading: ogLoading } = useOGData(shareUrl);
714
+ const { ogData, loading: ogLoading } = useOGData(previewImage ? void 0 : shareUrl);
665
715
  const handleImageLoad = (0, import_react2.useCallback)(() => {
666
716
  setImageLoaded(true);
667
717
  }, []);
@@ -674,6 +724,8 @@ function ShareSheetContent({
674
724
  downloadUrl,
675
725
  downloadFilename,
676
726
  emailSubject: shareText,
727
+ shareFile,
728
+ shareFilename,
677
729
  onNativeShare,
678
730
  onCopy,
679
731
  onDownload
@@ -719,11 +771,14 @@ function ShareSheetContent({
719
771
  // Use CSS var with fallback to platform color
720
772
  bgColor: cssVar(PLATFORM_CSS_VARS[id], PLATFORM_COLORS[id].bg),
721
773
  textColor: PLATFORM_COLORS[id].text,
722
- onClick: shareActions[id],
774
+ onClick: () => {
775
+ onShare?.(id);
776
+ shareActions[id]();
777
+ },
723
778
  condition
724
779
  };
725
780
  });
726
- }, [iconSize, labels, icons, dynamicLabels, shareActions, shareSheet.canNativeShare, shareSheet.platformAvailability, downloadUrl]);
781
+ }, [iconSize, labels, icons, dynamicLabels, shareActions, onShare, shareSheet.canNativeShare, shareSheet.platformAvailability, downloadUrl]);
727
782
  const visibleButtons = (0, import_react2.useMemo)(() => {
728
783
  if (show && show.length > 0) {
729
784
  return show.map((id) => buttons.find((btn) => btn.id === id)).filter((btn) => {
@@ -743,9 +798,10 @@ function ShareSheetContent({
743
798
  const shimmerColor = cssVar(CSS_VARS_UI.previewShimmer, CSS_VAR_UI_DEFAULTS[CSS_VARS_UI.previewShimmer]);
744
799
  const textColor = cssVar(CSS_VARS_UI.subtitleColor, CSS_VAR_UI_DEFAULTS[CSS_VARS_UI.subtitleColor]);
745
800
  const renderPreview = () => {
746
- const ogImage = ogData?.image;
801
+ const ogImage = previewImage || ogData?.image;
747
802
  const hasImage = ogImage && !imageError;
748
- if (ogLoading) {
803
+ const isLoading = !previewImage && ogLoading;
804
+ if (isLoading) {
749
805
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
750
806
  "div",
751
807
  {
@@ -778,7 +834,7 @@ function ShareSheetContent({
778
834
  }
779
835
  ) });
780
836
  }
781
- if (!ogData || !hasImage) {
837
+ if (!hasImage) {
782
838
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
783
839
  "div",
784
840
  {
@@ -831,6 +887,22 @@ function ShareSheetContent({
831
887
  }
832
888
  ) });
833
889
  }
890
+ const isDataUrl = typeof ogImage === "string" && ogImage.startsWith("data:");
891
+ if (isDataUrl) {
892
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
893
+ "img",
894
+ {
895
+ src: ogImage,
896
+ alt: "Preview",
897
+ className: cn(defaultClasses.previewImage, classNames.previewImage),
898
+ style: {
899
+ width: "100%",
900
+ maxWidth: "320px",
901
+ height: "auto"
902
+ }
903
+ }
904
+ ) });
905
+ }
834
906
  if (!imageLoaded) {
835
907
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: [
836
908
  /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
@@ -868,7 +940,7 @@ function ShareSheetContent({
868
940
  "img",
869
941
  {
870
942
  src: ogImage,
871
- alt: ogData.title || "Preview",
943
+ alt: ogData?.title || "Preview",
872
944
  onLoad: handleImageLoad,
873
945
  onError: handleImageError,
874
946
  style: { display: "none" }
@@ -880,15 +952,12 @@ function ShareSheetContent({
880
952
  "img",
881
953
  {
882
954
  src: ogImage,
883
- alt: ogData.title || "Preview",
884
- className: cn(defaultClasses.previewImage, classNames.previewImage),
955
+ alt: ogData?.title || "Preview",
956
+ className: cn(defaultClasses.previewImage, classNames.previewImage, "sharesheet-fadein"),
885
957
  style: {
886
958
  width: "100%",
887
959
  maxWidth: "320px",
888
- height: "auto",
889
- borderRadius: "12px",
890
- opacity: 1,
891
- transition: "opacity 0.3s ease-in-out"
960
+ height: "auto"
892
961
  }
893
962
  }
894
963
  ) });
@@ -964,12 +1033,26 @@ var defaultDrawerClasses = {
964
1033
  function cssVar2(name, fallback) {
965
1034
  return `var(${name}, ${fallback})`;
966
1035
  }
1036
+ var srOnlyStyle = {
1037
+ position: "absolute",
1038
+ width: "1px",
1039
+ height: "1px",
1040
+ padding: 0,
1041
+ margin: "-1px",
1042
+ overflow: "hidden",
1043
+ clip: "rect(0, 0, 0, 0)",
1044
+ whiteSpace: "nowrap",
1045
+ borderWidth: 0
1046
+ };
967
1047
  function ShareSheetDrawer({
968
1048
  title = "Share",
969
1049
  shareUrl,
970
1050
  shareText,
971
1051
  downloadUrl,
972
1052
  downloadFilename,
1053
+ previewImage,
1054
+ shareFile,
1055
+ shareFilename,
973
1056
  disabled,
974
1057
  children,
975
1058
  open: controlledOpen,
@@ -1021,7 +1104,7 @@ function ShareSheetDrawer({
1021
1104
  borderColor: cssVar2(CSS_VARS_UI.drawerBorder, CSS_VAR_UI_DEFAULTS[CSS_VARS_UI.drawerBorder])
1022
1105
  },
1023
1106
  children: [
1024
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_vaul.Drawer.Title, { className: "sr-only", children: title }),
1107
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_vaul.Drawer.Title, { style: srOnlyStyle, children: title }),
1025
1108
  /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1026
1109
  "div",
1027
1110
  {
@@ -1047,6 +1130,9 @@ function ShareSheetDrawer({
1047
1130
  shareText,
1048
1131
  downloadUrl,
1049
1132
  downloadFilename,
1133
+ previewImage,
1134
+ shareFile,
1135
+ shareFilename,
1050
1136
  className,
1051
1137
  classNames,
1052
1138
  buttonSize,