Skip to content

Commit 3c66cdd

Browse files
committed
fix: improve fit scaling on mobile
1 parent 5f31893 commit 3c66cdd

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

hooks/useFitScale.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ export function useFitScale<T extends HTMLElement>() {
1414
const host = el.closest(".h-full") as HTMLElement | null;
1515

1616
const measure = () => {
17-
let avail = window.innerHeight;
17+
const view = window.visualViewport?.height ?? window.innerHeight;
18+
let avail = view;
1819
if (host) {
1920
const cs = getComputedStyle(host);
2021
const pad = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
21-
avail = host.clientHeight - pad;
22+
avail = Math.min(view, Math.max(0, host.clientHeight - pad));
2223
}
2324

2425
const natural = el.scrollHeight || el.getBoundingClientRect().height;
26+
const coarse = window.matchMedia("(pointer: coarse)").matches;
27+
const base = coarse ? 760 : SHORT_HEIGHT;
28+
const min = coarse ? 0.86 : 0.78;
29+
const max = coarse ? 0.98 : 0.94;
2530
const fit = natural > 0 && avail > 0
26-
? Math.min(1, (avail - FIT_PADDING) / natural)
31+
? Math.min(1, Math.max(0, (avail - FIT_PADDING) / natural))
2732
: 1;
28-
const cap = avail < SHORT_HEIGHT
29-
? Math.min(0.94, Math.max(0.78, avail / SHORT_HEIGHT))
33+
const cap = avail < base
34+
? Math.min(max, Math.max(min, avail / base))
3035
: 1;
3136
const scale = Math.min(1, fit, cap);
3237
const height = Math.ceil(natural * scale);
@@ -43,12 +48,15 @@ export function useFitScale<T extends HTMLElement>() {
4348

4449
measure();
4550
const ro = new ResizeObserver(measure);
51+
const viewport = window.visualViewport;
4652
ro.observe(el);
4753
if (host) ro.observe(host);
4854
window.addEventListener("resize", measure);
55+
viewport?.addEventListener("resize", measure);
4956
return () => {
5057
ro.disconnect();
5158
window.removeEventListener("resize", measure);
59+
viewport?.removeEventListener("resize", measure);
5260
};
5361
}, []);
5462

0 commit comments

Comments
 (0)