Skip to content

Commit

Permalink
Add "Install App" button
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreb committed Nov 16, 2024
1 parent 087f227 commit 8376204
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/app/app.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,23 @@ button:hover {
font-size: 1.2rem;
color: #666;
}

.install-button {
position: fixed;
bottom: 20px;
right: 20px;
padding: 12px 24px;
background-color: #007bff;
color: white;
border: none;
border-radius: 24px;
cursor: pointer;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
z-index: 1000;
transition: transform 0.2s ease;
}

.install-button:hover {
transform: scale(1.05);
background-color: #0056b3;
}
6 changes: 6 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@
}
</div>

@if (showInstallButton) {
<button class="install-button" (click)="installPwa()">
Install App
</button>
}

<router-outlet />
32 changes: 32 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export class AppComponent {
blurredImageUrl: string | null = null;
isProcessing = false;
blurAmount = 100; // Default blur amount
private deferredPrompt: any;
showInstallButton = false;

constructor() {
this.handleInstallPrompt();
}

onDragOver(event: DragEvent) {
event.preventDefault();
Expand Down Expand Up @@ -148,6 +154,32 @@ export class AppComponent {
});
}

private handleInstallPrompt() {
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
this.deferredPrompt = e;
this.showInstallButton = true;
});

window.addEventListener('appinstalled', () => {
this.showInstallButton = false;
this.deferredPrompt = null;
});
}

async installPwa() {
if (!this.deferredPrompt) return;

this.deferredPrompt.prompt();
const { outcome } = await this.deferredPrompt.userChoice;

if (outcome === 'accepted') {
this.showInstallButton = false;
}

this.deferredPrompt = null;
}

downloadImage() {
if (this.blurredImageUrl) {
const link = document.createElement('a');
Expand Down

0 comments on commit 8376204

Please sign in to comment.