diff --git a/.changeset/round-popup-coordinates.md b/.changeset/round-popup-coordinates.md new file mode 100644 index 00000000000..c49bd6e4bec --- /dev/null +++ b/.changeset/round-popup-coordinates.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Place positioned popups on whole device pixels: `left` and `top` are rounded to the device pixel ratio, as Floating UI's `roundByDPR` does, so a popup next to an anchor at a fractional offset no longer draws its text and borders blurred. diff --git a/packages/effect/src/ui/kit/Positioning.ts b/packages/effect/src/ui/kit/Positioning.ts index 02d5823018b..4f19430c3f2 100644 --- a/packages/effect/src/ui/kit/Positioning.ts +++ b/packages/effect/src/ui/kit/Positioning.ts @@ -1681,14 +1681,18 @@ export const trackPosition = ( const dpr = view.devicePixelRatio || 1 const snap = (start: number, length: number) => (Math.round((start + length) * dpr) - Math.round(start * dpr)) / dpr + // A popup at a fractional device-pixel offset draws its text and borders + // blurred, so its corner lands on a whole device pixel, as Floating UI's + // `roundByDPR` does. + const pixel = (value: number) => Math.round(value * dpr) / dpr const next = new Map([ [ "left", - (placed.x - (side === "right" ? bridge : 0)) + "px" + pixel(placed.x - (side === "right" ? bridge : 0)) + "px" ], [ "top", - (placed.y - (side === "bottom" ? bridge : 0)) + "px" + pixel(placed.y - (side === "bottom" ? bridge : 0)) + "px" ], ["--anchor-width", snap(anchorRect.x, anchorRect.width) + "px"], ["--anchor-height", snap(anchorRect.y, anchorRect.height) + "px"], diff --git a/packages/effect/test/ui/kit/PositioningDocument.test.ts b/packages/effect/test/ui/kit/PositioningDocument.test.ts index 6b28011964e..5a60dc09ca9 100644 --- a/packages/effect/test/ui/kit/PositioningDocument.test.ts +++ b/packages/effect/test/ui/kit/PositioningDocument.test.ts @@ -187,6 +187,22 @@ describe("trackPosition in a document", { concurrent: false }, () => { assert.isFalse(placements[0]?.anchorHidden) }).pipe(Effect.scoped)) + it.effect("lands left and top on whole device pixels", () => + Effect.gen(function*() { + const at = { anchor: { x: 300.3, y: 200.6, width: 0, height: 0 } } + const one = yield* track(false, at) + assert.strictEqual(one.node.style.left, "300px") + assert.strictEqual(one.node.style.top, "201px") + Object.defineProperty(window, "devicePixelRatio", { + configurable: true, + value: 2 + }) + yield* Effect.addFinalizer(() => Effect.sync(() => delete (window as any).devicePixelRatio)) + const two = yield* track(false, at) + assert.strictEqual(two.node.style.left, "300.5px") + assert.strictEqual(two.node.style.top, "200.5px") + }).pipe(Effect.scoped)) + it.effect("a scroll inside the positioner doesn't place it again, and one outside does", () => Effect.gen(function*() { const { node } = yield* track(false)