feat(pwa): implement PWA installation banner and context provider
- Add `generate-icons.ts` script to generate PWA icons from SVG. - Create `InstallBanner` component for prompting users to install the app. - Implement `PwaProvider` to manage PWA installation state and events. - Export `PwaProvider` and `InstallBanner` from the PWA feature index.
This commit is contained in:
66
apps/web/scripts/generate-icons.ts
Normal file
66
apps/web/scripts/generate-icons.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Genera el set de iconos PWA desde el isotipo SVG de marca.
|
||||
*
|
||||
* Uso: bun --filter @gruperly/web icons
|
||||
*
|
||||
* Las salidas se versionan en `public/`; el build de Vite no las regenera
|
||||
* (sharp es una devDependency y no debe entrar en la imagen de Docker).
|
||||
* Re-correr solo cuando cambia `src/assets/brand/isotipo-y-organica.svg`.
|
||||
*/
|
||||
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import sharp from 'sharp'
|
||||
|
||||
const webDir = dirname(dirname(fileURLToPath(import.meta.url)))
|
||||
const source = join(webDir, 'src/assets/brand/isotipo-y-organica.svg')
|
||||
const publicDir = join(webDir, 'public')
|
||||
|
||||
/** Fondo del tema claro; también sirve de lienzo para los iconos opacos. */
|
||||
const BACKGROUND = '#ffffff'
|
||||
const TRANSPARENT = { r: 0, g: 0, b: 0, alpha: 0 } as const
|
||||
|
||||
type IconTarget = {
|
||||
file: string
|
||||
size: number
|
||||
/** Rellena el lienzo con `BACKGROUND` en vez de dejar transparencia. */
|
||||
opaque: boolean
|
||||
/** Fracción del lado que ocupa el logo. 1 = sin padding. */
|
||||
contentRatio: number
|
||||
}
|
||||
|
||||
const targets: IconTarget[] = [
|
||||
{ file: 'pwa-192x192.png', size: 192, opaque: false, contentRatio: 0.9 },
|
||||
{ file: 'pwa-512x512.png', size: 512, opaque: false, contentRatio: 0.9 },
|
||||
// Android recorta los iconos adaptativos en círculo: el contenido útil
|
||||
// vive en el 80% central del canvas, así que el logo va over white y escalado.
|
||||
{ file: 'pwa-maskable-512x512.png', size: 512, opaque: true, contentRatio: 0.8 },
|
||||
{ file: 'apple-touch-icon-180.png', size: 180, opaque: true, contentRatio: 0.82 },
|
||||
]
|
||||
|
||||
// El viewBox del isotipo no arranca en 0 y deja aire alrededor del glifo:
|
||||
// `trim` recorta ese margen para que el encuadre use el área real del dibujo.
|
||||
const logo = await sharp(await readFile(source), { density: 512 })
|
||||
.trim({ threshold: 1 })
|
||||
.png()
|
||||
.toBuffer()
|
||||
|
||||
await mkdir(publicDir, { recursive: true })
|
||||
|
||||
for (const { file, size, opaque, contentRatio } of targets) {
|
||||
const box = Math.round(size * contentRatio)
|
||||
const layer = await sharp(logo)
|
||||
.resize(box, box, { fit: 'inside' })
|
||||
.png()
|
||||
.toBuffer()
|
||||
|
||||
const output = await sharp({
|
||||
create: { width: size, height: size, channels: 4, background: opaque ? BACKGROUND : TRANSPARENT },
|
||||
})
|
||||
.composite([{ input: layer, gravity: 'center' }])
|
||||
.png({ compressionLevel: 9 })
|
||||
.toBuffer()
|
||||
|
||||
await writeFile(join(publicDir, file), output)
|
||||
console.log(`${file.padEnd(28)} ${size}x${size} ${(output.byteLength / 1024).toFixed(1)} KB`)
|
||||
}
|
||||
Reference in New Issue
Block a user