How to use in an external package consumed by other projects? #281
-
Reading the docs I see how to get set up and seems pretty straightforward for one project. How do you recommend setting this up where the theme and CSS would be in project-one which is published and then consumed by project-two/three/etc (think design system)? I could see either needing to compile before publishing or needing to have any consumer project adjust their build setup. Do you have any guides for this type of setup? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
I too would like to know more about this, maybe like a ContextProvider in vanilla-extract. |
Beta Was this translation helpful? Give feedback.
-
This is a serious issue as dynamic imports do not work in vanilla extract |
Beta Was this translation helpful? Give feedback.
-
I have been able to to get this to work using vite in lib mode. You essentially need to just Consuming the themes in a different package just involves importing the Here is a really simple example of what I have done: // contract.css.ts
const contract = createThemeContract({
brand: {
blue: {
primary: {
base: null,
low: null,
mid: null,
high: null,
},
secondary: {
base: null,
ultra: null,
low: null,
mid: null,
high: null,
},
}
}
}); // light.css.ts
const lightColors = createGlobalTheme("html[data-theme-mode='light']", contract, {
brand: {
blue: {
primary: {
base: toRGB("#23618E"),
low: toRGB("#A7C0D2"),
mid: toRGB("#1C4E72"),
high: toRGB("#153A55"),
},
secondary: {
base: toRGB("#3195D2"),
ultra: toRGB("#ECF7FF"),
low: toRGB("#9ED7FF"),
mid: toRGB("#2777A8"),
high: toRGB("#0A1E2A"),
},
},
}
}); // dark.css.ts
const darkColors = createGlobalTheme("html[data-theme-mode='dark']", contract, {
brand: {
blue: {
primary: {
base: toRGB("#9ED7FF"),
low: toRGB("#C5E7FF"),
mid: toRGB("#5AAADB"),
high: toRGB("#3195D2"),
},
secondary: {
base: toRGB("#3195D2"),
ultra: toRGB("#ECF7FF"),
low: toRGB("#9ED7FF"),
mid: toRGB("#2777A8"),
high: toRGB("#0A1E2A"),
},
},
}
}); // index.ts (entry point of the lib)
import colors from "./contract.css";
import light from "./light.css";
import dark from "./dark.css";
export { colors, light, dark }; Then using this in something like a react application. Import the generated css into the app /* index.css */
@import "@my-lib/css/style.css"; Use the contract vars in any component. Can be used with vanilla-extract, styled-components, emotion, etc. Any css-in-js lib you want :) // my-component.tsx
import { colors } from "@my-lib/css";
const MyComponent = () => {
return (
<div style={{ background: colors.brand.blue.primary.base }}>
My component text
</div>
);
}; |
Beta Was this translation helpful? Give feedback.
-
Pre-compiling and compiling in the consuming application could both work but I personally suggest adjusting the consuming applications bundling config if possible. So basically shipping .css.js files in your package to then be compiled into .js/.css in the consuming app. Our design system uses this approach. |
Beta Was this translation helpful? Give feedback.
-
I wanted to do the same thing and ended up pre-compiling largely based on @rtkaaho's approach. The package is cadells-vanilla-components and you can see how it's used in cadells-nextjs-template. I ended up using tsup which is based on esbuild after running into some issues with a rollup that I couldn't work out. Hope that helps someone! |
Beta Was this translation helpful? Give feedback.
Pre-compiling and compiling in the consuming application could both work but I personally suggest adjusting the consuming applications bundling config if possible. So basically shipping .css.js files in your package to then be compiled into .js/.css in the consuming app. Our design system uses this approach.