Skip to content

Commit 1a48d7d

Browse files
committed
feat: add install snippet on the home page
1 parent 6170bbb commit 1a48d7d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Web/Homepage/home.view.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
</svg>
4141
</a>
4242
</div>
43+
<button data-copy="#install-tempest-snippet" class="group mt-6 md:mt-8 xl:mt-10 flex items-center gap-x-2 text-base font-mono relative cursor-pointer">
44+
<x-icon name="tabler:terminal" class="size-5 text-(--ui-primary)" />
45+
<span id="install-tempest-snippet" class="text-(--ui-text-muted)">composer require tempest/framework:{{ \Tempest\Core\Kernel::VERSION }}</span>
46+
<span class="ml-4 flex items-center justify-center opacity-0 group-hover:opacity-100 transition text-(--ui-text-dimmed) bg-(--ui-bg-muted) rounded border border-(--ui-border)">
47+
<x-icon name="tabler:copy" class="size-5 absolute" />
48+
<x-icon name="tabler:copy-check-filled" class="size-5 absolute opacity-0 group-[[data-copied]]:opacity-100 transition text-(--ui-success)" />
49+
</span>
50+
</button>
4351
</section>
4452
<!-- Discovery -->
4553
<x-home-section

src/Web/scripts/copy-code-blocks.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function extractPlainText(pre: HTMLElement, button: HTMLButtonElement): string {
1+
function extractPlainText(pre: HTMLElement, button?: HTMLButtonElement): string {
22
return Array.from(pre.childNodes)
33
.filter((node) => node !== button)
44
.map((node) =>
@@ -39,3 +39,26 @@ document.addEventListener('DOMContentLoaded', () => {
3939
})
4040
})
4141
})
42+
43+
document.addEventListener('DOMContentLoaded', () => {
44+
document.querySelectorAll<HTMLPreElement>('button[data-copy]').forEach((button) => {
45+
button.addEventListener('click', () => {
46+
const target = document.querySelector(button.dataset.copy!) as HTMLElement
47+
console.log(target)
48+
49+
if (!target) {
50+
return
51+
}
52+
53+
const content = extractPlainText(target)
54+
55+
navigator.clipboard
56+
.writeText(content)
57+
.then(() => {
58+
button.setAttribute('data-copied', 'true')
59+
setTimeout(() => button.removeAttribute('data-copied'), 2000)
60+
})
61+
.catch((err) => console.error('Copy failed', err))
62+
})
63+
})
64+
})

0 commit comments

Comments
 (0)