2

I am having trouble understanding how to use certain Bootstrap SCSS functions within a Angular component, such as theme-color or color.

I have a component.scss that looks like:

.card {
  cursor: pointer;
  &:hover {
    background-color: theme-color("primary");
  }
}

But this doesn't work, without error may I add. If I prepend: @import '~node_modules/bootstrap/scss/bootstrap'; to the file it does, however, it doesn't take into account my global styling, it uses the default colours.

If I import only the functions SCSS then it doesn't understand the other imports, such as the ones that contain the vars, like $color.

Everything works fine when editing in my global styles.scss stylesheet.

It seems as though something about the view encapsulation is stopping global SCSS functions from Bootstrap filtering down to my component SCSS but I cannot quite find out what it is.

So, how do I get global SCSS functions, such as Bootstrap's into my individual component SCSS files?

3
  • Don't import the whole bootstrap Just import what you need: functions, variables and mixins. Commented Jul 27, 2019 at 21:24
  • @JBNizet it seems that if I just import functions then it doesn't recognise the colors variable, and I forgot to mention, that if I import that it doesn't use the colours I've applied in the global stylesheet, it only uses the default ones, it seems like I need to reimport the whole of styles.css Commented Jul 27, 2019 at 21:27
  • @JBNizet though having just tried that it seems it changes the import paths in such a way that non of the nested imports in my styles global sheet resolve anymore Commented Jul 27, 2019 at 21:28

1 Answer 1

4

Here's how I do it.

  1. In styles.scss:

    @import 'custom-bootstrap';
    // + custome global scss rules
    
  2. In custom-bootstrap.scss

    @import './common';
    @import '~bootstrap/scss/root';
    @import '~bootstrap/scss/reboot';
    // + other imports copied from the standard bootstrap.scss file
    // some of them being commented out because I don't need them
    
  3. In common.scss

    // potential bootstrap variable overrides here
    @import '~bootstrap/scss/functions';
    @import '~bootstrap/scss/variables';
    @import '~bootstrap/scss/mixins';
    
  4. In any component scss file:

    @import '../../common'; // the path may of course vary depending on the depth
    // and here I can use any bootstrap (or custom) variable, mixin or function
    
Sign up to request clarification or add additional context in comments.

2 Comments

Sweet, yep, that seems to work thanks, even thrown in my own _bootstrap-variables so I can get my full theme colouring now, is this is to do with view encapsulation? Is it isolating the component styles form the global symbol table, as such when I look for theme-color() it is working from a different symbol table?
Yes an no. The component scss files are not just appended together and then compiled all at once. AFAIK, they're compiled separately, and each component has its own, separate CSS file. Then, at runtime, the view encapsulation itself is used to avoid having the standard cascading rules to apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.