Skip to content

Commit b23161e

Browse files
committed
feat(AspectRatio): add AspectRatio compoennt
layout component that maintains the arpect ratio of the box
1 parent 4de9777 commit b23161e

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
"@radix-ui/colors": "^0.1.8",
151151
"@radix-ui/react-accordion": "^1.0.0",
152152
"@radix-ui/react-alert-dialog": "^1.0.0",
153+
"@radix-ui/react-aspect-ratio": "^1.0.0",
153154
"@radix-ui/react-avatar": "^1.0.0",
154155
"@radix-ui/react-checkbox": "^1.0.0",
155156
"@radix-ui/react-context-menu": "^1.0.0",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Meta, Story } from '@storybook/react'
2+
import React, { ComponentProps, useState } from 'react'
3+
import { AspectRatio } from '.'
4+
import { Box } from '../Box'
5+
import { Card, CardBody, CardHeading, CardSubheading } from '../Card'
6+
import { Row } from '../Flex'
7+
import { Image } from '../Image'
8+
import { Label } from '../Label'
9+
import { Slider } from '../Slider'
10+
11+
export default {
12+
title: 'Components/AspectRatio',
13+
component: AspectRatio,
14+
} as Meta
15+
16+
export const Default: Story<ComponentProps<typeof AspectRatio>> = ({
17+
ratio = 9 / 16,
18+
...args
19+
}) => (
20+
<Card css={{ width: '50%' }}>
21+
<CardHeading>Photo by Damian Markutt</CardHeading>
22+
<CardSubheading>@wildandfree_photography</CardSubheading>
23+
<CardBody>
24+
{/* ratio={ 9 / 16} */}
25+
<AspectRatio ratio={ratio} {...args}>
26+
<Image
27+
src="https://images.unsplash.com/photo-1572302895496-a09d147fa902?w=300&dpr=2&q=80"
28+
alt="Photo by Damian Markutt @wildandfree_photography"
29+
/>
30+
</AspectRatio>
31+
</CardBody>
32+
</Card>
33+
)
34+
35+
/**
36+
* The ratio can be entered as a numerator and denominator to give comm0n values like `16 / 9`.
37+
*/
38+
export const Dynamic: Story = () => {
39+
const [numerator, setNumerator] = useState([9])
40+
const [denominator, setDenominator] = useState([16])
41+
42+
return (
43+
<Row>
44+
<Box variant="wide">
45+
<Label>Numerator</Label>
46+
<Slider value={numerator} onValueChange={setNumerator} />
47+
<Label>Denominator</Label>
48+
<Slider value={denominator} onValueChange={setDenominator} />
49+
</Box>
50+
<Card css={{ width: '50%' }}>
51+
<CardHeading>Photo by Damian Markutt</CardHeading>
52+
<CardSubheading>@wildandfree_photography</CardSubheading>
53+
<CardBody>
54+
<AspectRatio ratio={numerator[0] / denominator[0]}>
55+
<Image
56+
src="https://images.unsplash.com/photo-1572302895496-a09d147fa902?w=300&dpr=2&q=80"
57+
alt="Photo by Damian Markutt @wildandfree_photography"
58+
/>
59+
</AspectRatio>
60+
</CardBody>
61+
</Card>
62+
</Row>
63+
)
64+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
import { renderDark, renderLight } from 'test-utils'
3+
import { Default } from './AspectRatio.stories'
4+
5+
it('renders light without error', () => {
6+
const { asFragment } = renderLight(<Default />)
7+
expect(asFragment()).toBeDefined()
8+
})
9+
10+
it('renders dark without error', () => {
11+
const { asFragment } = renderDark(<Default />)
12+
expect(asFragment()).toBeDefined()
13+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Root } from '@radix-ui/react-aspect-ratio'
2+
3+
/**
4+
* AspectRatio can be used to display content with a fixed aspect ratio
5+
*/
6+
export const AspectRatio = Root
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './AspectRatio'

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './Accordion'
22
export * from './Alert'
33
export * from './AppBar'
4+
export * from './AspectRatio'
45
export * from './Avatar'
56
export * from './Backdrop'
67
export * from './Badge'

0 commit comments

Comments
 (0)