-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d245e8d
commit cfc3367
Showing
9 changed files
with
268 additions
and
69 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"use client"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { RoundedButton } from "../Account/LoggedInView"; | ||
import { showSnackBar } from "../SnackBar/SnackBar"; | ||
|
||
interface BeforeInstallPromptEvent extends Event { | ||
prompt: () => Promise<void>; | ||
} | ||
|
||
declare global { | ||
interface WindowEventMap { | ||
beforeinstallprompt: BeforeInstallPromptEvent; | ||
appinstalled: Event; | ||
} | ||
} | ||
|
||
export const PWAButton = ({ | ||
defaultDisabled = true, | ||
}: { | ||
defaultDisabled?: boolean; | ||
}) => { | ||
const deferredPromptRef = useRef<BeforeInstallPromptEvent | null>(null); | ||
const [ready, setReady] = useState(false); | ||
const [isAleadyInstalled, setIsAlreadyInstalled] = useState(false); | ||
|
||
useEffect(() => { | ||
const checkIfInstalled = () => { | ||
const isStandalone = window.matchMedia( | ||
"(display-mode: standalone)" | ||
).matches; | ||
const isIosStandalone = | ||
"standalone" in window.navigator && | ||
(window.navigator as unknown as { standalone: boolean }).standalone; | ||
setIsAlreadyInstalled(isStandalone || Boolean(isIosStandalone)); | ||
}; | ||
|
||
checkIfInstalled(); | ||
|
||
const beforeInstallPromptHandler = (event: BeforeInstallPromptEvent) => { | ||
setReady(true); | ||
deferredPromptRef.current = event; | ||
}; | ||
|
||
const appInstalledHandler = () => { | ||
console.log("PWA was successfully installed!"); | ||
showSnackBar({ | ||
message: "アプリに追加しました。ホーム画面から起動してください。", | ||
type: "success", | ||
}); | ||
}; | ||
|
||
window.addEventListener("beforeinstallprompt", beforeInstallPromptHandler); | ||
window.addEventListener("appinstalled", appInstalledHandler); // appinstalledイベントをリッスン | ||
|
||
return () => { | ||
window.removeEventListener( | ||
"beforeinstallprompt", | ||
beforeInstallPromptHandler | ||
); | ||
window.removeEventListener("appinstalled", appInstalledHandler); | ||
}; | ||
}, []); | ||
|
||
const handleClickInstall = async () => { | ||
const deferredPrompt = deferredPromptRef.current; | ||
if (deferredPrompt) { | ||
deferredPrompt.prompt(); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{isAleadyInstalled ? ( | ||
<RoundedButton variant="outlined" color="success" disabled> | ||
アプリに追加済み! | ||
</RoundedButton> | ||
) : ( | ||
<RoundedButton | ||
variant="outlined" | ||
disabled={!ready || defaultDisabled} | ||
onClick={handleClickInstall} | ||
> | ||
アプリに追加 | ||
</RoundedButton> | ||
)} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.buttonContainer { | ||
display: flex; | ||
gap: 15px; | ||
justify-content: center; | ||
width: 100%; | ||
|
||
& > div { | ||
display: flex; | ||
width: 100%; | ||
|
||
&:nth-child(1) { | ||
justify-content: flex-end; | ||
} | ||
|
||
&:nth-child(2) { | ||
justify-content: flex-start; | ||
} | ||
} | ||
} |
Oops, something went wrong.