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/drawer.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/drawer.ts
@@ -32,7 +42,7 @@ __export(drawer_exports, {
32
42
  module.exports = __toCommonJS(drawer_exports);
33
43
 
34
44
  // src/ShareSheetDrawer.tsx
35
- var import_react3 = require("react");
45
+ var import_react3 = __toESM(require("react"));
36
46
  var import_vaul = require("vaul");
37
47
 
38
48
  // src/utils.ts
@@ -251,6 +261,8 @@ function useShareSheet({
251
261
  downloadUrl,
252
262
  downloadFilename,
253
263
  emailSubject = "Share",
264
+ shareFile,
265
+ shareFilename = "share.png",
254
266
  onNativeShare,
255
267
  onCopy,
256
268
  onDownload
@@ -279,10 +291,47 @@ function useShareSheet({
279
291
  }
280
292
  }, [safeUrl, onCopy]);
281
293
  const nativeShare = (0, import_react.useCallback)(async () => {
282
- if (!safeUrl) return;
283
294
  const nav = navigator;
284
295
  if (!("share" in nav) || typeof nav.share !== "function") return;
285
296
  try {
297
+ if (shareFile) {
298
+ let blob;
299
+ if (typeof shareFile === "string" && shareFile.startsWith("data:")) {
300
+ const response = await fetch(shareFile);
301
+ blob = await response.blob();
302
+ } else if (shareFile instanceof Blob) {
303
+ blob = shareFile;
304
+ } else {
305
+ if (!safeUrl) return;
306
+ await nav.share({
307
+ title: shareText,
308
+ text: shareText,
309
+ url: safeUrl
310
+ });
311
+ onNativeShare?.();
312
+ return;
313
+ }
314
+ const file = new File([blob], shareFilename, { type: blob.type || "image/png" });
315
+ const shareData = {
316
+ files: [file],
317
+ title: shareText,
318
+ text: shareText
319
+ };
320
+ if (nav.canShare && !nav.canShare(shareData)) {
321
+ if (!safeUrl) return;
322
+ await nav.share({
323
+ title: shareText,
324
+ text: shareText,
325
+ url: safeUrl
326
+ });
327
+ onNativeShare?.();
328
+ return;
329
+ }
330
+ await nav.share(shareData);
331
+ onNativeShare?.();
332
+ return;
333
+ }
334
+ if (!safeUrl) return;
286
335
  await nav.share({
287
336
  title: shareText,
288
337
  text: shareText,
@@ -291,7 +340,7 @@ function useShareSheet({
291
340
  onNativeShare?.();
292
341
  } catch {
293
342
  }
294
- }, [safeUrl, shareText, onNativeShare]);
343
+ }, [safeUrl, shareText, shareFile, shareFilename, onNativeShare]);
295
344
  const downloadFile = (0, import_react.useCallback)(async () => {
296
345
  const url = (downloadUrl ?? "").trim();
297
346
  if (!url) return;
@@ -528,7 +577,7 @@ var CSS_VAR_UI_DEFAULTS = {
528
577
  [CSS_VARS_UI.overlayBg]: "rgba(0, 0, 0, 0.7)",
529
578
  [CSS_VARS_UI.drawerBg]: "#09090b",
530
579
  [CSS_VARS_UI.drawerBorder]: "#27272a",
531
- [CSS_VARS_UI.handleBg]: "#27272a",
580
+ [CSS_VARS_UI.handleBg]: "rgba(255, 255, 255, 0.2)",
532
581
  [CSS_VARS_UI.titleColor]: "#ffffff",
533
582
  [CSS_VARS_UI.subtitleColor]: "#a1a1aa",
534
583
  [CSS_VARS_UI.buttonLabelColor]: "#ffffff",
@@ -547,9 +596,9 @@ var defaultClasses = {
547
596
  header: "text-center mb-2",
548
597
  title: "text-2xl font-black",
549
598
  subtitle: "mt-1 text-sm",
550
- preview: "flex justify-center mb-4 px-4",
599
+ preview: "flex justify-center my-4 px-4",
551
600
  previewSkeleton: "rounded-xl overflow-hidden",
552
- previewImage: "border border-white/10",
601
+ previewImage: "border border-white/10 rounded-xl",
553
602
  previewMeta: "",
554
603
  grid: "px-2 py-6 flex flex-row items-start gap-4 gap-y-6 flex-wrap justify-center",
555
604
  button: "flex flex-col items-center gap-0 text-xs w-[60px] outline-none cursor-pointer group",
@@ -578,12 +627,16 @@ function ShareSheetContent({
578
627
  shareText,
579
628
  downloadUrl,
580
629
  downloadFilename,
630
+ previewImage,
631
+ shareFile,
632
+ shareFilename,
581
633
  className,
582
634
  classNames = {},
583
635
  buttonSize = DEFAULT_BUTTON_SIZE,
584
636
  iconSize = DEFAULT_ICON_SIZE,
585
637
  onNativeShare,
586
638
  onCopy,
639
+ onShare,
587
640
  onDownload,
588
641
  hide = [],
589
642
  show,
@@ -592,7 +645,7 @@ function ShareSheetContent({
592
645
  }) {
593
646
  const [imageLoaded, setImageLoaded] = (0, import_react2.useState)(false);
594
647
  const [imageError, setImageError] = (0, import_react2.useState)(false);
595
- const { ogData, loading: ogLoading } = useOGData(shareUrl);
648
+ const { ogData, loading: ogLoading } = useOGData(previewImage ? void 0 : shareUrl);
596
649
  const handleImageLoad = (0, import_react2.useCallback)(() => {
597
650
  setImageLoaded(true);
598
651
  }, []);
@@ -605,6 +658,8 @@ function ShareSheetContent({
605
658
  downloadUrl,
606
659
  downloadFilename,
607
660
  emailSubject: shareText,
661
+ shareFile,
662
+ shareFilename,
608
663
  onNativeShare,
609
664
  onCopy,
610
665
  onDownload
@@ -650,11 +705,14 @@ function ShareSheetContent({
650
705
  // Use CSS var with fallback to platform color
651
706
  bgColor: cssVar(PLATFORM_CSS_VARS[id], PLATFORM_COLORS[id].bg),
652
707
  textColor: PLATFORM_COLORS[id].text,
653
- onClick: shareActions[id],
708
+ onClick: () => {
709
+ onShare?.(id);
710
+ shareActions[id]();
711
+ },
654
712
  condition
655
713
  };
656
714
  });
657
- }, [iconSize, labels, icons, dynamicLabels, shareActions, shareSheet.canNativeShare, shareSheet.platformAvailability, downloadUrl]);
715
+ }, [iconSize, labels, icons, dynamicLabels, shareActions, onShare, shareSheet.canNativeShare, shareSheet.platformAvailability, downloadUrl]);
658
716
  const visibleButtons = (0, import_react2.useMemo)(() => {
659
717
  if (show && show.length > 0) {
660
718
  return show.map((id) => buttons.find((btn) => btn.id === id)).filter((btn) => {
@@ -674,9 +732,10 @@ function ShareSheetContent({
674
732
  const shimmerColor = cssVar(CSS_VARS_UI.previewShimmer, CSS_VAR_UI_DEFAULTS[CSS_VARS_UI.previewShimmer]);
675
733
  const textColor = cssVar(CSS_VARS_UI.subtitleColor, CSS_VAR_UI_DEFAULTS[CSS_VARS_UI.subtitleColor]);
676
734
  const renderPreview = () => {
677
- const ogImage = ogData?.image;
735
+ const ogImage = previewImage || ogData?.image;
678
736
  const hasImage = ogImage && !imageError;
679
- if (ogLoading) {
737
+ const isLoading = !previewImage && ogLoading;
738
+ if (isLoading) {
680
739
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
681
740
  "div",
682
741
  {
@@ -709,7 +768,7 @@ function ShareSheetContent({
709
768
  }
710
769
  ) });
711
770
  }
712
- if (!ogData || !hasImage) {
771
+ if (!hasImage) {
713
772
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
714
773
  "div",
715
774
  {
@@ -762,6 +821,22 @@ function ShareSheetContent({
762
821
  }
763
822
  ) });
764
823
  }
824
+ const isDataUrl = typeof ogImage === "string" && ogImage.startsWith("data:");
825
+ if (isDataUrl) {
826
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
827
+ "img",
828
+ {
829
+ src: ogImage,
830
+ alt: "Preview",
831
+ className: cn(defaultClasses.previewImage, classNames.previewImage),
832
+ style: {
833
+ width: "100%",
834
+ maxWidth: "320px",
835
+ height: "auto"
836
+ }
837
+ }
838
+ ) });
839
+ }
765
840
  if (!imageLoaded) {
766
841
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", width: "100%" }, children: [
767
842
  /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
@@ -799,7 +874,7 @@ function ShareSheetContent({
799
874
  "img",
800
875
  {
801
876
  src: ogImage,
802
- alt: ogData.title || "Preview",
877
+ alt: ogData?.title || "Preview",
803
878
  onLoad: handleImageLoad,
804
879
  onError: handleImageError,
805
880
  style: { display: "none" }
@@ -811,13 +886,12 @@ function ShareSheetContent({
811
886
  "img",
812
887
  {
813
888
  src: ogImage,
814
- alt: ogData.title || "Preview",
889
+ alt: ogData?.title || "Preview",
815
890
  className: cn(defaultClasses.previewImage, classNames.previewImage, "sharesheet-fadein"),
816
891
  style: {
817
892
  width: "100%",
818
893
  maxWidth: "320px",
819
- height: "auto",
820
- borderRadius: "12px"
894
+ height: "auto"
821
895
  }
822
896
  }
823
897
  ) });
@@ -887,6 +961,16 @@ var defaultDrawerClasses = {
887
961
  handle: "mx-auto w-12 h-1.5 shrink-0 rounded-full mb-6",
888
962
  trigger: ""
889
963
  };
964
+ function renderTrigger(children, className) {
965
+ if (import_react3.default.isValidElement(children)) {
966
+ if (!className) return children;
967
+ const childClassName = children.props.className;
968
+ return import_react3.default.cloneElement(children, {
969
+ className: cn(childClassName, className)
970
+ });
971
+ }
972
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className, children });
973
+ }
890
974
  function cssVar2(name, fallback) {
891
975
  return `var(${name}, ${fallback})`;
892
976
  }
@@ -907,6 +991,9 @@ function ShareSheetDrawer({
907
991
  shareText,
908
992
  downloadUrl,
909
993
  downloadFilename,
994
+ previewImage,
995
+ shareFile,
996
+ shareFilename,
910
997
  disabled,
911
998
  children,
912
999
  open: controlledOpen,
@@ -928,17 +1015,11 @@ function ShareSheetDrawer({
928
1015
  const open = isControlled ? controlledOpen : internalOpen;
929
1016
  const setOpen = isControlled ? (value) => controlledOnOpenChange?.(value) : setInternalOpen;
930
1017
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_vaul.Drawer.Root, { open, onOpenChange: setOpen, shouldScaleBackground: true, children: [
931
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_vaul.Drawer.Trigger, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
932
- "div",
933
- {
934
- className: cn(
935
- defaultDrawerClasses.trigger,
936
- classNames.trigger,
937
- disabled ? "pointer-events-none opacity-50" : ""
938
- ),
939
- children
940
- }
941
- ) }),
1018
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_vaul.Drawer.Trigger, { asChild: true, children: renderTrigger(children, cn(
1019
+ defaultDrawerClasses.trigger,
1020
+ classNames.trigger,
1021
+ disabled ? "pointer-events-none opacity-50" : ""
1022
+ )) }),
942
1023
  /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_vaul.Drawer.Portal, { children: [
943
1024
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
944
1025
  import_vaul.Drawer.Overlay,
@@ -984,6 +1065,9 @@ function ShareSheetDrawer({
984
1065
  shareText,
985
1066
  downloadUrl,
986
1067
  downloadFilename,
1068
+ previewImage,
1069
+ shareFile,
1070
+ shareFilename,
987
1071
  className,
988
1072
  classNames,
989
1073
  buttonSize,