You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think I have a great system setup for using expo icons with Dripsy. Figured I'd share it here.
I wanted type-safe props that used my theme values for colors.
Note, you should replace useTheme with your own custom useTheme function, as detailed at #72 (comment)
Click here to see code
importReactfrom'react'importIconfrom'@expo/vector-icons/build/Ionicons'import{styled,useDripsyThemeasuseTheme}from'dripsy'constStyledIcon=styled(Icon)({})typeColor=(string&{})|keyofReturnType<typeofuseTheme>['colors']typeName=React.ComponentProps<typeofStyledIcon>['name']typeIcon=|{name: Namesize?: numbercolor?: Color}|React.ReactElement|NametypeBaseProps={size?: numbercolor?: Colorname: Name}exporttypeIconProps={icon: Icon}exporttypeIconsBaseProps=BasePropsexporttypeAllIconProps=(IconProps|BaseProps)&{sx?: React.ComponentProps<typeofStyledIcon>['sx']selectable?: boolean}functionisIconProps(props: AllIconProps): props is IconProps{return!!(propsasIconProps).icon}exportdefaultfunctionIonicons(props: AllIconProps){const{ colors }=useTheme()leticon: Icon|undefinedletcolor: Color=colors.textif(isIconProps(props)){icon=props.icon}else{icon={name: props.name,color: props.color,size: props.size,}}if(React.isValidElement(icon)){returnicon}// this exists for conditional propsif(typeoficon==='boolean')returnnullletname: React.ComponentProps<typeofStyledIcon>['name']|null=nullletsize=22if(typeoficon==='string'){name=iconasReact.ComponentProps<typeofStyledIcon>['name']}elseif(icon?.name){name=icon.nameif(icon.size)size=icon.sizeif(icon.color){color=colors?.[icon.color]??icon.color}}if(!name)returnnullreturn(<StyledIcon{...props}color={color}size={size}name={name}sx={props.sx}/>)}
This allows for nice, strictly-typed colors for your icons, and has intellisense for your theme's colors. It also has multiple modes of composition:
Direct props
<Ioniconscolor="text"name="close"/>
Object props
<Ioniconsicon={{color: 'text',name: 'close'}}/>
While more obscure, the object prop patterns allows you to easily compose other components that wrap the icon. For example, a Button component might have a iconPrefix prop.
I think I have a great system setup for using expo icons with Dripsy. Figured I'd share it here.
I wanted type-safe props that used my theme values for colors.
Note, you should replace
useTheme
with your own customuseTheme
function, as detailed at #72 (comment)Click here to see code
This allows for nice, strictly-typed colors for your icons, and has intellisense for your theme's colors. It also has multiple modes of composition:
Direct props
Object props
While more obscure, the object prop patterns allows you to easily compose other components that wrap the icon. For example, a
Button
component might have aiconPrefix
prop.Now, you can compose the button with ease:
The text was updated successfully, but these errors were encountered: