Learn how to define styles for your storefront components, including how to export CSS handles for further customization.
Once you have defined the blocks for your theme, the next step is to consider the style you want to apply to them. This includes defining the color scheme, typography, and other design elements that will establish the visual identity of your app when it is rendered.
Before you begin
Customizing the style in a component requires basic CSS knowledge. If you're new to CSS, we recommended checking the documentation for this language before following the instructions detailed in this article.
Instructions
Store Framework provides three different tools for applying CSS to a storefront:
- Tachyons - Main option for styling components from CSS classes. It offers a wide range of pre-defined styles and enables you to customize them as needed.
- CSS Modules - Secondary option for styling components from CSS classes. It should only be used when the desired style cannot be achieved with Tachyons.
- CSS handles - This option exports generic CSS classes that allow users that work with your component to customize it according to their individual needs.
Now that you know the various styling option available on the platform, it is time to delve deeper into each of them and explore their features and functionalities. Below you will find detailed instructions on how to work with each of them.
Using Tachyons
VTEX Tachyons is a functional CSS structure built on Tachyons. VTEX Tachyons offers single-purpose CSS classes that can be combined to create complex layouts and responsive components for your storefront app.
To better understand VTEX Tachyons tokens for CSS classes, it is important to familiarize yourself with the underlying Tachyons framework. We highly recommend carefully reading through Tachyons documentation to learn more about this solution.
- Open your app code in your code editor.
- In the
react
folder, access the file created for the component you want to style. - Check the VTEX Tachyons documentation and look for the most appropriate CSS classes for the customization you want to apply.
- In the React component code, declare the desired CSS classes. For example:
_10const Example = () => (_10 <div className="flex justify-center pv4 ph3 bg-base--inverted">_10 <p>Hello, World!</p>_10 </div>_10)
- Save your changes.
Using CSS Modules
CSS Modules is a tool for defining CSS classes within a separate CSS file that is scoped to the storefront app exporting the component. In this regard, the use of CSS Modules may be limiting when it comes to importing and exporting components between different apps. Thus, it is recommended to use VTEX Tachyons as the primary CSS styling tool in your project. CSS Modules should only be used when the desired style cannot be achieved using Tachyons.
Keep in mind that using CSS Modules should be avoided whenever possible in favor of VTEX Tachyons. This will ensure greater compatibility and flexibility when importing and exporting components between different storefront apps.
- In your app's
react
folder, create thestyles.css
file. - In this file, create the desired CSS classes (the ones not understood by Tachyons tokens). For example:
_10.myButton {_10 background-color: red;_10}
- In the
react
folder, access the component file being customized and import thestyles
file created in step 1. For example:
_10import styles from './styles.css'
- You can also import the
styles
file so that the generated CSS classes are private; that is, they are generated with a unique identifier (hash) instead of the traditionalvendor-app-major-x-format-classname
. For this, you need to do an import following the model below:
_10import styleModule from './style.module.css'
- After this, the
styles
imported for your component will be an object whose keys will be the names of the classes you created (className
). For example:
_10/* /react/MyButton.tsx */_10import styles from './styles.css'_10_10export default function MyButton() {_10 return (_10 <button className={styles.myButton}>_10 My button_10 </button>_10 )_10}
Remember that the name of the created classes will depend on the import model you used (step 3 or step 4 in this section).
- Save your changes and link the app.
When rendered and inspected, the component will now have the following HTML structure if you chose to follow the import model shown in step 3:
_10<button class="vtex-my-app-name-0-x-myButton">My button</button>
Using CSS handles
A CSS handle is an HTML element identifier. Handles are useful when creating store themes, allowing your app users to apply CSS classes based on the visual identity they want.
To make handles available for future app users, add vtex.css-handles
to the dependencies
list in the manifest.json
file:
_10 "dependencies": {_10+ "vtex.css-handles": "0.x" _10 }
Once vtex.css-handles
is added as a dependency, your app is ready to generate CSS handles.
However, when generating them, you will need to pay attention to how your app's React component(s) are imported and defined.
There are two possible scenarios:
- Function components - React components are defined in your app using a function.
- Class components - React components are defined in your app using a class.
Creating CSS handles for function components
- In your app's
react
folder, go to the file of the React component you want. - Copy and paste the following code example in the file:
_10import { useCssHandles } from 'vtex.css-handles'_10_10const CSS_HANDLES = ['container', 'background', 'text', 'item'] as const_10/* Using `as const` at the end hints to Typescript that this array_10* won't change, thus allowing autocomplete and other goodies. */_10_10const Component: FC = () => {_10 const handles = useCssHandles(CSS_HANDLES)
- In the
CSS_HANDLES
array, replace the exemple strings with the names of the CSS handles you want. You can choose any name for the CSS handle; but, remember, your app users will use it with CSS classes to personalize HTML elements in stores. We recommend choosing intuitive names. Following the above example, the variable(CSS_HANDLES)
will be an object using the following format:
_10{_10 container: 'vtex-foobar-1-x-container',_10 background: 'vtex-foobar-1-x-background',_10 text: 'vtex-foobar-1-x-text',_10 item: 'vtex-foobar-1-x-item',_10}
Note that the CSS handles generated and stored in the object follow this pattern:
vtex-{appName}-{majorVersion}-x-{handleName}
.
- Add the new CSS handle to the desired CSS class–according to the HTML element to be customized by app users. Remember to use the
handle
variable when adding it and also the CSS handle name defined in theCSS_HANDLES
array (likecontainer
). For example:
_13 <div className={handles.container}>_13 <div className={`$\{handles.background\} bg-red`}>_13 <h1 className={`$\{handles.text\} f1 c-white`}>Hello world</h1>_13 <ul>_13 {['blue', 'yellow', 'green'].map(color => (_13 <li_13 className={`$\{handles.item\} bg-$\{color\}`}>_13 Color {color}_13 </li>_13 ))}_13 </ul>_13 </div>_13 </div>
Creating CSS handles for class components
- In your app's
react
folder, go to the file of the React component you want. - Copy and paste the following code example in the file:
_10import { withCssHandles } from 'vtex.css-handles'_10_10const CSS_HANDLES = ['text'] as const_10_10class ExampleComponent extends Component {_10 render() {_10 const { cssHandles } = this.props
Since components defined as classes cannot use hooks, we will use an HOC called
withCssHandles
.
- In the
CSS_HANDLES
array, replace the example strings with the names of the CSS handles you want. You can choose any name for the CSS handle; but, remember, your app users will use it with CSS classes to personalize HTML elements. We recommend using intuitive names.
Following the above example, the variable { cssHandles }
will be an object using the following format:
_10{_10 text: 'vtex-foobar-1-x-text',_10}
Note that the CSS handles generated and stored in the object follow this pattern:
vtex-{appName}-{majorVersion}-x-{handleName}
.
- Add the new CSS handle to the desired CSS class–according to the HTML element to be customized by app users. Remember to use the
handle
variable when adding it, as well as the CSS handle name defined in theCSS_HANDLES
array (liketext
). For example:
_10<div className={`$\{cssHandles.text\} f1 c-white`}>Hello world</div>
After saving your changes, your app will be able to not only export a predefined style based on the CSS classes you set up but also export CSS handles, giving your users more customization flexibility.
Learn more about how CSS handles can be used to define styles in Using CSS handles for store customization.