4

I have an object of type block to get a WYSIWYG editor. It looks like this:

{
  title: "Block",
  type: "block",

  styles: [
    { title: "Normal", value: "normal" },
    { title: "H1", value: "h1" },
    { title: "H2", value: "h2" },
    { title: "H3", value: "h3" },
    { title: "H4", value: "h4" },
    { title: "Quote", value: "blockquote" }
  ],
  lists: [{ title: "Bullet", value: "bullet" }],
  marks: {
    decorators: [
      { title: "Strong", value: "strong" },
      { title: "Emphasis", value: "em" }
    ],
    annotations: [
      {
        title: "URL",
        name: "link",
        type: "object",
        fields: [
          {
            title: "URL",
            name: "href",
            type: "url"
          }
        ]
      }
    ]
  }
}

But I see no option to be able to choose the color of the text. Is there a way to enable this? Maybe a plugin?

1
  • I don't know anything about your use-case, but keep in mind that the block type is only in the business of producing structured text and should probably not be seen as a WYSIWYG editor. "What You Get" is for the front end to decide. That's the underlying design philosophy, anyway. Use it however you like :)
    – thomax
    Nov 26, 2018 at 10:37

1 Answer 1

4

There is indeed a plugin for this. In your terminal, cd to you Sanity Content Studio folder, then run:

sanity install @sanity/color-input

This will append @sanity/color-input to the plugins array in your sanity.json file and locally install the @sanity/color-input npm package.

Then, go ahead and add the color type to the annotations array in the block content where you want to enable text color. E.g.:

export default {
  name: 'blockContent',
  type: 'array',
  title: 'Block Content with Color',
  of: [
    {
      type: 'block',
      marks: {
        annotations: [
          {name: 'color', title: 'Color', type: 'color'}
        ]
      }
    }
  ]
}

Also, keep in mind that you'll now get text annotated with color specifics. How (and if) your front-end(s) choose to render the structured text is up to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.