Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 71512a4

Browse files
Merge pull request #398 from IHIutch/fix/nested-aspect-ratio
fix: Deeply nested aspect ratio
2 parents 106a7ff + 77594d6 commit 71512a4

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

packages/chakra-ui-core/src/CAspectRatioBox/CAspectRatioBox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const CAspectRatioBox = {
6565
left: 0,
6666
...vnode.data.attrs
6767
}
68-
})
68+
}, vnode.componentOptions.children || [])
6969

7070
return h('div', {
7171
class: this.className,

packages/chakra-ui-core/src/CAspectRatioBox/CAspectRatioBox.stories.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,23 @@ storiesOf('UI | AspectRatioBox', module)
2222
}
2323
}
2424
}))
25+
.add('Nested Elements', () => ({
26+
components: { CAspectRatioBox, CBox },
27+
template: `
28+
<div style="width: 100vh; height: 100vw;">
29+
<CAspectRatioBox maxW="560px" :ratio="16 / 9" bg="gray.200">
30+
<CFlex align="center" justify="center">
31+
<CBox text-align="center">
32+
<CHeading>See the Vue</CHeading>
33+
<CText mt="4">Vue makes front-end development a breeze.</CText>
34+
</CBox>
35+
</CFlex>
36+
</CAspectRatioBox>
37+
</div>
38+
`,
39+
data () {
40+
return {
41+
showCollapsed: true
42+
}
43+
}
44+
}))

packages/chakra-ui-core/src/CAspectRatioBox/tests/CAspectRatioBox.test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const renderComponent = (props) => {
66
components: { CAspectRatioBox, CBox },
77
template: `
88
<CAspectRatioBox maxW="400px" data-testid="aspectRatioBox" ${inlineAttrs}>
9-
<CBox as="img" src="https://bit.ly/naruto-sage" alt="naruto" objectFit="cover" data-testid="image" />
9+
<CBox data-testid="child">
10+
<CBox as="img" src="https://bit.ly/naruto-sage" alt="naruto" objectFit="cover" h="100%" w="100%" data-testid="image" />
11+
</CBox>
1012
</CAspectRatioBox>
1113
`,
1214
...props
@@ -22,35 +24,50 @@ it('should render correctly', () => {
2224
const [emotionClassName] = [...screen.getByTestId('aspectRatioBox').classList]
2325
const pseudoStyles = getElementStyles(`.${emotionClassName}:before`)
2426

25-
expect(pseudoStyles).toContain(`
27+
expect(pseudoStyles).toContain(
28+
`
2629
padding-bottom: 100%
27-
`.trim())
30+
`.trim()
31+
)
2832
})
2933

3034
it('should have correct styles', () => {
3135
const inlineAttrs = ':ratio="2"'
3236
renderComponent({ inlineAttrs })
3337
const image = screen.getByTestId('image')
3438
const aspectRatioBox = screen.getByTestId('aspectRatioBox')
39+
const child = screen.getByTestId('child')
3540

3641
const [emotionClassName] = [...aspectRatioBox.classList] // second className has the pseudo styles
3742
const pseudoStyles = getElementStyles(`.${emotionClassName}:before`)
3843

39-
expect(pseudoStyles).toContain(`
44+
expect(pseudoStyles).toContain(
45+
`
4046
padding-bottom: 50%
41-
`.trim())
47+
`.trim()
48+
)
4249

4350
expect(aspectRatioBox).toHaveStyle(`
4451
max-width: 400px;
4552
position: relative;
4653
`)
4754

48-
expect(image).toHaveStyle(`
49-
object-fit: cover;
55+
// aspectRatioBox should contain exactly 1 immediate child
56+
expect(aspectRatioBox.childElementCount).toEqual(1)
57+
58+
expect(child).toHaveStyle(`
5059
position: absolute;
5160
width: 100%;
5261
height: 100%;
5362
top: 0px;
5463
left: 0px;
5564
`)
65+
66+
expect(child).not.toBeEmpty()
67+
68+
expect(image).toHaveStyle(`
69+
object-fit: cover;
70+
width: 100%;
71+
height: 100%;
72+
`)
5673
})

packages/chakra-ui-core/src/CAspectRatioBox/tests/__snapshots__/CAspectRatioBox.test.js.snap

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ exports[`should render correctly 1`] = `
77
data-chakra-component="CAspectRatioBox"
88
data-testid="aspectRatioBox"
99
>
10-
<img
11-
alt="naruto"
12-
class="css-1mlkgdx"
10+
<div
11+
class="css-k57fb3"
1312
data-chakra-component="CBox"
14-
data-testid="image"
15-
src="https://bit.ly/naruto-sage"
16-
/>
13+
data-testid="child"
14+
>
15+
<img
16+
alt="naruto"
17+
class="css-ygtapu"
18+
data-chakra-component="CBox"
19+
data-testid="image"
20+
src="https://bit.ly/naruto-sage"
21+
/>
22+
</div>
1723
</div>
1824
</DocumentFragment>
1925
`;

0 commit comments

Comments
 (0)