mirror of
https://github.com/Vikeo/LifeTrinket.git
synced 2025-11-14 06:58:00 +00:00
show and hide things based on ios, add button for installing PWA
This commit is contained in:
45
src/Components/Misc/InstallPWAButton.tsx
Normal file
45
src/Components/Misc/InstallPWAButton.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { BeforeInstallPromptEvent } from '../../global';
|
||||
import { useAnalytics } from '../../Hooks/useAnalytics';
|
||||
|
||||
export const InstallPWA = () => {
|
||||
const supportsPWARef = useRef<boolean>(false);
|
||||
const [promptInstall, setPromptInstall] =
|
||||
useState<BeforeInstallPromptEvent | null>(null);
|
||||
|
||||
const analytics = useAnalytics();
|
||||
|
||||
const handler = (e: BeforeInstallPromptEvent) => {
|
||||
e.preventDefault();
|
||||
supportsPWARef.current = true;
|
||||
setPromptInstall(e);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('beforeinstallprompt', handler);
|
||||
|
||||
return () => window.removeEventListener('transitionend', handler);
|
||||
}, []);
|
||||
|
||||
if (!supportsPWARef.current) {
|
||||
return 'lull';
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
className="mt-1 mb-1 bg-primary-main px-3 py-1 rounded-md duration-200 ease-in-out shadow-[1px_2px_4px_0px_rgba(0,0,0,0.3)] hover:bg-primary-dark font-bold"
|
||||
aria-label="Install app"
|
||||
title="Install app"
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
if (!promptInstall) {
|
||||
return;
|
||||
}
|
||||
analytics.trackEvent('install_pwa_prompt_shown');
|
||||
promptInstall.prompt();
|
||||
}}
|
||||
>
|
||||
Install as a PWA
|
||||
</button>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user