Skip to content

Commit 0c6a633

Browse files
authored
Merge pull request #123 from styled-components/aspect-ratio
Add aspect ratio support
2 parents 3caa16c + e81a388 commit 0c6a633

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
@@ -6,6 +6,7 @@ import {
66
UNSUPPORTED_LENGTH_UNIT,
77
WORD,
88
} from '../tokenTypes'
9+
import aspectRatio from './aspectRatio'
910
import border from './border'
1011
import boxShadow from './boxShadow'
1112
import flex from './flex'
@@ -51,6 +52,7 @@ const textShadowOffset = tokenStream => ({
5152
})
5253

5354
export default {
55+
aspectRatio,
5456
background,
5557
border,
5658
borderColor,

0 commit comments

Comments
 (0)