@xsynaptic/share-kit 0.3.0 → 0.5.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.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/count.ts
2
2
  const urlPattern = /https?:\/\/[\w!#$%&'()*+,./:;=?@~-]+/giu;
3
+ const xLinkPattern = /https?:\/\/[\w!#$%&'()*+,./:;=?@~-]+|(?<![\w#./@-])(?:[\da-z][\da-z-]*\.)+(?:[a-z]{2}|com|org|net|edu|gov|info|biz|app|dev|xyz|site|online|blog|news|shop|store|page|tech|art|link|live|social|club|world|asia)(?![\w-])(?:\/[\w!#$%&'()*+,./:;=?@~-]*)?/giu;
3
4
  const urlTrailingPunctuation = /[!'),.:;?]+$/u;
4
5
  const graphemeSegmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
5
6
  const utf8Encoder = new TextEncoder();
@@ -65,25 +66,28 @@ function shareKitCountGraphemes(text) {
65
66
  function shareKitCountUtf8(text) {
66
67
  return utf8Encoder.encode(text).length;
67
68
  }
68
- function splitLinks(text) {
69
+ function splitLinks(text, pattern) {
69
70
  const links = [];
70
71
  return {
71
72
  links,
72
- rest: text.replaceAll(urlPattern, (match) => {
73
+ rest: text.replaceAll(pattern, (match) => {
73
74
  const trailing = urlTrailingPunctuation.exec(match)?.[0] ?? "";
74
75
  links.push(match.slice(0, match.length - trailing.length));
75
76
  return trailing;
76
77
  })
77
78
  };
78
79
  }
79
- function shareKitWithLinkCost(count, cost) {
80
+ function withLinkCost(count, cost, pattern) {
80
81
  return (text) => {
81
- const { links, rest } = splitLinks(text);
82
+ const { links, rest } = splitLinks(text, pattern);
82
83
  let total = count(rest);
83
84
  for (const link of links) total += typeof cost === "number" ? cost : cost(link);
84
85
  return total;
85
86
  };
86
87
  }
88
+ function shareKitWithLinkCost(count, cost) {
89
+ return withLinkCost(count, cost, urlPattern);
90
+ }
87
91
  // src/actions.ts
88
92
  const shareCapabilities = [
89
93
  "files",
@@ -121,6 +125,14 @@ function getShareCapabilities(preset) {
121
125
  function isDomError(error, name) {
122
126
  return error instanceof DOMException && error.name === name;
123
127
  }
128
+ async function shareData(data) {
129
+ try {
130
+ await navigator.share(data);
131
+ return "shared";
132
+ } catch (error) {
133
+ return isDomError(error, "AbortError") ? "dismissed" : "error";
134
+ }
135
+ }
124
136
  async function fetchFile({ filename, src, type }, signal) {
125
137
  const response = await fetch(src, {
126
138
  cache: "no-cache",
@@ -427,7 +439,7 @@ const shareKitFacebook = {
427
439
  },
428
440
  composer: ({ url }) => withQuery("https://www.facebook.com/sharer/sharer.php", { u: url }),
429
441
  count: shareKitCountLength,
430
- hint: "Facebook only takes the link. Your caption is copied, so paste it into your post.",
442
+ hint: "Facebook only takes the link. Your text is copied, so paste it into your post.",
431
443
  id: "facebook",
432
444
  images: [shareKitLandscapeFormat, shareKitPortraitFormat],
433
445
  label: "Facebook",
@@ -436,11 +448,11 @@ const shareKitFacebook = {
436
448
  };
437
449
  const shareKitInstagram = {
438
450
  actions: {
439
- desktop: [],
451
+ desktop: ["save-image"],
440
452
  mobile: ["share-image"]
441
453
  },
442
454
  count: shareKitCountLength,
443
- hint: "Links in Instagram captions aren't clickable. Point people to the link in your bio.",
455
+ hint: "Your text is copied, so paste it as the caption. Links in it aren't clickable.",
444
456
  id: "instagram",
445
457
  images: [shareKitPortraitFormat],
446
458
  label: "Instagram",
@@ -449,11 +461,11 @@ const shareKitInstagram = {
449
461
  };
450
462
  const shareKitInstagramStory = {
451
463
  actions: {
452
- desktop: [],
464
+ desktop: ["save-image"],
453
465
  mobile: ["share-image"]
454
466
  },
455
467
  count: shareKitCountLength,
456
- hint: "Add the link to your Story with a link sticker.",
468
+ hint: "The link is copied, so add it to your Story with a link sticker.",
457
469
  id: "instagram-story",
458
470
  images: [shareKitStoryFormat, shareKitPortraitFormat],
459
471
  label: "Story",
@@ -466,18 +478,32 @@ const shareKitLinkedin = {
466
478
  },
467
479
  composer: ({ url }) => withQuery("https://www.linkedin.com/sharing/share-offsite/", { url }),
468
480
  count: shareKitCountLength,
469
- hint: "LinkedIn only takes the link. Your caption is copied, so paste it into your post.",
481
+ hint: "LinkedIn only takes the link. Your text is copied, so paste it into your post.",
470
482
  id: "linkedin",
471
483
  images: [shareKitLandscapeFormat],
472
484
  label: "LinkedIn",
473
485
  limit: 3e3,
474
486
  shape: "full"
475
487
  };
488
+ const shareKitLine = {
489
+ actions: {
490
+ desktop: ["open-composer"],
491
+ mobile: ["open-composer"]
492
+ },
493
+ composer: ({ device, text, url }) => device === "mobile" ? `https://line.me/R/share?text=${encodeURIComponent(text)}` : `https://social-plugins.line.me/lineit/share?url=${encodeURIComponent(url)}&text=${encodeURIComponent(text)}`,
494
+ count: shareKitCountLength,
495
+ hint: "Opens the LINE app if you have it, or LINE on the web, which asks you to sign in.",
496
+ id: "line",
497
+ images: [shareKitLandscapeFormat],
498
+ label: "LINE",
499
+ shape: "full"
500
+ };
476
501
  const shareKitMastodon = {
477
502
  actions: {
478
- desktop: [],
479
- mobile: ["share-image"]
503
+ desktop: ["open-composer"],
504
+ mobile: ["share-image", "open-composer"]
480
505
  },
506
+ composer: ({ text }) => `https://share.joinmastodon.org/#text=${encodeURIComponent(text)}`,
481
507
  count: shareKitWithLinkCost(shareKitCountStringz, fixedLinkCost),
482
508
  id: "mastodon",
483
509
  images: [shareKitPortraitFormat],
@@ -549,7 +575,7 @@ const shareKitX = {
549
575
  mobile: ["open-composer"]
550
576
  },
551
577
  composer: ({ text }) => withQuery("https://x.com/intent/tweet", { text }),
552
- count: shareKitWithLinkCost(shareKitCountWeighted, fixedLinkCost),
578
+ count: withLinkCost(shareKitCountWeighted, fixedLinkCost, xLinkPattern),
553
579
  id: "x",
554
580
  images: [shareKitLandscapeFormat, shareKitPortraitFormat],
555
581
  label: "X",
@@ -567,6 +593,7 @@ function canRun(action, input) {
567
593
  switch (action) {
568
594
  case "copy-caption": return true;
569
595
  case "open-composer": return Boolean(input.composerUrl);
596
+ case "save-image": return input.hasSuggestedImage;
570
597
  case "share-image": return input.canShareFiles && input.hasSuggestedImage;
571
598
  case "share-link":
572
599
  case "share-text": return input.canShareText;
@@ -689,17 +716,12 @@ var ShareFeedback = class {
689
716
  }
690
717
  };
691
718
  // src/icons.ts
692
- const shareKitInterfaceIconNames = [
693
- "close",
694
- "more",
695
- "qr"
696
- ];
719
+ const shareKitInterfaceIconNames = ["close", "more"];
697
720
  const shareKitInterfaceIcons = {
698
721
  any: "M3.16 14.47L5.99 11.65A4.5 4.5 0 0 1 12.35 18.01L9.53 20.84A4.5 4.5 0 0 1 3.16 14.47ZM4.58 15.89A2.5 2.5 0 0 0 8.11 19.42L10.94 16.6A2.5 2.5 0 0 0 7.4 13.06ZM11.65 5.99L14.47 3.16A4.5 4.5 0 0 1 20.84 9.53L18.01 12.35A4.5 4.5 0 0 1 11.65 5.99ZM13.06 7.4A2.5 2.5 0 0 0 16.6 10.94L19.42 8.11A2.5 2.5 0 0 0 15.89 4.58ZM8.46 14.12L14.12 8.46L15.54 9.88L9.88 15.54Z",
699
722
  close: "M6 7.41 7.41 6 12 10.59 16.59 6 18 7.41 13.41 12 18 16.59 16.59 18 12 13.41 7.41 18 6 16.59 10.59 12Z",
700
723
  messages: "M5 4h14a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2ZM5 6v9h14V6ZM7 17v4l4-4Z",
701
- more: "M4 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0ZM10 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0ZM16 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0Z",
702
- qr: "M3 3h7v7H3ZM4.5 4.5v4h4v-4ZM5.5 5.5h2v2h-2ZM14 3h7v7h-7ZM15.5 4.5v4h4v-4ZM16.5 5.5h2v2h-2ZM3 14h7v7H3ZM4.5 15.5v4h4v-4ZM5.5 16.5h2v2h-2ZM14 14h2v2h-2ZM18 14h3v2h-3ZM16 16h2v2h-2ZM14 18h2v3h-2ZM18 18h3v3h-3Z"
724
+ more: "M4 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0ZM10 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0ZM16 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0Z"
703
725
  };
704
726
  var ShareIconSource = class {
705
727
  get icons() {
@@ -855,27 +877,32 @@ const shareKitStringDefaults = {
855
877
  assetCopy: "Copy",
856
878
  assetSave: "Save",
857
879
  assetShare: "Share",
858
- captionCopy: "Copy caption",
859
880
  captionOver: "{count} over the limit for {destination}",
860
881
  captionReset: "Reset",
861
- captionTitle: "Caption",
862
882
  close: "Close",
863
883
  copied: "Copied",
864
- copyManual: "Couldn't copy. The caption is selected, so you can copy it yourself.",
884
+ copyManual: "Couldn't copy. The text is selected, so you can copy it yourself.",
885
+ copyText: "Copy text",
886
+ copyTitle: "Copy title",
887
+ countOver: "{count} over",
865
888
  destinationsMore: "More",
866
889
  destinationsTitle: "Post to",
867
890
  error: "Something went wrong. Try saving instead.",
891
+ fieldText: "Text",
892
+ fieldTitle: "Title",
893
+ imagesHint: "Save or copy one, then add it to your post.",
894
+ imagesHintMobile: "Share one straight to an app, or save it.",
868
895
  imagesTitle: "Images",
869
896
  linkCopy: "Copy link",
870
897
  linkTitle: "Link",
871
- primaryCopy: "Copy caption",
872
898
  primaryOpen: "Open {destination}",
899
+ primarySaveImage: "Save image",
873
900
  primaryShareImage: "Share image",
874
901
  primaryShareLink: "Share link",
875
902
  primaryShareText: "Share",
876
903
  qrAlt: "QR code for this page",
877
904
  qrTitle: "Scan to continue on your phone",
878
- qrToggle: "Continue on your phone",
905
+ qrToggle: "Open on phone",
879
906
  retry: "Ready. Tap again to share.",
880
907
  saved: "Saved",
881
908
  saveError: "Couldn't save. Try again.",
@@ -926,9 +953,8 @@ function renderHeading({ classNames }, children, id) {
926
953
  id
927
954
  }, children);
928
955
  }
929
- function renderIconButton({ icons }, { action, attributes, fallback, icon, label }) {
956
+ function renderIconButton({ icons }, { action, fallback, icon, label }) {
930
957
  return createElement("button", {
931
- ...attributes,
932
958
  "aria-label": label,
933
959
  class: "share-kit-icon-button",
934
960
  "data-share-kit-action": action,
@@ -937,25 +963,19 @@ function renderIconButton({ icons }, { action, attributes, fallback, icon, label
937
963
  }, [createIconSlot(icon, icons[icon], fallback)]);
938
964
  }
939
965
  function renderHeader(context) {
940
- const { capabilities, id, strings } = context;
966
+ const { id, strings } = context;
941
967
  const heading = createElement("h2", {
942
968
  class: "share-kit-title",
943
969
  id: `${id}-title`,
944
970
  tabindex: "-1"
945
971
  }, [strings.title]);
946
972
  return {
947
- header: createElement("header", { class: "share-kit-header" }, [heading, createElement("div", { class: "share-kit-header-actions" }, [capabilities.has("mobile") ? void 0 : renderIconButton(context, {
948
- action: "qr",
949
- attributes: { "aria-haspopup": "dialog" },
950
- fallback: "QR",
951
- icon: "qr",
952
- label: strings.qrToggle
953
- }), renderIconButton(context, {
973
+ header: createElement("header", { class: "share-kit-header" }, [heading, renderIconButton(context, {
954
974
  action: "close",
955
975
  fallback: "×",
956
976
  icon: "close",
957
977
  label: strings.close
958
- })])]),
978
+ })]),
959
979
  heading
960
980
  };
961
981
  }
@@ -971,7 +991,7 @@ function renderDestination(context, destination) {
971
991
  type: "button"
972
992
  }, [renderDestinationIcon(context, destination.id), createElement("span", {}, [getDestinationLabel(strings, destination)])]);
973
993
  }
974
- function renderDestinations(context) {
994
+ function renderDestinationRow(context) {
975
995
  const { destinations, destinationsMore, id, strings } = context;
976
996
  const headingId = `${id}-destinations`;
977
997
  const more = destinationsMore.length > 0 ? createElement("button", {
@@ -991,21 +1011,36 @@ function renderDestinations(context) {
991
1011
  }, [...destinations.map((destination) => renderDestination(context, destination)), ...extra]);
992
1012
  return {
993
1013
  more,
994
- section: createElement("section", { class: "share-kit-section" }, [renderHeading(context, [strings.destinationsTitle], headingId), createElement("div", { class: "share-kit-destinations-row" }, [group, more])])
1014
+ nodes: [renderHeading(context, [strings.destinationsTitle], headingId), createElement("div", { class: "share-kit-destinations-row" }, [group, more])]
1015
+ };
1016
+ }
1017
+ function renderDestinations(context) {
1018
+ const hint = createElement("p", {
1019
+ class: "share-kit-hint",
1020
+ id: `${context.id}-hint`
1021
+ });
1022
+ if (context.destinations.length + context.destinationsMore.length === 1) return {
1023
+ hint,
1024
+ more: void 0,
1025
+ section: hint
1026
+ };
1027
+ const { more, nodes } = renderDestinationRow(context);
1028
+ return {
1029
+ hint,
1030
+ more,
1031
+ section: createElement("section", { class: "share-kit-section" }, [...nodes, hint])
995
1032
  };
996
1033
  }
997
1034
  function renderCaption(context) {
998
1035
  const { classNames, id, payload, strings } = context;
999
1036
  const captionId = `${id}-caption`;
1000
1037
  const countId = `${id}-count`;
1001
- const hintId = `${id}-hint`;
1002
1038
  const caption = createElement("textarea", {
1003
- "aria-describedby": `${countId} ${hintId}`,
1039
+ "aria-describedby": `${countId} ${id}-hint`,
1004
1040
  class: joinClasses("share-kit-caption", classNames.input),
1005
1041
  dir: "auto",
1006
1042
  id: captionId,
1007
- lang: payload.locale,
1008
- rows: "6"
1043
+ lang: payload.locale
1009
1044
  });
1010
1045
  const count = createElement("span", {
1011
1046
  class: "share-kit-count",
@@ -1013,25 +1048,30 @@ function renderCaption(context) {
1013
1048
  });
1014
1049
  const captionCopy = renderAction(context, {
1015
1050
  action: "copy-caption",
1016
- label: strings.captionCopy
1051
+ label: ""
1017
1052
  });
1018
- const hint = createElement("p", {
1019
- class: "share-kit-hint",
1020
- id: hintId
1053
+ const reset = renderAction(context, {
1054
+ action: "reset",
1055
+ label: strings.captionReset
1021
1056
  });
1057
+ const label = createElement("label", { for: captionId });
1058
+ const alert = createElement("p", {
1059
+ class: "share-kit-visually-hidden",
1060
+ role: "status"
1061
+ });
1062
+ reset.hidden = true;
1022
1063
  return {
1064
+ alert,
1023
1065
  caption,
1024
1066
  captionCopy,
1067
+ captionLabel: label,
1025
1068
  count,
1026
- hint,
1069
+ reset,
1027
1070
  section: createElement("section", { class: "share-kit-section" }, [
1028
- renderHeading(context, [createElement("label", { for: captionId }, [strings.captionTitle])]),
1071
+ renderHeading(context, [label]),
1029
1072
  caption,
1030
- createElement("div", { class: "share-kit-caption-meta" }, [count, createElement("span", { class: "share-kit-actions" }, [renderAction(context, {
1031
- action: "reset",
1032
- label: strings.captionReset
1033
- }), captionCopy])]),
1034
- hint
1073
+ createElement("div", { class: "share-kit-caption-meta" }, [count, createElement("span", { class: "share-kit-actions" }, [reset, captionCopy])]),
1074
+ alert
1035
1075
  ])
1036
1076
  };
1037
1077
  }
@@ -1043,6 +1083,7 @@ function renderTile(context, { actions, asset, index }) {
1043
1083
  class: "share-kit-tile",
1044
1084
  "data-share-kit-asset": asset.id
1045
1085
  }, [
1086
+ createElement("div", { class: "share-kit-tile-label" }, [createElement("span", { id: labelId }, [asset.label]), badge]),
1046
1087
  createElement("img", {
1047
1088
  alt: asset.alt,
1048
1089
  class: "share-kit-preview",
@@ -1052,7 +1093,6 @@ function renderTile(context, { actions, asset, index }) {
1052
1093
  src: asset.previewSrc,
1053
1094
  width: String(asset.width)
1054
1095
  }),
1055
- createElement("div", { class: "share-kit-tile-label" }, [createElement("span", { id: labelId }, [asset.label]), badge]),
1056
1096
  createElement("div", { class: "share-kit-actions" }, actions.map(([action, label]) => renderAction(context, {
1057
1097
  action,
1058
1098
  describedBy: labelId,
@@ -1062,14 +1102,14 @@ function renderTile(context, { actions, asset, index }) {
1062
1102
  }
1063
1103
  function getAssetActions({ capabilities, strings }) {
1064
1104
  const actions = [];
1065
- const canShare = capabilities.has("files");
1105
+ const canShare = capabilities.has("files") && capabilities.has("mobile");
1066
1106
  if (canShare) actions.push(["share", strings.assetShare]);
1067
- if (capabilities.has("copy-image") && !(canShare && capabilities.has("mobile"))) actions.push(["copy", strings.assetCopy]);
1107
+ if (!canShare && capabilities.has("copy-image")) actions.push(["copy", strings.assetCopy]);
1068
1108
  actions.push(["save", strings.assetSave]);
1069
1109
  return actions;
1070
1110
  }
1071
1111
  function renderImages(context) {
1072
- const { payload, strings } = context;
1112
+ const { capabilities, payload, strings } = context;
1073
1113
  const actions = getAssetActions(context);
1074
1114
  const assets = /* @__PURE__ */ new Map();
1075
1115
  for (const [index, asset] of payload.assets.entries()) assets.set(renderTile(context, {
@@ -1081,15 +1121,29 @@ function renderImages(context) {
1081
1121
  return {
1082
1122
  assets,
1083
1123
  grid,
1084
- section: createElement("section", { class: "share-kit-section" }, [renderHeading(context, [strings.imagesTitle]), grid])
1124
+ section: createElement("section", { class: "share-kit-section" }, [
1125
+ renderHeading(context, [strings.imagesTitle]),
1126
+ createElement("p", { class: "share-kit-hint" }, [capabilities.has("mobile") ? strings.imagesHintMobile : strings.imagesHint]),
1127
+ grid
1128
+ ])
1085
1129
  };
1086
1130
  }
1087
1131
  function renderLink(context) {
1088
- const { payload, strings } = context;
1089
- return createElement("section", { class: "share-kit-section" }, [renderHeading(context, [strings.linkTitle]), createElement("div", { class: "share-kit-link-row" }, [createElement("p", { class: "share-kit-link" }, [payload.url]), renderAction(context, {
1090
- action: "copy-link",
1091
- label: strings.linkCopy
1092
- })])]);
1132
+ const { capabilities, strings } = context;
1133
+ const link = createElement("p", { class: "share-kit-link" });
1134
+ const qr = capabilities.has("mobile") ? void 0 : createElement("button", {
1135
+ "aria-haspopup": "dialog",
1136
+ class: joinClasses("share-kit-action", context.classNames.action),
1137
+ "data-share-kit-action": "qr",
1138
+ type: "button"
1139
+ }, [strings.qrToggle]);
1140
+ return {
1141
+ link,
1142
+ section: createElement("section", { class: "share-kit-section" }, [renderHeading(context, [strings.linkTitle]), createElement("div", { class: "share-kit-link-row" }, [link, createElement("span", { class: "share-kit-actions" }, [renderAction(context, {
1143
+ action: "copy-link",
1144
+ label: strings.linkCopy
1145
+ }), qr])])])
1146
+ };
1093
1147
  }
1094
1148
  function renderShareKit(input) {
1095
1149
  const context = {
@@ -1100,6 +1154,7 @@ function renderShareKit(input) {
1100
1154
  const destinations = renderDestinations(context);
1101
1155
  const caption = renderCaption(context);
1102
1156
  const images = renderImages(context);
1157
+ const link = renderLink(context);
1103
1158
  const status = createElement("p", {
1104
1159
  class: "share-kit-status",
1105
1160
  role: "status"
@@ -1120,52 +1175,69 @@ function renderShareKit(input) {
1120
1175
  destinations.section,
1121
1176
  caption.section,
1122
1177
  images.section,
1123
- renderLink(context)
1178
+ link.section
1124
1179
  ]),
1125
1180
  createElement("footer", { class: "share-kit-footer" }, [status, primary])
1126
1181
  ])]);
1127
1182
  return {
1183
+ alert: caption.alert,
1128
1184
  assets: images.assets,
1129
1185
  caption: caption.caption,
1130
1186
  captionCopy: caption.captionCopy,
1187
+ captionLabel: caption.captionLabel,
1188
+ captionSection: caption.section,
1131
1189
  count: caption.count,
1132
1190
  dialog,
1133
1191
  grid: images.grid,
1134
1192
  heading,
1135
- hint: caption.hint,
1193
+ hint: destinations.hint,
1194
+ link: link.link,
1136
1195
  more: destinations.more,
1137
1196
  primary,
1197
+ reset: caption.reset,
1138
1198
  status,
1139
1199
  tiles: [...images.assets.keys()]
1140
1200
  };
1141
1201
  }
1142
1202
  // src/sync.ts
1143
1203
  const primaryLabels = {
1144
- "copy-caption": "primaryCopy",
1145
1204
  "open-composer": "primaryOpen",
1205
+ "save-image": "primarySaveImage",
1146
1206
  "share-image": "primaryShareImage",
1147
1207
  "share-link": "primaryShareLink",
1148
1208
  "share-text": "primaryShareText"
1149
1209
  };
1150
- function syncCaptionView({ destination, parts, primaryAction, strings }) {
1210
+ const copyLabels = {
1211
+ full: "copyText",
1212
+ text: "copyText",
1213
+ title: "copyTitle",
1214
+ url: "linkCopy"
1215
+ };
1216
+ function syncCaptionView({ destination, isEdited, parts, primaryAction, strings }) {
1151
1217
  const { limit } = destination;
1152
1218
  const count = destination.count(parts.caption.value);
1153
1219
  const isOver = limit !== void 0 && count > limit;
1154
1220
  const label = getDestinationLabel(strings, destination);
1155
- parts.count.textContent = limit === void 0 ? String(count) : `${String(count)} / ${String(limit)}`;
1221
+ const over = String(count - (limit ?? 0));
1222
+ const copyLabel = strings[copyLabels[destination.shape]];
1223
+ const total = limit === void 0 ? String(count) : `${String(count)} / ${String(limit)}`;
1224
+ const overNote = isOver ? ` · ${formatShareString(strings.countOver, { count: over })}` : "";
1225
+ parts.count.textContent = `${total}${overNote}`;
1156
1226
  parts.count.toggleAttribute("data-share-kit-over", isOver);
1157
1227
  parts.caption.setAttribute("aria-invalid", String(isOver));
1228
+ parts.captionCopy.textContent = copyLabel;
1158
1229
  parts.captionCopy.hidden = primaryAction === "copy-caption";
1159
- parts.primary.textContent = formatShareString(strings[primaryLabels[primaryAction]], { destination: label });
1230
+ parts.reset.hidden = !isEdited;
1231
+ parts.primary.textContent = primaryAction === "copy-caption" ? copyLabel : formatShareString(strings[primaryLabels[primaryAction]], { destination: label });
1160
1232
  return {
1161
1233
  isOver,
1162
1234
  message: formatShareString(strings.captionOver, {
1163
- count: String(count - (limit ?? 0)),
1235
+ count: over,
1164
1236
  destination: label
1165
1237
  })
1166
1238
  };
1167
1239
  }
1168
- function syncDestinationView({ assets, destination, parts, strings, suggested }) {
1240
+ function syncDestinationView({ assets, destination, parts, strings, suggested, url }) {
1169
1241
  for (const radio of parts.dialog.querySelectorAll("[role=\"radio\"]")) {
1170
1242
  const isSelected = radio.dataset.shareKitDestination === destination.id;
1171
1243
  radio.setAttribute("aria-checked", String(isSelected));
@@ -1173,6 +1245,11 @@ function syncDestinationView({ assets, destination, parts, strings, suggested })
1173
1245
  }
1174
1246
  parts.hint.textContent = getDestinationHint(strings, destination) ?? "";
1175
1247
  parts.hint.hidden = parts.hint.textContent === "";
1248
+ parts.captionSection.hidden = destination.shape === "url";
1249
+ parts.captionLabel.textContent = destination.shape === "title" ? strings.fieldTitle : strings.fieldText;
1250
+ parts.caption.rows = destination.shape === "title" ? 2 : 6;
1251
+ parts.link.textContent = url;
1252
+ parts.link.title = url;
1176
1253
  for (const asset of assets) {
1177
1254
  const badge = asset.querySelector(".share-kit-badge");
1178
1255
  if (badge) badge.hidden = asset !== suggested;
@@ -1199,7 +1276,7 @@ function isKeyOf(record, key) {
1199
1276
  return Object.hasOwn(record, key);
1200
1277
  }
1201
1278
  const BaseElement = typeof HTMLElement === "undefined" ? Object : HTMLElement;
1202
- var ShareKitElement = class extends BaseElement {
1279
+ var ShareKitElement = class ShareKitElement extends BaseElement {
1203
1280
  catalogue = /* @__PURE__ */ new Map();
1204
1281
  iconSource = new ShareIconSource({});
1205
1282
  #abortController;
@@ -1218,7 +1295,7 @@ var ShareKitElement = class extends BaseElement {
1218
1295
  },
1219
1296
  "copy-link": (button) => {
1220
1297
  this.#track("copy-link");
1221
- this.#feedback?.copyText(this.#payload?.url ?? "", button);
1298
+ this.#feedback?.copyText(this.#getUrl(), button);
1222
1299
  },
1223
1300
  destination: (button) => {
1224
1301
  const destination = this.#getRendered(button.dataset.shareKitDestination);
@@ -1226,8 +1303,8 @@ var ShareKitElement = class extends BaseElement {
1226
1303
  this.#selectDestination(destination);
1227
1304
  this.#track("destination");
1228
1305
  },
1229
- more: (button) => {
1230
- this.#showMore(button);
1306
+ more: () => {
1307
+ this.#showMore()[0]?.focus();
1231
1308
  },
1232
1309
  primary: () => {
1233
1310
  this.#primaryActions[this.#getPrimaryAction()]();
@@ -1238,12 +1315,13 @@ var ShareKitElement = class extends BaseElement {
1238
1315
  reset: () => {
1239
1316
  this.#isCaptionEdited = false;
1240
1317
  this.#syncDestination();
1318
+ this.#parts?.caption.focus();
1241
1319
  },
1242
1320
  save: (_button, tile) => {
1243
- if (tile) this.#saveAsset(tile);
1321
+ if (tile) this.#settle("save", this.#files?.save(tile), tile);
1244
1322
  },
1245
1323
  share: (_button, tile) => {
1246
- if (tile) this.#shareAssets([tile]);
1324
+ if (tile) this.#settle("share", this.#files?.share([tile]), tile);
1247
1325
  }
1248
1326
  };
1249
1327
  #capabilities = /* @__PURE__ */ new Set();
@@ -1268,19 +1346,25 @@ var ShareKitElement = class extends BaseElement {
1268
1346
  this.#copyCaption();
1269
1347
  openComposer(href);
1270
1348
  },
1349
+ "save-image": () => {
1350
+ const tile = this.#getSuggestedAsset();
1351
+ if (!tile) return;
1352
+ this.#copyCaption();
1353
+ this.#settle("save", this.#files?.save(tile), tile);
1354
+ },
1271
1355
  "share-image": () => {
1272
1356
  const tile = this.#getSuggestedAsset();
1273
1357
  if (!tile) return;
1274
1358
  this.#copyCaption();
1275
- this.#shareAssets([tile]);
1359
+ this.#settle("share", this.#files?.share([tile]), tile);
1276
1360
  },
1277
1361
  "share-link": () => {
1278
1362
  this.#copyCaption();
1279
- this.#shareData("share-link", { url: this.#getUrl() });
1363
+ this.#settle("share-link", shareData({ url: this.#getUrl() }));
1280
1364
  },
1281
1365
  "share-text": () => {
1282
1366
  const text = this.#parts?.caption.value;
1283
- if (text) this.#shareData("share-text", { text });
1367
+ if (text) this.#settle("share-text", shareData({ text }));
1284
1368
  }
1285
1369
  };
1286
1370
  #qr;
@@ -1319,9 +1403,9 @@ var ShareKitElement = class extends BaseElement {
1319
1403
  this.addEventListener("pointerover", ({ target }) => this.#files?.warm(target), { signal });
1320
1404
  for (const type of iconIntentEvents) this.addEventListener(type, this.#handleTriggerIntent, { signal });
1321
1405
  window.addEventListener("hashchange", this.#handleHashChange, { signal });
1322
- this.querySelector("[data-share-kit-trigger]")?.toggleAttribute("disabled", false);
1406
+ for (const trigger of this.querySelectorAll("[data-share-kit-trigger]")) trigger.toggleAttribute("disabled", false);
1323
1407
  queueMicrotask(() => {
1324
- if (!signal.aborted && isOpenHash(payload.settings.openHash)) this.#open();
1408
+ if (!signal.aborted && this.#ownsOpenHash()) this.#open();
1325
1409
  });
1326
1410
  }
1327
1411
  disconnectedCallback() {
@@ -1341,8 +1425,17 @@ var ShareKitElement = class extends BaseElement {
1341
1425
  this.#isOver = false;
1342
1426
  }
1343
1427
  async #copyCaption(button) {
1344
- const caption = this.#parts?.caption;
1345
- if (caption) await this.#feedback?.copyText(caption.value, button, caption);
1428
+ const parts = this.#parts;
1429
+ if (!parts) return;
1430
+ const fallback = parts.captionSection.hidden ? void 0 : parts.caption;
1431
+ await this.#feedback?.copyText(parts.caption.value, button, fallback);
1432
+ }
1433
+ #dispatch(detail) {
1434
+ this.dispatchEvent(new CustomEvent(shareKitActionEventName, {
1435
+ bubbles: true,
1436
+ composed: true,
1437
+ detail
1438
+ }));
1346
1439
  }
1347
1440
  #getAssets() {
1348
1441
  return (this.#parts?.tiles ?? []).filter((tile) => tile.dataset.shareKitState !== "failed");
@@ -1363,6 +1456,15 @@ var ShareKitElement = class extends BaseElement {
1363
1456
  url: this.#getUrl()
1364
1457
  });
1365
1458
  }
1459
+ #getDetail(action, tile) {
1460
+ if (!this.#destination) return;
1461
+ const detail = {
1462
+ action,
1463
+ destination: this.#destination.id
1464
+ };
1465
+ if (tile?.dataset.shareKitAsset) detail.asset = tile.dataset.shareKitAsset;
1466
+ return detail;
1467
+ }
1366
1468
  #getDevice() {
1367
1469
  return this.#capabilities.has("mobile") ? "mobile" : "desktop";
1368
1470
  }
@@ -1397,8 +1499,9 @@ var ShareKitElement = class extends BaseElement {
1397
1499
  }
1398
1500
  #handleClick = (event) => {
1399
1501
  if (!(event.target instanceof Element)) return;
1400
- if (event.target.closest("[data-share-kit-trigger]")) {
1401
- this.#open();
1502
+ const trigger = event.target.closest("[data-share-kit-trigger]");
1503
+ if (trigger) {
1504
+ this.#open(trigger.dataset.shareKitTrigger);
1402
1505
  return;
1403
1506
  }
1404
1507
  const button = event.target.closest("[data-share-kit-action]");
@@ -1410,7 +1513,7 @@ var ShareKitElement = class extends BaseElement {
1410
1513
  clearOpenHash(this.#payload?.settings.openHash);
1411
1514
  };
1412
1515
  #handleHashChange = () => {
1413
- if (isOpenHash(this.#payload?.settings.openHash)) this.#open();
1516
+ if (this.#ownsOpenHash()) this.#open();
1414
1517
  };
1415
1518
  #handleInput = (event) => {
1416
1519
  if (event.target === this.#parts?.caption) this.#isCaptionEdited = true;
@@ -1445,14 +1548,18 @@ var ShareKitElement = class extends BaseElement {
1445
1548
  if (!signal.aborted && this.#parts) patchIcons(this.#parts.dialog, icons);
1446
1549
  });
1447
1550
  }
1448
- #open() {
1551
+ #open(destinationId) {
1449
1552
  this.#loadIcons();
1450
1553
  const parts = this.#parts ?? this.#render();
1451
1554
  if (!parts || parts.dialog.open) return;
1555
+ const destination = this.#getRendered(destinationId);
1556
+ if (destination) this.#selectDestination(destination);
1557
+ if (destination && this.#lists?.more.includes(destination)) this.#showMore();
1452
1558
  parts.dialog.showModal();
1453
1559
  parts.heading.focus();
1454
1560
  this.#track("open");
1455
1561
  this.#isOver = false;
1562
+ parts.alert.textContent = "";
1456
1563
  this.#syncCaption();
1457
1564
  }
1458
1565
  async #openQr() {
@@ -1463,6 +1570,12 @@ var ShareKitElement = class extends BaseElement {
1463
1570
  this.#feedback?.announce(this.#strings.error);
1464
1571
  }
1465
1572
  }
1573
+ #ownsOpenHash() {
1574
+ const openHash = this.#payload?.settings.openHash;
1575
+ if (!isOpenHash(openHash)) return false;
1576
+ for (const element of document.querySelectorAll(this.localName)) if (element instanceof ShareKitElement && element.#payload?.settings.openHash === openHash) return element === this;
1577
+ return false;
1578
+ }
1466
1579
  #prefetch() {
1467
1580
  if (!this.#capabilities.has("files") || !this.#capabilities.has("mobile")) return;
1468
1581
  const tile = this.#getSuggestedAsset();
@@ -1513,10 +1626,6 @@ var ShareKitElement = class extends BaseElement {
1513
1626
  if (this.isConnected) this.connectedCallback();
1514
1627
  }, { once: true });
1515
1628
  }
1516
- async #saveAsset(tile) {
1517
- this.#track("save", tile);
1518
- this.#report(await this.#files?.save(tile));
1519
- }
1520
1629
  #selectDestination(destination) {
1521
1630
  const previousUrl = this.#getUrl();
1522
1631
  const caption = this.#parts?.caption;
@@ -1527,25 +1636,21 @@ var ShareKitElement = class extends BaseElement {
1527
1636
  }
1528
1637
  this.#syncDestination();
1529
1638
  }
1530
- async #shareAssets(tiles) {
1531
- const [first] = tiles;
1532
- if (!first) return;
1533
- this.#track("share", first);
1534
- this.#report(await this.#files?.share(tiles));
1535
- }
1536
- async #shareData(action, data) {
1537
- this.#track(action);
1538
- try {
1539
- await navigator.share(data);
1540
- } catch (error) {
1541
- if (!isDomError(error, "AbortError")) this.#feedback?.announce(this.#strings.error);
1542
- }
1639
+ async #settle(action, pending, tile) {
1640
+ const detail = this.#getDetail(action, tile);
1641
+ const outcome = await pending;
1642
+ this.#report(outcome);
1643
+ if (!detail || !outcome) return;
1644
+ this.#dispatch({
1645
+ ...detail,
1646
+ outcome: outcome === "unsaved" ? "error" : outcome
1647
+ });
1543
1648
  }
1544
- #showMore(button) {
1649
+ #showMore() {
1545
1650
  const hidden = [...this.querySelectorAll("[role=\"radio\"][hidden]")];
1546
1651
  for (const radio of hidden) radio.hidden = false;
1547
- button.hidden = true;
1548
- hidden[0]?.focus();
1652
+ if (this.#parts?.more) this.#parts.more.hidden = true;
1653
+ return hidden;
1549
1654
  }
1550
1655
  #syncCaption() {
1551
1656
  const parts = this.#parts;
@@ -1553,12 +1658,14 @@ var ShareKitElement = class extends BaseElement {
1553
1658
  if (!parts || !destination) return;
1554
1659
  const { isOver, message } = syncCaptionView({
1555
1660
  destination,
1661
+ isEdited: this.#isCaptionEdited,
1556
1662
  parts,
1557
1663
  primaryAction: this.#getPrimaryAction(),
1558
1664
  strings: this.#strings
1559
1665
  });
1560
- if (isOver && !this.#isOver && parts.dialog.open) this.#feedback?.announce(message);
1561
- this.#isOver = isOver;
1666
+ const isAnnounced = isOver && parts.dialog.open;
1667
+ if (isAnnounced !== this.#isOver) parts.alert.textContent = isAnnounced ? message : "";
1668
+ this.#isOver = isAnnounced;
1562
1669
  }
1563
1670
  #syncDestination() {
1564
1671
  const parts = this.#parts;
@@ -1570,7 +1677,8 @@ var ShareKitElement = class extends BaseElement {
1570
1677
  destination,
1571
1678
  parts,
1572
1679
  strings: this.#strings,
1573
- suggested: this.#getSuggestedAsset()
1680
+ suggested: this.#getSuggestedAsset(),
1681
+ url: this.#getUrl()
1574
1682
  });
1575
1683
  if (!this.#isCaptionEdited) parts.caption.value = fitCaption({
1576
1684
  description: payload.description,
@@ -1581,17 +1689,8 @@ var ShareKitElement = class extends BaseElement {
1581
1689
  this.#prefetch();
1582
1690
  }
1583
1691
  #track(action, tile) {
1584
- if (!this.#destination) return;
1585
- const detail = {
1586
- action,
1587
- destination: this.#destination.id
1588
- };
1589
- if (tile?.dataset.shareKitAsset) detail.asset = tile.dataset.shareKitAsset;
1590
- this.dispatchEvent(new CustomEvent(shareKitActionEventName, {
1591
- bubbles: true,
1592
- composed: true,
1593
- detail
1594
- }));
1692
+ const detail = this.#getDetail(action, tile);
1693
+ if (detail) this.#dispatch(detail);
1595
1694
  }
1596
1695
  };
1597
1696
  // src/define.ts
@@ -1631,6 +1730,6 @@ function defineShareKit({ destinations, icons = {} }) {
1631
1730
  warnMissingComposers(destinations);
1632
1731
  customElements.define(tagName, createElementClass(catalogue, new ShareIconSource(icons)));
1633
1732
  }
1634
- export { defineShareKit, defineShareKitDestination, getShareKitImageFormats, serializeShareKitPayload, shareKitActionEventName, shareKitAnywhere, shareKitBluesky, shareKitCountGraphemes, shareKitCountLength, shareKitCountStringz, shareKitCountUtf8, shareKitCountWeighted, shareKitFacebook, shareKitInstagram, shareKitInstagramStory, shareKitInterfaceIcons, shareKitLandscapeFormat, shareKitLinkedin, shareKitMastodon, shareKitMessages, shareKitPinFormat, shareKitPinterest, shareKitPortraitFormat, shareKitReddit, shareKitShortenedLinkCost, shareKitStoryFormat, shareKitThreads, shareKitWithLinkCost, shareKitX };
1733
+ export { defineShareKit, defineShareKitDestination, getShareKitImageFormats, serializeShareKitPayload, shareKitActionEventName, shareKitAnywhere, shareKitBluesky, shareKitCountGraphemes, shareKitCountLength, shareKitCountStringz, shareKitCountUtf8, shareKitCountWeighted, shareKitFacebook, shareKitInstagram, shareKitInstagramStory, shareKitInterfaceIcons, shareKitLandscapeFormat, shareKitLine, shareKitLinkedin, shareKitMastodon, shareKitMessages, shareKitPinFormat, shareKitPinterest, shareKitPortraitFormat, shareKitReddit, shareKitShortenedLinkCost, shareKitStoryFormat, shareKitThreads, shareKitWithLinkCost, shareKitX };
1635
1734
 
1636
1735
  //# sourceMappingURL=index.mjs.map