i am using bootstrap v5 using npm and using sass compiler to compile the scss. i want to customize .btn-primary .so in my dirctory _button.sccs file i made a .btn-primary class with icluding the button-variant mixin given from bootstrap.
but at compiling time it gives an error.
The code is below :
This is my _custom.scss file.
$yellow: #f8b534;
$small: 576px;
$medium: 768px;
$large: 992px;
//bootstrap imported
// Option A: Include all of Bootstrap
// Include any default variable overrides here (though functions won't be available)
#import "../node_modules/bootstrap/scss/bootstrap";
// Then add additional custom code here
// Copy
// Custom.scss
// Option B: Include parts of Bootstrap
// 1. Include functions first (so you can manipulate colors, SVGs, calc, etc)
#import "../node_modules/bootstrap/scss/functions";
// 2. Include any default variable overrides here
// 3. Include remainder of required Bootstrap stylesheets
#import "../node_modules/bootstrap/scss/variables";
#import "../node_modules/bootstrap/scss/mixins";
// 4. Include any optional Bootstrap components as you like
#import "../node_modules/bootstrap/scss/root";
#import "../node_modules/bootstrap/scss/reboot";
#import "../node_modules/bootstrap/scss/type";
#import "../node_modules/bootstrap/scss/images";
#import "../node_modules/bootstrap/scss/containers";
#import "../node_modules/bootstrap/scss/grid";
Then my _button.scss file:
#use '../custom' as *;
$btn-padding-y: $input-btn-padding-y;
$btn-padding-x: $input-btn-padding-x;
$btn-font-family: $input-btn-font-family;
$btn-font-size: $input-btn-font-size;
$btn-line-height: $input-btn-line-height;
$btn-white-space: null; // Set to `nowrap` to prevent text wrapping
$btn-padding-y-sm: $input-btn-padding-y-sm;
$btn-padding-x-sm: $input-btn-padding-x-sm;
$btn-font-size-sm: $input-btn-font-size-sm;
$btn-padding-y-lg: $input-btn-padding-y-lg;
$btn-padding-x-lg: $input-btn-padding-x-lg;
$btn-font-size-lg: $input-btn-font-size-lg;
$btn-border-width: $input-btn-border-width;
$btn-font-weight: $font-weight-normal;
$btn-box-shadow: inset 0 1px 0 rgba($white, .15), 0 1px 1px rgba($black, .075);
$btn-focus-width: $input-btn-focus-width;
$btn-focus-box-shadow: $input-btn-focus-box-shadow;
$btn-disabled-opacity: .65;
$btn-active-box-shadow: inset 0 3px 5px rgba($black, .125);
$btn-link-color: $link-color;
$btn-link-hover-color: $link-hover-color;
$btn-link-disabled-color: $gray-600;
// Allows for customizing button radius independently from global border radius
$btn-border-radius: $border-radius;
$btn-border-radius-sm: $border-radius-sm;
$btn-border-radius-lg: $border-radius-lg;
$btn-transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
$btn-hover-bg-shade-amount: 15%;
$btn-hover-bg-tint-amount: 15%;
$btn-hover-border-shade-amount: 20%;
$btn-hover-border-tint-amount: 10%;
$btn-active-bg-shade-amount: 20%;
$btn-active-bg-tint-amount: 20%;
$btn-active-border-shade-amount: 25%;
$btn-active-border-tint-amount: 10%;
.btn-primary {
#include button-variant(
$background,
$border,
$color: color-contrast($background),
$hover-background:
if(
$color == $color-contrast-light,
shade-color($background, $btn-hover-bg-shade-amount),
tint-color($background, $btn-hover-bg-tint-amount)
),
$hover-border:
if(
$color == $color-contrast-light,
shade-color($border, $btn-hover-border-shade-amount),
tint-color($border, $btn-hover-border-tint-amount)
),
$hover-color: color-contrast($hover-background),
$active-background:
if(
$color == $color-contrast-light,
shade-color($background, $btn-active-bg-shade-amount),
tint-color($background, $btn-active-bg-tint-amount)
),
$active-border:
if(
$color == $color-contrast-light,
shade-color($border, $btn-active-border-shade-amount),
tint-color($border, $btn-active-border-tint-amount)
),
$active-color: color-contrast($active-background),
$disabled-background: $background,
$disabled-border: $border,
$disabled-color: color-contrast($disabled-background)
)
};
But when compiling gives this error in below:
Error: Undefined variable.
╷
49 │ $background,
│ ^^^^^^^^^^^
╵
scss\components\_button.scss 49:3 #use
scss\sections\_navbar.scss 2:1 #use
scss\style.scss 6:1 root stylesheet
Related
Github project
Hello every one ! I'm on an angular project and i'm stuck with my scss.
I have a mixin (in its own file mixins.scss :
BUTTON MIXINS
===================== */
#mixin btn($color:false,$bgColor:false,$size:false,$hoverBgColor:false){
button {
#if $color {
color: $color;
}
#if $bgColor {
background-color: $bgColor;
}
#if $size {
font-size: $size;
}
#if $hoverBgColor {
&:hover {
background-color: $hoverBgColor;
}
}
}
}
Also i have a global scss file with my variables :
$colors: (
primary-black: #000000,
primary-white: #FFFFFF,
medium-grey: #7A746E,
light-cream: #FFF7F0,
);
/* =====================
BUTTON VARIABLE
===================== */
$btnColor:(
btnHeaderFooterColor: map-get($colors, "primary-black"),
btnBody: map-get($colors, "light-red"),
);
When i want use my mixin for my button-component nothing happen, this is an example of my code.
button-component.scss :
#use "sass:map";
#import './../../../../styles/base/mixins';
#import './../../../../styles/base/global';
button {
.btn {
padding: 0.5rem 1.7rem;
border-radius: 1.8rem;
border-color: transparent;
&__headerFooter {
#include btn(map.get($btnColor, btnHeaderFooterColor));
}
}
}
I already tried #usebut i have the same result, my code doesn't appears in the browser inspector, i applied the class in my HTML, but if you want check the project the link is available just on top of this post.
I really appreciate your help because for me it's a mystery this situation !
Thanks you for reading me. :)
Hello i am getting error while i am trying to inject variables from .css to .scss .
Reason why i am doing this is because javascript(React) changes the colors in .css and then these css variables are used in scss. It works as it should but not when i try to modify bootstrap of version 5 colors with css variables it throws errors. (using normal colors , non-variables for bootstrap works ).
Code looks like this:
The CSS file:injectors.css
--primary: #757575;
--secondary: #757575;
--success: #757575;
--info: #757575;
--warning: #757575;
--danger: #757575;
--light: #757575;
--dark: #757575;
The variables.scss
//Fonts
$mainfont: var(--fontFamily);
$lighterFont: var(--lighterFont);
//Colors
$grey: var(--greyColor);
//Bootstrap theming
$primary: var(--primary);
$secondary: var(--secondary);
$success: var(--success);
$info: var(--info);
$warning: var(--warning);
$danger: var(--danger);
$light: var(--light);
$dark: var(--dark);
#import "bootstrap/scss/functions";
#import "bootstrap/scss/variables";
#import "injectors.css";
#import "bootstrap/scss/bootstrap.scss";
And the error message is this one:
Compiled with problems:X
ERROR in ./src/common/styles/authPage.scss (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[2].use[1]!./node_modules/sass-loader/dist/cjs.js!./src/common/styles/authPage.scss)
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: $color: var(--primary) is not a color.
╷
37 │ #return red($value), green($value), blue($value);
│ ^^^^^^^^^^^
╵
node_modules/bootstrap/scss/_functions.scss 37:11 to-rgb()
node_modules/bootstrap/scss/_functions.scss 61:36 map-loop()
node_modules/bootstrap/scss/_variables.scss 94:20 #import
src/common/styles/variables.scss 30:9 #import
src/common/styles/authPage.scss 1:9 root stylesheet
I have a scss file with my color scheme
_colors.scss
#import './variables';
$colors: (
p: $color-primary,
s: $color-secondary,
t: $color-tertiary,
q: $color-quartary,
);
#each $name, $hsl in $colors {
.c-#{name} {
color: $hsl;
}
.bg-#{name} {
background: $hsl;
}
}
And file with variables
_variables.scss
$color-primary: hsl(113, 99%, 53%);
$color-secondary: hsl(0, 7%, 19%);
$color-tertiary: hsl(0, 7%, 78%);
$color-quartary: hsl(2, 87%, 48%);
And finally
main.scss
#import './variables';
#import './colors';
body {
color: $color-tertiary;
background-color: $color-secondary;
}
So the question is why I can't use the classes generated in #each which should be c-p, bg-p, c-s, etc.
If I manually define for example c-p class
.c-p {
color: $color-primary;
}
It doesn't matter where I define it in _colors.scss or main.scss and either it before or after #each statement. It works just fine.
Your loop currently generates CSS classes like .bg-name and .c-name as you're using the string name instead of the variable $name. The following should work:
#each $name, $hsl in $colors {
.c-#{$name} {
color: $hsl;
}
.bg-#{$name} {
background: $hsl;
}
}
I need to change the colors theme of my app according to a parameter and it should be a very simple operation. In my app I use a Fuse angular material theme. When I try to switch from the primary to the secondary theme, the accent color of the dialog component does not change while the other components (for example the navigation bar) do.
_theme.scss
#import '~#angular/material/theming';
#import './component-themes';
$primary: mat-palette($mat-fusedark);
$accent: mat-palette($mat-light-blue, 600, 400, 700);
$warn: mat-palette($mat-red);
$white: mat-palette($mat-white);
$black: mat-palette($mat-black);
$first-theme: mat-light-theme($primary, $accent, $warn);
$background: map-get($first-theme, background);
$foreground: map-get($first-theme, foreground);
#include angular-material-theme($first-theme);
#include component-themes($first-theme);
$second-primary: mat-palette($mat-fusedark);
$second-accent: mat-palette($mat-green, 600, 400, 700);
$second-warn: mat-palette($mat-red);
$second-theme: mat-light-theme($second-primary, $second-accent, $second-warn);
.second-theme {
#include angular-material-theme($second-theme);
#include component-themes($second-theme);
}
component-theme.scss
#import "../partials/navigation";
#import "../partials/dialog";
#mixin component-themes($theme) {
#include navigation-theme($theme);
#include dialog-theme($theme);
}
_dialog.scss
#import '~#angular/material/theming';
#mixin dialog-theme($theme) {
$accent: map-get($theme, accent);
.dialog-wrapper {
.mat-dialog-container {
padding: 0;
overflow: hidden;
}
.mat-dialog-title {
display: flex;
flex: 1 1 auto;
justify-content: space-between;
margin: 0;
padding: 24px;
}
.mat-toolbar {
background-color: mat-color($accent) !important;
}
.mat-dialog-content {
padding: 16px 32px 0px;
margin: 0;
}
.mat-dialog-actions {
display: flex;
flex: 1 1 auto;
margin: 1px 0 0 0;
padding: 0px 32px 16px;
}
}
}
If I change in the _dialog.scss the value
background-color: mat-color($accent) !important;
into
background-color: green !important;
it works properly. It looks like mat-color($accent)does not work but only for the scss of this component.
For who runs into this problem, I have finally found a solution. I could not change the theme of some components because they are nested and the overlay container blocks the theme propagation. In order to fix it, I imported the overlayContainer and add the theme class into the overlayContainer in the ngOnInit method.
import {OverlayContainer} from '#angular/cdk/overlay';
constructor(
private overlayContainer: OverlayContainer
) {}
ngOnInit() {
if (this.data.theme === 'second-theme') {
this.overlayContainer.getContainerElement().classList.add(this.data.theme);
}
}
Very easy solution when you know what is the problem, the hard part was to find it out :)
I have the following scss file being imported into my Angular Project
#import '~#angular/material/theming';
#import "./mat-palette.scss";
#include mat-core();
$ombi-app-primary: mat-palette($ombi-primary, 500);
$ombi-app-accent : mat-palette($ombi-accent, A200, A100, A400);
$background: white;
$ombi-app-warn : mat-palette($mat-deep-orange);
$ombi-app-theme: mat-light-theme($ombi-app-primary, $ombi-app-accent, $ombi-app-warn);
#include angular-material-theme($ombi-app-theme);
// Define an alternate dark theme.
$ombi-dark-app-primary: mat-palette($mat-grey, 800);
$ombi-dark-app-accent: mat-palette($mat-amber, A200, A100, A400);
$ombi-dark-app-warn: mat-palette($mat-deep-orange);
$dark-theme: mat-dark-theme($ombi-dark-app-primary, $ombi-dark-app-accent, $ombi-dark-app-warn);
$primary: mat-color($ombi-app-primary);
$accent: mat-color($ombi-app-accent);
$warn: mat-color($ombi-app-warn);
.dark {
#include angular-material-theme($dark-theme);
$panel: mat-color(mat-palette($mat-grey, 800));
$primary: mat-color($ombi-dark-app-primary);
$accent: mat-color($ombi-dark-app-accent);
$warn: mat-color($ombi-dark-app-warn);
$background: #424242;
}
Now when the .dark class is applied to the body I expect it to apply the variables inside the .dark section above.
I am importing this file into other scss files so I can use the variables e.g. $background.
So when the .dark class is applied, it seems that $background is set to white and not #424242 and I cannot work out why.
Here is an example on how I am importing it:
#import "~styles/Styles.scss";
.details {
background: $background;
}