function, you can easily extend existing components and make them customizable and themeable.
constStyledButton=styled(
Pressable,
{
bg:"$primary600",
px:"$6",
py:"$4",
_dark:{
bg:"$gray800",
},
},
{},// componentConfig,
{}// extendedBaseConfig,
)
componentConfig
Step
1
:
descendantStyle is an array of string that denotes the descendants of that component that should respond to parents state. This is useful for creating complex styles that is dependent on the component that this prop is passed in.
{
descendantStyle:["_text","_spinner"]
// ancestorStyle: ['_text'];
// resolveProps: ['placeholderTextColor'];
}
Step
2
:
ancestorStyle is an array of string that denotes the ancestors of that component. This is useful for creating complex styles that are dependent on the ancestor component.
{
// descendantStyle: ['_text', '_spinner'];
ancestorStyle:["_text"]
// resolveProps: ['placeholderTextColor'];
}
So if this component finds a parent component with
_text
prop, it will apply the styles to the component.
Step
3
:
resolveProps is an array of string that denotes the props that should be resolved. This is useful for props that are not passed into the style property rather goes in as a prop. For example,
placeholderTextColor
in TextInput.
{
// descendantStyle: ['_text', '_spinner'];
// ancestorStyle: ['_text'];
resolveProps:["placeholderTextColor"]
}
Note: For this to work you also need to add the prop in
propertyTokenMap
, that can be defined in the next parameter of styled function i.e. extendedBaseConfig.
extendedBaseConfig
extendedBaseConfig is an object that let’s you extend the base config for the component. This is useful when you want to define some aliases, tokens, propertyTokenMap,
and propertyResolver that is specific to the component.
this will merge the following to the base config (gluestack-style.config.js) for the scope of that component.
StyledProvider
gluestack-style Style exports a StyledProvider component that can be used to configure your components. This is useful when you want to define some aliases, tokens, propertyTokenMap, propertyResolver and globalStyle at a global level throughout your app. You can define them in
gluestack-style.config.js
file and pass it to the StyledProvider.
Apart from config StyledProvider also accepts colorMode, which denotes the current active colorMode of the application and globalStyleInjector, which is a function that can be used to inject global styles.
If your component has different colorModes than you can write colorMode specific styles like:
constStyledInput=styled(
TextInput,
{
py:"$2",
bg:"$gray50",
borderRadius:"$2",
_dark:{
bg:"$gray800",
},
_light:{
bg:"$gray50",
},
},
componentConfig,
extendedBaseConfig
)
These
colorMode
styles are applied when your style colorMode matches with the active colorMode, the colorMode is passed in
colorMode
prop of the StyledProvider.
<StyledProvidercolorMode="dark">
<App/>
</StyledProvider>
sx
sx
prop is a prop that lets you write inline styles for the component.
constStyledInput=styled(
TextInput,
{
py:"$2",
bg:"$gray50",
borderRadius:"$2",
},
componentConfig,
extendedBaseConfig
)
;<StyledInputsx={{ py:"$4"}}/>
Note:
sx
prop will override the styles defined in the component config. We highly recommend you to use this as little as possible. Mostly when you want to write some layout styles like margin or padding for the component.
Utility Props
gluestack-style Style provides utility props that can be used to style the component. These props get converted to sx prop internally. There is a simple rule, all the utility props are the final property you want to get attached on the style of your component, It will override all other stylings except style prop.