Files
jorgecuadros-platform/apps/web/src/components/FontScaleControl.tsx
T
rmancinasandClaude Opus 5 c100dfa224
Build and Push Images / Build jorgecuadros-web (push) Successful in 2m13s
Build and Push Images / Build jorgecuadros-api (push) Successful in 3m16s
feat(web,api): scale spacing with text size, persist preference per account
Two follow-ups to the text-size control.

Spacing now scales with the text. All padding, margin, gap and min-height
declarations in globals.css move from px to rem (263 declarations, converted
mechanically), so --ui-scale drives the whole layout rather than just the
glyphs. Deliberately left in px: border widths, which must stay hairlines;
box-shadow offsets; border-radius, which reads as bloated when scaled on large
cards; --shell-max, a container cap that must not outgrow the viewport; and
media-query breakpoints, which are conditions rather than declarations. With
spacing following along, the presets gain a 1.5 "Máximo" step and MAX_UI_SCALE
rises from 1.4.

The preference now lives on the account instead of only in one browser.
User.uiScale (Float, default 1) is added to the schema and to the safe select,
so it rides along on /auth/login and /auth/me. PATCH /auth/preferences writes
it, guarded by AuthenticatedGuard only — every role including VIEWER may set
their own, and the target is always the session's user id, never a body
parameter, so this cannot be used to touch another account. The global
ValidationPipe's whitelist rejects any extra field, so role cannot ride in
alongside uiScale.

localStorage stays, demoted to a pre-paint cache for the layout.tsx script;
AppShell reconciles it against the account once /auth/me answers, with the
account winning. FontScaleControl becomes a controlled component since the
same value is now edited from the appbar and the drawer.

Verified against the dev API: PATCH persists and is reflected by a subsequent
/auth/me, out-of-range values are rejected 400, and an extra "role" field in
the body is rejected 400.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 22:30:06 -07:00

115 lines
3.4 KiB
TypeScript

"use client";
import { useEffect, useRef, useState } from "react";
import { UI_SCALES } from "@/lib/ui-scale";
/**
* Text-size picker. Controlled: AppShell owns the value and handles applying
* and persisting it, because the same setting is edited from two places (the
* appbar popover and the mobile drawer) and reconciled against the account on
* load.
*
* `variant="menu"` is the compact appbar popover; `variant="inline"` is the
* flat row used inside the mobile drawer, where a popover inside a popover
* would be awkward.
*/
export function FontScaleControl({
value,
onChange,
variant = "menu",
}: {
value: number;
onChange: (scale: number) => void;
variant?: "menu" | "inline";
}) {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
function onPointerDown(event: MouseEvent) {
if (ref.current && !ref.current.contains(event.target as Node)) {
setOpen(false);
}
}
function onKeyDown(event: KeyboardEvent) {
if (event.key === "Escape") setOpen(false);
}
document.addEventListener("mousedown", onPointerDown);
document.addEventListener("keydown", onKeyDown);
return () => {
document.removeEventListener("mousedown", onPointerDown);
document.removeEventListener("keydown", onKeyDown);
};
}, [open]);
function choose(next: number) {
onChange(next);
setOpen(false);
}
const options = UI_SCALES.map((option) => ({
...option,
selected: Math.abs(value - option.value) < 0.001,
}));
if (variant === "inline") {
return (
<div className="scale-inline" role="radiogroup" aria-label="Tamaño de texto">
<span className="appbar-drawer-heading">Tamaño de texto</span>
<div className="scale-inline-options">
{options.map((option) => (
<button
key={option.value}
type="button"
role="radio"
aria-checked={option.selected}
className={`scale-chip${option.selected ? " active" : ""}`}
style={{ fontSize: `${option.value}em` }}
onClick={() => choose(option.value)}
>
{option.short}
<span className="sr-only"> {option.label}</span>
</button>
))}
</div>
</div>
);
}
return (
<div className="appbar-menu" ref={ref}>
<button
type="button"
className="appbar-scale-trigger"
aria-expanded={open}
aria-haspopup="true"
aria-label="Tamaño de texto"
title="Tamaño de texto"
onClick={() => setOpen((v) => !v)}
>
<span aria-hidden="true">
A<span className="appbar-scale-big">A</span>
</span>
</button>
{open && (
<div className="appbar-dropdown appbar-dropdown-right" role="menu">
{options.map((option) => (
<button
key={option.value}
type="button"
role="menuitemradio"
aria-checked={option.selected}
className={`appbar-dropdown-link scale-option${option.selected ? " active" : ""}`}
onClick={() => choose(option.value)}
>
<span style={{ fontSize: `${option.value}em` }}>{option.short}</span>
<span className="scale-option-label">{option.label}</span>
</button>
))}
</div>
)}
</div>
);
}