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

@tylermcrobert/svelte-sanity-image

v0.3.12

Published

A Svelte component that allows you to easily create responsive images from images stored in Sanity.io. This is powered by the [Sanity Image Builder](https://www.sanity.io/docs/image-url) under the hood. Package Inspired by [next-sanity-image](https://gith

Downloads

319

Readme

svelte-sanity-image

A Svelte component that allows you to easily create responsive images from images stored in Sanity.io. This is powered by the Sanity Image Builder under the hood. Package Inspired by next-sanity-image.

:warning: This package is under active development and could introduce breaking changes.

See the demo here

Features of this package:

  • Creates a set of responsive image sizes.
  • Automatically sets width and height of image tag to prevent layout shifts.
  • Allows for defining a custom aspect ratio.
  • Allows passing all standard HTMLImageElement props to component.
  • Defaults with configurable performance benefits like lazy loading, auto formatting to webp, and quality.
  • Allows for image preloading in svelte:head with the priority prop.
  • Provides helpful errors for malformed or empty image sources.
  • Includes helpful utility functions for working with sanity image image asset sources.
  • Fully typed and exposes relevant types.

📦 Installation

npm install @tylermcrobert/svelte-sanity-image

🚀 Usage

Start by creating a GROQ query to fetch a Sanity document containing an image.

import type { SanityImageObject } from '@tylermcrobert/svelte-sanity-image'; // Optional typing

export async function load() {
  const imageQuery = `*[_type == 'yourDoc'][0].yourImage`;
  const image: SanityImageObject = await client.fetch(imageQuery);

  return { image };
}

Next, supply the SanityImage component with the image from Sanity along with your configured sanity client.

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

This represents a basic implementation of the component. Usage is similar to a standard <img /> tag, but instead takes a image and a client. For more details on those and other configuration options, refer to the props table.

⚙️ Component Props

| Property | Type | Description | Required | | ------------ | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | | client | Object | A configured Sanity client. | Yes | | image | Object | Image data returned from sanity API. | Yes | | alt | String | null | Descriptive alt text for image accessibility. | Yes | | sizes | String | A responsive image size string. Read more about that in the MDN image reference. | Yes | | quality | Number | Quality 0-100. Specify the compression quality (where applicable). Defaults are 75 for JPG and WebP per Sanity's defaults. | – | | loading | String | null | Set the browser’s native lazy loading attribute. Available options are "lazy", "eager", or null. Defaults to "lazy". | – | | autoFormat | Boolean | Uses webp format if browser supports it. Defaults to true | – | | aspect | Number | Enforces an aspect ratio on the image. | – | | onLoad | Function | Runs on image load and provides an event object | |

🧰 Utilities

  • getReferenceId - Supplies the reference ID from any valid GROQ image asset result
  • getDimsFromRefString - Pulls out the original image dimensions from a reference ID.
  • getImageDimensions - Takes a GROQ image source and extracts the dimensions (including if the image is cropped within sanity.)

🤝 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

📜 License

Copyright ©2023 Tyler McRobert. Available under the MIT License.