From 34c10765232cda927bed2734afa1ec5af00c4dda Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 19 Dec 2024 19:17:26 +0100 Subject: [PATCH] feat: share using navigator api --- components/recipe/view.vue | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/components/recipe/view.vue b/components/recipe/view.vue index 56c9d4c..4630118 100644 --- a/components/recipe/view.vue +++ b/components/recipe/view.vue @@ -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."); + } +};