feat(mobile): set data-form-factor body attr from User-Agent in root layout

This commit is contained in:
Matt Ciaccio
2026-04-29 13:59:03 +02:00
parent 906127a292
commit 9f786fbcf3
3 changed files with 74 additions and 2 deletions

14
src/lib/form-factor.ts Normal file
View File

@@ -0,0 +1,14 @@
export type FormFactor = 'mobile' | 'desktop';
const MOBILE_TOKENS = ['Mobile', 'iPhone', 'iPad', 'Android'] as const;
/**
* Classify a User-Agent string as 'mobile' or 'desktop'.
* Defaults to 'desktop' when the UA is missing or unrecognized — the CSS
* media-query fallback in globals.css handles desktop browsers resized below
* the lg breakpoint, so a wrong-but-defaultish classification never breaks UX.
*/
export function classifyFormFactor(userAgent: string | null | undefined): FormFactor {
if (!userAgent) return 'desktop';
return MOBILE_TOKENS.some((token) => userAgent.includes(token)) ? 'mobile' : 'desktop';
}