Use custom properties (CSS variables) in bootstrap
Created by: tobi-or-not-tobi
I like sass variables and they're great during buildtime. But projects that need a microfrontend architecture (i.e. webcomponents) tend not to rebuild components. They rather reuse existing components and provide the style context to it.
Using CSS variables will allow to provide runtime configuration for styling, such as colors. CSS variables can work in conjunction of SASS. I'm happy with a few the first couple of CSS variables in bootstrap, but as already mentioned they're only exposed and not used by bootstrap itself. This means, if a project likes to override these CSS variables at runtime, there's no effect with the bootstrap components themself.
Is there anything against going into the CSS variable direction? Browser support is really great and the fallback option will keep non-supported browsers happy.
I understand this is a big change though. It woudl work like this:
Intialize CSS vars. We use use double-double fallback to ensure we have a fallack and do not need to repeately add the fallback everywhere.
:root {
--primary: var(--primary-color, #{$primary});
}
Then for each and every component that uses the primary color, we could use the --primary variable instead. For example on a button, it would go like this (just a few lines that i'm using in my sandbox project, so its not so relevant to bootstrap sources):
.btn {
&-primary {
background-color: var(--primary);
border-color: var(--primary);
&:hover,
&:not(:disabled):not(.disabled):active {
background-color: var(--primary-darken);
border-color: var(--primary-darken);
}
}
}
This would allow to change the color at runtime.