Pass props to Sanity desk structure icon component
Looking at the Structure Builder API, you can pass props to icons by wrapping them in a function component! The .icon() method accepts a React component, so instead of passing the component directly, you can pass a function that returns the component with your desired props.
Here's how to do it:
S.listItem()
.title("Pages")
.id("pages")
.child(S.documentTypeList("page").title("Pages overview"))
.schemaType("page")
.icon(() => <File color="#FF0000" />)The key is wrapping your icon component in an arrow function. This way, you're passing a component (function) to .icon() that returns your File icon with custom props.
You can also use a regular function if you prefer:
.icon(function() { return <File color="#FF0000" /> })This pattern works because the Structure Builder API documentation specifies that the .icon() method accepts a "React component" as its parameter. When you wrap your JSX in a function, you're creating a functional React component that renders your icon with the custom props.
This same approach works for other Structure Builder methods that accept icons, like documentListItem(), menuItem(), and view icons in document nodes. So you can customize colors, sizes, weights, or any other props your icon library supports! π
Show original thread8 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.