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

Add support for negative number in interpolate styles #3418

Merged
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
19 changes: 19 additions & 0 deletions packages/animation-utils/src/test/interpolate-styles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,22 @@ test('Should interpolate between 0 and values with units', () => {
padding: '5px',
});
});

test('Should interpolate between negative values with units', () => {
expect(
interpolateStyles(
0.5,
[0, 1],
[
{
left: '-10px',
},
{
left: '-20px',
},
],
),
).toEqual({
left: '-15px',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ const classifyParts = (parts: string[]) => {
return {function: {name: functionName, values: functionValues}};
}

// Check for a number possibly followed by a unit
const numberUnitMatch = part.match(/^(\d+(?:\.\d+)?)([a-zA-Z%]*)$/);
// Check for a number possibly followed by a unit like '10px' or '10' or '-10px'
const numberUnitMatch = part.match(/^(-?\d+(?:\.\d+)?)([a-zA-Z%]*)$/);
if (numberUnitMatch) {
const number = parseFloat(numberUnitMatch[1]);
const unit = numberUnitMatch[2];
return unit ? {number, unit} : {number};
}

// Check for a number without a unit
const numberMatch = part.match(/^(\d+(?:\.\d+)?)$/);
// Check for a number without a unit like '10' or '-10'
const numberMatch = part.match(/^(-?\d+(?:\.\d+)?)$/);
if (numberMatch) {
const number = parseFloat(numberMatch[1]);
return {number};
Expand Down