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

Commit ae1d19e

Browse files
Merge pull request #446 from chakra-ui/fix/img-src-attr
fix: img src attr
2 parents fafc52c + 737631e commit ae1d19e

File tree

11 files changed

+75
-7
lines changed

11 files changed

+75
-7
lines changed

.changeset/curvy-squids-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chakra-ui/vue': patch
3+
---
4+
5+
Fixes CImage src attribute bug

.codesandbox/ci.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"installCommand": "csb:install",
3+
"buildCommand": "build",
4+
"packages": [
5+
"packages/chakra-ui-core",
6+
"packages/chakra-ui-nuxt"
7+
],
8+
"sandboxes": ["chakra-ui-vue-0x-starter-template-2sy0g", "chakra-ui-nuxt-demo-f8tq4"],
9+
"node": "14"
10+
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ config/.env
3737
packages/chakra-ui-nuxt/.github
3838
packages/chakra-ui-docs/static/sw.js
3939
website/static/sw.js
40-
.npmrc
40+
.npmrc
41+
**/static/sw.js

.storybook/assets/chakra.png

96.9 KB
Loading

.storybook/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module.exports = async ({ config, mode }) => {
1515
'sass-loader'
1616
],
1717
});
18+
config.resolve.alias = {
19+
...config.resolve.alias,
20+
"@/": path.resolve(__dirname, "./"),
21+
};
1822

1923
// Return the altered config
2024
return config;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"scripts": {
2323
"postinstall": "lerna link",
24+
"csb:install": "yarn && yarn bootstrap",
2425
"bootstrap": "lerna bootstrap --use-workspaces",
2526
"build:dev": "yarn workspace @chakra-ui/vue run rollup --config rollup.dev.config.js",
2627
"build": "yarn workspace @chakra-ui/vue build",

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ const CImage = {
5555
methods: {
5656
loadImage (src, srcset) {
5757
const image = new window.Image()
58-
image.srcset = srcset
59-
image.src = src
58+
if (srcset) {
59+
image.srcset = srcset
60+
}
61+
if (src) {
62+
image.src = src
63+
}
6064

6165
image.onload = (event) => {
6266
this.hasLoaded = true

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@ storiesOf('UI | Image', module)
88
<CImage
99
shadow="sm"
1010
htmlWidth="100px"
11-
src="https://bit.ly/chakra-jonathan-bakebwa"
11+
src="https://bit.ly/chakra-jonathan-bakebw3a"
12+
srcset="https://bit.ly/chakra-jonathan-bakebwa"
13+
/>
14+
`
15+
}))
16+
.add('Webpack required image', () => ({
17+
components: { CImage },
18+
template: `
19+
<CImage
20+
shadow="sm"
21+
htmlWidth="100px"
22+
:src="require('@/assets/chakra.png')"
1223
/>
1324
`
1425
}))

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ it('fallback src works', async () => {
4646
})
4747
})
4848

49-
it('srcset works', async () => {
50-
renderComponent({ template: '<CImage alt="My Image Description" src="LOAD_SUCCESS_SRC" srcset="LOAD_SUCCESS_SRC 400w" />' })
49+
it('"srcset" should work and be prioritized over "src" if provided to CImage', async () => {
50+
const { asFragment } = renderComponent({ template: '<CImage alt="My Image Description" src="LOAD_SUCCESS_SRC" srcset="LOAD_SUCCESS_SRC 400w" />' })
5151
await wait(() => {
5252
expect(screen.getByAltText(/My Image Description/i)).toHaveAttribute('srcset', 'LOAD_SUCCESS_SRC 400w')
53+
expect(asFragment()).toMatchSnapshot()
54+
})
55+
})
56+
57+
it('should use src if srcset provided is undefined', async () => {
58+
const { asFragment } = renderComponent({ template: '<CImage alt="My Image Description" src="LOAD_SUCCESS_SRC" srcset="LOAD_FAILURE_SRC" />' })
59+
await wait(() => {
60+
expect(screen.getByAltText(/My Image Description/i)).toHaveAttribute('src', 'LOAD_SUCCESS_SRC')
61+
expect(asFragment()).toMatchSnapshot()
5362
})
5463
})

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`"srcset" should work and be prioritized over "src" if provided to CImage 1`] = `
4+
<DocumentFragment>
5+
<img
6+
alt="My Image Description"
7+
class="css-0"
8+
data-chakra-component="CImage"
9+
src="LOAD_SUCCESS_SRC"
10+
srcset="LOAD_SUCCESS_SRC 400w"
11+
/>
12+
</DocumentFragment>
13+
`;
14+
315
exports[`should render correctly 1`] = `
416
<DocumentFragment>
517
<img
@@ -11,3 +23,15 @@ exports[`should render correctly 1`] = `
1123
/>
1224
</DocumentFragment>
1325
`;
26+
27+
exports[`should use src if srcset provided is undefined 1`] = `
28+
<DocumentFragment>
29+
<img
30+
alt="My Image Description"
31+
class="css-0"
32+
data-chakra-component="CImage"
33+
src="LOAD_SUCCESS_SRC"
34+
srcset="LOAD_FAILURE_SRC"
35+
/>
36+
</DocumentFragment>
37+
`;

website/static/sw.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)