0

I am having problems declaring the scss styles using Bootstrap's media-breakpoint-up mixin after adding a new breakpoint to $grid-breakpoints.

While the d-{infix}-{display} classes come out right and the section with them works just fine, the component-level scss styles are not seeing the new breakpoint, despite imports.

My questions is - am I doing something wrong here? like order of imports for instance?

Angular CLI app with these two dependencies:

"bootstrap": "^4.1.3",
"ngx-bootstrap": "^3.0.1",

styles.scss

@import 'scss/my.variables';
@import 'scss/my.theme';
@import 'scss/bootstrap.partials';

my.variables.scss (nothing of importance)

$brand-primary:#00a651
$fixed-header-min-height: 70px;    
$box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1);
$box-shadow-hover: 0 4px 12px 0 rgba(0, 0, 0, 0.3);

my.theme.scss

$font-family-sans-serif:  proxima-nova, Helvetica, Arial, sans-serif;
$font-family-monospace:   Menlo, Monaco, Consolas, "Courier New", monospace;
$font-family-base:        $font-family-sans-serif;
$font-weight-normal:      200;
$font-weight-base:        $font-weight-normal;
$body-bg:                 #fff;

bootstrap.partials.scss

$grid-breakpoints: (
  xs: 0,
  sm: 576px,
  md: 768px,
  lg: 992px,
  xl: 1200px,
  xxl: 1600px
);

$container-max-widths: (
  sm: 540px,
  md: 720px,
  lg: 960px,
  xl: 1140px,
  xxl: 1540px
);

// Bootstrap partial imports to keep the bloat away
@import '~bootstrap/scss/functions';
@import '~bootstrap/scss/variables';
@import '~bootstrap/scss/mixins';
@import '~bootstrap/scss/reboot';
@import '~bootstrap/scss/type';
@import '~bootstrap/scss/images';
@import '~bootstrap/scss/grid';
@import '~bootstrap/scss/utilities';
@import '~bootstrap/scss/buttons';
@import '~bootstrap/scss/transitions';
@import '~bootstrap/scss/modal';
@import '~bootstrap/scss/close';
@import '~bootstrap/scss/nav';
@import '~bootstrap/scss/navbar';

app.components.html

<h1>Custom breakpoint - xxl introduced (1600px)</h1>

<h2>media queries (media-breakpoint-up et al.)</h2>
<div class="main">
  <p>
    this text should be blue, but red above xxl
  </p>

  <small>
    This Text Should All Be Lowercase, but Uppercase above xxl
  </small>
</div>

<h2>d-* classes</h2>

<p class="d-block">Visible always</p>
<p class="d-none d-lg-block">Visible above lg</p>
<p class="d-none d-xxl-block">Visible above xxl</p>

app.component.scss

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";
@import '~bootstrap/scss/mixins/_breakpoints';

.main {
  color: blue;
  @debug $grid-breakpoints;

  @include media-breakpoint-up(xxl) {
    /* I have also tried without repeating the selector */
    .main {
      color: red;
    }
  }

  small {
    text-transform: uppercase;
    @include media-breakpoint-up(xxl) {
      small {
        text-transform: lowercase;
      }
    }
  }
}

Output

DEBUG: (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)        

No xxl.

Edit 1:

I created a custom wrapper mixin which confirmed the breakpoint is not found.

@mixin respond-above($breakpoint) {
  @if map-has-key($breakpoints, $breakpoint) {
    @include media-breakpoint-up($breakpoint){
      @content;
    }
  } @else {
    @warn 'Missing breakpoint: #{$breakpoint}.';
  }
}
user776686
  • 7,933
  • 14
  • 71
  • 124

1 Answers1

0

I can think of two potential things it could be.

1) You are including bootstrap.partials'; in the global styles.scss, but if there are any variables or mixins defined there, they would be "compiled out" of the final global css, and would not be available in the component partials. Therefore, you may have to include it in your app.component.scss

2) What does your angular.json look like in the root directory? Try adding the path to the node_modules bootstrap directory to stylePreprocessorOptions.includePaths[] in angular.json.

{
    "projects": {
        "myProject": {
            "architect": {
                "build": {
                    "options": {
                        "stylePreprocessorOptions": {
                            "includePaths": [
                                "node_modules/bootstrap"
                            ]
                        },
                    }
                }
            }
        }
    }
}
miir
  • 1,814
  • 1
  • 17
  • 25
  • 1
    Thanks a bunch, miir! It helped. While (2) didn't change anything, (1) did a whole lot! I will try to reorganize the files, as it feels bad to import the entire bootstrap bucket into every file. Btw, the stylePreprocessorOptions are now within `architect/build/options`. Accepting your answer. – user776686 Oct 19 '18 at 19:08
  • No problem, glad something was helpful! Yes, I agree, there may be some issues around including it everywhere, especially if there are css rules as those will be repeated, so if its possible to isolate mixins/variables and only import those, that would be the best. This is something I've been thinking over myself, and I'm not sure what the best solution is yet. I wonder if it would be enough to have it in to the top level root component scss? Maybe not, because component styles are supposed to be isolated from other components, and the scss wouldstill compile down and lose the vars and mixins. – miir Oct 19 '18 at 20:17
  • The sad part is that the bundle size of (!) main.js bundle (js!!!) went up by ~300kb when I imported the partial bucket. When I peeked into the js code there were some traces of jQuery ('on' listeners) and easing functions. I am baffled. For now this is not a solution :( – user776686 Oct 19 '18 at 20:52
  • Oh, no... that is unfortunate! :( I think the alternative would be to bypass CLI's lazy loading, and use a more traditional sass-build setup with gulp or grunt tasks... or isolate bootstrap partials down to only mixins/variables, and only include those? – miir Oct 19 '18 at 21:50