Skip to main content

Variants

Fast Styles supports variants, which allows you to define different styles for a component based on specific variations or conditions.

Variants are a fundamental part of this library, and they are designed to offer maximum performance, surpassing other libraries or even react-native vanilla.

Adding Variants to your component​

Let's assume that we want to enhance the button we created earlier and add the colorScheme variant. This variant allows the user to choose a predefined color scheme for the button. In this case, we will add the options primary, positive, and negative.

Loading...
tip

All the examples in this guide use a live editor. You can try the examples in real-time and see how the code behaves. Feel free to make changes and experiment with different styles.

Default variants​

If you don't define any default variants, Fast Styles will take the first option you have defined for each variant as the default. However, if you wish, you can set default variants using the defaultVariants attribute:

const ButtonRoot = styled(TouchableOpacity, {
//...
defaultVariants: {
colorScheme: "primary",
},
});

Compound Variants​

Fast Styles goes beyond supporting basic variants and also includes compound variants. Compound variants are a powerful feature that allows you to define styles based on combinations of multiple variations.

For example, you can create styles for a button that is both outlined and primary, or solid and primary. This provides you with finer control over the styles applied to your components, making them highly versatile and customizable.

Loading...

Variants and Nested Components​

In the previous section, we mentioned that Fast Styles avoids using underscore props to pass props to child components. This is because it's easy to ensure that child components respond to the same variant.

This approach makes each component responsible for its own changes, simplifying component creation and keeping the code clean.

Loading...

To Conclude​

At this point, we have already seen how to create and use basic variants and compound variants.

In the examples, we created a button and demonstrated the functionality by changing colors. However, you might wonder if all this is necessary just to define a color. The answer is no. If your component has a few properties that will change with each use, fast styles provides a way to map these properties to styles called styleProps, and we will explore that in the next section.