Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

translate: support percentage values #194

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
14 changes: 14 additions & 0 deletions src/__tests__/transform.js
Original file line number Diff line number Diff line change
@@ -12,6 +12,20 @@ it('transforms a single transform value with string', () => {
})
})

it('transforms a single transform value with percentage', () => {
expect(transformCss([['transform', 'translate(100%, 100%)']])).toEqual({
transform: [{ translateY: '100%' }, { translateX: '100%' }],
})
})

it('transforms multiple transform values with percentage', () => {
expect(
transformCss([['transform', 'translateY(100%) translateX(100%)']])
).toEqual({
transform: [{ translateX: '100%' }, { translateY: '100%' }],
})
})

it('transforms multiple transform values', () => {
expect(transformCss([['transform', 'scaleX(5) skewX(1deg)']])).toEqual({
transform: [{ skewX: '1deg' }, { scaleX: 5 }],
30 changes: 15 additions & 15 deletions src/transforms/transform.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { SPACE, COMMA, LENGTH, NUMBER, ANGLE } from '../tokenTypes'
import { SPACE, COMMA, LENGTH, NUMBER, ANGLE, PERCENT } from '../tokenTypes'

const oneOfType = tokenType => functionStream => {
const value = functionStream.expect(tokenType)
const oneOfTypes = tokenTypes => functionStream => {
const value = functionStream.expect(...tokenTypes)
functionStream.expectEmpty()
return value
}

const singleNumber = oneOfType(NUMBER)
const singleLength = oneOfType(LENGTH)
const singleAngle = oneOfType(ANGLE)
const xyTransformFactory = tokenType => (
const singleNumber = oneOfTypes([NUMBER])
const singleLengthOrPercent = oneOfTypes([LENGTH, PERCENT])
const singleAngle = oneOfTypes([ANGLE])
const xyTransformFactory = tokenTypes => (
key,
valueIfOmitted
) => functionStream => {
const x = functionStream.expect(tokenType)
const x = functionStream.expect(...tokenTypes)

let y
if (functionStream.hasTokens()) {
functionStream.expect(COMMA)
y = functionStream.expect(tokenType)
y = functionStream.expect(...tokenTypes)
} else if (valueIfOmitted !== undefined) {
y = valueIfOmitted
} else {
@@ -31,18 +31,18 @@ const xyTransformFactory = tokenType => (

return [{ [`${key}Y`]: y }, { [`${key}X`]: x }]
}
const xyNumber = xyTransformFactory(NUMBER)
const xyLength = xyTransformFactory(LENGTH)
const xyAngle = xyTransformFactory(ANGLE)
const xyNumber = xyTransformFactory([NUMBER])
const xyLengthOrPercent = xyTransformFactory([LENGTH, PERCENT])
const xyAngle = xyTransformFactory([ANGLE])

const partTransforms = {
perspective: singleNumber,
scale: xyNumber('scale'),
scaleX: singleNumber,
scaleY: singleNumber,
translate: xyLength('translate', 0),
translateX: singleLength,
translateY: singleLength,
translate: xyLengthOrPercent('translate', 0),
translateX: singleLengthOrPercent,
translateY: singleLengthOrPercent,
rotate: singleAngle,
rotateX: singleAngle,
rotateY: singleAngle,