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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tylermcrobert/svelte-sanity-image

v1.2.3

Published

A Svelte component for creating responsive, optimized images from Sanity.io. Powered by the [Sanity Image Builder](https://www.sanity.io/docs/image-url) under the hood, it simplifies responsive image handling and layout shift prevention in your Svelte pro

Downloads

122

Readme

svelte-sanity-image

A Svelte component for creating responsive, optimized images from Sanity.io. Powered by the Sanity Image Builder under the hood, it simplifies responsive image handling and layout shift prevention in your Svelte projects.

✨ Features

  • Prevents layout shifts by setting image dimensions automatically.
  • Creates srcset attributes for responsive images out of the box.
  • Supports custom aspect ratios.
  • Provides control of native <img /> attributes.
  • Available preloading of images with the preload prop.
  • Fully typed with TypeScript support and unit-tested for reliability.

📦 Installation

Install the package via npm:

npm install @tylermcrobert/svelte-sanity-image

🚀 Quick Start

A minimal example of using svelte-sanity-image:

<SanityImage
	{client}
	{image}
	sizes="(max-width: 768px) 50vw, 100vw"
	alt="The Beatles crossing Abbey Road in London."
/>

This component extends the standard <img /> element, so you can use any native attributes or events.

An example creating a wrapper component:

<script lang="ts">
	import { client } from '$lib/sanity';
	import Image, { type SvelteSanityImageProps } from '@tylermcrobert/svelte-sanity-image';

	type ImageProps = Omit<SvelteSanityImageProps, 'client'> & {
		aboveTheFold: boolean;
	};

	let { aboveTheFold, ...props }: ImageProps = $props();
</script>

<Image
	{...props}
	{client}
	quality={80}
	loading={aboveTheFold ? 'eager' : 'lazy'}
	fetchpriority={aboveTheFold ? 'high' : undefined}
/>

An example using getImageProps:

<script lang="ts">
	import { client } from '$lib/sanity';
	import Image, { type getImageProps } from '@tylermcrobert/svelte-sanity-image';

	type ImageProps = { image: SanityImageSource };

	let { image }: ImageProps = $props();

	let imageProps = $derived(getImageProps(client, image, { quality: 80 }));
</script>

<img {...imageProps} />

⚙️ Component Props

| Property | Type | Description | Required | | ------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | | client | Object | A configured Sanity client or project details. | Yes | | image | Object | Image data returned from Sanity API. | Yes | | alt | String | Descriptive alt text for accessibility. | Yes | | sizes | String | null | A responsive image size string (MDN reference). Set to null to bypass responsivity. | Yes | | aspect | Number | Enforces an aspect ratio on the image. | – | | preload | Boolean | Adds a <link rel="preload" /> in <svelte:head> for prioritized loading. | – | | srcsetBreakpoints | string[] | Overrides the default breakpoints for srcset. Defaults to 640, 750, 828, 1080, 1200, 1920, 2048, 3840. | – |

Optimization Defaults

This package makes similar default optimizations as Next/Image.

| Property | Value | Description | | ------------ | ------ | ----------------------------------------------------------------------------------------------------------------------- | | loading | lazy | Defers loading of images until they are near the viewport. | | autoFormat | true | Automatically return an image in the most optimized format supported by the browser as determined by its Accept header. | | quality | 75 | Set automatically to 75 by Sanity`s internal image transformations |

Supported Sanity transformations:

svelte-sanity-image supports the following Image Transformations:

blur, bg, dpr, width, height, quality, sharpen, format, invert, download, flipHorizontal, flipVertical, saturation, and frame.

🤝 Contributing

Go into the package.json and give your package the desired name through the "name" option. Also consider adding a "license" field and point it to a LICENSE file which you can create from a template (one popular option is the MIT license).

📜 License

MIT License © 2024 Tyler McRobert