Skip to content

Commit

Permalink
add install button
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhattin committed Dec 27, 2024
1 parent 89128c7 commit 3f1da6a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/components/TopAppBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import { YoutubeIcon } from '~/components/icons';
import { page } from '$app/stores';
import { PAGE_TITLES } from '~/state/page_titles';
import { pwa_event_triggerer, pwa_install_event_fired } from '~/state/main';
import { OiDownload24 } from 'svelte-icons-pack/oi';
type Props = {
start?: import('svelte').Snippet;
Expand Down Expand Up @@ -109,6 +111,17 @@
/>
<span>Project's Github Page</span>
</a>
{#if $pwa_install_event_fired}
<button
class="will-close select-none gap-1 px-2 py-1 text-sm outline-none"
onclick={async () => {
if ($pwa_install_event_fired) await $pwa_event_triggerer.prompt();
}}
>
<Icon src={OiDownload24} class="-mt-1 text-base" />
Install
</button>
{/if}
<div class="wont-close flex space-x-3 rounded-md px-2 py-1">
<span class="mt-1">Set Theme</span>
<ThemeChanger />
Expand Down
10 changes: 10 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import { QueryClientProvider } from '@tanstack/svelte-query';
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools';
import { queryClient } from '~/state/query';
import { pwa_event_triggerer, pwa_install_event_fired } from '~/state/main';
import { onMount } from 'svelte';
interface Props {
children?: import('svelte').Snippet;
Expand All @@ -19,6 +21,14 @@
initializeStores();
storePopup.set({ computePosition, autoUpdate, offset, shift, flip, arrow });
onMount(() => {
window.addEventListener('beforeinstallprompt', (event) => {
event.preventDefault();
$pwa_event_triggerer = event;
$pwa_install_event_fired = true;
});
});
</script>

<svelte:head>
Expand Down
7 changes: 7 additions & 0 deletions src/state/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { writable } from 'svelte/store';

/**
* This should not be true if PWA is not installable or is already installed
*/
export let pwa_install_event_fired = writable(false);
export let pwa_event_triggerer = writable<any | null>(null);

0 comments on commit 3f1da6a

Please sign in to comment.