Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add visual viewport support #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const scan = (
`font-weight:bold;font-size:14px;font-weight:bold;font-family:${MONO_FONT}`,
);

const ctx = createFullscreenCanvas();
const canvasInstance = createFullscreenCanvas();
const status = createStatus();

const handleCommitFiberRoot = (_rendererID: number, root: FiberRoot) => {
Expand Down Expand Up @@ -114,7 +114,7 @@ export const scan = (
...outlines,
]);

if (nextPendingOutlines.length && ctx) {
if (nextPendingOutlines.length && canvasInstance.ctx) {
for (let i = 0, len = nextPendingOutlines.length; i < len; i++) {
const outline = nextPendingOutlines[i];
totalSelfTime += outline.selfTime;
Expand All @@ -123,7 +123,7 @@ export const scan = (
let text = `×${totalCount}`;
if (totalSelfTime > 0) text += ` (${totalSelfTime.toFixed(2)}ms)`;
status.textContent = `${text} · react-scan`;
flushOutlines(ctx);
flushOutlines(canvasInstance.ctx);
}
};

Expand Down
43 changes: 37 additions & 6 deletions src/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
ChangedProp,
OutlinePaintTask,
} from './types';
import { onIdle, fastSerialize } from './utils';
import { debounce, onIdle, fastSerialize } from './utils';
import { getCurrentOptions } from './auto';
import { MONO_FONT, PURPLE_RGB } from './constants';

Expand Down Expand Up @@ -382,23 +382,47 @@ export const createFullscreenCanvas = () => {
let resizeScheduled = false;

const resize = () => {
activeOutlines = [];
pendingOutlines = [];

const dpi = window.devicePixelRatio;
canvas.width = dpi * window.innerWidth;
canvas.height = dpi * window.innerHeight;
const visualViewport = window.visualViewport;

// Use visualViewport for mobile and fallback to regular viewport for desktop
const width = visualViewport?.width ?? window.innerWidth;
const height = visualViewport?.height ?? window.innerHeight;

canvas.width = dpi * width;
canvas.height = dpi * height;

ctx?.setTransform(1, 0, 0, 1, 0, 0);
ctx?.scale(dpi, dpi);

// Only apply offset on mobile devices using visualViewport
if (visualViewport && /Mobi|Android/i.test(navigator.userAgent)) {
ctx?.translate(-visualViewport.offsetLeft, -visualViewport.offsetTop);
}

resizeScheduled = false;
};

resize();

window.addEventListener('resize', () => {
const events = ['resize', 'scroll', 'touchend', 'orientationchange'];

const handleViewportChange = debounce(() => {
if (!resizeScheduled) {
resizeScheduled = true;
requestAnimationFrame(() => {
resize();
});
}
});
}, 100);

for (let i = 0, len = events.length; i < len; i++) {
const event = events[i];
window.addEventListener(event, handleViewportChange, { passive: true });
}

onIdle(() => {
const prevCanvas = document.getElementById('react-scan-canvas');
Expand All @@ -408,7 +432,14 @@ export const createFullscreenCanvas = () => {
document.documentElement.appendChild(canvas);
});

return ctx;
const cleanup = () => {
for (let i = 0, len = events.length; i < len; i++) {
const event = events[i];
window.removeEventListener(event, handleViewportChange);
}
};

return { ctx, cleanup };
};

export const createStatus = () => {
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ export const isProd = () => {
export const NO_OP = () => {
/**/
};

export const debounce = <T extends (...args: any[]) => any>(
fn: T,
delay: number
): ((...args: Parameters<T>) => void) => {
let timeoutId: ReturnType<typeof setTimeout>;

return (...args: Parameters<T>) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), delay);
};
};