Develop a design system with Bootstrap 5 and SCSS with a theming API using CSS custom properties on top of Bootstrap's Sass map. Allow switching the primary color at runtime with JavaScript without recompiling SCSS.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Designing runtime theming with Bootstrap 5 and CSS variables
The plan is to generate CSS custom properties from a Sass map of theme colors, override Bootstrap’s $theme-colors to reference these CSS variables, and compile—so component styles use var(--bs-primary) etc. This enables runtime color switching by updating CSS variables via JavaScript without recompiling SCSS, though dynamic lighten/darken effects used at compile time won’t update automatically.Extending runtime theming by overriding Bootstrap’s button styles with CSS variables
To handle hover and focus states relying on lighten/darken colors computed at compile time, override .btn-primary CSS to use custom CSS variables for background, border, and shadows derived from --bs-primary. This fully CSS-variable-based approach lets runtime JS update all related colors consistently without recompiling SCSS. The final design system sets up CSS variables from Sass maps and includes JS to update primary colors dynamically.
Below is a small “design system” layer on top of Bootstrap 5 built with SCSS.
The key idea is:
Keep Bootstrap’s $theme-colors map as the single source of truth.
Emit matching CSS custom properties (--bs-*) from that map.
Override Bootstrap’s theme map to use those CSS custom properties.
At runtime, change the custom property (e.g. --bs-primary) with JavaScript—no SCSS recompilation needed.
SCSS (design-system.scss)
scss
// 1) Pull in Bootstrap’s Sass helpers
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/maps";
// 2) Your base palette (can be anything you like)
$ds-theme-colors: (
"primary": #0d6efd,
"secondary": #6c757d,
"success": #198754,
"info": #0dcaf0,
"warning": #ffc107,
"danger": #dc3545,
"light": #f8f9fa,
"dark": #212529
);
// 3) Emit CSS custom properties for each theme color
:root {
@each $name, $value in $ds-theme-colors {
--bs-#{$name}: #{$value};
--bs-#{$name}-rgb: #{to-rgb($value)};
}
}
// 4) Override Bootstrap’s map to point to the CSS variables
// This lets .bg-primary, .text-primary, etc. reference var(--bs-primary)
$theme-colors: ();
@each $name, $value in $ds-theme-colors {
$theme-colors: map-merge($theme-colors, ($name: var(--bs-#{$name})));
}
// Optional: ensure $primary matches the CSS variable
$primary: map-get($theme-colors, "primary");
// 5) Import the full Bootstrap build with the overridden map
@import "bootstrap/scss/bootstrap";
Note:
Bootstrap components that do their own “lighten/darken” at build time will not recompute those shades when you change --bs-primary at runtime. In practice this is mostly a concern for hover/active states of buttons. In Bootstrap 5.2+ many components expose their own CSS custom properties (e.g. --bs-btn-bg, --bs-btn-hover-bg), so you can also set those in :root or per-theme if you need dynamic hover/active shades.
The SCSS loops through your palette map and emits a --bs-* custom property and an --bs-*-rgb counterpart for each color.
$theme-colors is redefined to use those var(--bs-*) values, so Bootstrap’s utilities and components that reference $theme-colors pick up the custom properties.
At runtime, you call setPrimary('#e83e8c') (or any color) to set --bs-primary (and its RGB). Everything that uses .text-primary, .bg-primary, .btn-primary (base state) will update instantly—no rebuild needed.
That gives you a simple theming API on top of Bootstrap’s Sass map plus runtime color switching via CSS custom properties and JavaScript.