|
1 | 1 | Using Bootstrap CSS & JS
|
2 | 2 | ========================
|
3 | 3 |
|
4 |
| -TODO - show how to bring in bootstrap JS and bootstrap-sass. |
| 4 | +Want to use Bootstrap (or something similar) in your project? No problem! |
| 5 | +First, install it. To be able to customize things further, we'll install |
| 6 | +``bootstrap-sass``: |
5 | 7 |
|
6 |
| -Here is an example from before to include... because I think it's really nice :). |
| 8 | +.. code-block:: terminal |
7 | 9 |
|
8 |
| - // when no file path is defined (i.e. no file extension) Webpack loads the |
9 |
| - // given JavaScript module installed in node_modules/ dir (Webpack knows all |
10 |
| - // the specific files that must be loaded and in which order) |
11 |
| - require('bootstrap-star-rating'); |
| 10 | + $ yarn add bootstrap-sass --dev |
| 11 | +
|
| 12 | +Importing Bootstrap SASS |
| 13 | +------------------------ |
| 14 | + |
| 15 | +Now that ``bootstrap-sass`` lives in your ``node_modules`` directory, you can |
| 16 | +import it from any SASS or JavaScript file. For example, if you're already have |
| 17 | +a ``global.scss`` file, import it from there: |
| 18 | + |
| 19 | +.. code-block:: css |
| 20 | +
|
| 21 | + // assets/css/global.scss |
| 22 | +
|
| 23 | + // customize some Bootstrap variables |
| 24 | + $brand-primary: darken(#428bca, 20%); |
| 25 | +
|
| 26 | + // the ~ allows you to reference things in node_modules |
| 27 | + @import '~bootstrap-sass/assets/stylesheets/bootstrap'; |
| 28 | +
|
| 29 | +That's it! This imports the ``node_modules/bootstrap-sass/assets/stylesheets/_bootstrap.scss`` |
| 30 | +file into ``global.scss``. You can even customize the Bootstrap variables first! |
| 31 | + |
| 32 | +.. tip:: |
| 33 | + |
| 34 | + If you don't need *all* of Bootstrap's features, you can include specific files |
| 35 | + in the ``bootstrap`` directory instead - e.g. ``~bootstrap-sass/assets/stylesheets/bootstrap/alerts``. |
| 36 | + |
| 37 | +After including ``bootstrap-sass``, your webpack builds might become slow. To fix |
| 38 | +this, you can use the ``resolve_url_loader`` option: |
| 39 | + |
| 40 | +.. code-block:: diff |
| 41 | +
|
| 42 | + // webpack.config.js |
| 43 | + Encore |
| 44 | + + enableSassLoader({ |
| 45 | + + 'resolve_url_loader' => false |
| 46 | + + }) |
| 47 | + ; |
| 48 | +
|
| 49 | +This disables the ``resolve-url-loader`` in Webpack, which means that any |
| 50 | +``url()`` paths in your SASS files must now be relative to the original source |
| 51 | +entry file instead of whatever file you're inside of (see `Problems with url()`_). |
| 52 | +To load Bootstrap, you'll need to override the path to its icons: |
| 53 | + |
| 54 | +.. code-block:: diff |
| 55 | +
|
| 56 | + // assets/css/global.scss |
| 57 | +
|
| 58 | + + $icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/"; |
| 59 | +
|
| 60 | + + // set if you're also including font-awesome |
| 61 | + + // $fa-font-path: "~font-awesome/fonts"; |
12 | 62 |
|
13 |
| - // when a file path is given, but it doesn't start with '/' or './', the file |
14 |
| - // path is considered relative to node_modules/ dir |
| 63 | + @import '~bootstrap-sass/assets/stylesheets/bootstrap'; |
| 64 | +
|
| 65 | +Importing Bootstrap JavaScript |
| 66 | +------------------------------ |
| 67 | + |
| 68 | +Bootstrap JavaScript requires jQuery, so make sure you have this installed: |
| 69 | + |
| 70 | +.. code-block:: terminal |
| 71 | +
|
| 72 | + $ yarn add jquery --dev |
| 73 | +
|
| 74 | +Next, make sure call ``.autoProvidejQuery()`` in your ``webpack.config.js`` file: |
| 75 | + |
| 76 | +.. code-block:: diff |
| 77 | +
|
| 78 | + // webpack.config.js |
| 79 | + Encore |
| 80 | + // ... |
| 81 | + + .autoProvidejQuery() |
| 82 | +
|
| 83 | +This is needed because Bootstrap expects jQuery to be available as a global |
| 84 | +variable. Now, require bootstrap from any of your JavaScript files: |
| 85 | + |
| 86 | +.. code-block:: javascript |
| 87 | +
|
| 88 | + // main.js |
| 89 | +
|
| 90 | + var $ = require('jquery'); |
| 91 | + // JS is equivalent to the normal "bootstrap" package |
| 92 | + // no need to set this to a variable, just require it |
| 93 | + require('bootstrap-sass'); |
| 94 | +
|
| 95 | + // or you can include specific pieces |
| 96 | + // require('bootstrap-sass/javascripts/bootstrap/tooltip'); |
| 97 | + // require('bootstrap-sass/javascripts/bootstrap/popover'); |
| 98 | +
|
| 99 | + $(document).ready(function() { |
| 100 | + $('[data-toggle="popover"]').popover(); |
| 101 | + }); |
| 102 | +
|
| 103 | +Thanks to ``autoProvidejQuery()``, you can require any other jQuery |
| 104 | +plugins in a similar way: |
| 105 | + |
| 106 | +.. code-block:: javascript |
| 107 | +
|
| 108 | + // ... |
| 109 | +
|
| 110 | + // require the JavaScript |
| 111 | + require('bootstrap-star-rating'); |
| 112 | + // require 2 CSS files needed |
15 | 113 | require('bootstrap-star-rating/css/star-rating.css');
|
| 114 | + require('bootstrap-star-rating/themes/krajee-svg/theme.css'); |
16 | 115 |
|
17 |
| - // when a file path is given and it starts with '/', './', or '../', it's considered |
18 |
| - // as the full file path for the asset (it can live outside the node_modules/ dir) |
19 |
| - require('../../../../../node_modules/bootstrap-star-rating/themes/krajee-svg/theme.css'); |
| 116 | +.. _`Problems with url()`: https://github.com/webpack-contrib/sass-loader#problems-with-url |
0 commit comments