A multi-tag input for sanity studio.

Overview

sanity-plugin-tags

A multi-tag input for sanity studio. Fully featured with autocomplete capabilities, live updates, predefined tag options, style and component customizability, and much more.

Example Picture

Install

sanity install tags

Use

No configuration necessary! Simply use 'tag' or 'tags' as a type (single or multi select respectively) in your fields. If you want autocompletion, set the includeFromRelated option to the name of your field.

That's it! It will even update the autocompletion list live as changes are made to other documents!

Dive into the Options Section for more advanced use cases like predefined tags and the onCreate hook.

{
  name: 'myTags',
  title: 'Tags',
  type: 'tags',
  options: {
    includeFromRelated: 'myTags'
    ...
  }
}

Options

{
  name: string,
  type: "tags" | "tag",
  options: {
    predefinedTags?: Tag | Tag[] | () => Tag[] | Tag | () => Promise<Tag[] | Tag>
    includeFromReference?: false | string
    includeFromRelated?: false | string
    customLabel?: string
    customValue?: string
    allowCreate?: boolean
    onCreate?: (inputValue: string) => Tag
    reactSelectOptions?: {
      [key: string]: any
    }
  },
  //... all other Sanity Properties
},

What is a Tag?

A tag is simply an object with a label and value. Example:

{
  "label": "My Tag",
  "value": "my-tag"
}

This can be used for all sorts of things: categorization, single select, and much more. Essentially, if you want to limit people to a single or multi list of strings, tags will fit your use case perfectly.

predefinedTags

default: []

This option allows you to add any tags that you would like to the autocomplete list. This can take any form from a single tag to an array of tags, to a function that dynamically returns a tag or tags.

{
  // ...
  predefinedTags: { label: "My Tag", value: 'my-tag' }
  // ...
}
{
  // ...
  predefinedTags: [
    {label: 'My Tag 1', value: 'my-tag-1'},
    {label: 'My Tag 2', value: 'my-tag-2'},
  ]
  // ...
}
{
  // ...
  predefinedTags: async () => client.fetch(...)
  // ...
}

includeFromReference

default: false

If you already have a sanity schema that contains a tag-like structure and want to add them to the autocomplete list, set this option to the name of your sanity schema document. This option applies no filters. If you would like to filter, use the predefinedTags option.

{
  // ...
  includeFromReference: 'category'
  // ...
}

includeFromRelated

default: false

This option is similar to includeFromReference, but it allows you to add to the autocomplete list from a field in the related document. Typically, you would set this option to the name of the current field to allow autocompletion for tags that were already selected previously.

{
  // ...
  includeFromRelated: 'category'
  // ...
}

customLabel

default: 'label'

If you want to change the label key for your tags, set this option. Useful when you want to use the default label key to store some other value.

Note: If you set this option, all tags specified by predefinedTags and the structure returned by onCreate must use this custom label

{
  // ...
  customLabel: 'myLabelKey'
  // ...
}

customValue

default: 'value'

If you want to change the value key for your tags, set this option. Useful when you want to use the default value key to store some other value.

Note: If you set this option, all tags specified by predefinedTags and the structure returned by onCreate must use this custom value

{
  // ...
  customValue: 'myValueKey'
  // ...
}

allowCreate

default: true

By default, new tags can be created inline from this input. If you implement the input with a reference, this does not work. See Parts for more information.

{
  // ...
  allowCreate: false
  // ...
}

onCreate

default: (value) => ({ [customLabel]: value, [customValue]: value})

If you want to edit the label or value of the tag when a new one is created before saving it, use this hook. You do not need to specify this property if you set customLabel or customValue and like the default value. If you do specify it, make sure it returns an object that contains the custom label key and the custom value key. This hook provides an easy solution for 'slugifying' the label.

{
  // ...
  onCreate: (value) => ({
    label: value,
    value: value.toLowerCase().replace(/\W/g, '-'),
  })
  // ...
}

reactSelectOptions

default: {}

The input component uses React Select under the hood. If you want to change and override any of the options passed to the select component, specify this option. Specify this option at your own risk!

If you want to override React Select's components see Parts for more information.

{
  // ...
  reactSelectOptions: {
    closeMenuOnSelect: false
  }
  // ...
}

Parts

part:tags/components/input

The main benefit of this plugin is its input component. To maximize its usefulness, you can import the Input component and use it elsewhere. If you already have a Tag document and want to create tags by reference rather than by object, you could add a field that uses this as an input component.

import Input from 'part:tags/components/input'

export default {
  name: 'my-tag-select',
  title: 'Select your Tags',
  type: 'array',
  inputComponent: Input,
  of: [{type: 'reference', to: [{type: 'myTagDocument'}]}],
  options: {
    ...
  }
}

Note that if you do choose to do this, tags by reference are limited by the input component in that they do not support predefinedTags or allowCreate.

You can also implement this input component yourself if you would like.

part:tags/components/select

If you would like to specify custom components for React Select without having to rewrite the entire Input component specified by this plugin, implement this part and export an object containing each component. See React Select's documentation for more information.

The current implementation defines no custom components, so you lose no functionality by implementing this part yourself.

part:tags/schemas/tags-default

If you need to import the current tags schema, use this part.

part:tags/schemas/tag-default

If you need to import the current tag schema, use this part.

Contribute

I love feedback, and any help is appreciated! Feel free to install the plugin, submit an issue, or open a PR.

To install the plugin for development follow these steps:

  1. Clone the repository
  2. cd sanity-plugin-tags
  3. npm install
  4. npm link
  5. npm run watch (this will begin building the plugin for development)
  6. cd into the sanity studio that you will be using for testing
  7. npm link sanity-plugin-tags
  8. Add 'tags' to your sanity plugins inside your sanity.json file
  9. sanity start

That's it! Now any changes you make to the plugin will appear automatically in your sanity studio development environment as you work in it!

Acknowledgements

This plugin is based off of sanity-plugin-autocomplete-tags, though it enhances it by adding a couple additional options while improving support for default sanity values like initialValues and readOnly.

Comments
  • Plugin causes error when deploying graphql playground

    Plugin causes error when deploying graphql playground

    Having this plugin installed will cause the following error when running the CLI command sanity graphql deploy:

    /path to project/node_modules/sanity-plugin-tags/lib/schemas/tags.js:1
    import t from"part:tags/components/input";const e={name:"tags",title:"Tags",type:"array",inputComponent:t,of:[{type:"tag"}]};export{e as default};
    ^^^^^^
    SyntaxError: Cannot use import statement outside a module
        at Object.compileFunction (node:vm:352:18)
        at wrapSafe (node:internal/modules/cjs/loader:1031:15)
        at Module._compile (node:internal/modules/cjs/loader:1065:27)
        at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
        at Module.require (node:internal/modules/cjs/loader:1005:19)
        at require (node:internal/modules/cjs/helpers:102:18)
    

    This seems to be the same issue mentioned in this thread on the Sanity Exchange, which ultimately appears to have been solved in this pull request to the original plugin repo.

    enhancement 
    opened by mmgj 3
  • Is there support?

    Is there support?

    Hi there,

    I am trying to add this to a project of mine and pull in tags from a document on my blog page.

    I have followed the instructions and referenced my tags document but no tags are pulling through.

    Is there any default tag document example you can provide?

    Thanks, Jack

    opened by jackkemmish 2
  • Issue when clearing selected option with type 'tag'

    Issue when clearing selected option with type 'tag'

    const tempTag: GeneralTag = { ...tag, label: tag._label_temp, value: tag._value_temp, }

    I am using sanity-plugin-tags and implement a dropdown withisClearable : true in my schema , problem is that when I am clearing the dropdown sanity studio notify some error

    image

    opened by parthpatel108 2
  • Background clipping through dropdown list

    Background clipping through dropdown list

    Hi! It seems like when you move the mouse away from the input field in Sanity studio the background turns transparent, or the underlying text clips through.

    https://user-images.githubusercontent.com/22595827/176862315-287a7a8c-2d44-42ea-9eaa-86b2bd466fec.mov

    wontfix 
    opened by wfeiring 1
  • feat: :sparkles: Added a new 'check valid' feature

    feat: :sparkles: Added a new 'check valid' feature

    This option allows one to validate tag creation prior to creating the tag. The 'Create Tag' dialogue will not appear unless this function returns true. Fixes #8.

    opened by pcbowers 0
  • On Create Bug

    On Create Bug

    Currently, there is no check to make sure whether or not the create option is valid. Every once in a while, this causes a bug where the value is not distinct but the label is. Ideally, isValidNewOption from react select would be used to support this by checking it against current options.

    opened by pcbowers 0
  • perf: :zap: Improved Bundle Size and Performance

    perf: :zap: Improved Bundle Size and Performance

    Added Vite as a bundler for the plugin. This dramatically reduces the total bundle size while also improving speed of development. This also provides a better eslint experience across the repository during development.

    (Fixes #2)

    opened by pcbowers 0
  • Improve Bundle Size

    Improve Bundle Size

    While many of the packages used in this plugin are already installed by Sanity Studio, the standalone bundle size is hovering at around 700kb (including all dependencies). This could be improved with a package bundler like Parcel or Vite.

    enhancement 
    opened by pcbowers 0
  • Compiling Failed

    Compiling Failed

    I installed the plugin using the command sanity install tags and when I try to start the project, I received this problem.

    $ sanity start
    ✔ Checking configuration files...
    ⠸ Compiling...webpack built 380c2db10379be37151f in 12011ms
    ✔ Compiling...
    Failed to compile.
    
    Error in ./node_modules/@floating-ui/dom/dist/floating-ui.dom.esm.js
    Module parse failed: Unexpected token (306:11)
    You may need an appropriate loader to handle this file type.
    |   }
    | 
    |   return { ...rect,
    |     x: rect.x - scroll.scrollLeft + offsets.x,
    |     y: rect.y - scroll.scrollTop + offsets.y
     @ ./node_modules/react-select/dist/react-select.esm.js 23:0-26
     @ ./node_modules/sanity-plugin-tags/lib/components/Input.js (part:tags/components/input)
     @ ./node_modules/sanity-plugin-tags/lib/schemas/tags.js
     @ ./node_modules/sanity-plugin-tags/lib/schemas/tags.js (all:part:@sanity/base/schema-type)
     @ ./schemas/schema.js (part:@sanity/base/schema)
     @ ./node_modules/@sanity/default-layout/lib/schemaErrors/SchemaErrorReporter.js
     @ ./node_modules/@sanity/default-layout/lib/defaultLayout/DefaultLayout.js
     @ ./node_modules/@sanity/default-layout/lib/defaultLayout/index.js
     @ ./node_modules/@sanity/default-layout/lib/Root.js (part:@sanity/base/root)
     @ ./node_modules/@sanity/base/lib/components/SanityRoot.js (part:@sanity/base/sanity-root)
     @ ./node_modules/@sanity/server/lib/browser/entry-dev.js
     @ multi ./node_modules/@sanity/server/lib/browser/entry-dev.js
    
    opened by emersonlaurentino 5
  • Keep the Tag input active after creating new tag

    Keep the Tag input active after creating new tag

    Its obvious that we use tags to add multiple items, but this plugins removes focus after creating new tag. I guess the plugin should keep the focus in the input so we can add multiple items without using cursor for every tag.

    Here's a gif showing the issue:

    CleanShot 2022-10-05 at 19 55 35

    opened by surjithctly 0
  • includeFromRelated / getTagsFromRelated within Object

    includeFromRelated / getTagsFromRelated within Object

    Hi, I'm using a localization approach very similar to this: https://www.sanity.io/docs/localization

    As a result my tags are part of an object, that will output like this:

        "de": [
          {
            "label": "Test DE",
            "value": "Test DE"
          }
        ],
        "en": [
          {
            "label": "Test EN",
            "value": "Test EN"
          }
        ]
      }
    

    Is there any way to make "includeFromRelated" work in this case?

    Possibly by allowing for a custom query instead of a string? (Bypassing or extending getTagsFromRelated() maybe?)

    Thanks!

    opened by philipppolder 1
  • Dropdown can be displayed behind of another filed in the row

    Dropdown can be displayed behind of another filed in the row

    I'm playing with this plugin. And I found 2 problems:

    1. When having tag field and field with the rich text editor I'm getting to the problem that tag dropdown can be displayed behind of rich text editor. The user experience when interacting with tag field isn't great in this situation. Is it possible to fix styling of dropdown? Snímek obrazovky 2022-08-10 v 11 14 02

    2. When having tag field in my schema of some document, it seems that changes aren't highlighted. Changes are highlighted for all other fields in my document excepts fields with type="tags". Snímek obrazovky 2022-08-10 v 11 19 23

    Is it possible to fix these problems? Thank you.

    opened by martinpesout 0
  • Improve Documentation

    Improve Documentation

    Currently, all documentation is found within the README. Ideally, a GitHub pages website or something similar would be created to provide better docs for this plugin.

    documentation 
    opened by pcbowers 1
Releases(1.5.0)
  • 1.5.0(Apr 26, 2022)

    v1.5.0 | Added checkValid Option

    This option allows one to validate the created tags before one tries to create them.

    What's Changed

    • feat: :sparkles: Added a new 'check valid' feature by @pcbowers in https://github.com/pcbowers/sanity-plugin-tags/pull/9

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.4.1...1.5.0

    Source code(tar.gz)
    Source code(zip)
  • 1.4.1(Apr 26, 2022)

    v1.4.1 Update Dependencies

    Added sanity v3 as a peer dependency on top of v2. Hopefully resolves any peer dependency conflicts.

    What's Changed

    • build: :arrow_up: Upgrade sanity to version 3 by @pcbowers in https://github.com/pcbowers/sanity-plugin-tags/pull/7

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.4.0...1.4.1

    Source code(tar.gz)
    Source code(zip)
  • 1.4.0(Apr 15, 2022)

    v1.4.0 | ES5 Transpilation

    What's Changed

    • build: :rotating_light: Added Transpilation to ES5 by @pcbowers in https://github.com/pcbowers/sanity-plugin-tags/pull/5

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.3.0...1.4.0

    Source code(tar.gz)
    Source code(zip)
  • 1.3.0(Apr 5, 2022)

    v1.3.0 | Improved Bundle Size, Linting, & Performance

    This release dramatically decreased the overall bundle size along with several other changes to improve overall development.

    • Added eslint
    • Added Vite as the package bundler
    • Corrected dependencies, devDependencies, and peerDependencies
    • Ensured all files properly matched the new eslint rules

    What's Changed

    • perf: :zap: Improved Bundle Size and Performance by @pcbowers in https://github.com/pcbowers/sanity-plugin-tags/pull/3

    New Contributors

    • @pcbowers made their first contribution in https://github.com/pcbowers/sanity-plugin-tags/pull/3

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.2.0...1.3.0

    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Apr 2, 2022)

    v1.2.0 | Code Splitting, Typescript, and Other Refactors ♻️

    This release comes with a full refactor of the plugin. The following items were changed:

    • A utils folder was added with several hooks, mutators, observables, and helper functions to decrease the size of the Input Component.
    • Typescript types were full implemented across every variable, including parts modules to improve IntelliSense.
    • Typescript overloads were also added to several functions for similar reasons.
    • Any previous observable implementations were improved with better typing and custom pipes.
    • Overall redundancy was improved across all functions, and several implementations were abstracted into their own functions.
    • Types were added globally to reduce import and export statements.

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.1.7...1.2.0

    Source code(tar.gz)
    Source code(zip)
  • 1.1.7(Mar 30, 2022)

    v1.1.7

    Shrunk the package size considerably by removing the docs.

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.1.6...1.1.7

    Source code(tar.gz)
    Source code(zip)
  • 1.1.6(Mar 29, 2022)

    v.1.1.6

    Finally fixed CI build. Any new releases automatically trigger a publish to npmjs.

    • Fixed CI Build

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.1.5...1.1.6

    Source code(tar.gz)
    Source code(zip)
  • 1.1.5(Mar 29, 2022)

  • 1.1.4(Mar 29, 2022)

  • 1.1.3(Mar 29, 2022)

    v1.1.3

    • Fixed CI bug by including package-lock.json in repository.

    Full Changelog: https://github.com/pcbowers/sanity-plugin-tags/compare/1.1.2...1.1.3

    Source code(tar.gz)
    Source code(zip)
  • 1.1.2(Mar 29, 2022)

  • 1.1.1(Mar 29, 2022)

    Initial Release 🥳🎉🎈

    A multi-tag input for sanity studio. Fully featured with autocomplete capabilities, live updates, predefined tag options, style and component customizability, and much more.

    Source code(tar.gz)
    Source code(zip)
Owner
P Christopher Bowers
A systems admin by day and a web developer by any-free-time-I-get
P Christopher Bowers
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Simple tag input for Bootstrap

Tagin Simple tag input for Bootstrap. Supports bootstrap v4 and v5. Demo: https://tagin.netlify.app/ Features Custom separators Enable/disable duplica

Erwin Heldy G 37 Sep 28, 2022
VanillaSelectBox - A dropdown menu with lots of features that takes a select tag and transforms it into a single or multi-select menu with 1 or 2 levels

vanillaSelectBox v1.0.0 vanillaSelectBox : v1.00 : Adding a package.json file New : Possibility to change the dropdown tree and change the remote sear

philippe MEYER 103 Dec 16, 2022
A Bootstrap plugin to create input spinner elements for number input

bootstrap-input-spinner A Bootstrap / jQuery plugin to create input spinner elements for number input. Demo page with examples Examples with floating-

Stefan Haack 220 Nov 7, 2022
A phone input component that uses intl-tel-input for Laravel Filament

Filament Phone Input This package provides a phone input component for Laravel Filament. It uses International Telephone Input to provide a dropdown o

Yusuf Kaya 24 Nov 29, 2022
A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

Ink codebase browser, "Kin" ?? The Ink codebase browser is a tool to explore open-source code on GitHub, especially my side projects written in the In

Linus Lee 20 Oct 30, 2022
proxy 🦄 yxorp is your Web Proxy as a Service (SAAS) Multi-tenant, Multi-Threaded, with Cache & Article Spinner

proxy ?? yxorp is your Web Proxy as a Service (SAAS) Multi-tenant, Multi-Threaded, with Cache & Article Spinner. Batteries are included, Content Spinning and Caching Engine, all housed within a stunning web GUI. A unique high-performance, plug-and-play, multi-threaded website mirror and article spinner

4D/ҵ.com Dashboards 13 Dec 30, 2022
The backend for our Airbnb App, built using Sanity.io.

AirBnb Sanity.io Backend This repository is to support my tutorial on how to build an AirBnb Clone with strucutred content using Sanity.io and Next.js

Ania Kubow 73 Dec 28, 2022
Airbnb-sanity - 🧳 Chrome extension to hide Airbnb listings you don't like

Airbnb Sanity ?? Coming soon to the Chrome Webstore! Chrome extension to hide Airbnb listings you don't like. Tired of looking through the same Airbnb

Johannes Rieke 4 Apr 4, 2022
Rede social baseada no ShareMe para compartilhamento e download de fotos em React,Sanity.io e TailWindCSS

Photophan ?? Descrição phothopan é uma rede social para o compartilhamento e download de imagens,foi desenvolvida utilizando React(Frontend),Sanity(Ba

Bruno Ariel 2 Jan 8, 2022
This is the repo for the Medium2 project with Next.js, Sanity CMS, React and Tailwind CSS

Next.js + Tailwind CSS Example This example shows how to use Tailwind CSS (v3.0) with Next.js. It follows the steps outlined in the official Tailwind

null 1 Jan 22, 2022
Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill).

@sanity/client Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill). Requirements Sanity Clien

Sanity 23 Nov 29, 2022
Medium-Clone with Next.JS, Typescript, Tailwindcss, and Sanity!!

Medium Clone ?? Overview /pages ✔️ pages/index.tsx = Homepage and list all Blogs ✔️ pages/post/[slug].tsx = Details Blog /pages/api ✔️ pages/api/creat

argikurnia 23 Nov 16, 2022
Monorepo for open source libraries used by nrkno-sanity

NRK.no Sanity libraries NRK.no Sanity libraries contains an assortment of plugins and libraries used by NRK.no to extend Sanity Studio and apps using

Norsk rikskringkasting (NRK) 10 Nov 30, 2022
Stablo is a minimal blog website template built with Next.js, TailwindCSS & Sanity CMS

Stablo Blog Template - Next.js & Sanity CMS Stablo is a JAMStack Starter template built with Next.js, Tailwind CSS & Sanity CMS by Web3Templates. Clic

Web3Templates 159 Dec 30, 2022
Sanity plugin for viewing resources which reference a particular resource.

@indent-oss/sanityio-referenced-by Plugin to see which documents reference a particular document referenced-by-sanityio.mov Video Alt Text: Demonstrat

Indent 16 Nov 2, 2022
Enhanced Sanity.io plugin development experience.

@sanity/plugin-kit NOTE This is a developer preview package meant for Sanity Studio v3 plugin development. For a v2 alternative, consider using Sanipa

Sanity 40 Dec 27, 2022