-
-
Notifications
You must be signed in to change notification settings - Fork 529
feat: add Share button to package pages via Web Share API #2997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomayac
wants to merge
11
commits into
npmx-dev:main
Choose a base branch
from
tomayac:feat/web-share-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8d40798
feat: add Share button to package pages via Web Share API
tomayac cc549bc
Merge branch 'main' into feat/web-share-button
tomayac c5e8af6
Merge branch 'main' into feat/web-share-button
tomayac 73be0d0
Merge branch 'main' into feat/web-share-button
tomayac be39f21
Merge branch 'main' into feat/web-share-button
tomayac 21c948c
[autofix.ci] apply automated fixes
autofix-ci[bot] 63cf743
Merge branch 'main' into feat/web-share-button
tomayac 1e2d7be
Merge branch 'main' into feat/web-share-button
tomayac 85e786d
chore: use share logic at share button at header
userquin 5af436f
chore: add package share shortcut at appfooter
userquin d1f61a5
chore: enable shortcut when keyboard shortcuts setting is enabled
userquin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <script setup lang="ts"> | ||
| const props = defineProps<{ | ||
| packageName: string | ||
| description?: string | null | ||
| }>() | ||
|
|
||
| const canShare = 'share' in navigator | ||
| const buttonRef = useTemplateRef<{ click: () => void }>('buttonRef') | ||
| const keyboardShortcutsEnabled = useKeyboardShortcuts() | ||
|
|
||
| if (canShare) { | ||
| onKeyStroke( | ||
| e => | ||
| keyboardShortcutsEnabled.value && | ||
| isKeyWithoutModifiers(e, 'v') && | ||
| !isEditableElement(e.target), | ||
| e => { | ||
| e.preventDefault() | ||
| // Click the button element so the browser anchors the share sheet at | ||
| // the button's position rather than the current mouse cursor position. | ||
| buttonRef.value?.click() | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| async function getOgImageFile(): Promise<File | null> { | ||
| // Files sharing is not universally supported; bail out early if not available. | ||
| if (!navigator.canShare?.({ files: [new File([], 'x.png', { type: 'image/png' })] })) { | ||
| return null | ||
| } | ||
| try { | ||
| const ogMeta = document.querySelector<HTMLMetaElement>('meta[property="og:image"]') | ||
| if (!ogMeta?.content) return null | ||
| const blob = await fetch(ogMeta.content).then(r => r.blob()) | ||
| if (!blob.type.startsWith('image/')) return null | ||
| return new File([blob], `${props.packageName}.png`, { type: blob.type }) | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| const sharing = shallowRef(false) | ||
|
|
||
| async function share() { | ||
| if (sharing.value) { | ||
| return | ||
| } | ||
| sharing.value = true | ||
| const shareData: ShareData = { | ||
| title: props.packageName, | ||
| text: props.description ?? props.packageName, | ||
| url: window.location.href, | ||
| } | ||
|
|
||
| const imageFile = await getOgImageFile() | ||
| if (imageFile) { | ||
| const shareDataWithFile: ShareData = { ...shareData, files: [imageFile] } | ||
| // Some implementations support sharing files or url/text, but not the | ||
| // combination of both. Only attach the file once the full payload | ||
| // (title + text + url + files) is confirmed shareable, so we keep the | ||
| // url/text fallback otherwise. | ||
| if (navigator.canShare?.(shareDataWithFile)) { | ||
| shareData.files = shareDataWithFile.files | ||
| } | ||
| } | ||
|
|
||
| await navigator | ||
| .share(shareData) | ||
| .catch(() => {}) | ||
| .finally(() => (sharing.value = false)) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| defineExpose({ sharePackage: share }) | ||
| </script> | ||
|
|
||
| <template> | ||
| <ButtonBase | ||
| v-if="canShare" | ||
| ref="buttonRef" | ||
| variant="secondary" | ||
| :classicon="sharing ? 'i-svg-spinners:ring-resize' : 'i-lucide:share-2'" | ||
| :aria-label="$t('package.share_aria_label', { package: packageName })" | ||
| :ariaKeyshortcuts="keyboardShortcutsEnabled ? 'v' : undefined" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I press Ctrl + click in my IDE (WebStorm) on |
||
| @click="share" | ||
| > | ||
| <span class="max-sm:sr-only">{{ $t('package.links.share') }}</span> | ||
| </ButtonBase> | ||
| </template> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.