image icon indicating copy to clipboard operation
image copied to clipboard

Improve sizes handling for mobile-first

Open MielPoule opened this issue 4 years ago • 3 comments

As v.0.4.9, we need to specify a size for every type of screen even if the size is the same. E.g. for an image that is always 100% of the screen : sizes="xs:100vw sm:100vw md:100vw lg:100vw xl:100vw 2xl:100vw" If not, only the srcset of the specified screen will be generated.

We could automatically generate other srcset with a mobile-first rule.

For exemple: If we specify only sizes="100vw", we loop through every screens size to generate appropriate srcset. If we specify sizes="100vw xl:400px", we loop through every screens size under xl to generate srcset with screen width and then for xl and larger we generate 400px srcset. etc

Would it make sense ?

MielPoule avatar May 13 '21 15:05 MielPoule

Hi @MielPoule. Seems a nice idea. We (with @Atinux) was thinking about something similar for fluid behavior. It seems to me a more generic shortcut with sizes="xvw" considering it fluid. In contrast sizes="fluid:x" seems easier to implement and less risky against API breaking changes...

pi0 avatar May 13 '21 18:05 pi0

Should be easy enough to rig something up by writing a slim wrapper component for nuxt-image, right?

Then manually setting the sizes prop to the specified one, maybe even dynamically reading from the module config which sizes are available.

reneroth avatar Apr 26 '23 12:04 reneroth

I’ve created helper for this.

# plugins/const.ts
export default defineNuxtPlugin(() => {
  return {
    provide: {
      allSizes: 'xs:100vw sm:100vw md:100vw lg:100vw xl:100vw 2xl:100vw',
    }
  }
})

<NuxtImg src="/image.jpg" :sizes="$allSizes" />

iBobik avatar Oct 05 '24 15:10 iBobik