Skip to content

Commit

Permalink
feat: share button
Browse files Browse the repository at this point in the history
  • Loading branch information
yongsk0066 committed Jun 15, 2024
1 parent a6a1671 commit 8a835a1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ pnpm-debug.log*

# macOS-specific files
.DS_Store

Ø
Binary file modified .yarn/install-state.gz
Binary file not shown.
22 changes: 22 additions & 0 deletions src/components/Head.astro
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,28 @@ const { title, description, image = "/nano.png" } = Astro.props;
const backToPrev = document.getElementById("back-to-prev");
backToPrev?.addEventListener("click", () => window.history.back());

const shareButton = document.getElementById("share-button");
shareButton?.addEventListener("click", () => {
const url = window.location.href;
if (navigator.share) {
navigator.share({
title: document.title,
text: document.querySelector('meta[name="description"]').content,
url: url,
});
} else {
navigator.clipboard
.writeText(url)
.then(() => {
alert("링크가 복사되었어요!");
})
.catch((error) => {
console.error("클립보드에 복사 실패:", error);
prompt("아래 링크를 복사하세요!", url);
});
}
});

const lightThemeButton = document.getElementById("light-theme-button");
lightThemeButton?.addEventListener("click", () => {
localStorage.setItem("theme", "light");
Expand Down
18 changes: 18 additions & 0 deletions src/components/Share.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<button
id="share-button"
class="relative group w-fit flex pl-8 pr-3 py-1.5 flex-nowrap rounded border border-black/15 dark:border-white/20 hover:bg-black/5 dark:hover:bg-white/5 hover:text-black dark:hover:text-white transition-colors duration-300 ease-in-out
mx-auto my-4"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
class="absolute top-1/2 left-2 -translate-y-1/2 size-4 stroke-2 fill-none stroke-current
group-hover:rotate-180 transition-transform duration-300 ease-in-out"
>
<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"
></path>
<path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"
></path>
</svg>
<div class="text-sm">공유 하기</div>
</button>
4 changes: 3 additions & 1 deletion src/pages/blog/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FormattedDate from "@components/FormattedDate.astro";
import { readingTime } from "@lib/utils";
import BackToPrev from "@components/BackToPrev.astro";
import Comments from "@components/Comments.astro";
import Share from "@components/Share.astro";
export async function getStaticPaths() {
const posts = (await getCollection("blog"))
Expand Down Expand Up @@ -101,7 +102,8 @@ const { Content } = await post.render();
</div>
)
}
<Comments />
<Share />
<Comments client:idle />
</Container>
</PageLayout>
<!--- Starts script --->
Expand Down

0 comments on commit 8a835a1

Please sign in to comment.