Custom slug input component for dynamic URL description without schema changes
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:
- Uses the same field name (
slug) - Uses the same field type (
slug) - 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 thread4 replies
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.