createTheme
Creates a locally scoped theme class and a theme contract which can be consumed within your styles.
🎨 New to theming in vanilla-extract? Make sure you’ve read the theming overview first.
theme.css.ts
import { createTheme, style } from '@vanilla-extract/css';
export const [themeClass, vars] = createTheme({
color: {
brand: 'blue'
},
font: {
body: 'arial'
}
});
export const brandText = style({
color: vars.color.brand,
fontFamily: vars.font.body
});CSS
.theme_themeClass__p73exr0 {
--color-brand__p73exr1: blue;
--font-body__p73exr2: arial;
}
.theme_brandText__p73exr3 {
color: var(--color-brand__p73exr1);
font-family: var(--font-body__p73exr2);
}Creating theme variants
Theme variants can be created by passing a theme contract as the first argument to createTheme.
All theme values must be provided or it’s a type error.
themes.css.ts
import { createTheme, style } from '@vanilla-extract/css';
export const [themeA, vars] = createTheme({
color: {
brand: 'blue'
},
font: {
body: 'arial'
}
});
export const themeB = createTheme(vars, {
color: {
brand: 'pink'
},
font: {
body: 'comic sans ms'
}
});
export const brandText = style({
color: vars.color.brand,
fontFamily: vars.font.body
});CSS
.themes_themeA__1tovyy70 {
--color-brand__1tovyy71: blue;
--font-body__1tovyy72: arial;
}
.themes_themeB__1tovyy73 {
--color-brand__1tovyy71: pink;
--font-body__1tovyy72: comic sans ms;
}
.themes_brandText__1tovyy74 {
color: var(--color-brand__1tovyy71);
font-family: var(--font-body__1tovyy72);
}@layer
Themes can be assigned to a layer by name using the @layer key at the top-level of the theme definition.
🚧 Ensure your target browsers support layers. Vanilla Extract supports the layers syntax but does not polyfill the feature in unsupported browsers.
themes.css.ts
import { createTheme, layer } from '@vanilla-extract/css';
export const themeLayer = layer();
export const [themeA, vars] = createTheme({
'@layer': themeLayer,
color: {
brand: 'blue'
},
font: {
body: 'arial'
}
});CSS
@layer themes_themeLayer__1oyu9720;
@layer themes_themeLayer__1oyu9720 {
.themes_themeA__1oyu9721 {
--color-brand__1oyu9722: blue;
--font-body__1oyu9723: arial;
}
}