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

sanity-plugin-hotspot-array

v2.1.2

Published

A configurable Custom Input for Arrays that will add and update items by clicking on an Image

Downloads

34,230

Readme

sanity-plugin-hotspot-array

This is a Sanity Studio v3 plugin. For the v2 version, please refer to the v2-branch.

What is it?

A configurable Custom Input for Arrays that will add and update items by clicking on an Image

Installation

npm install --save sanity-plugin-hotspot-array

or

yarn add sanity-plugin-hotspot-array

Usage

Add it as a plugin in sanity.config.ts (or .js):

import { imageHotspotArrayPlugin } from "sanity-plugin-hotspot-array";

export default defineConfig({
  // ...
  plugins: [
    imageHotspotArrayPlugin(),
  ] 
})

Now you will have imageHotspot available as an options on array fields:

import {defineType, defineField} from 'sanity'

export const productSchema = defineType({
  name: `product`,
  title: `Product`,
  type: `document`,
  fields: [
    defineField({
      name: `hotspots`,
      type: `array`,
      of: [
        // see `Spot object` setup below
      ],
      options: {
        // plugin adds support for this option
        imageHotspot: {
          // see `Image and description path` setup below
          imagePath: `featureImage`,
          descriptionPath: `details`,
          // see `Custom tooltip` setup below
          tooltip: undefined,
        }
      },
    }),
    // ...all your other fields
    // ...of which one should be featureImage in this example
  ],
})

There is no need to provide an explicit input component, as that is handled by the plugin.

The plugin makes a number of assumptions to add and update data in the array. Including:

  • The array field is an array of objects, with a single object type
  • The object contains two number fields named x and y
  • You'll want to save those values as % from the top left of the image
  • The same document contains the image you want to add hotspots to

Image path

The custom input has the current values of all fields in the document, and so can "pick" the image out of the document by its path.

For example, if you want to add hotspots to an image, and that image is uploaded to the field featuredImage, your fields options would look like:

options: {
  imageHotspot: {
    imagePath: `featureImage`
  }
}

To pick the image out of the hotspot-array parent object, use

options: {
  imageHotspot: {
    pathRoot: 'parent'
  }
}

Description path

The custom input can also pre-fill a string or text field with a description of the position of the spot to make them easier to identify. Add a path relative to the spot object for this field.

options: {
  imageHotspot: {
    descriptionPath: `details`
  }
}

Spot object

Here's an example object schema complete with initial values, validation, fieldsets and a styled preview.

defineField({
  name: 'spot',
  type: 'object',
  fieldsets: [{name: 'position', options: {columns: 2}}],
  fields: [
    {name: 'details', type: 'text', rows: 2},
    {
      name: 'x',
      type: 'number',
      readOnly: true,
      fieldset: 'position',
      initialValue: 50,
      validation: (Rule) => Rule.required().min(0).max(100),
    },
    {
      name: 'y',
      type: 'number',
      readOnly: true,
      fieldset: 'position',
      initialValue: 50,
      validation: (Rule) => Rule.required().min(0).max(100),
    },
  ],
  preview: {
    select: {
      title: 'details',
      x: 'x',
      y: 'y',
    },
    prepare({title, x, y}) {
      return {
        title,
        subtitle: x && y ? `${x}% x ${y}%` : `No position set`,
      }
    },
  },
})

Custom tooltip

You can customise the Tooltip to display any Component, which will receive value (the hotspot value with x and y), schemaType (schemaType of the hotspot value), and renderPreview (callback for rendering Studio preview).

Example 1 - use default hotspot preview

import { Box } from "@sanity/ui";
import { HotspotTooltipProps } from "sanity-plugin-hotspot-array";

export function HotspotPreview({
  value,
  schemaType,
  renderPreview,
}: HotspotTooltipProps) {
  return (
    <Box padding={2} style={{ minWidth: 200 }}>
      {renderPreview({
        value,
        schemaType,
        layout: "default",
      })}
    </Box>
  );
}

Then back in your schema definition

options: {
  imageHotspot: {
    tooltip: HotspotPreview
  }
}

Example 2 - reference value in hotspot

In this example our value object has a reference field to the product schema type, and will show a document preview.

import {useSchema }from 'sanity'
import {Box} from '@sanity/ui'

export function ProductPreview({value, renderPreview}) {
  const productSchemaType = useSchema().get('product')
  return (
    <Box padding={2} style={{minWidth: 200}}>
      {value?.product?._ref ? (
        renderPreview({
           value,
           schemaType: productSchemaType,
           layout: "default"
        })
      ) : (
        `No reference selected`
      )}
    </Box>
  )
}

Then back in your schema definition

options: {
  imageHotspot: {
    tooltip: ProductPreview
  }
}

License

MIT-licensed. See LICENSE.

Develop & test

This plugin uses @sanity/plugin-kit with default configuration for build & watch scripts.

See Testing a plugin in Sanity Studio on how to run this plugin with hotreload in the studio.

Release new version

Run "CI & Release" workflow. Make sure to select the main branch and check "Release new version".

Semantic release will only release on configured branches, so it is safe to run release on any branch.