-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also extracted a re-usable share button component.
- Loading branch information
1 parent
e9d4807
commit 2de8847
Showing
3 changed files
with
98 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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,66 @@ | ||
<script> | ||
import {Button, Modal, Toast} from 'spaper' | ||
import {getContext} from 'svelte' | ||
import ShareIcon from '$lib/ShareIcon.svelte' | ||
const {getAnalytics} = getContext('analytics') | ||
export let url | ||
export let shareText | ||
export let fallback = () => { | ||
Modal.open({ | ||
title: 'Copy the below link and share it with your friends!', | ||
content: url, | ||
}) | ||
} | ||
async function share() { | ||
try { | ||
await navigator.share({ | ||
title: 'Schedule Your Social', | ||
text: shareText, | ||
url, | ||
}) | ||
trackShareInviteLink('native') | ||
} catch (error) { | ||
console.warn('Unable to use native sharing', error) | ||
if (error.name === 'AbortError') return // If we try to fallback to 'copy' after this it results in a NotAllowedErorr. I'm guesing this is because when the user aborts share it resets Safari's concept of if is a valid and safe clipboard operation triggered by a user interaction (see https://webkit.org/blog/10247/new-webkit-features-in-safari-13-1/). | ||
try { | ||
await copy() | ||
Toast.open({ | ||
message: 'Link copied to clipiboard', | ||
type: 'success', | ||
position: 'top', | ||
}) | ||
trackShareInviteLink('auto copy with dialog') | ||
} catch (error) { | ||
// One example of when this would happen is in some Android Webviews that don't provide copy permissions. | ||
console.warn('Unable to copy to clipboard', error) | ||
trackShareInviteLink('manual copy with dialog') | ||
fallback() | ||
} | ||
} | ||
} | ||
async function copy() { | ||
if (!navigator.clipboard) | ||
throw new Error('Navigator clipboard not available') | ||
await navigator.clipboard.writeText(url) | ||
} | ||
function trackShareInviteLink(method) { | ||
getAnalytics().trackEvent('Share invite link', { | ||
props: { | ||
method, | ||
}, | ||
}) | ||
} | ||
</script> | ||
|
||
<Button on:click={share} type="success"> | ||
<span style:margin-right="0.2rem"> | ||
<slot /> | ||
</span> | ||
<ShareIcon /> | ||
</Button> |
This file contains 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