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

Commit ace19f2

Browse files
Merge pull request #423 from MWFIAE/patch-1
feat(image): Add srcset support to CImage
2 parents 782fecd + 1eddbe1 commit ace19f2

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const CImage = {
2525
mixins: [createStyledAttrsMixin('CImage')],
2626
props: {
2727
src: String,
28+
srcset: String,
2829
fallbackSrc: String,
2930
ignoreFalback: Boolean,
3031
htmlWidth: String,
@@ -48,12 +49,13 @@ const CImage = {
4849
created () {
4950
// Should only invoke window.Image in the browser.
5051
if (process.browser) {
51-
this.loadImage(this.src)
52+
this.loadImage(this.src, this.srcset)
5253
}
5354
},
5455
methods: {
55-
loadImage (src) {
56+
loadImage (src, srcset) {
5657
const image = new window.Image()
58+
image.srcset = srcset
5759
image.src = src
5860

5961
image.onload = (event) => {
@@ -70,9 +72,9 @@ const CImage = {
7072
render (h) {
7173
let imageProps
7274
if (this.ignoreFallback) {
73-
imageProps = { src: this.src }
75+
imageProps = { src: this.src, srcset: this.srcset }
7476
} else {
75-
imageProps = { src: this.hasLoaded ? this.src : this.fallbackSrc }
77+
imageProps = { src: this.hasLoaded ? this.src : this.fallbackSrc, srcset: this.srcset }
7678
}
7779
return h(CNoSsr, [
7880
h('img', {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ it('fallback src works', async () => {
4545
expect(screen.getByAltText(/Mesut Koca/i)).toHaveAttribute('src', 'LOAD_FALLBACK_SRC')
4646
})
4747
})
48+
49+
it('srcset works', async () => {
50+
renderComponent({ template: '<CImage alt="My Image Description" src="LOAD_SUCCESS_SRC" srcset="LOAD_SUCCESS_SRC 400w" />' })
51+
await wait(() => {
52+
expect(screen.getByAltText(/My Image Description/i)).toHaveAttribute('srcset', 'LOAD_SUCCESS_SRC 400w')
53+
})
54+
})

0 commit comments

Comments
 (0)