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

@otterstack/sanity-img-svelte

v0.2.3

Published

A svelte framework components for rendering responsive `<img>` elements for images fetched from [Sanity](https://www.sanity.io). It will generate the elements with a srcset optimised for a range of resolutions and formats, using sanity's image API to serv

Downloads

3

Readme

@otterstack/sanity-img-svelte

A svelte framework components for rendering responsive <img> elements for images fetched from Sanity. It will generate the elements with a srcset optimised for a range of resolutions and formats, using sanity's image API to serve the optimised images. Then you provide the sizes attribute, to ensure the browser delivers the ideal source. Refer to Responsive images - MDN for information on providing responsive images.

Then usage can be as simple as:

<script>
  import SanityImg from "@otterstack/sanity-img-svelte"
</script>
---
   <SanityImg
    src={myImage}
    sizes="(min-width:768px) 50vw, 100vw"
  /> 

Setup

Setting defaults for all components

You will likely want to provide a default configuration for all your components, at least to provide an image url builder from your sanity client. For this, the function setSanityImgDefaults can be used. Defaults will be set across all components across all @otterstack packages:

---
import { setSanityImgDefaults } from "@otterstack/sanity-img-svelte";

setSanityImgDefaults({ imageUrlBuilder: myImageUrlBuilder, options: {auto: "format" } })
---

In astro

If using the svelte component in astro, you likely will want to set up the integration in @otterstack/sanity-img-astro. This will set a default image builder and other options, making using the component simpler. Refer to the README in that package for instructions.

Usage

The key properties to provide for responsive images are:

  • src: Image from sanity data
  • sizes: Sizes string to allow browser to select ideal image from automatically generated srcset
<script>
import SanityImg from "@otterstack/sanity-img-svelte"
</script>

   <SanityImg
    src={myImage}
    sizes="(min-width:768px) 50vw, 100vw"
  /> 

Additional properties are:

  • imageUrlBuilder: A Sanity Image url builder to use for this element. Only necessary if default is not set. Refer to setting defaults for how to set default.
  • widths: An array of widths to generate for srcset, or an autowidths struct { maxWidth: number; step: number; } to inform the component how to generate sources up to the image's original size
  • options: Additional options to pass to image builder

Additionally, the component extends the img element, and can accept any other props that img does, eg alt:

   <SanityImg
    src={myImage}
    sizes="(min-width:768px) 50vw, 100vw"
    alt="My Image"
  /> 

Fetching the image with groq

The component will work with images fetched with a simple groq query without fetching any image metadata, eg

const query = groq`*[_id == 'homePage'][0] {
     ...etc,
     myBackgroundImage,
     ...etc,
  }`

However the tag is able to optimise itself more, such as setting width and height attributes to lower LCP, when the image metadata is fetched. To assist with this, you can use the image function:

import { image } from '@otterstack/sanity-img-svelte'

const query = groq`*[_id == 'homePage'][0] {
  ...etc,
  ${image('myBackgroundImage')},
  ...etc
}`