0.3.11 β€’ Published 3 days ago

@tylermcrobert/svelte-sanity-image v0.3.11

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

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

PropertyTypeDescriptionRequired
clientObjectA configured Sanity client.Yes
imageObjectImage data returned from sanity API.Yes
altString | nullDescriptive alt text for image accessibility.Yes
sizesStringA responsive image size string. Read more about that in the MDN image reference.Yes
qualityNumberQuality 0-100. Specify the compression quality (where applicable). Defaults are 75 for JPG and WebP per Sanity's defaults.–
loadingString | nullSet the browser’s native lazy loading attribute. Available options are "lazy", "eager", or null. Defaults to "lazy".–
autoFormatBooleanUses webp format if browser supports it. Defaults to true–
aspectNumberEnforces an aspect ratio on the image.–
onLoadFunctionRuns 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.

0.3.11

3 days ago

0.4.0-next.2

3 days ago

0.4.0-next.1

4 days ago

0.3.0

4 days ago

0.3.6

4 days ago

0.3.5

4 days ago

0.3.8

3 days ago

0.3.7

3 days ago

0.3.2

4 days ago

0.3.1

4 days ago

0.3.4

4 days ago

0.3.3

4 days ago

0.3.9

3 days ago

0.2.22

4 days ago

0.2.21

5 days ago

0.2.20

5 days ago

0.2.19

5 days ago

0.2.18

5 days ago

0.2.17

6 days ago

0.2.16

6 days ago

0.2.15

6 days ago

0.2.14

6 days ago

0.2.13

6 days ago

0.2.12

6 days ago

0.2.11

6 days ago

0.2.10

7 days ago

0.3.0-next.2

10 days ago

0.3.0-next.1

10 days ago

0.2.7

11 days ago

0.2.9

10 days ago

0.2.8

11 days ago

0.2.6

8 months ago

0.2.5

8 months ago

0.2.4

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.0

8 months ago

0.0.14

8 months ago

0.0.13

8 months ago

0.0.11

8 months ago

0.0.10

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.1

8 months ago