npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kaliber/sanity-image

v1.3.1

Published

Use Sanity images without worrying about srcSet, sizes, or any other of that stuff.

Downloads

16

Readme

@kaliber/sanity-image

An image component for Sanity, which manages resizing, cropping, srcSet and sizes (the attribute) for you.

Motivation

Sanity provides a lot of options when it comes to image optimization and this component tries to be the one size fits all solution, making sure you don't have to think about these. It also manages srcSet and sizes for you by looking at the actual displayed size of the image. This fits much better within a component driven model, where you never know in advance where your component is going to be used (and which sizes value you should therefore use).

Even though the components work without a sizes prop, they do accept one. This allows the image to be displayed immediately, without your javascript bundle being parsed. Useful for images that are displayed above the fold, to reduce your LCP time. Think: hero images.

Installation

Add the following libraries to your compileWithBabel array:

yarn add @kaliber/use-element-size @kaliber/sanity-image @sanity/image-url
compileWithBabel: [
  /@kaliber\/sanity-image/,
  /@kaliber\/use-element-size/,
  /@kaliber\/use-observed-ref/,
]

Usage

Data requirements

The components are meant to be used with the Sanity image type.

Components

This library exports three components, Image, ImageCropped and ImageCover, each with slightly different usecases.

  • Use Image when you want to use an image as is, no cropping, no object-fit: cover.
  • Use ImageCropped when you want to use a cropped image. The crop is determined by the aspectRatio you provide (this respects the hotspot, when configured in Sanity).
  • Use ImageCover when you need an image that covers a size defined in CSS, but this size doesn't have a fixed aspect ratio. This component compensates for upscaling due to using object-fit: cover.

All images accept a sizes prop. If this is given, any calculated size is ignored.

Image

import { Image } from '@kaliber/sanity-image'

function Component({ image }) {
  const { sanity } = useConfig()

  return (
    <Image
      sanityConfig={sanity.client}
      imgProps={{ alt: 'Alt text' }}
      {...{ image }} 
    />
  )
}

ImageCropped

Don't use this component if your aspectRatio is dynamic, since it will regenerate the images when an unknown aspectRatio is used. In this case, use ImageCover instead.

import { ImageCropped } from '@kaliber/sanity-image'

function Component({ image }) {
  const { sanity } = useConfig()

  return (
    <ImageCropped
      sanityConfig={sanity.client}
      aspectRatio={16/9}
      imgProps={{ loading: 'lazy' }} 
      {...{ image }} 
    />
  )
}

ImageCover

Try to use an aspectRatio that's close to the dynamic aspect ratio of your image. This way, you will download the smallest image needed.

import { ImageCover } from '@kaliber/sanity-image'

function Component({ image }) {
  const { sanity } = useConfig()

  return (
    <ImageCover
      sanityConfig={sanity.client}
      aspectRatio={16/9}
      imgProps={{ loading: 'lazy' }} 
      {...{ image }} 
    />
  )
}

Disclaimer

This library is intended for internal use, we provide no support, use at your own risk. It does not import React, but expects it to be provided, which @kaliber/build can handle for you.

This library is not transpiled.