Skip to content

Commit 6535684

Browse files
committed
feat(scan): add safeArea option + allow text selection inside toolbar
safeArea (#430) --------------- Other dev overlays (most notably the Next.js dev indicator) sit in the same screen corners as the React Scan toolbar. The expanded toolbar and collapsed bookmark would overlap them with no way for the user to inset the toolbar. Adds an \`Options.safeArea\` field that accepts either: - a single number (insets all four edges, default 24) - a per-edge \`{ top?, right?, bottom?, left? }\` object Refactors the hard-coded \`SAFE_AREA\` constant out of widget positioning math (\`calculatePosition\`, \`calculateNewSizeAndPosition\`, \`WindowDimensions\`, collapsed-corner positioning, max-size CSS) to read from a new \`getSafeArea()\` helper that resolves per-edge from options at runtime. Module-init defaults still use the raw \`SAFE_AREA\` constant — options haven't been set yet at that point, and reading them through \`ReactScanInternals.options.value\` from a top-level module would create a load-bearing optional chain through a circular import. \`defaultWidgetConfig\` is preserved as a deprecated alias of \`getDefaultWidgetConfig()\` for one minor version. \`parseSafeAreaOption\` is split out as a standalone utility (one function per file per AGENTS.md) with a unit-test matrix covering arrays, NaN, negative numbers, partial objects, and non-objects. Toolbar text selection (#415) ----------------------------- The toolbar root has \`select-none\` (so dragging the bar doesn't start a text selection), and its root \`onPointerDown\` handler calls \`e.preventDefault()\` (which suppresses focus and selection on every click). Together those two made the AI-prompt \`<pre>\` text impossible to highlight or copy. Fix: - New \`TOOLBAR_INTERACTIVE_SELECTOR\` constant lists the elements that should bypass drag and accept native focus/selection. Used from both the JSX (drag handler bail-out) and the CSS (\`user-select: text\` re-enable). Single source of truth. - \`handleDrag\` now bails — without calling \`preventDefault()\` — when the pointerdown lands on a button, link, input, textarea, select, pre, contenteditable, or \`[data-react-scan-selectable]\`. - Flat CSS selectors (no nesting) so the rule applies in older Firefox/Safari that lack native CSS nesting. Note: this is a small behavior change for button clicks — the original code called \`preventDefault()\` even on button hits; buttons now receive native focus on pointerdown.
1 parent cb8e2cb commit 6535684

8 files changed

Lines changed: 272 additions & 60 deletions

File tree

‎packages/scan/src/web/assets/css/styles.tailwind.css‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ svg {
107107
backface-visibility: hidden;
108108
}
109109

110+
/* The toolbar uses select-none for drag UX, so re-enable text selection on
111+
* content elements (e.g. AI prompt <pre>, search inputs). See #415. */
112+
#react-scan-toolbar pre,
113+
#react-scan-toolbar textarea,
114+
#react-scan-toolbar input[type='text'],
115+
#react-scan-toolbar input[type='search'],
116+
#react-scan-toolbar [data-react-scan-selectable] {
117+
@apply select-text;
118+
cursor: text;
119+
}
120+
110121
.button {
111122
&:hover {
112123
background: rgba(255, 255, 255, 0.1);

‎packages/scan/src/web/constants.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ export const MIN_CONTAINER_WIDTH = 240;
1010
export const LOCALSTORAGE_KEY = "react-scan-widget-settings-v2";
1111
export const LOCALSTORAGE_COLLAPSED_KEY = "react-scan-widget-collapsed-v1";
1212
export const LOCALSTORAGE_LAST_VIEW_KEY = "react-scan-widget-last-view-v1";
13+
14+
// CSS selector for elements inside #react-scan-toolbar that should NOT
15+
// trigger drag and SHOULD allow native text selection / focus (#415).
16+
// Keep in sync with the matching CSS rule in styles.tailwind.css.
17+
export const TOOLBAR_INTERACTIVE_SELECTOR =
18+
"button, a, input, textarea, select, pre, [contenteditable], [data-react-scan-selectable]";

‎packages/scan/src/web/state.ts‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "./constants";
99
import { IS_CLIENT } from "./utils/constants";
1010
import { readLocalStorage, saveLocalStorage } from "./utils/helpers";
11+
import { getSafeArea } from "./utils/safe-area";
1112
import type { Corner, WidgetConfig, WidgetSettings } from "./widget/types";
1213
import type { CollapsedPosition } from "./widget/types";
1314

@@ -16,8 +17,10 @@ export const signalRefWidget = /* @__PURE__ */ signal<HTMLDivElement | null>(
1617
null
1718
);
1819

19-
export const defaultWidgetConfig = {
20-
corner: "bottom-right" as Corner,
20+
// Use the raw SAFE_AREA constant (not getSafeArea()) here: this runs at
21+
// module-init time, before any user has called scan() with options.
22+
export const getDefaultWidgetConfig = (): WidgetConfig => ({
23+
corner: "bottom-right" satisfies Corner,
2124
dimensions: {
2225
isFullWidth: false,
2326
isFullHeight: false,
@@ -35,30 +38,36 @@ export const defaultWidgetConfig = {
3538
componentsTree: {
3639
width: MIN_CONTAINER_WIDTH,
3740
},
38-
} as WidgetConfig;
41+
});
42+
43+
// Deprecated alias kept for one minor version to avoid breaking downstream
44+
// imports of the pre-refactor `defaultWidgetConfig` const.
45+
/** @deprecated use {@link getDefaultWidgetConfig} */
46+
export const defaultWidgetConfig: WidgetConfig = getDefaultWidgetConfig();
3947

4048
export const getInitialWidgetConfig = (): WidgetConfig => {
49+
const defaults = getDefaultWidgetConfig();
4150
const stored = readLocalStorage<WidgetSettings>(LOCALSTORAGE_KEY);
4251
if (!stored) {
4352
saveLocalStorage(LOCALSTORAGE_KEY, {
44-
corner: defaultWidgetConfig.corner,
45-
dimensions: defaultWidgetConfig.dimensions,
46-
lastDimensions: defaultWidgetConfig.lastDimensions,
47-
componentsTree: defaultWidgetConfig.componentsTree,
53+
corner: defaults.corner,
54+
dimensions: defaults.dimensions,
55+
lastDimensions: defaults.lastDimensions,
56+
componentsTree: defaults.componentsTree,
4857
});
4958

50-
return defaultWidgetConfig;
59+
return defaults;
5160
}
5261

5362
return {
54-
corner: stored.corner ?? defaultWidgetConfig.corner,
55-
dimensions: stored.dimensions ?? defaultWidgetConfig.dimensions,
63+
corner: stored.corner ?? defaults.corner,
64+
dimensions: stored.dimensions ?? defaults.dimensions,
5665

5766
lastDimensions:
5867
stored.lastDimensions ??
5968
stored.dimensions ??
60-
defaultWidgetConfig.lastDimensions,
61-
componentsTree: stored.componentsTree ?? defaultWidgetConfig.componentsTree,
69+
defaults.lastDimensions,
70+
componentsTree: stored.componentsTree ?? defaults.componentsTree,
6271
};
6372
};
6473

@@ -69,12 +78,13 @@ export const updateDimensions = (): void => {
6978

7079
const { dimensions } = signalWidget.value;
7180
const { width, height, position } = dimensions;
81+
const safeArea = getSafeArea();
7282

7383
signalWidget.value = {
7484
...signalWidget.value,
7585
dimensions: {
76-
isFullWidth: width >= window.innerWidth - SAFE_AREA * 2,
77-
isFullHeight: height >= window.innerHeight - SAFE_AREA * 2,
86+
isFullWidth: width >= window.innerWidth - safeArea.left - safeArea.right,
87+
isFullHeight: height >= window.innerHeight - safeArea.top - safeArea.bottom,
7888
width,
7989
height,
8090
position,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { parseSafeAreaOption } from '~web/utils/parse-safe-area-option';
3+
4+
describe('parseSafeAreaOption', () => {
5+
it('accepts a non-negative finite number', () => {
6+
expect(parseSafeAreaOption(0)).toEqual({ ok: true, value: 0 });
7+
expect(parseSafeAreaOption(24)).toEqual({ ok: true, value: 24 });
8+
expect(parseSafeAreaOption(96)).toEqual({ ok: true, value: 96 });
9+
});
10+
11+
it('rejects negative numbers', () => {
12+
const result = parseSafeAreaOption(-1);
13+
expect(result.ok).toBe(false);
14+
});
15+
16+
it('rejects NaN and Infinity', () => {
17+
expect(parseSafeAreaOption(Number.NaN).ok).toBe(false);
18+
expect(parseSafeAreaOption(Number.POSITIVE_INFINITY).ok).toBe(false);
19+
expect(parseSafeAreaOption(Number.NEGATIVE_INFINITY).ok).toBe(false);
20+
});
21+
22+
it('accepts a partial per-edge object', () => {
23+
expect(parseSafeAreaOption({ right: 96 })).toEqual({
24+
ok: true,
25+
value: { right: 96 },
26+
});
27+
expect(parseSafeAreaOption({ top: 0, bottom: 40 })).toEqual({
28+
ok: true,
29+
value: { top: 0, bottom: 40 },
30+
});
31+
});
32+
33+
it('accepts a full per-edge object', () => {
34+
expect(
35+
parseSafeAreaOption({ top: 1, right: 2, bottom: 3, left: 4 }),
36+
).toEqual({
37+
ok: true,
38+
value: { top: 1, right: 2, bottom: 3, left: 4 },
39+
});
40+
});
41+
42+
it('rejects per-edge object with a negative value', () => {
43+
const result = parseSafeAreaOption({ right: -10 });
44+
expect(result.ok).toBe(false);
45+
});
46+
47+
it('rejects per-edge object with a non-number value', () => {
48+
const result = parseSafeAreaOption({ top: '24' });
49+
expect(result.ok).toBe(false);
50+
});
51+
52+
it('rejects arrays (not plain objects)', () => {
53+
const result = parseSafeAreaOption([10, 20, 30, 40]);
54+
expect(result.ok).toBe(false);
55+
});
56+
57+
it('rejects null and undefined', () => {
58+
expect(parseSafeAreaOption(null).ok).toBe(false);
59+
expect(parseSafeAreaOption(undefined).ok).toBe(false);
60+
});
61+
62+
it('rejects strings', () => {
63+
expect(parseSafeAreaOption('24').ok).toBe(false);
64+
expect(parseSafeAreaOption('').ok).toBe(false);
65+
});
66+
67+
it('ignores undefined edges in a per-edge object', () => {
68+
expect(
69+
parseSafeAreaOption({ top: 5, right: undefined }),
70+
).toEqual({ ok: true, value: { top: 5 } });
71+
});
72+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Options } from '~core/index';
2+
3+
type SafeAreaOption = NonNullable<Options['safeArea']>;
4+
5+
export type ParsedSafeAreaOption =
6+
| { ok: true; value: SafeAreaOption }
7+
| { ok: false; error: string };
8+
9+
const SAFE_AREA_EDGES = ['top', 'right', 'bottom', 'left'] as const;
10+
11+
const isFinitePositive = (value: unknown): value is number =>
12+
typeof value === 'number' && Number.isFinite(value) && value >= 0;
13+
14+
const isPlainObject = (value: unknown): value is Record<string, unknown> =>
15+
Boolean(value) && typeof value === 'object' && !Array.isArray(value);
16+
17+
export const parseSafeAreaOption = (value: unknown): ParsedSafeAreaOption => {
18+
if (isFinitePositive(value)) {
19+
return { ok: true, value };
20+
}
21+
22+
if (!isPlainObject(value)) {
23+
return {
24+
ok: false,
25+
error: `- safeArea must be a non-negative number or { top?, right?, bottom?, left? }. Got "${JSON.stringify(value)}"`,
26+
};
27+
}
28+
29+
const inset: Partial<Record<(typeof SAFE_AREA_EDGES)[number], number>> = {};
30+
for (const edge of SAFE_AREA_EDGES) {
31+
const edgeValue = value[edge];
32+
if (edgeValue === undefined) continue;
33+
if (!isFinitePositive(edgeValue)) {
34+
return {
35+
ok: false,
36+
error: `- safeArea.${edge} must be a non-negative number. Got "${JSON.stringify(edgeValue)}"`,
37+
};
38+
}
39+
inset[edge] = edgeValue;
40+
}
41+
return { ok: true, value: inset };
42+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { ReactScanInternals } from '~core/index';
2+
import { SAFE_AREA } from '~web/constants';
3+
4+
export interface SafeAreaInsets {
5+
top: number;
6+
right: number;
7+
bottom: number;
8+
left: number;
9+
}
10+
11+
const isFinitePositive = (value: unknown): value is number =>
12+
typeof value === 'number' && Number.isFinite(value) && value >= 0;
13+
14+
const isPlainObject = (value: unknown): value is Record<string, unknown> =>
15+
Boolean(value) && typeof value === 'object' && !Array.isArray(value);
16+
17+
export const getSafeArea = (): SafeAreaInsets => {
18+
const value = ReactScanInternals.options.value.safeArea;
19+
20+
if (isFinitePositive(value)) {
21+
return { top: value, right: value, bottom: value, left: value };
22+
}
23+
24+
if (isPlainObject(value)) {
25+
const top = value.top;
26+
const right = value.right;
27+
const bottom = value.bottom;
28+
const left = value.left;
29+
return {
30+
top: isFinitePositive(top) ? top : SAFE_AREA,
31+
right: isFinitePositive(right) ? right : SAFE_AREA,
32+
bottom: isFinitePositive(bottom) ? bottom : SAFE_AREA,
33+
left: isFinitePositive(left) ? left : SAFE_AREA,
34+
};
35+
}
36+
37+
return {
38+
top: SAFE_AREA,
39+
right: SAFE_AREA,
40+
bottom: SAFE_AREA,
41+
left: SAFE_AREA,
42+
};
43+
};

0 commit comments

Comments
 (0)