@tristan2828/ui-foundation 4.150.0 → 4.151.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@ to the device, where the app's service worker shows it.
9
9
 
10
10
  | Part | Where | What it does |
11
11
  |---|---|---|
12
- | `pwa({ name })` | `vite.config.ts`, from `@tristan2828/ui-foundation/vite` | The app installable: a manifest, icons in the theme's colours, and `/push-sw.js`, the service worker that shows pushes and opens their path |
12
+ | `pwa({ name })` | `vite.config.ts`, from `@tristan2828/ui-foundation/vite` | The app installable: a manifest, icons in the theme's colours (or the app's own), and `/push-sw.js`, the service worker that shows pushes and opens their path |
13
13
  | `usePushPermission()` | the package | Whether this device gets reminders, and `enable`/`disable` |
14
14
  | `AllowReminders` | the package | The control: says what reminders are, then asks, on a click |
15
15
  | `/push/public-key`, `/push/subscriptions` | `openapi.yaml`, the foundation's part (optional) | The VAPID key to subscribe with; this device's subscription saved or dropped |
@@ -53,6 +53,38 @@ there is no push at all, so `usePushPermission` says `needs-install` and
53
53
  `AllowReminders` shows the steps. `pwa()`'s manifest (`display:
54
54
  standalone`) is what makes the Home Screen app an app.
55
55
 
56
+ ## The app's own icon
57
+
58
+ Without one, `pwa()` draws the icons: an SVG with the name's first letter,
59
+ and PNGs of the theme colour alone (a letter in a PNG needs a font
60
+ rasteriser the package doesn't carry). An app with its own mark passes it,
61
+ each a path from the Vite root:
62
+
63
+ ```ts
64
+ pwa({
65
+ name: 'Quick Jot',
66
+ icon: 'icons/icon-512.png', // a square PNG, 512×512 or more
67
+ smallIcon: 'icons/icon-192.png', // the same at 192×192
68
+ maskableIcon: 'icons/icon-maskable-512.png', // the mark inside the safe zone, background to the edges
69
+ iconSvg: 'icons/icon.svg', // the mark as an SVG
70
+ })
71
+ ```
72
+
73
+ | Option | In the manifest | Used for |
74
+ |---|---|---|
75
+ | `icon` (a PNG) | an `any` entry | iOS's Home Screen (the apple-touch-icon), a push's icon without `smallIcon`, and wherever nothing better fits |
76
+ | `smallIcon` (a PNG) | an `any` entry | Windows' Start menu and taskbar and the install prompt, which would scale the large one down; a push's icon |
77
+ | `maskableIcon` (a PNG) | the `maskable` entry | Android's Home Screen, cropped to the launcher's circle or squircle. Keep the mark inside the central 80% circle and the background to every edge. Without one, Android draws `icon` shrunk on a plain disc |
78
+ | `iconSvg` (an SVG) | an `any` entry with `sizes: "any"` | Chrome and Android, sharp at every size |
79
+
80
+ Each PNG's `sizes` is read from its own header, so a 1024×1024 `icon` is
81
+ named 1024x1024. The three beside `icon` need it (iOS takes a PNG only)
82
+ and are each optional: `icon` alone gives the manifest that one entry, as
83
+ before. `pwa()` can't resize a PNG (that needs a rasteriser, which isn't on
84
+ the allowlist), so export the 192 and maskable copies from the same artwork
85
+ in an image editor and commit the PNGs beside the large one. An SVG passed as `icon` is refused:
86
+ pass it as `iconSvg` beside a PNG.
87
+
56
88
  ## Home-screen shortcuts
57
89
 
58
90
  `pwa({ name, shortcuts: [{ name: 'Add To-Do', url: '/todo/add' }] })` writes
@@ -214,9 +214,10 @@ function CollapsibleNavGroup({ group, activeTo }) {
214
214
  }
215
215
  // The phone's back arrow, below the md breakpoint only: the
216
216
  // shell's own way up from a page below a nav entry. Its label is the
217
- // place it goes, so it reads "Back to Accounts".
217
+ // place it goes, so it reads "Back to Accounts". A finger's 44px tall on
218
+ // a touch screen, inside the 56px bar; a mouse keeps the 32px button.
218
219
  function PhoneBack({ back }) {
219
- return (_jsxs(Link, { to: back.to, "data-slot": "app-shell-back", "aria-label": `Back to ${back.label}`, className: cn(buttonVariants({ variant: 'ghost', size: 'sm' }), '-ml-2 max-w-[60vw] gap-0.5 px-1.5 text-link hover:text-link md:hidden'), children: [_jsx(ChevronLeftIcon, { "aria-hidden": "true" }), _jsx("span", { className: "truncate", children: back.label })] }));
220
+ return (_jsxs(Link, { to: back.to, "data-slot": "app-shell-back", "aria-label": `Back to ${back.label}`, className: cn(buttonVariants({ variant: 'ghost', size: 'sm' }), '-ml-2 max-w-[60vw] gap-0.5 px-1.5 text-link hover:text-link pointer-coarse:h-11 md:hidden'), children: [_jsx(ChevronLeftIcon, { "aria-hidden": "true" }), _jsx("span", { className: "truncate", children: back.label })] }));
220
221
  }
221
222
  function AppShellSkeleton() {
222
223
  return (_jsxs("div", { className: "flex h-screen w-full gap-4 p-4", "data-state": "loading", children: [_jsx(Skeleton, { className: "h-full w-56 shrink-0" }), _jsxs("div", { className: "flex-1 space-y-4 pt-2", children: [_jsx(Skeleton, { className: "h-8 w-64" }), _jsx(Skeleton, { className: "h-40 w-full" })] })] }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tristan2828/ui-foundation",
3
- "version": "4.150.0",
3
+ "version": "4.151.0",
4
4
  "description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/vite/index.d.ts CHANGED
@@ -45,9 +45,32 @@ export type PwaOptions = {
45
45
  backgroundColor?: string
46
46
  /**
47
47
  * The app's own icon, a square PNG of at least 512×512 (path from the Vite
48
- * root). Replaces the generated ones everywhere.
48
+ * root). Replaces the generated ones everywhere: the manifest's `any`
49
+ * entry (its `sizes` read from the PNG), the apple-touch-icon and, without
50
+ * a `smallIcon`, a push's icon. Alone it is the manifest's only icon;
51
+ * the three below add the rest.
49
52
  */
50
53
  icon?: string
54
+ /**
55
+ * A smaller copy of `icon`, a square PNG of 192×192 (path from the Vite
56
+ * root): a second `any` entry, for the places that would scale the large
57
+ * one down (Windows, the install prompt), and a push's icon. Needs `icon`.
58
+ */
59
+ smallIcon?: string
60
+ /**
61
+ * The icon to crop to the device's mask shape (Android's circle or
62
+ * squircle), a square PNG of at least 512×512 (path from the Vite root)
63
+ * with the mark inside the central 80% circle and the background to the
64
+ * edges: the manifest's `maskable` entry. Without one, Android draws
65
+ * `icon` shrunk on a plain disc. Needs `icon`.
66
+ */
67
+ maskableIcon?: string
68
+ /**
69
+ * The app's mark as an SVG (path from the Vite root): an `any` entry
70
+ * with `sizes: 'any'`, which Chrome and Android draw sharp at every size.
71
+ * Needs `icon`, since iOS's Home Screen takes a PNG only.
72
+ */
73
+ iconSvg?: string
51
74
  /**
52
75
  * Home-screen shortcuts: actions offered when the installed icon is
53
76
  * long-pressed (Android, Chrome and Edge on desktop). Left out, the manifest
package/vite/pwa.d.ts CHANGED
@@ -5,6 +5,7 @@ import type { PwaOptions } from './index'
5
5
 
6
6
  export function themeToken(css: string, name: string): string
7
7
  export function oklchToHex(value: string): string
8
+ export function pngSize(bytes: Buffer): { width: number; height: number } | undefined
8
9
  export function solidPng(size: number, hex: string): Buffer
9
10
  export function letterSvg(name: string, background: string, foreground: string): string
10
11
  export function pwaFiles(
package/vite/pwa.js CHANGED
@@ -11,7 +11,9 @@
11
11
  // - /icons/: an SVG with the name's first letter (Chrome, Android) and
12
12
  // plain PNGs in the theme colour (iOS's Home Screen icon must be a PNG,
13
13
  // and drawing a letter into one needs a font rasteriser the package
14
- // doesn't carry). `icon` replaces them all with the app's own PNG.
14
+ // doesn't carry). `icon` replaces them all with the app's own PNG, and
15
+ // `smallIcon`, `maskableIcon` and `iconSvg` add its other sizes and
16
+ // shapes beside it; each PNG's `sizes` is read from its header.
15
17
  // - /push-sw.js: ./push-sw.js, showing pushes, opening their path, and
16
18
  // posting a message's action (a Done button) to the app's own path.
17
19
  // And adds the manifest, theme-color and apple-touch-icon tags to
@@ -150,6 +152,37 @@ export function letterSvg(name, background, foreground) {
150
152
  )
151
153
  }
152
154
 
155
+ // ---------------------------------------------------------- own icons
156
+
157
+ const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
158
+
159
+ /** A PNG's width and height, from its IHDR chunk; undefined if it isn't a PNG. */
160
+ export function pngSize(bytes) {
161
+ if (bytes.length < 24 || !bytes.subarray(0, 8).equals(PNG_SIGNATURE)) return undefined
162
+ if (bytes.toString('ascii', 12, 16) !== 'IHDR') return undefined
163
+ return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20) }
164
+ }
165
+
166
+ /** An app's own PNG (path from the Vite root), with the manifest's `sizes` read from it. */
167
+ function ownPng(file, option, root) {
168
+ const own = path.resolve(root, file)
169
+ if (own.toLowerCase().endsWith('.svg')) {
170
+ throw new Error(`pwa: ${option} must be a PNG file; an SVG goes in \`iconSvg\`, beside a PNG \`icon\` (iOS's Home Screen takes a PNG only).`)
171
+ }
172
+ const bytes = fs.existsSync(own) ? fs.readFileSync(own) : undefined
173
+ const size = bytes && pngSize(bytes)
174
+ if (!size) throw new Error(`pwa: ${option} must be a PNG file; ${own} isn't one.`)
175
+ return { bytes, sizes: `${size.width}x${size.height}` }
176
+ }
177
+
178
+ /** An app's own SVG (path from the Vite root). */
179
+ function ownSvg(file, option, root) {
180
+ const own = path.resolve(root, file)
181
+ const source = own.toLowerCase().endsWith('.svg') && fs.existsSync(own) ? fs.readFileSync(own, 'utf8') : undefined
182
+ if (!source || !/<svg[\s>]/.test(source)) throw new Error(`pwa: ${option} must be an SVG file; ${own} isn't one.`)
183
+ return source
184
+ }
185
+
153
186
  // --------------------------------------------------------------- plugin
154
187
 
155
188
  /**
@@ -169,13 +202,34 @@ export function pwaFiles(options, base = '/', root = process.cwd()) {
169
202
  const files = new Map()
170
203
  let icons
171
204
  let touchIcon
205
+ let workerIcon
172
206
  if (options.icon) {
173
- const own = path.resolve(root, options.icon)
174
- if (!own.endsWith('.png') || !fs.existsSync(own)) throw new Error(`pwa: icon must be a PNG file; ${own} isn't one.`)
175
- files.set('icons/icon.png', { type: 'image/png', source: fs.readFileSync(own) })
176
- icons = [{ src: at('icons/icon.png'), sizes: '512x512', type: 'image/png', purpose: 'any' }]
207
+ // The app's own: its PNG (the Home Screen's on iOS, the push's), and
208
+ // beside it any of a smaller PNG, a maskable PNG and an SVG it has.
209
+ const own = ownPng(options.icon, 'icon', root)
210
+ files.set('icons/icon.png', { type: 'image/png', source: own.bytes })
211
+ icons = [{ src: at('icons/icon.png'), sizes: own.sizes, type: 'image/png', purpose: 'any' }]
212
+ workerIcon = 'icons/icon.png'
213
+ if (options.smallIcon) {
214
+ const small = ownPng(options.smallIcon, 'smallIcon', root)
215
+ files.set('icons/icon-small.png', { type: 'image/png', source: small.bytes })
216
+ icons.unshift({ src: at('icons/icon-small.png'), sizes: small.sizes, type: 'image/png', purpose: 'any' })
217
+ workerIcon = 'icons/icon-small.png'
218
+ }
219
+ if (options.iconSvg) {
220
+ files.set('icons/icon.svg', { type: 'image/svg+xml', source: ownSvg(options.iconSvg, 'iconSvg', root) })
221
+ icons.unshift({ src: at('icons/icon.svg'), sizes: 'any', type: 'image/svg+xml', purpose: 'any' })
222
+ }
223
+ if (options.maskableIcon) {
224
+ const maskable = ownPng(options.maskableIcon, 'maskableIcon', root)
225
+ files.set('icons/icon-maskable.png', { type: 'image/png', source: maskable.bytes })
226
+ icons.push({ src: at('icons/icon-maskable.png'), sizes: maskable.sizes, type: 'image/png', purpose: 'maskable' })
227
+ }
177
228
  touchIcon = 'icons/icon.png'
178
229
  } else {
230
+ for (const option of ['iconSvg', 'smallIcon', 'maskableIcon']) {
231
+ if (options[option]) throw new Error(`pwa: \`${option}\` goes with \`icon\`, the app's own PNG; pass that too.`)
232
+ }
179
233
  files.set('icons/icon.svg', { type: 'image/svg+xml', source: letterSvg(shortName, theme, onTheme) })
180
234
  for (const size of [192, 512]) files.set(`icons/icon-${size}.png`, { type: 'image/png', source: solidPng(size, theme) })
181
235
  files.set('icons/apple-touch-icon.png', { type: 'image/png', source: solidPng(180, theme) })
@@ -187,6 +241,7 @@ export function pwaFiles(options, base = '/', root = process.cwd()) {
187
241
  { src: at('icons/icon-512.png'), sizes: '512x512', type: 'image/png', purpose: 'maskable' },
188
242
  ]
189
243
  touchIcon = 'icons/apple-touch-icon.png'
244
+ workerIcon = 'icons/icon-192.png'
190
245
  }
191
246
  const shortcuts = (options.shortcuts ?? []).map((shortcut, index) => {
192
247
  const label = shortcut?.name?.trim()
@@ -197,11 +252,10 @@ export function pwaFiles(options, base = '/', root = process.cwd()) {
197
252
  }
198
253
  let icon
199
254
  if (shortcut.icon) {
200
- const own = path.resolve(root, shortcut.icon)
201
- if (!own.endsWith('.png') || !fs.existsSync(own)) throw new Error(`pwa: shortcuts[${index}].icon must be a PNG file; ${own} isn't one.`)
255
+ const own = ownPng(shortcut.icon, `shortcuts[${index}].icon`, root)
202
256
  const file = `icons/shortcut-${index}.png`
203
- files.set(file, { type: 'image/png', source: fs.readFileSync(own) })
204
- icon = [{ src: at(file), sizes: '192x192', type: 'image/png' }]
257
+ files.set(file, { type: 'image/png', source: own.bytes })
258
+ icon = [{ src: at(file), sizes: own.sizes, type: 'image/png' }]
205
259
  }
206
260
  return {
207
261
  name: label,
@@ -226,7 +280,7 @@ export function pwaFiles(options, base = '/', root = process.cwd()) {
226
280
  type: 'application/manifest+json',
227
281
  source: `${JSON.stringify(manifest, null, 2)}\n`,
228
282
  })
229
- const worker = fs.readFileSync(WORKER_SOURCE, 'utf8').replace('__ICON__', options.icon ? at('icons/icon.png') : at('icons/icon-192.png'))
283
+ const worker = fs.readFileSync(WORKER_SOURCE, 'utf8').replace('__ICON__', at(workerIcon))
230
284
  files.set('push-sw.js', { type: 'text/javascript', source: worker })
231
285
  return {
232
286
  files,