Vanilla extract integration
Vanilla extract is a TypeScript CSS preprocessor that generates static CSS files at build time. It is a great alternative to CSS Modules if you prefer to write your styles in TypeScript.
Vanilla extract vs CSS Modules
Vanilla extract and CSS Modules do the same thing, but with different syntax. Common features of Vanilla extract and CSS Modules:
- Styles are generated at build time – no runtime and performance overhead
- Classes names are scoped to the styles file
Differences between Vanilla extract and CSS Modules:
- Vanilla extract styles are type safe
- You can use any JavaScript/TypeScript code in Vanilla extract styles, including color functions
- With Vanilla extract you do not have access to postcss-preset-mantine features like
light-dark
function andhover
mixin. Because of this, you will not be able to copy-paste all demos from Mantine documentation and use them with Vanilla extract. - Vanilla extract requires additional configuration and setup that may not be available for your build tool/framework. Most popular tools like Next.js and Vite have plugins for Vanilla extract, but if you are using something more niche, you might need to configure it yourself.
Note that you can use both Vanilla extract and CSS Modules in the same project, it will not cause any issues: performance will be the same and the bundle size will not be impacted.
Installation
Follow the installation instructions to install vanilla extract.
Then install @mantine/vanilla-extract
package, it exports themeToVars
function to convert Mantine theme to CSS variables:
Templates
You can use one of the following templates to get started or a reference for your own setup. Note that all templates include only minimal setup.
next-vanilla-extract-template Next.js template with Vanilla extract example | Use template | |
vite-vanilla-extract-template Vite template with Vanilla extract example | Use template |
Theming
Vanilla extract provides createTheme
function which converts given theme object into CSS variables and assigns them to :root
or other selector.
You should not use Vanilla extract createTheme
to generate Mantine theme tokens – all Mantine theme
properties are already exposed as CSS variables. Instead, use themeToVars
function from @mantine/vanilla-extract
package
to create an object with CSS variables from Mantine theme:
Styling
Import vars
object in *.css.ts
files to access Mantine CSS variables:
rem and em
To convert px to rem or em use rem
and em
functions from @mantine/core
package:
light and dark selectors
vars
object contains lightSelector
and darkSelector
properties which can be used to
apply styles only in light or dark color scheme:
Note that usually it is more convenient to use only one of them:
apply styles for light color scheme and then override them for dark color scheme
with vars.darkSelector
(or vice versa):
largerThan and smallerThan
vars
object contains largerThan
and smallerThan
properties which can be used in
@media
as a shorthand for min-width
and max-width
:
rtl selector
Use vars.rtlSelector
to apply styles only in rtl direction: