Skip to content

Commit e81a388

Browse files
committed
Add aspect ratio support
1 parent 26f4b49 commit e81a388

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/__tests__/aspectRatio.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import transformCss from '..'
2+
3+
it('handles regular aspect ratio values', () => {
4+
expect(transformCss([['aspect-ratio', '1.5']])).toEqual({
5+
aspectRatio: 1.5,
6+
})
7+
})
8+
9+
it('handles CSS-style aspect ratios', () => {
10+
expect(transformCss([['aspect-ratio', '3 / 2']])).toEqual({
11+
aspectRatio: 1.5,
12+
})
13+
})
14+
15+
it('handles CSS-style aspect ratios without spaces', () => {
16+
expect(transformCss([['aspect-ratio', '3/2']])).toEqual({
17+
aspectRatio: 1.5,
18+
})
19+
})
20+
21+
it('throws when omitting second value after slash', () => {
22+
expect(() => transformCss([['aspect-ratio', '3/']])).toThrow()
23+
})

src/transforms/aspectRatio.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NUMBER, SLASH } from '../tokenTypes'
2+
3+
export default tokenStream => {
4+
let aspectRatio = tokenStream.expect(NUMBER)
5+
6+
if (tokenStream.hasTokens()) {
7+
tokenStream.expect(SLASH)
8+
aspectRatio /= tokenStream.expect(NUMBER)
9+
}
10+
11+
return { aspectRatio }
12+
}

src/transforms/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PERCENT,
88
AUTO,
99
} from '../tokenTypes'
10+
import aspectRatio from './aspectRatio'
1011
import border from './border'
1112
import boxShadow from './boxShadow'
1213
import flex from './flex'
@@ -53,6 +54,7 @@ const textShadowOffset = tokenStream => ({
5354
})
5455

5556
export default {
57+
aspectRatio,
5658
background,
5759
border,
5860
borderColor,

0 commit comments

Comments
 (0)