Custom slug input component for dynamic URL description without schema changes

4 replies
Last updated: May 25, 2022
If I wanted to have the "slug" input show a description underneath that revealed the full (intended) URL to my site, am I right in thinking that would require a custom input component, since "description" appears to be a normal property?
Also, if I am "just" changing the component itself, I don't have to have a new/different schema/field and experience data loss if the slug fields already exist, correct? It's just a different way to "dress up" the field produced ( a misnomer, I know, since it's functional as well as aesthetic -- I am just thinking in terms of, it's all just a different way to get data stored into the system, still )
AI Update

You're absolutely correct on both counts!

Yes, you need a custom input component to show a dynamic description under the slug field. The description property in Sanity schemas is indeed a static string, so if you want to display something dynamic (like showing the full intended URL based on the current slug value), you'll need to create a custom input component.

And yes, you won't experience data loss when switching to a custom input component. You're spot on with your thinking - the custom component is just a different way to "dress up" the field. The underlying data structure remains the same. As long as your custom component:

  1. Uses the same field name (slug)
  2. Uses the same field type (slug)
  3. Properly implements the input component interface (receiving and handling the value, onChange, etc.)

...your existing slug data will work seamlessly with the new component. You're essentially just changing the UI layer while the data layer stays intact.

Here's a basic example of what a custom slug input component might look like:

import { Stack, Text } from '@sanity/ui'
import { StringInputProps, set, unset } from 'sanity'

export function CustomSlugInput(props: StringInputProps) {
  const { value, onChange, renderDefault } = props
  const slug = value?.current || ''
  
  // Your site's base URL
  const fullUrl = `https://yoursite.com/${slug}`
  
  return (
    <Stack space={2}>
      {/* Render the default slug input behavior */}
      {renderDefault(props)}
      
      {/* Dynamic description showing full URL */}
      {slug && (
        <Text size={1} muted>
          Full URL: <strong>{fullUrl}</strong>
        </Text>
      )}
    </Stack>
  )
}

Then in your schema, you'd add it like this:

{
  name: 'slug',
  type: 'slug',
  options: {
    source: 'title'
  },
  components: {
    input: CustomSlugInput
  }
}

The key here is using renderDefault(props) which renders the standard slug input (including the "Generate" button), and then you're just adding your dynamic description below it. This approach preserves all the built-in slug functionality while adding your custom UI enhancement.

Show original thread
4 replies
Does displaying the full url require you to perform a query?
Thanks for responding. Not necessarily -- all my slug fields are on their own separate schema, of course, and I can of course hardcode a contextual prefix leading up to the spot where the slug would go, but I'd like it to update as they write or upon "committing" the change to the slug with the handy Generate button.
As an example, if the slug field is on the document type "listing", there's no query necessary for me to hardcode "
domainname.com/listing/ " some place like the description, but as they type 123-main-st for their slug, it'd be great if the URL display string would follow in kind and ultimately join the hardcoded bit to form "domainname.com/listing/123-main-st ".
Bonus points if I could make it a true external link via React / JSX like some of the fun label experiments we've seen lately.

As far as I am currently aware we can do
amazing transformations on the actual slug itself interactively, but not the label-y kind of things like, er, the label, and the description.
Am I overthinking things again and there's a simpler, smarter approach to the use case? =P I think even more than I type, so just imagine the signal/noise ratio inside my head haha =)
Ah, I see! You can use JSX in a description, like so:
      description: (
        <p>
          This is a description with a <a href='<https://www.googlecom>'>link</a>
        </p>
      ),
But since you need it to be aware of the value of the
slug
field, you will need to do a completely custom component. You can build it yourself or implement this plugin πŸ˜‰
Thanks for the assist!
user B
brilliance strikes again!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?