How to create a checkbox in Sanity Studio and conditionally add a CSS class

6 replies
Last updated: Jan 19, 2024
I am new to Sanity.How do you create a checkbox in the studio and use either a value of True or False to conditionally add a CSS class?
Jan 19, 2024, 12:08 PM
Checkboxes are not one of Sanity's schemas types but can be created through custom components.
As for conditionally adding a CSS class you can a field like the one below to your document:

  title: 'Add CSS class?',
  name: 'isCSSClass',
  type: 'boolean',
And then in your frontend for example if you are using React you could:

<div class={isCSSClass ? 'text-large purple-bg' : ''} />
Or use a tool like clsx
Jan 19, 2024, 12:39 PM
What is this custom component look like in the CMS UI?
Jan 19, 2024, 2:51 PM
It looks like whatever you want it to be.
In your case it would to render out a checkbox.

But since you only need to a field which needs a true or false value, a field with type Boolean is more than enough.

Here's an example guide of a custom component -
Jan 19, 2024, 3:00 PM
Thank you so much!
Jan 19, 2024, 3:10 PM
Please don’t post your question to multiple channels.
Jan 19, 2024, 4:53 PM
I wasn't sure which channel I should post. Won't do it again.
Jan 19, 2024, 6:41 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?