Using @layer and defining a reset layer #1467
-
I am trying to sort out how to get VE working with the new SetupI'm using yarn workspaces to plug my component-library into my app. Both use VE, and my app uses Remix, like so:
Component-libraryNow, I've put all my component-library styling in it's own layer like so: import { layer } from '@vanilla-extract/css';
export const library = layer('component-library'); And used it like so: // button.css.ts
export const button = style({
'@layer': {
[library]: {
height: '1em',
transform: 'scale(1.125)',
transformOrigin: 'center',
},
},
}); AppIn my App, I've defined two layers and import the import { layer } from '@vanilla-extract/css';
import { layerLibrary } from '@component-library';
export const reset = layer('reset');
globalLayer(layerLibrary);
export const app = layer('app'); And I've used the
In my Remix App, I'm using these as follows: // in root.tsx
import "./Layers.css";
import "./Reset.css"; ResultsSo I expect this order order of layers:
Instead however, my Now I have also seen an example in which reset styles are added to each I'd love your opinion or pointers on how to make this work! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You're correct in that your component library's layer is ordered first because it's imported first. Layers are created as a side-effect of calling the To fix your problem, you'll need to ensure your layers are imported in the correct order. Generally, when dealing import-order-sensitive CSS, such as reset styles and/or layers, I tend to try and organize the imports in the app entrypoint, e.g. For this specific example, I also think it could be worth creating a separate entrypoint in your library that just exports the layer. For example: // component-library/layer entrypoint
import { layer } from '@vanilla-extract/css';
export const libraryLayer = layer("library"); That way you can do something like this: // App.tsx
// Creates reset layer and defines reset styles
import "./reset.css.ts";
// Creates a layer for your component library
import "component-library/layer";
// Creates a layer for your app
import "./appLayer.css.ts"; You could also just import |
Beta Was this translation helpful? Give feedback.
-
Just as an alternative- imo layer importing or rather declaring should not be a side effect and should be done manually and deliberately. Vanilla extract already supports this but the docs should be amended to make this more clear - at least for us folks who’re using it to create component libraries. in our component library we bypassed the import { generateIdentifier, globalLayer } from "@vanilla-extract/css";
export const ui = "example";
// these 2 layers are public and should be exported inside `layerLibrary` object
export const theme = `${ui}.theme`;
export const base = `${ui}.base`;
// reset and components layers are internal/private and hence use hash ids
export const reset = `${ui}.${generateIdentifier()}`;
export const components = `${ui}.${generateIdentifier()}`;
globalLayer([ui, theme, base, reset, components].join(", ")); and for your use case one recommendation I’d make is to provide an already built global or base layer that consumers can use. so in our case we have a top layer for our component library and then nested inside it we have our theme/reset/etc layers: @layer example.theme, example.base, example.xyzabc; so consumers can use the import { layerLibrary } from '@component-library';
globalStyle('html, *, *::before, *::after', {
'@layer': {
// layerLibrary.base = 'example.base'
[layerLibrary.base]: {
// …
},
},
}); |
Beta Was this translation helpful? Give feedback.
You're correct in that your component library's layer is ordered first because it's imported first. Layers are created as a side-effect of calling the
layer
function, which also means you don't need to defineglobalLayer(layerLibrary)
; it's redundant.To fix your problem, you'll need to ensure your layers are imported in the correct order. Generally, when dealing import-order-sensitive CSS, such as reset styles and/or layers, I tend to try and organize the imports in the app entrypoint, e.g.
App.tsx
.For this specific example, I also think it could be worth creating a separate entrypoint in your library that just exports the layer. For example: