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

feat: add roundOffsets option to computeStyles modifier #1193

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/modifiers/computeStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import getBasePlacement from '../utils/getBasePlacement';
export type Options = {
gpuAcceleration: boolean,
adaptive: boolean,
roundOffsets: boolean,
};

const unsetSides = {
Expand All @@ -30,7 +31,7 @@ const unsetSides = {
// Round the offsets to the nearest suitable subpixel based on the DPR.
// Zooming can change the DPR, but it seems to report a value that will
// cleanly divide the values into the appropriate subpixels.
function roundOffsets({ x, y }): Offsets {
function roundOffsetsByDPR({ x, y }): Offsets {
const win: Window = window;
const dpr = win.devicePixelRatio || 1;

Expand All @@ -48,6 +49,7 @@ export function mapToStyles({
position,
gpuAcceleration,
adaptive,
roundOffsets,
}: {
popper: HTMLElement,
popperRect: Rect,
Expand All @@ -56,8 +58,9 @@ export function mapToStyles({
position: PositioningStrategy,
gpuAcceleration: boolean,
adaptive: boolean,
roundOffsets: boolean,
}) {
let { x, y } = roundOffsets(offsets);
let { x = 0, y = 0 } = roundOffsets ? roundOffsetsByDPR(offsets) : offsets;

const hasX = offsets.hasOwnProperty('x');
const hasY = offsets.hasOwnProperty('y');
Expand Down Expand Up @@ -118,7 +121,11 @@ export function mapToStyles({
}

function computeStyles({ state, options }: ModifierArguments<Options>) {
const { gpuAcceleration = true, adaptive = true } = options;
const {
gpuAcceleration = true,
adaptive = true,
roundOffsets = true,
} = options;

if (__DEV__) {
const transitionProperty =
Expand All @@ -127,7 +134,7 @@ function computeStyles({ state, options }: ModifierArguments<Options>) {
if (
adaptive &&
['transform', 'top', 'right', 'bottom', 'left'].some(
property => transitionProperty.indexOf(property) >= 0
(property) => transitionProperty.indexOf(property) >= 0
)
) {
console.warn(
Expand Down Expand Up @@ -162,6 +169,7 @@ function computeStyles({ state, options }: ModifierArguments<Options>) {
offsets: state.modifiersData.popperOffsets,
position: state.options.strategy,
adaptive,
roundOffsets,
}),
};
}
Expand All @@ -174,6 +182,7 @@ function computeStyles({ state, options }: ModifierArguments<Options>) {
offsets: state.modifiersData.arrow,
position: 'absolute',
adaptive: false,
roundOffsets,
}),
};
}
Expand Down
5 changes: 5 additions & 0 deletions src/modifiers/computeStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ it('computes the popper styles', () => {
position: 'absolute',
gpuAcceleration: true,
adaptive: true,
roundOffsets: true,
})
).toMatchSnapshot();

Expand All @@ -25,6 +26,7 @@ it('computes the popper styles', () => {
position: 'absolute',
gpuAcceleration: false,
adaptive: true,
roundOffsets: true,
})
).toMatchSnapshot();

Expand All @@ -37,6 +39,7 @@ it('computes the popper styles', () => {
position: 'absolute',
gpuAcceleration: false,
adaptive: true,
roundOffsets: true,
})
).toMatchSnapshot();

Expand All @@ -49,6 +52,7 @@ it('computes the popper styles', () => {
position: 'absolute',
gpuAcceleration: false,
adaptive: true,
roundOffsets: true,
})
).toMatchSnapshot();

Expand All @@ -65,6 +69,7 @@ it('computes the arrow styles', () => {
position: 'absolute',
gpuAcceleration: true,
adaptive: false,
roundOffsets: true,
})
).toMatchSnapshot();
});
6 changes: 2 additions & 4 deletions src/utils/computeOffsets.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ export default function computeOffsets({
switch (variation) {
case start:
offsets[mainAxis] =
Math.floor(offsets[mainAxis]) -
Copy link
Contributor Author

@piecyk piecyk Nov 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we had this round functions on start/end variation? Imho they are not needed as latter we still round the offsets 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atomiks? 👀

Copy link
Contributor Author

@piecyk piecyk Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should add more tests 🤔 Also this change didn't break any existing one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good, I'm just a bit worried about this bit. Let's give @atomiks a couple more days to review it and if he doesn't find time I'll merge it as dog food it to the whole user base :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like no other comments. IMHO let's merge this 🤞 💪

Math.floor(reference[len] / 2 - element[len] / 2);
offsets[mainAxis] - (reference[len] / 2 - element[len] / 2);
break;
case end:
offsets[mainAxis] =
Math.floor(offsets[mainAxis]) +
Math.ceil(reference[len] / 2 - element[len] / 2);
offsets[mainAxis] + (reference[len] / 2 - element[len] / 2);
break;
default:
}
Expand Down