react-sharesheet 1.8.0 → 1.9.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.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -304,6 +314,8 @@ function useShareSheet({
304
314
  downloadUrl,
305
315
  downloadFilename,
306
316
  emailSubject = "Share",
317
+ shareFile,
318
+ shareFilename = "share.png",
307
319
  onNativeShare,
308
320
  onCopy,
309
321
  onDownload
@@ -332,10 +344,47 @@ function useShareSheet({
332
344
  }
333
345
  }, [safeUrl, onCopy]);
334
346
  const nativeShare = (0, import_react.useCallback)(async () => {
335
- if (!safeUrl) return;
336
347
  const nav = navigator;
337
348
  if (!("share" in nav) || typeof nav.share !== "function") return;
338
349
  try {
350
+ if (shareFile) {
351
+ let blob;
352
+ if (typeof shareFile === "string" && shareFile.startsWith("data:")) {
353
+ const response = await fetch(shareFile);
354
+ blob = await response.blob();
355
+ } else if (shareFile instanceof Blob) {
356
+ blob = shareFile;
357
+ } else {
358
+ if (!safeUrl) return;
359
+ await nav.share({
360
+ title: shareText,
361
+ text: shareText,
362
+ url: safeUrl
363
+ });
364
+ onNativeShare?.();
365
+ return;
366
+ }
367
+ const file = new File([blob], shareFilename, { type: blob.type || "image/png" });
368
+ const shareData = {
369
+ files: [file],
370
+ title: shareText,
371
+ text: shareText
372
+ };
373
+ if (nav.canShare && !nav.canShare(shareData)) {
374
+ if (!safeUrl) return;
375
+ await nav.share({
376
+ title: shareText,
377
+ text: shareText,
378
+ url: safeUrl
379
+ });
380
+ onNativeShare?.();
381
+ return;
382
+ }
383
+ await nav.share(shareData);
384
+ onNativeShare?.();
385
+ return;
386
+ }
387
+ if (!safeUrl) return;
339
388
  await nav.share({
340
389
  title: shareText,
341
390
  text: shareText,
@@ -344,7 +393,7 @@ function useShareSheet({
344
393
  onNativeShare?.();
345
394
  } catch {
346
395
  }
347
- }, [safeUrl, shareText, onNativeShare]);
396
+ }, [safeUrl, shareText, shareFile, shareFilename, onNativeShare]);
348
397
  const downloadFile = (0, import_react.useCallback)(async () => {
349
398
  const url = (downloadUrl ?? "").trim();
350
399
  if (!url) return;
@@ -604,7 +653,7 @@ var CSS_VAR_UI_DEFAULTS = {
604
653
  [CSS_VARS_UI.overlayBg]: "rgba(0, 0, 0, 0.7)",
605
654
  [CSS_VARS_UI.drawerBg]: "#09090b",
606
655
  [CSS_VARS_UI.drawerBorder]: "#27272a",
607
- [CSS_VARS_UI.handleBg]: "#27272a",
656
+ [CSS_VARS_UI.handleBg]: "rgba(255, 255, 255, 0.2)",
608
657
  [CSS_VARS_UI.titleColor]: "#ffffff",
609
658
  [CSS_VARS_UI.subtitleColor]: "#a1a1aa",
610
659
  [CSS_VARS_UI.buttonLabelColor]: "#ffffff",
@@ -623,9 +672,9 @@ var defaultClasses = {
623
672
  header: "text-center mb-2",
624
673
  title: "text-2xl font-black",
625
674
  subtitle: "mt-1 text-sm",
626
- preview: "flex justify-center mb-4 px-4",
675
+ preview: "flex justify-center my-4 px-4",
627
676
  previewSkeleton: "rounded-xl overflow-hidden",
628
- previewImage: "border border-white/10",
677
+ previewImage: "border border-white/10 rounded-xl",
629
678
  previewMeta: "",
630
679
  grid: "px-2 py-6 flex flex-row items-start gap-4 gap-y-6 flex-wrap justify-center",
631
680
  button: "flex flex-col items-center gap-0 text-xs w-[60px] outline-none cursor-pointer group",
@@ -654,12 +703,16 @@ function ShareSheetContent({
654
703
  shareText,
655
704
  downloadUrl,
656
705
  downloadFilename,
706
+ previewImage,
707
+ shareFile,
708
+ shareFilename,
657
709
  className,
658
710
  classNames = {},
659
711
  buttonSize = DEFAULT_BUTTON_SIZE,
660
712
  iconSize = DEFAULT_ICON_SIZE,
661
713
  onNativeShare,
662
714
  onCopy,
715
+ onShare,
663
716
  onDownload,
664
717
  hide = [],
665
718
  show,
@@ -668,7 +721,7 @@ function ShareSheetContent({
668
721
  }) {
669
722
  const [imageLoaded, setImageLoaded] = (0, import_react2.useState)(false);
670
723
  const [imageError, setImageError] = (0, import_react2.useState)(false);
671
- const { ogData, loading: ogLoading } = useOGData(shareUrl);
724
+ const { ogData, loading: ogLoading } = useOGData(previewImage ? void 0 : shareUrl);
672
725
  const handleImageLoad = (0, import_react2.useCallback)(() => {
673
726
  setImageLoaded(true);
674
727
  }, []);
@@ -681,6 +734,8 @@ function ShareSheetContent({
681
734
  downloadUrl,
682
735
  downloadFilename,
683
736
  emailSubject: shareText,
737
+ shareFile,
738
+ shareFilename,
684
739
  onNativeShare,
685
740
  onCopy,
686
741
  onDownload
@@ -726,11 +781,14 @@ function ShareSheetContent({
726
781
  // Use CSS var with fallback to platform color
727
782
  bgColor: cssVar(PLATFORM_CSS_VARS[id], PLATFORM_COLORS[id].bg),
728
783
  textColor: PLATFORM_COLORS[id].text,
729
- onClick: shareActions[id],
784
+ onClick: () => {
785
+ onShare?.(id);
786
+ shareActions[id]();
787
+ },
730
788
  condition
731
789
  };
732
790
  });
733
- }, [iconSize, labels, icons, dynamicLabels, shareActions, shareSheet.canNativeShare, shareSheet.platformAvailability, downloadUrl]);
791
+ }, [iconSize, labels, icons, dynamicLabels, shareActions, onShare, shareSheet.canNativeShare, shareSheet.platformAvailability, downloadUrl]);
734
792
  const visibleButtons = (0, import_react2.useMemo)(() => {
735
793
  if (show && show.length > 0) {
736
794
  return show.map((id) => buttons.find((btn) => btn.id === id)).filter((btn) => {
@@ -750,9 +808,10 @@ function ShareSheetContent({
750
808
  const shimmerColor = cssVar(CSS_VARS_UI.previewShimmer, CSS_VAR_UI_DEFAULTS[CSS_VARS_UI.previewShimmer]);
751
809
  const textColor = cssVar(CSS_VARS_UI.subtitleColor, CSS_VAR_UI_DEFAULTS[CSS_VARS_UI.subtitleColor]);
752
810
  const renderPreview = () => {
753
- const ogImage = ogData?.image;
811
+ const ogImage = previewImage || ogData?.image;
754
812
  const hasImage = ogImage && !imageError;
755
- if (ogLoading) {
813
+ const isLoading = !previewImage && ogLoading;
814
+ if (isLoading) {
756
815
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
757
816
  "div",
758
817
  {
@@ -785,7 +844,7 @@ function ShareSheetContent({
785
844
  }
786
845
  ) });
787
846
  }
788
- if (!ogData || !hasImage) {
847
+ if (!hasImage) {
789
848
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
790
849
  "div",
791
850
  {
@@ -838,6 +897,22 @@ function ShareSheetContent({
838
897
  }
839
898
  ) });
840
899
  }
900
+ const isDataUrl = typeof ogImage === "string" && ogImage.startsWith("data:");
901
+ if (isDataUrl) {
902
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
903
+ "img",
904
+ {
905
+ src: ogImage,
906
+ alt: "Preview",
907
+ className: cn(defaultClasses.previewImage, classNames.previewImage),
908
+ style: {
909
+ width: "100%",
910
+ maxWidth: "320px",
911
+ height: "auto"
912
+ }
913
+ }
914
+ ) });
915
+ }
841
916
  if (!imageLoaded) {
842
917
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: [
843
918
  /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
@@ -875,7 +950,7 @@ function ShareSheetContent({
875
950
  "img",
876
951
  {
877
952
  src: ogImage,
878
- alt: ogData.title || "Preview",
953
+ alt: ogData?.title || "Preview",
879
954
  onLoad: handleImageLoad,
880
955
  onError: handleImageError,
881
956
  style: { display: "none" }
@@ -887,13 +962,12 @@ function ShareSheetContent({
887
962
  "img",
888
963
  {
889
964
  src: ogImage,
890
- alt: ogData.title || "Preview",
965
+ alt: ogData?.title || "Preview",
891
966
  className: cn(defaultClasses.previewImage, classNames.previewImage, "sharesheet-fadein"),
892
967
  style: {
893
968
  width: "100%",
894
969
  maxWidth: "320px",
895
- height: "auto",
896
- borderRadius: "12px"
970
+ height: "auto"
897
971
  }
898
972
  }
899
973
  ) });
@@ -956,7 +1030,7 @@ function ShareSheetContent({
956
1030
  var ShareMenuContent = ShareSheetContent;
957
1031
 
958
1032
  // src/ShareSheetDrawer.tsx
959
- var import_react3 = require("react");
1033
+ var import_react3 = __toESM(require("react"));
960
1034
  var import_vaul = require("vaul");
961
1035
  var import_jsx_runtime3 = require("react/jsx-runtime");
962
1036
  var defaultDrawerClasses = {
@@ -966,6 +1040,16 @@ var defaultDrawerClasses = {
966
1040
  handle: "mx-auto w-12 h-1.5 shrink-0 rounded-full mb-6",
967
1041
  trigger: ""
968
1042
  };
1043
+ function renderTrigger(children, className) {
1044
+ if (import_react3.default.isValidElement(children)) {
1045
+ if (!className) return children;
1046
+ const childClassName = children.props.className;
1047
+ return import_react3.default.cloneElement(children, {
1048
+ className: cn(childClassName, className)
1049
+ });
1050
+ }
1051
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className, children });
1052
+ }
969
1053
  function cssVar2(name, fallback) {
970
1054
  return `var(${name}, ${fallback})`;
971
1055
  }
@@ -986,6 +1070,9 @@ function ShareSheetDrawer({
986
1070
  shareText,
987
1071
  downloadUrl,
988
1072
  downloadFilename,
1073
+ previewImage,
1074
+ shareFile,
1075
+ shareFilename,
989
1076
  disabled,
990
1077
  children,
991
1078
  open: controlledOpen,
@@ -1007,17 +1094,11 @@ function ShareSheetDrawer({
1007
1094
  const open = isControlled ? controlledOpen : internalOpen;
1008
1095
  const setOpen = isControlled ? (value) => controlledOnOpenChange?.(value) : setInternalOpen;
1009
1096
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_vaul.Drawer.Root, { open, onOpenChange: setOpen, shouldScaleBackground: true, children: [
1010
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_vaul.Drawer.Trigger, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1011
- "div",
1012
- {
1013
- className: cn(
1014
- defaultDrawerClasses.trigger,
1015
- classNames.trigger,
1016
- disabled ? "pointer-events-none opacity-50" : ""
1017
- ),
1018
- children
1019
- }
1020
- ) }),
1097
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_vaul.Drawer.Trigger, { asChild: true, children: renderTrigger(children, cn(
1098
+ defaultDrawerClasses.trigger,
1099
+ classNames.trigger,
1100
+ disabled ? "pointer-events-none opacity-50" : ""
1101
+ )) }),
1021
1102
  /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_vaul.Drawer.Portal, { children: [
1022
1103
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1023
1104
  import_vaul.Drawer.Overlay,
@@ -1063,6 +1144,9 @@ function ShareSheetDrawer({
1063
1144
  shareText,
1064
1145
  downloadUrl,
1065
1146
  downloadFilename,
1147
+ previewImage,
1148
+ shareFile,
1149
+ shareFilename,
1066
1150
  className,
1067
1151
  classNames,
1068
1152
  buttonSize,