Skip to content

Commit

Permalink
show native share sheet on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
tsriram committed Oct 12, 2023
1 parent 0544993 commit 11d45ac
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/lib/components/ShareGameInfobox.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script lang="ts">
import { browser } from '$app/environment';
import { trackShare } from '$lib/analytics';
import Button from '$lib/components/Button.svelte';
let url = '';
const showShareButton = Boolean(navigator.share);
if (browser) {
url = window.location.toString();
Expand All @@ -10,14 +13,33 @@
console.error('Unable to copy text: ', err);
});
}
function share() {
if (navigator.share) {
navigator
.share({
title: 'Connect 4',
text: `Hey! Let's play Connect 4 online?`,
url: window.location.toString()
})
.then(trackShare)
.catch((error) => console.log('Error sharing', error));
} else {
console.log('NO SHARE');
}
}
</script>

<div class="info-container">
<h4>Share this link with your friend to start playing:</h4>
<p class="info">
<span class="url">{url}</span>
<button class="copy-btn" on:click={onCopyClick}>Copy</button>
</p>
{#if showShareButton}
<Button on:click={share}>Invite a fried to play!</Button>
{:else}
<h4>Share this link with your friend to start playing:</h4>
<p class="info">
<span class="url">{url}</span>
<button class="copy-btn" on:click={onCopyClick}>Copy</button>
</p>
{/if}
</div>

<style>
Expand Down

0 comments on commit 11d45ac

Please sign in to comment.