Table of Contents for
Bootstrap 4 – Responsive Web Design

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Bootstrap 4 – Responsive Web Design by Jason Marah Published by Packt Publishing, 2017
  1. Cover
  2. Table of Contents
  3. Bootstrap 4 – Responsive Web Design
  4. Bootstrap 4 – Responsive Web Design
  5. Credits
  6. Preface
  7. What you need for this learning path
  8. Who this learning path is for
  9. Reader feedback
  10. Customer support
  11. 1. Module 1
  12. 1. Getting Started
  13. Setting up the framework
  14. Building our first Bootstrap example
  15. Optionally using the CDN setup
  16. Community activity
  17. Bootstrap and web applications
  18. Browser compatibility
  19. Summary
  20. 2. Creating a Solid Scaffolding
  21. Building our scaffolding
  22. Fluid container
  23. We need some style!
  24. Manipulating tables
  25. Like a boss!
  26. Final thoughts
  27. Summary
  28. 3. Yes, You Should Go Mobile First
  29. Bootstrap and the mobile-first design
  30. How to debug different viewports at the browser
  31. Cleaning up the mess
  32. Creating the landing page for different devices
  33. Summary
  34. 4. Applying the Bootstrap Style
  35. Summary
  36. 5. Making It Fancy
  37. Paying attention to your navigation
  38. Dropping it down
  39. Making an input grouping
  40. Getting ready for flexbox!
  41. Summary
  42. 6. Can You Build a Web App?
  43. Adding the navigation
  44. Do a grid again
  45. Playing the cards
  46. Implementing the main content
  47. Creating breadcrumbs
  48. Finishing with the right-hand-side content
  49. Summary
  50. 7. Of Course, You Can Build a Web App!
  51. Waiting for the progress bar
  52. Creating a settings page
  53. Summary
  54. 8. Working with JavaScript
  55. Awesome Bootstrap modals
  56. Creating our custom modal
  57. A tool for your tip
  58. Pop it all over
  59. Making the menu affix
  60. Finishing the web app
  61. Summary
  62. 9. Entering in the Advanced Mode
  63. The last navigation bar with flexbox
  64. Filling the main fluid content
  65. Filling the main content
  66. Overhead loading
  67. Fixing the toggle button for mobile
  68. Summary
  69. 10. Bringing Components to Life
  70. Fixing the mobile viewport
  71. Learning more advanced plugins
  72. Summary
  73. 11. Making It Your Taste
  74. Working with plugin customization
  75. The additional Bootstrap plugins
  76. Creating our Bootstrap plugin
  77. Defining the plugin methods
  78. Creating additional plugin methods
  79. Summary
  80. 2. Module 2
  81. 1. Introducing Bootstrap 4
  82. Summary
  83. 2. Using Bootstrap Build Tools
  84. Download the Bootstrap source files
  85. Setting up the blog project
  86. Setting up the JSON files
  87. Creating our first page template
  88. Summary
  89. 3. Jumping into Flexbox
  90. Ordering your Flexbox
  91. Wrapping your Flexbox
  92. Setting up the Bootstrap Flexbox layout grid
  93. Setting up a Flexbox project
  94. Designing a single blog post
  95. Summary
  96. 4. Working with Layouts
  97. Inserting rows into your layout
  98. Adding columns to your layout
  99. Choosing a column class
  100. Creating a simple three-column layout
  101. Mixing column classes for different devices
  102. Coding the blog home page
  103. Using responsive utility classes
  104. Summary
  105. 5. Working with Content
  106. Learning to use typography
  107. Customizing headings
  108. How to style images
  109. Coding tables
  110. Summary
  111. 6. Playing with Components
  112. Basic button examples
  113. Creating outlined buttons
  114. Checkbox and radio buttons
  115. Coding forms in Bootstrap 4
  116. Creating an inline form
  117. Adding validation to inputs
  118. Using the Jumbotron component
  119. Adding the Label component
  120. Using the Alerts component
  121. Using Cards for layout
  122. Updating the Blog index page
  123. How to use the Navs component
  124. Adding Breadcrumbs to a page
  125. Using the Pagination component
  126. How to use the List Group component
  127. Summary
  128. 7. Extending Bootstrap with JavaScript Plugins
  129. Coding Tooltips
  130. Avoiding collisions with our components
  131. Using Popover components
  132. Using the Collapse component
  133. Coding an Accordion with the Collapse component
  134. Coding a Bootstrap Carousel
  135. Summary
  136. 8. Throwing in Some Sass
  137. Using Sass in the blog project
  138. Importing partials in Sass
  139. Creating a collection of variables
  140. Customizing components
  141. Writing a theme
  142. Summary
  143. 9. Migrating from Version 3
  144. Big changes in version 4
  145. Updating your variables
  146. Additional global changes
  147. Other font updates
  148. Migrating components
  149. Migrating JavaScript
  150. Miscellaneous migration changes
  151. Summary
  152. 3. Module 3
  153. 1. Revving Up Bootstrap
  154. What Bootstrap 4 Alpha 4 has to offer
  155. Setting up our project
  156. Summary
  157. 2. Making a Style Statement
  158. Image elements
  159. Responsive utilities
  160. Helper classes
  161. Text alignment and transformation
  162. Summary
  163. 3. Building the Layout
  164. Adding Bootstrap components
  165. Summary
  166. 4. On Navigation, Footers, Alerts, and Content
  167. Improving navigation using Scrollspy
  168. Customizing scroll speed
  169. Icons
  170. Using and customizing alerts
  171. Creating a footer
  172. Creating and customizing forms
  173. Form validation
  174. Progress indicators
  175. Adding content using media objects
  176. Figures
  177. Quotes
  178. Abbreviations
  179. Summary
  180. 5. Speeding Up Development Using jQuery Plugins
  181. Enhanced pagination using bootpag
  182. Displaying images using Bootstrap Lightbox
  183. Improving our price list with DataTables
  184. Summary
  185. 6. Customizing Your Plugins
  186. Customizing plugins
  187. Writing a custom Bootstrap jQuery plugin
  188. Summary
  189. 7. Integrating Bootstrap with Third-Party Plugins
  190. Hover
  191. Summary
  192. 8. Optimizing Your Website
  193. Minifying CSS and JavaScript
  194. Introducing Grunt
  195. Running tasks automatically
  196. Stripping our website of unused CSS
  197. JavaScript file concatenation
  198. Summary
  199. 9. Integrating with AngularJS and React
  200. Introducing React
  201. Summary
  202. Bibliography
  203. Index

Introducing React

React is a JavaScript library created by Facebook. While AngularJS positions itself as a framework, React is very clear in its position as a library. React prides itself on being responsible for the visual aspect of the application, "Just the UI", as the React landing page professes. By concerning itself modularly with this single aspect, React is relatively small in size compared to AngularJS and other one-stop-shop frameworks.

React employs a modular approach to the UI, with the idea of components. Components are similar to the directives we used with AngularJS and to the idea of web components. That is, components are reusable pieces of UI functionality, adhering to the "do one thing, do one thing well" ideology. React really pushes the modular approach in how components are usually composed, with tight coupling between styles, HTML, and JavaScript.

Typically, all component specific code is contained within one file. The HTML, the CSS rules, and the JS logic are all included within this file. While at first glance, this approach arguably flies in the face of the approach of separation of concerns, it does totally separate the concerns of components from each other. In other words, making changes to one part of the application should have no impact on another.

React is famously fast when manipulating the DOM, using the virtual DOM approach to figuring out which parts of the UI to update, as opposed to the dirty-checking technique employed by AngularJS. The virtual DOM is essentially the idea of keeping a copy of the real DOM in memory, and updating the copy with necessary changes. The virtual DOM is then periodically compared with the real DOM; any differences then result in that specific piece of the DOM being re-evaluated and re-rendered.

React also promotes the usage of JSX with React applications. JSX is a programming language which compiles into JavaScript, thus requiring a compilation step in the development process. JSX offers a layer of object-oriented style programming on top of JavaScript, such as Java-like class systems and static typing.

Now, let's set up React.

Setting up React

There are several ways of getting setup with React. As the React team maintain a Bower package, we will use Bower as we have done throughout this book. From the terminal, let's pull down React through Bower. We are going to use 0.14.6 version of React:

bower install react#0.14.6

With that, we have a downloaded React to src/bower_components/react . Here you will see react.js , react-dom.js , and react-dom-server.js , along with their minified versions. Here react.js is the core React library, react-dom.js takes responsibility for the actual rendering of the React components in the DOM, and react-dom-server.js allows for server-side rendering of React components.

Create a copy of src/index.html to src/index-react.html , and add the minified versions of React and ReactDOM to the head of the page:

    <script src="bower_components/react/react.min.js"></script>
    <script src="bower_components/react/react-dom.min.js"></script>

We also need to include Babel, a JavaScript compiler which caters for JSX. Babel does not maintain a Bower package, but it is available on npm. However, Babel maintains a version of its library for browsers on a CDN. Include the following in the head of our page:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/
    5.8.23/browser.min.js"></script>

The browser.min.js file will transform any JSX code in HTML, within script tags with a type attribute with the value text/babel . Let's test it out, to make sure we have everything set up correctly. Above our footer, add a div with an id of test:

    <div id="test"></div>

Next, let's write the simplest of React components. At the bottom of index-react.js , after the footer element, include the following:

    <script type="text/babel">
        ReactDOM.render(
            <div className='container-fluid myphoto-section bg-myphoto-
            dark'>Test</div>,
            document.getElementById('test')
        );
    </script>

Let's walk through what is happening here. First, as we said before, our script tag needs a type attribute with the value text/babel so that Babel knows to compile it into JavaScript before execution. Within the script tags, we have our first real interaction with React: ReactDOM and its render function. The render function takes two arguments here: the first is raw HTML, the second is an element selector. What is happening here is pretty self-explanatory: we want React to find the test element, and replace it with the HTML we passed as the first parameter. You may notice that in our HTML, we have a className attribute. We use className instead of class , as class is a reserved word in JavaScript. The className attribute is converted to class when the component is rendered in the DOM. Open index-react.html and check if we now have a Test section in our page:

Setting up React

Figure 9.2: The Test section displaying as expected

We are now all set up to integrate React into MyPhoto. Let's do something more substantial. We are going to convert the carousel in the Gallery component into a React component. Before we get to that, remove the test component, both the div and the JSX we added.

Making a Gallery component in React

To understand how we can integrate React with our application, we are going to make a reusable Gallery component using React. The first thing we are going to do is create a new file, src/app/components/gallery.js , and include it in the head of index-react.html :

    <script type="text/babel" src="app/components/gallery.js"></script>

Notice the type attribute, again set to text/babel , so gallery.js will be compiled to JavaScript at runtime. As you can imagine, this is a slow operation. This method is recommended to be used only for development purposes. For production, all JSX should be precompiled into JavaScript. For the purposes of this example, we will continue with the runtime-compile method.

Let's add some code to gallery.js . Take the gallery markup, the div element with the id gallery-carousel and its nested elements, and add it to gallery . js as the first argument for ReactDOM.render . Make sure to also change the class attributes to className . We also need to make sure that we use handlebar expressions when defining inline styles, as React parses the style attribute as an object, rather than a string:

    ReactDOM.render(
        <div id="gallery-carousel" className="carousel slide" 
        data-ride="carousel" data-interval="3000">
            <div className="carousel-inner" role="listbox">
                <div style={{height: 400px}} className="carousel-item
                active">
                    <img data-modal-picture="#carouselModal" 
                    src="images/brazil.png">
                    <div className="carousel-caption">
                        Brazil
                    </div>
                </div>
                <div style={{height: 400px}} className="carousel-item">
                    <img data-modal-picture="#carouselModal" 
                    src="images/datsun.png">
                    <div className="carousel-caption">
                        Datsun 260Z
                    </div>
                </div>
                <div style={{height: 400px}} className="carousel-item">
                    <img data-modal-picture="#carouselModal" 
                    src="images/skydive.png">
                    <div className="carousel-caption">
                        Skydive
                    </div>
                </div>
            </div>
            <a className="left carousel-control" href="#gallery-
            carousel" role="button" data-slide="prev">
                <span className="icon-prev" aria-hidden="true"></span>
            </a>
            <a className="right carousel-control" href="#gallery-
            carousel" role="button" data-slide="next">
                <span className="icon-next" aria-hidden="true"></span>
            </a>
            <ol className="carousel-indicators">
            <li data-target="#gallery-carousel" data-slide-to="0" 
            className="active"></li>
            <li data-target="#gallery-carousel" data-slide-to="1"></li>
            <li data-target="#gallery-carousel" data-slide-to="2"></li>
            </ol>
        </div>,
            document.getElementById('react-gallery')
    )

The second argument we pass is an element selector, targeting an element with an id of react-gallery . Replace the gallery-carousel element in index-react.js with the target element for the gallery . js React component:

    <div id="react-gallery"></div>

Open index-react.js and we should see the Gallery component, but this time it is being generated by React:

Making a Gallery component in React

Figure 9.3: The React Gallery component

Oh, that isn't what we want. Obviously something has gone wrong here. Let's check out the browser's Developer Console to see if there are any errors reported:

Making a Gallery component in React

Figure 9.4: Chrome's Developer Console displaying errors

So, Babel has thankfully given us an explicit error, it is expecting a closing tag for the img tags in gallery.js . Let's make sure all our img tags are closed in gallery.js :

    <div style={{height: 400px}} className="carousel-item active">
        <img data-modal-picture="#carouselModal" src="images/
        brazil.png"/>
        <div className="carousel-caption">
            Brazil
        </div>
    </div>
    <div style={{height: 400px}} className="carousel-item">
        <img data-modal-picture="#carouselModal" src="images/datsun.png" />
        <div className="carousel-caption">
            Datsun 260Z
        </div>
    </div>
    <div style={{height: 400px}} className="carousel-item">
        <img data-modal-picture="#carouselModal" src="images/skydive.png" 
        />
        <div className="carousel-caption">
            Skydive
        </div>
    </div>

Let's give index-react.js another go and we should now have our React-powered Gallery tab:

Making a Gallery component in React

Figure 9.5: The functioning React Gallery component displaying an image of the Botanical Garden in Rio de Janeiro

Great! We now have a functioning React component. But, it isn't exactly reusable in terms of a carousel. If we wanted another carousel elsewhere, we would need to create another component. Let's make the carousel reusable by passing in options to the React component.

Using carousel in React

To write a reusable component like this, we create the component as a custom React class. This class essentially returns the markup to be rendered by ReactDOM.render , but gives us more power to manipulate our template. Like AngularJS directive custom React classes are extensions of the DOM, allowing us to create new DOM tags. For example, we could create a Carousel element. Note that custom React classes always begin with an uppercase letter:

    <Carousel></Carousel>

Before we do anything, let's analyze the component we have and understand which values we want to make mutable. In the root element, the id and data-interval values need to be changeable. The component also needs to allow the images and caption to be set. Ideally, this should be passed to the component as an array. Finally, the component needs to take in the value for data-modal-picture .

In all, the component needs to take four values. So, the markup for the component would look something like:

    <Carousel id="<value>" interval="<value>" carousel-modal-
    picture="<value>" carousel-images="<[images]"></Carousel>

In gallery.js , we can access component attributes using this.props . Wrapping this.props with curly braces allows these attribute values to be accessed within the markup of the component code. Add the following to the beginning of gallery.js :

    var Carousel = React.createClass({
        render: function () {
            var props = this.props
            return (
                <div id={props.id} className="carousel slide" 
                data-ride="carousel" data-interval={props.interval}>
                    <div className="carousel-inner" role="listbox">
                        { props.images.map(function(item, index) {
                            var itemClass;
                            if (index === 0)
                                itemClass = "active"
                            else 
                                itemClass = ""
                            return (
                                <div className={ 'carousel-item ' +
                                itemClass }>
                                    <img data-modal-picture={'#' +                                                  props.carouselModalPicture} 
                                    src={item.src} />
                            <div className="carousel-caption">
                                {item.caption}
                            </div>
                        </div>
                        )
                    })}
                </div>
                <a className="left carousel-control" href={'#' + props.id } 
                role="button" data-slide="prev">
                    <span className="icon-prev" aria-hidden="true"></span>
                </a>
                <a className="right carousel-control" href={'#' + props.id
                } 
                role="button" data-slide="next">
                    <span className="icon-next" aria-hidden="true"></span>
                </a>
                <ol className="carousel-indicators">
                    { props.images.map(function(item, index) {
                        var liClass;
                        if (index === 0)
                            liClass = "active"
                        else 
                            liClass = ""
                        return (
                            <li data-target={'#' + props.id } data-slide-
                            to={index} className={ liClass }></li>
                        )
                    })
                }
            </ol>
        </div>
        )
      }
    })

We have created a new React class using React.createClass , which has a render property. The render property is simply a function which returns an HTML template. The template is essentially the markup for the gallery-carousel component, except we are accessing some dynamic properties. We have replaced all references to the carousel id with this.props.id , all references to the id of the modal window to open up the carousel modal to this.props.carouselModalPicture, and the data-interval to this.props.interval . We will come back to the images and the captions later. We assign this custom React class to the variable Carousel .

Now that we have this reusable class, we no longer need the template within the ReactDOM.render function. Replace the function with the following:

    ReactDOM.render(
        <Carousel id="gallery-carousel" interval="3000"                          carouselModalPicture="carouselModal"></Carousel>,
        document.getElementById('react-gallery')
    )

We are now using the Carousel tag in the render method. We are passing three attributes to Carousel - id , interval, and carouselModalPicture . The values of these attributes will then be used in the template returned by Carousel . render . ReactDOM . render will then replace the react - gallery element in index - react . js with the carousel template, with these defined attributes. Check it out and see if we have a fully functioning carousel in the Gallery tab. Change some of the attribute values and see if the carousel component works as expected.

Now, let's put the images and image captions in as an option. In reality, these values would come from an API, or will be otherwise dynamically generated. For the sake of this example, we are going to create a variable with the array of values. To demonstrate that the values are being passed through as an attribute, we will change the captions slightly. Add the following to the beginning of gallery.js :

    var carouselImages = [
        {
            src: "images/brazil.png",
            caption: "Lake in Brazil"
        },
        {
            src: "images/datsun.png",
            caption: "Datsun Fairlady Z"
        },
        {
            src: "images/skydive.png",
            caption: "Team Skydive"
        }
    ]

Now, we can pass carouselImages as an attribute of the Carousel tag:

    <Carousel id="gallery-carousel" interval="3000"
    carouselModalPicture="carouselModal" images={carouselImages}>
    </Carousel>

In the carousel template, we need to loop through the data passed into the images attribute, and create a new slide for each entry, as well as a new indicator in the carousel-indicators list. We will loop through the dataset using the map function. As map creates a closure, we first need to create a reference to this . props , as this will be different in the context of the closure. At the beginning of the render function, assign this.props to props :

    var props = this.props

Next, remove the slides from the carousel-inner element and add the following:

    { props.images.map(function(item, index) {
        var itemClass;
        if (index === 0) {
            itemClass = "active"
        } else {
            itemClass = ""
        }
        return (
            <div className={ 'item md ' + itemClass }>
            <img data-modal-picture={'#' + props.carouselModalPicture} 
            src={item.src} />
            <div className="carousel-caption">
                {item.caption}
            </div>
        </div>
        )
    })}

We are looping through props.images using the map function. We want to set the first slide in the array to be the active slide, so we check its index and assign the itemClass variable accordingly. We then define the template for the slide. We pass itemClass into the className attribute to denote the initially active slide, we then use the item.src property as the src of the img element, and item.caption as the caption of the slide. Next, remove all the list items from the carousel-indicators list, replacing them with the following:

    { props.images.map(function(item, index) {
        var liClass;
        if (index === 0) {
            liClass = "active"
    } else {
        liClass = ""
    }
        return (
            <li data-target={'#' + props.id } data-slide-to
            ={index} className={ liClass }></li>
            )
        })
    }

Similarly, we loop through the images and assign the first slide as the active slide. That is everything our component needs to create our Gallery carousel. Let's check it out:

Using carousel in React

Figure 9.6: The functioning React Gallery component displaying an image of the Botanical Garden in Rio de Janeiro

As you can see from the caption, the carousel is loading from the imagesCarousel array. Now, we have a customizable, reusable, and easily maintainable React-powered carousel component that can be used anywhere across MyPhoto.