radix-ui / themes

Radix Themes is an open-source component library optimized for fast development, easy maintenance, and accessibility. Maintained by @workos.

Home Page:https://radix-ui.com/themes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Avatar] Conditionally set color

pnilssson opened this issue · comments

I would like to be able to conditionally set color of all components that has the color prop i.e Avatar, Badge and so on. Locally I manage to do it by just returning a string from a method but it complains about wanting the correct type which crashes the build. I tried to import the declared const of avatarPropDefs but it doesn't seem to work to have avatarPropDefs.color as a return type of a method and use it like color={methodThatReturns<avatarPropDefs.color>}.

Thanks for the lovelt library!

You'll need to tweak the method implementation and/or type signature so that it returns the allowed colors. It's hard to say more without seeing the code of your function.

Something like this should do the trick.

function getAvatarColor(): (typeof avatarPropDefs.color.values)[number] 

Or

function getAvatarColor(): (typeof avatarPropDefs.color.default)

Works like a charm! Many thanks :)