Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt-img): responsive prop #155

Merged
merged 4 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<NuxtImg src="https://nuxtjs.org/logos/nuxt.svg" width="400" height="400" />

<h2>JPEG image inside project</h2>
<NuxtImg src="/images/damavand.jpg" />
<NuxtImg responsive src="/images/damavand.jpg" />

<h2>JPEG image from remote url</h2>
<NuxtImg src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Aconcagua2016.jpg/600px-Aconcagua2016.jpg" />
Expand Down
33 changes: 31 additions & 2 deletions src/runtime/components/nuxt-img.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
:height="height"
:src="nSrc"
:alt="nAlt"
v-bind="nAttrs"
>
</template>

<script lang="ts">
import { generateAlt, useObserver } from '~image'
import { generateAlt, useObserver, parseSize } from '~image'

const EMPTY_GIF = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'

Expand All @@ -32,7 +33,8 @@ export default {

// options
preset: { type: String, required: false, default: undefined },
provider: { type: String, required: false, default: undefined }
provider: { type: String, required: false, default: undefined },
responsive: { type: Boolean, required: false, default: false }
},
data () {
return {
Expand All @@ -53,6 +55,15 @@ export default {
modifiers: this.modifiers
}).url
},
nAttrs () {
const attrs: any = {}
if (this.responsive) {
const { sizes, srcSet } = this.getResponsive()
attrs.sizes = sizes
attrs.srcSet = srcSet
}
return attrs
},
modifiers () {
return {
width: this.width,
Expand All @@ -64,6 +75,12 @@ export default {
}
}
},
created () {
if (process.server && process.static) {
// Force compute sources into ssrContext
this.getResponsive()
}
},
mounted () {
if (this.usePlaceholder) {
this.observe()
Expand All @@ -73,6 +90,18 @@ export default {
this.unobserve()
},
methods: {
getResponsive () {
const sizes = this.$img.getSizes(this.src, {
sizes: this.sizes,
width: parseSize(this.width),
height: parseSize(this.height),
modifiers: this.modifiers
})
return {
sizes: sizes.map(({ width }) => `(max-width: ${width}px) ${width}px`),
srcSet: sizes.map(({ width, src }) => `${src} ${width}w`)
}
},
observe () {
this._removeObserver = useObserver(this.$el, () => {
this.usePlaceholder = false
Expand Down