feat: share using navigator api

This commit is contained in:
Ruidy 2024-12-19 19:17:26 +01:00
parent c22169def7
commit 34c1076523
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -2,6 +2,25 @@
import type { Recipe } from "~/types/recipe";
defineProps<{ recipe: Recipe }>();
const shareRecipe = async (recipe: Recipe) => {
const url =
useRequestURL().href.split("/").slice(0, -1).join("/") + "/" + recipe.id;
if (navigator.share) {
try {
await navigator.share({
title: recipe.title,
text: `Check out this recipe: ${recipe.title}`,
url,
});
} catch (error) {
alert("Failed to share the recipe.");
}
} else {
alert("Sharing not supported on this device.");
}
};
</script>
<template>
@ -29,6 +48,10 @@ defineProps<{ recipe: Recipe }>();
<p class="prose prose-lg max-w-none w-full">
{{ recipe.instructions }}
</p>
<button class="btn btn-accent mt-4" @click="shareRecipe(recipe)">
<icon name="uil:share-alt" class="mr-2 w-6 h-6" />
Share Recipe
</button>
</div>
</div>
</template>