Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/round-popup-coordinates.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 6 additions & 2 deletions packages/effect/src/ui/kit/Positioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>([
[
"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"],
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/test/ui/kit/PositioningDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading