Categories
BootstrapVue

BootstrapVue — Media and Modal

To make good looking Vue apps, we need to style our components.

To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to add media and modal components.

Media

We can add components to show media that sits beside another component.

To do that, we add the b-media component.

We can use it as follows:

<template>
  <div id="app">
    <b-media>
      <template v-slot:aside>
        <b-img blank blank-color="#ccc" width="50" alt="placeholder"></b-img>
      </template>

      <h5 class="mt-0">Title</h5>
      <p>foo</p>
      <p>bar</p>
    </b-media>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

We put something in the aside slot as indicated with the v-slot:aside directive.

We put an image inside.

Outside of it, we add some text.

aside will put the image on the left and the text will be on the right.

We can also nest them, so we can write:

<template>
  <div id="app">
    <b-media>
      <template v-slot:aside>
        <b-img blank blank-color="#ccc" width="50" alt="placeholder"></b-img>
      </template>

      <h5 class="mt-0">Title</h5>
      <p>foo</p>
      <b-media>
        <template v-slot:aside>
          <b-img blank blank-color="green" width="50" alt="placeholder"></b-img>
        </template>

        <h5 class="mt-0">Nested Media</h5>
        <p class="mb-0">
          bar
        </p>
      </b-media>
    </b-media>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

Then we have another b-media component inside the outer one.

Align Image

We can align images the way we like.

To do that, we use the b-media-aside component with the vertical-align prop.

For instance, we can write:

<template>
  <div id="app">
    <b-media no-body>
      <b-media-aside vertical-align="center">
        <b-img blank blank-color="#ccc" width="64" height="128" alt="placeholder"></b-img>
      </b-media-aside>

      <b-media-body class="ml-3">foo</b-media-body>
    </b-media>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

We have the b-media-aside component to put the image on the left side.

vertica-align='center' will make the image vertically centered.

b-media-body create a body to the right of the image.

Order

We can flip the order of the image and the text with the right-align prop.

For example, we can write:

<template>
  <div id="app">
    <b-media right-align>
      <template v-slot:aside>
        <b-img blank blank-color="#ccc" width="30" alt="placeholder"></b-img>
      </template>

<p>foo</p>
    </b-media>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

Now we have the b-img component displayed on the right instead of the text.

Media List

The tag prop lets us render our media b-media components with the tag we want.

For example, we can write:

<template>
  <div id="app">
    <ul>
      <b-media tag="li">
        <template v-slot:aside>
          <b-img blank blank-color="#ccc" width="30" alt="placeholder"></b-img>
        </template>

<p>foo</p>
      </b-media>

      <b-media tag="li">
        <template v-slot:aside>
          <b-img blank blank-color="blue" width="30" alt="placeholder"></b-img>
        </template>

         <p>bar</p>
      </b-media>

      <b-media tag="li">
        <template v-slot:aside>
          <b-img blank blank-color="green" width="30" alt="placeholder"></b-img>
        </template>

          <p>baz</p>
      </b-media>
    </ul>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

Then we render our b-media component as li elements.

Modals

Modals are dialog boxes that are powered by JavaScript and CSS.

We can create a simple one with the b-modal component.

For example, we can write:

<template>
  <div id="app">
    <b-button v-b-modal.modal>open modal</b-button>

    <b-modal id="modal" title="Hello">
      <p>Hello!</p>
    </b-modal>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

We have the b-button with the v-b-modal.modal directive.

The modal modifier is the value of the id prop of the modal we want to open.

The b-modal component is the modal itself.

title is the title of the modal and it’s displayed on the top.

The content is between the tags.

It also has the OK and Cancel buttons by default.

We can hide the Cancel button with the ok-only prop.

The ok-title prop lets us change the OK button’s text.

The cancel-title prop lets us change the Cancel button’s text.

We can use the modal-ok and modal-cancel slots to change the button content.

The modal-title slot lets us change the title’s content.

The modal-header slot lets us change the modal header.

The modal-footer slot lets us change the footer content.

Conclusion

The b-media component lets us add an image with text side by side.

Modals are dialog boxes that overlay our main content.

Categories
BootstrapVue

BootstrapVue — Navbar

To make good looking Vue apps, we need to style our components.

To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to add navbars to our app.

Navbar

The b-navbar component lets us add a navbar with branding.

For instance, we can write:

<template>  
  <div id="app">  
    <b-navbar toggleable="lg" type="dark" variant="info">  
      <b-navbar-brand href="#">brand</b-navbar-brand>

      <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>

      <b-collapse id="nav-collapse" is-nav>  
        <b-navbar-nav>  
          <b-nav-item href="#">foo</b-nav-item>  
          <b-nav-item href="#" disabled>bar</b-nav-item>  
        </b-navbar-nav>  
      </b-collapse>  
    </b-navbar>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have a navbar that we added with the b-navbar component.

b-collapse lets us add links that are responsive.

So they’ll be condensed into a hamburger menu if we have a small screen.

b-nav-item is the nav item.

The toggleable prop is for setting the breakpoint for which the navbar will expand from a menu to the full navbar.

variant is the styling variant.

type is the text color style.

Dropdowns

We can add dropdowns to a navbar.

We use the b-nav-item-dropdown and b-dropdown-item to do that:

<template>  
  <div id="app">  
    <b-navbar toggleable="lg" type="dark" variant="info">  
      <b-navbar-nav class="ml-auto">  
        <b-nav-item-dropdown text="Menu" right>  
          <b-dropdown-item href="#">foo</b-dropdown-item>  
          <b-dropdown-item href="#">bar</b-dropdown-item>  
        </b-nav-item-dropdown>  
      </b-navbar-nav>  
    </b-navbar>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have a menu that has a dropdown inside it.

It’s responsive so it’ll fit on any screen.

The right prop aligns the dropdown to the right.

Customizing Dropdown Button

We can customize the dropdown button with slots.

For instance, we can populate the button-content slot to add our own content:

<template>  
  <div id="app">  
    <b-navbar toggleable="lg" type="dark" variant="info">  
      <b-navbar-nav class="ml-auto">  
        <b-nav-item-dropdown right>            
          <template v-slot:button-content>  
            <strong>foo</strong>  
          </template>  
          <b-dropdown-item href="#">bar</b-dropdown-item>  
          <b-dropdown-item href="#">baz</b-dropdown-item>  
        </b-nav-item-dropdown>  
      </b-navbar-nav>  
    </b-navbar>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Now the dropdown button is bold.

Inline Form

We can add the inline form.

For example, we can write:

<template>  
  <div id="app">  
    <b-navbar toggleable="lg" type="dark" variant="info">  
      <b-navbar-nav class="ml-auto">  
        <b-nav-form>  
          <b-form-input size="sm" class="mr-sm-2" placeholder="Input"></b-form-input>  
          <b-button size="sm" class="my-2 my-sm-0" type="submit">Submit</b-button>  
        </b-nav-form>  
      </b-navbar-nav>  
    </b-navbar>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have the b-navbar-nav which houses the b-nav-form .

Inside it, we have the b-form-input inside the form.

b-navbar-brand

The brand is added to the top left of the navbar.

For example, we can write:

<template>  
  <div id="app">  
    <b-navbar variant="faded" type="light">  
      <b-navbar-brand href="#">Brand</b-navbar-brand>  
    </b-navbar>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have changed the style variant with variant and the text color with type.

We can also add images:

<template>  
  <div id="app">  
    <b-navbar>  
      <b-navbar-brand href="#">  
        <img src="https://placekitten.com/30/30" alt="cat">  
      </b-navbar-brand>  
    </b-navbar>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We added an img element into the b-navbar-brand .

We’ve to make sure the image fits properly in the nav with our own styles.

Custom Navbar Toggle

To customize the navbar toggle, we can use the b-navbar-toggle component.

For instance, we can write:

<template>  
  <div id="app">  
    <b-navbar toggleable >  
      <b-navbar-brand href="#">brand</b-navbar-brand>

      <b-navbar-toggle target="navbar-toggle-collapse">  
        <template v-slot:default="{ expanded }">  
          <b-icon v-if="expanded" icon="chevron-bar-up"></b-icon>  
          <b-icon v-else icon="chevron-bar-down"></b-icon>  
        </template>  
      </b-navbar-toggle>

      <b-collapse id="navbar-toggle-collapse" is-nav>  
        <b-navbar-nav class="ml-auto">  
          <b-nav-item href="#">foo</b-nav-item>  
          <b-nav-item href="#">bar</b-nav-item>  
        </b-navbar-nav>  
      </b-collapse>  
    </b-navbar>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have the b-navbar-toggle component to custom the toggle.

The default slot is populated so that we can customize it.

Conclusion

We can create a navbar with various customizations.

Menus, forms, and links can be added.

Categories
BootstrapVue

BootstrapVue — Customizing List Groups

To make good looking Vue apps we need to style our components. To make our lives easier we can use components with styles built-in. In this article, we’ll look at how to add list groups.

Disabled Items

We can disable list group items by adding the disabled prop.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item disabled>foo</b-list-group-item>  
      <b-list-group-item>bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Then we’ll see the first item grayed out.

Actionable List Group Items

We can add the href prop to a list group item.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item href="http://example.com">foo</b-list-group-item>  
      <b-list-group-item href="http://medium.com">bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Then we can go to the websites that are added to the href prop.

We can change the list group item to a button by using the button prop.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item button>foo</b-list-group-item>  
      <b-list-group-item button>bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Now the items are displayed as buttons.

Styling Variants

We can add the variant prop to change the styling variant of the list group item.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item variant="success">foo</b-list-group-item>  
      <b-list-group-item>bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We set the value of variant to 'success' .

Then we’ll see that it’s green.

Possible values include primary , secondary , success , danger , warning , info , light , and dark .

Add Badges

We can add badged to a list group item by using the b-badge component.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item>foo  
        <b-badge variant="primary" pill>14</b-badge>  
      </b-list-group-item>  
      <b-list-group-item>bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have the variant prop on b-badge like we have on list-group-items .

The possible values are the same.

pill makes the badge look like a pill.

primary makes the badge blue.

Inside Cards

We can put lust groups inside a card.

To remove the border from the list group, we can add the flush prop.

For instance, we can write:

<template>  
  <div id="app">  
    <b-card header="Card">  
      <b-list-group>  
        <b-list-group-item href="#">foo</b-list-group-item>  
        <b-list-group-item href="#">bar</b-list-group-item>  
      </b-list-group>  
    </b-card>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

or:

<template>  
  <div id="app">  
    <b-card header="Card">  
      <b-list-group flush>  
        <b-list-group-item href="#">foo</b-list-group-item>  
        <b-list-group-item href="#">bar</b-list-group-item>  
      </b-list-group>  
    </b-card>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Horizontal List Groups

The list group can be made horizontal.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group horizontal>  
      <b-list-group-item href="#">foo</b-list-group-item>  
      <b-list-group-item href="#">bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Then we can see list group items side by side.

Custom Content

We can add anything inside a list group item.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item href="#">  
        <h5 class="mb-1">Foo</h5>  
        <small class="text-muted">3 days ago</small>  
      </b-list-group-item>  
      <b-list-group-item href="#">  
        <h5 class="mb-1">Bar</h5>  
        <small class="text-muted">3 days ago</small>  
      </b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We add come utility classes to our content which we nest inside the b-list-group-item .

Conclusion

We can customize list group items so that they are horizontal. Also, we can add badges or any other content we want. List groups can also be put inside cards.

Categories
BootstrapVue

BootstrapVue — More Complex Navigation

To make good looking Vue apps, we need to style our components.

To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to add navigation components.

Inline Forms

The b-nav-form lets us add forms into the navbar.

For example, we can write:

<template>
  <div id="app">
    <b-nav>
      <b-nav-item active>foo</b-nav-item>
      <b-nav-item>bar</b-nav-item>
      <b-nav-form @submit.stop.prevent="submit">
        <b-form-input></b-form-input>
        <b-button type="submit">Ok</b-button>
      </b-nav-form>
    </b-nav>
  </div>
</template>
<script>
export default {
  name: "App",
  methods: {
    submit(){
      alert('success')
    }
  }
};
</script>

We have the b-form-input inside the b-nav-form .

We can call a submit handler by setting the submit event handler on the b-nav-form .

Card Integration

We can add a navbar inside a card.

For instance, we can write:

<template>
  <div id="app">
    <b-card title="Card Title" no-body>
      <b-card-header header-tag="nav">
        <b-nav card-header tabs>
          <b-nav-item active>foo</b-nav-item>
          <b-nav-item>bar</b-nav-item>
          <b-nav-item disabled>baz</b-nav-item>
        </b-nav>
      </b-card-header>

      <b-card-body class="text-center">
        <b-card-text>Content.</b-card-text>

        <b-button variant="primary">Do Something</b-button>
      </b-card-body>
    </b-card>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

We just put the b-nav inside the b-card-header .

Then we add the card-header prop to make it fit inside the card header.

tabs makes it show up as tabs.

Alternatively, we can change it to the pills style:

<template>
  <div id="app">
    <b-card title="Card Title" no-body>
      <b-card-header header-tag="nav">
        <b-nav card-header pills>
          <b-nav-item active>foo</b-nav-item>
          <b-nav-item>bar</b-nav-item>
          <b-nav-item disabled>baz</b-nav-item>
        </b-nav>
      </b-card-header>

      <b-card-body class="text-center">
        <b-card-text>Content.</b-card-text>

        <b-button variant="primary">Do Something</b-button>
      </b-card-body>
    </b-card>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

We can also remove the card-header prop if we want a plain style navbar:

<template>
  <div id="app">
    <b-card title="Card Title" no-body>
      <b-card-header header-tag="nav">
        <b-nav>
          <b-nav-item active>foo</b-nav-item>
          <b-nav-item>bar</b-nav-item>
          <b-nav-item disabled>baz</b-nav-item>
        </b-nav>
      </b-card-header>

      <b-card-body class="text-center">
        <b-card-text>Content.</b-card-text>

        <b-button variant="primary">Do Something</b-button>
      </b-card-body>
    </b-card>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

Use with Vue Router

We can use nav with Vue Router. To do this, we have to define a parent route and a set of child routes.

For example, we can write:

main.js

import Vue from "vue";
import App from "./App.vue";
import { BootstrapVue } from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";
import VueRouter from "vue-router";
import Foo from "./components/Foo";
import Bar from "./components/Bar";
import Root from "./components/Root";

const routes = [
  {
    path: "/",
    component: Root,
    children: [
      {
        path: "foo",
        component: Foo
      },
      {
        path: "bar",
        component: Bar
      }
    ]
  }
];

const router = new VueRouter({
  routes
});

Vue.use(VueRouter);
Vue.use(BootstrapVue);
Vue.config.productionTip = false;

new Vue({
  router,
  render: h => h(App)
}).$mount("#app");

We added the VueRouter plugin and the routes.

We have to have a parent route for the nav and child routes for displaying the content in the route.

The child routes are in the children prop.

Then we add:

App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

This is the parent router-view .

Then we add our nav and child router-view :

components/Root.vue

<template>
  <div id="app">
    <b-card title="Card Title" no-body>
      <b-card-header header-tag="nav">
        <b-nav>
          <b-nav-item to="/foo" exact exact-active-class="active">foo</b-nav-item>
          <b-nav-item to="/bar" exact exact-active-class="active">bar</b-nav-item>
        </b-nav>
      </b-card-header>

      <b-card-body class="text-center">
        <router-view></router-view>
      </b-card-body>
    </b-card>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

We have b-nav-item with the to prop. And we have the exact prop to match the route exactly.

exact-active-class is a CSS class that’s applied when the route is navigated to.

Then we add our child routes:

component/Foo.vue :

<template>
  <p>foo</p>
</template>

<script>
export default {};
</script>

component/Bar.vue :

<template>
  <p>bar</p>
</template>

<script>
export default {};
</script>

Conclusion

We can add various things to our nav like inline forms.

Also, we can put the nav in cards.

It also integrates with Vue Router so we can navigate to routes with navs.

Categories
BootstrapVue

BootstrapVue — Layouts and Lists

To make good looking Vue apps, we need to style our components. To make our lives easier, we can use components with styles built-in. In this article, we’ll look at how to add layouts.

Offsetting Columns

We can add offsets to columns to spread them out.

For example, we can write:

<template>  
  <div id="app">  
    <b-container>  
      <b-row>  
        <b-col md="4">foo</b-col>  
        <b-col md="4" offset-md="4">bar</b-col>  
      </b-row>  
    </b-container>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have offset-md='4' to space them out at the md breakpoint by 4 columns.

We can change the offsets at different breakpoints.

For instance, we can write:

<template>  
  <div id="app">  
    <b-container>  
      <b-row>  
        <b-col md="4">foo</b-col>  
        <b-col md="4" offset-md="4" col-lg="6" offset-lg="0">bar</b-col>  
      </b-row>  
    </b-container>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We set the column to 6 for the lg breakpoint to set the column width to 6 for wide screens.

offset-lg is 0 to remove column spaces for wide screens.

Margins

We can add classes like mr-auto or ml-auto to add margins to our columns.

For example, we can write:

<template>  
  <div id="app">  
    <b-container>  
      <b-row>  
        <b-col md="4">foo</b-col>  
        <b-col md="4" class="ml-md-auto">bar</b-col>  
      </b-row>  
    </b-container>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Then we add some margins to our right column.

Nesting Grids

We can nest grids.

For instance, we can write:

<template>  
  <div id="app">  
    <b-container>  
      <b-row>  
        <b-col md="4">  
          <b-row>  
            <b-col cols="8" sm="6">foo</b-col>  
            <b-col cols="4" sm="6">bar</b-col>  
          </b-row>  
        </b-col>  
      </b-row>  
    </b-container>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We just put a b-row inside a b-col to nest it.

Row Columns

We can set the cols prop on b-row to specify the number of columns that can be in a row.

For instance, we can write:

<template>  
  <div id="app">  
    <b-container>  
      <b-row cols="2">  
        <b-col>foo</b-col>  
        <b-col>bar</b-col>  
        <b-col>baz</b-col>  
      </b-row>  
    </b-container>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have 3 columns but col is set to 2.

So the last b-col is below the rest.

We can also have a different number of columns at different breakpoints.

For instance, we can write:

<template>  
  <div id="app">  
    <b-container>  
      <b-row cols="1" cols-sm="2" cols-md="4" cols-lg="6">  
        <b-col>foo</b-col>  
        <b-col>bar</b-col>  
        <b-col>baz</b-col>  
        <b-col>a</b-col>  
        <b-col>b</b-col>  
      </b-row>  
    </b-container>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We display 1, 2, 4, or 6 columns depending on the breakpoint.

Links

BootstrapVue has the b-link component to let us add links.

For example, we can write:

<template>  
  <div id="app">  
    <b-link href="http://example.com">example</b-link>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

We have the b-link component and the href prop to set the link URL.

The content is displayed in between the tags.

Disabled Link

The disabled prop disables the link.

For example, we can write:

<template>  
  <div id="app">  
    <b-link href="http://example.com" disabled>example</b-link>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Now we have a link that does nothing when we click it.

List Group

The b-list-group component lets us create a list.

For example, we can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item>foo</b-list-group-item>  
      <b-list-group-item>bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Then we display a box with a list that has the texts.

Photo by Keyur Nandaniya on Unsplash

Active List Group Items

We can add the active prop to set an item as the active item.

We can write:

<template>  
  <div id="app">  
    <b-list-group>  
      <b-list-group-item active>foo</b-list-group-item>  
      <b-list-group-item>bar</b-list-group-item>  
    </b-list-group>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

to make the first item active.

Conclusion

We can add a list group. Columns can be spaced out or we can specify the number of columns in a row.