Categories
BootstrapVue

BootstrapVue — Badges, Breadcrumbs, and Buttons

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 badges, breadcrumbs, and buttons with BootstrapVue.

Badges

Badges are small tags for adding context to any content.

We can use it by using the b-badge component:

<template>
  <div id="app">
    <h2>Feature
      <b-badge>New</b-badge>
    </h2>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We just add it straight into the template without any props.

There are variations like primary , secondary , etc.

We can add the variant prop to set the variant.

For instance, we can write:

<template>
  <div id="app">
    <b-badge variant="primary">New</b-badge>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Pill badges are badges that are more rounded than usual.

We just add the pill prop:

<template>
  <div id="app">
    <b-badge pill variant="primary">Primary</b-badge>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Breadcrumbs

Breadcrumbs are a navigation widget that shows the hierarchy that leads to the current page.

BootstrapVue has the b-breadcrumb component to let us add breadcrumbs.

For instance, we can write:

<template>
  <div id="app">
    <b-breadcrumb :items="items"></b-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      items: [
        {
          text: "Home",
          href: "#"
        },
        {
          text: "Settings",
          href: "#"
        },
        {
          text: "Library",
          active: true
        }
      ]
    };
  }
};
</script>

We have an items with the text and the URL to go to.

We set that as the value of the items prop and they’ll be displayed in the same order.

We can use the b-breadcrumb-item component to place items manually.

For instance, we can write:

<template>
  <div id="app">
    <b-breadcrumb>
      <b-breadcrumb-item href="#foo">Foo</b-breadcrumb-item>
    </b-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we put the breadcrumbs item inside the breadcrumb manually.

href is the URL for the link.

Buttons

BootstrapVue comes with many varieties of buttons.

To add a Bootstrap button, we can use the b-button component.

We can write:

<template>
  <div id="app">
    <b-button>Button</b-button>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we see a button displayed in a gray color.

We can use the variant prop to change the styles.

For instance, we can write:

<template>
  <div id="app">
    <b-button variant="success">Button</b-button>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we get a green button displayed.

Size

We can change the size of a button with the size prop.

For example, we can write:

<template>
  <div id="app">
    <b-button size="sm">Button</b-button>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

sm means small.

Outline

We can also make buttons with a white fill and a colored outline.

To do that, we prefix the variant value with outline- .

For instance, we can write:

<template>
  <div id="app">
    <b-button variant="outline-primary">Primary</b-button>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we get a button with the blue outline with blue text.

Rounded Button

We can add the pill prop to make buttons more rounded.

For instance, we can write:

<template>
  <div id="app">
    <b-button pill>Button</b-button>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We’ll get a button that’s more round than the default style.

Disabled Button

Also, we can set a button to be disabled with the disabled prop.

For instance, we can write:

<template>
  <div id="app">
    <b-button disabled>Button</b-button>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we get a button that we can’t click on.

Pressed State

We can set the pressed state of the button to make them look pressed or not.

To make them look pressed, we can write:

<template>
  <div id="app">
    <b-button :pressed="true">Button</b-button>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We used the pressed prop and set it to true to make it look pressed.

Likewise, we can set it to false to make them not pressed.

Conclusion

With BootstrapVue, we can add badges and add tag text.

Also, we can add breadcrumbs with a breadcrumb and breadcrumb item components.

BootstrapVue also comes with many varieties of buttons.

Categories
BootstrapVue

Getting Started with BootstrapVue

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 get started with BootstrapVue.

Getting Started

To get started we need Vue 2.6 or later. Bootstrap 4.3.1 is required. Popper.js is required for dropdowns, tooltips, and popovers. jQuery isn’t required.

Installation

To install it, we install the required dependencies by running:

npm install vue bootstrap-vue bootstrap

with NPM or:

yarn add vue bootstrap-vue bootstrap

with Yarn.

Then we can register the plugin by writing:

import Vue from "vue";
import App from "./App.vue";
import { BootstrapVue } from "bootstrap-vue";

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

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

in main.js.

We called Vue.use on BootstrapVue to register the plugin globally.

Next, we add the CSS that’s required by BootstrapVue:

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Then we get:

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";

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

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

Picking Individual Components

If we only want to use individual components, then we can import them and add them to a component ourselves.

For instance, we can write:

<template>
  <div id="app">

</div>
</template>

<script>
import { BModal } from 'bootstrap-vue'

export default {
  name: "App",
  components: {
    'b-modal': BModal
  }
};
</script>

Then we just imported the BModal component into our component.

We can also register a component globally.

For instance, we can write:

import Vue from "vue";
import App from "./App.vue";
import { BModal } from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";

Vue.component("b-modal", BModal);
Vue.config.productionTip = false;

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

Then we only added the BModal component globally.

Alerts

Now, we can use the components in our app.

We can add an alert component by using the b-alert component.

We can write:

<template>
  <div id="app">
    <b-alert show>Alert</b-alert>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we get alert box in our component.

It also comes with various variants.

We can write:

<b-alert variant="success" show>Success</b-alert>

to create a success alert, which is green.

To add a dismissible alert, we can write:

<template>
  <div id="app">
    <b-alert v-model="showAlert" variant="danger" dismissible>Dismissible Alert!</b-alert>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      showAlert: true
    };
  }
};
</script>

We added the v-model directive to use the showAlert state to toggle the modal off.

We set showAlert to true initially so that we can see it.

The dismissible directive makes it dismissible. This means we see an ‘x’ that we can click to dismiss it.

We can create an alert that can be dismissed after some time by writing:

<template>
  <div id="app">
    <b-alert
      :show="dismissCountDown"
      dismissible
      variant="warning"
      @dismissed="dismissCountDown = 0"
      @dismiss-count-down="countDownChanged"
    >
      <p>alert will dismiss {{ dismissCountDown }} seconds...</p>
      <b-progress variant="warning" :max="dismissSecs" :value="dismissCountDown" height="4px"></b-progress>
    </b-alert>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      dismissCountDown: 15,
      dismissSecs: 15
    };
  },
  methods: {
    countDownChanged(e){
      this.dismissCountDown = e;
    }
  }
};
</script>

We added the dismissCountDown state which tracks the time before the alert is dismissed.

Variant 'warning' shows us a yellow alert.

The dismissed handler takes a condition of when the alert will be dismissed.

In this case, when dismissCountDown is 0.

We have the countDownChanged which takes an event parameter with the number of seconds left to set it to dismissCountDown to update the progress bar.

Avatar

We can add an avatar by using the b-avatar component.

We can use it by writing:

<template>
  <div id="app">
    <b-avatar></b-avatar>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we get a generic avatar.

To add initials to the avatar, we can write:

<template>
  <div id="app">
    <b-avatar variant="primary" text="AB"></b-avatar>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We set the variant to primary to show the blue color.

text has the initials that we want to display.

Also, we can set an image by writing:

<template>
  <div id="app">
    <b-avatar variant="info" src="https://placekitten.com/g/100/100"></b-avatar>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

to add an image to the avatar with the src prop.

Conclusion

We can add alerts and avatars easily with BootstrapVue.

It’s also easy to install.

Categories
BootstrapVue

BootstrapVue — Form Basics

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 create basic forms with BootstrapVue.

Form

BootstrapVue has plenty of components for creating forms our way.

To create a form we can use the b-form , b-form-group , and b-form-input components.

For instance, we can create a simple form by writing:

<template>
  <div id="app">
    <b-form @submit="onSubmit" @reset="onReset">
      <b-form-group id="input-group-1" label="name" label-for="input-1" description="your name">
        <b-form-input id="input-1" v-model="form.name" type="text" required placeholder="name"></b-form-input>
      </b-form-group>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App",
  data() {
    return {
      form: {}
    };
  },
  methods: {
    onSubmit() {},
    onReset() {}
  }
};
</script>

We created a simple form with an input box by using those components.

The b-form-input is a text input box that renders the input element.

v-model binds the inputted value to the model.

It also takes the @submit and @reset handlers to allow us to handle submit and reset events triggered on a form.

Inline Form

We can make a form for input inline by adding the inline prop to b-form .

For instance, we can write:

<template>
  <div id="app">
    <b-form inline>
      <label class="sr-only" for="name">Name</label>
      <b-input id="name" placeholder="name"></b-input>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App"
};
</script>

We just add the inline prop to b-form to make our form inline instead of a block element.

We can add multiple form input elements side by side by writing:

`<template>
  <div id="app">
    <b-form inline>
      <label class="sr-only" for="name">name</label>
      <b-input class="mb-2 mr-sm-2 mb-sm-0" id="name" placeholder="name"></b-input>
`
<label class="sr-only" for="email">email</label>
      <b-input-group prepend="@" class="mb-2 mr-sm-2 mb-sm-0">
        <b-input id="email" placeholder="email"></b-input>
      </b-input-group>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App"
};
</script>

Now we have 2 input boxes side by side.

They’re responsive, so their size will adjust according to the viewport width.

Form Text Helper

We can add a b-form-text element to add an explanation for our input field.

For example, we can write:

<template>
  <div id="app">
    <b-form inline>
      <label for="name">name</label>
      <b-input type="text" id="name"></b-input>
      <b-form-text id="name">
        10 or more characters
      </b-form-text>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App"
};
</script>

We have the b-form-text to add a description below the input box.

Feedback Helpers

BootstrapVue provides us with the b-form-invalid-feedback component to display form validation errors.

It also has the b-form-valid-feedback to display a message if the form input value is valid.

For instance, we can display form validation errors by writing:

<template>
  <div id="app">
    <b-form inline>
      <label for="feedback-user">name</label>
      <b-input v-model="name" :state="validation" id="name"></b-input>
      <b-form-invalid-feedback :state="validation">please enter a name</b-form-invalid-feedback>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App",
  data() {
    return {
      name: ""
    };
  },
  computed: {
    validation() {
      return this.name.length > 0;
    }
  }
};
</script>

We have a b-input component with the b-form-invalid-feedback below it.

The state prop takes an expression that indicates the form value validation state.

The b-form-invalid-feedback also takes the same state prop.

Now if we type something, we see a green border and a checkmark.

If we typed nothing, we see a red border and an exclamation mark with the form validation message.

We just have the validation computed property to return the input validation state.

To add a message to show something when we input something valid, we can write:

<template>
  <div id="app">
    <b-form inline>
      <label for="feedback-user">name</label>
      <b-input v-model="name" :state="validation" id="name"></b-input>
      <b-form-invalid-feedback :state="validation">please enter a name</b-form-invalid-feedback>
      <b-form-valid-feedback :state="validation">good job</b-form-valid-feedback>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App",
  data() {
    return {
      name: ""
    };
  },
  computed: {
    validation() {
      return this.name.length > 0;
    }
  }
};
</script>

We added:

<b-form-valid-feedback :state="validation">good job</b-form-valid-feedback>

to display a message when we entered something to the input box.

Conclusion

BootstrapVue provides us with lots of form components to add input boxes and validation indicators.

It can bind input values to model fields automatically.

Categories
BootstrapVue

BootstrapVue — Dropdown Customization and Embedding

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 customize BootstrapVue dropdowns and embed media.

Disabling Flipping

The alignment of a dropdown may change according to its viewport position.

To prevent that from happening, we can add the no-filip prop to our b-dropdown .

Menu Offset

We can add an offset to our dropdown with the offset prop.

For instance, we can write:

<template>
  <div id="app">
    <b-dropdown id="dropdown-offset" offset="25" text="Offset Dropdown" class="m-2">
      <b-dropdown-item href="#">Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we move the dropdown 25 pixels to the right.

Split Button

We can create a menu button that has a button that does something other than opening the dropdown in addition to opening it.

To do that, we add the split prop:

<template>
  <div id="app">
    <b-dropdown split id="dropdown" text="Dropdown" class="m-2">
      <b-dropdown-item href="#">Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Now we only have an arrow on the right that opens the dropdown.

Split Button Link

The part of a split button that doesn’t open the dropdown can open a URL.

We can add the split-href prop to open a URL.

For instance, we can write:

<template>
  <div id="app">
    <b-dropdown split id="dropdown" split-href="#foo" text="Dropdown" class="m-2">
      <b-dropdown-item href="#">Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We added split-href=”#foo” so that we’ll opening the #foo path when we click on the left part.

Split Button Type

Split button can have all the types of a button.

To specify the type, we use the split-button-type prop.

The value can be 'button' , 'submit' or 'reset' .

Sizing

We can change the size of the dropdown button with the size prop.

For instance, we can write:

<template>
  <div id="app">
    <b-dropdown id="dropdown" size="lg" text="Dropdown" class="m-2">
      <b-dropdown-item href="#">Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we get an extra big button.

'sm' will make the button smaller than the default size.

Dropdown Color Variants

We can set the variant prop to change the dropdown button color variant.

For instance, if we have:

<template>
  <div id="app">
    <b-dropdown id="dropdown" variant="primary" text="Dropdown" class="m-2">
      <b-dropdown-item href="#">Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we make it blue.

All the outline and fill variants are available.

Split Button Color Variant

If we have a split button, we can set different variants for the left and right parts.

To set the left part, we use the split-variant prop.

To set the right part, we use the variant prop.

For instance, we can write:

<template>
  <div id="app">
    <b-dropdown
      id="dropdown"
      split
      split-variant="outline-primary"
      variant="primary"
      text="Dropdown"
      class="m-2"
    >
      <b-dropdown-item href="#">Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Now the left part has a blue outline and text.

And the right part has a blue background and a white arrow tip.

Block Level Dropdowns

Dropdowns can be block level.

This means it fills the whole page width.

To make the dropdown a block element, we add the block prop

For instance, we can write:

<template>
  <div id="app">
    <b-dropdown id="dropdown" block text="Dropdown" class="m-2">
      <b-dropdown-item href="#">Action</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Now the button fills the viewport since we added the block prop to b-dropdown .

block can be combined with the split button props to make a block-level split button.

Embedding Items Responsively

BootstrapVue comes with a b-embed component to embed things responsively.

To use it, we can write:

<template>
  <div id="app">
    <b-embed
      type="iframe"
      aspect="16by9"
      src="https://www.youtube.com/embed/wtH-hdOF1uA?rel=0"
      allowfullscreen
    ></b-embed>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We just set the src prop to the YouTube video’s embed URL to embed a YouTube video.

allowfullscreen means that we allow the video to go full screen.

aspect is the aspect ratio, which we set to 16 by 9.

type is the type of object we want to embed. YouTube videos embedded in an iframe, so we set it to 'iframe' .

In addition to iframes, we can also embed video, embed and object elements.

We can also add source elements inside b-embed components to embed what we want.

Conclusion

We can customize our dropdowns with various options.

b-embed can be used to embed items.

Categories
BootstrapVue

BootstrapVue — Accordions and Dropdowns

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 an accordion and a dropdown with BootstrapVue.

Accordions

We can create an accordion with the b-collapse component.

To create one, we’ve to nest it inside a b-card component.

For instance, we can write:

<template>
  <div id="app">
    <b-card no-body class="mb-1">
      <b-card-header header-tag="header" class="p-1" role="tab">
        <b-button block href="#" v-b-toggle.accordion-1 variant="info">accordion 1</b-button>
      </b-card-header>
      <b-collapse id="accordion-1" visible accordion="my-accordion" role="tabpanel">
        <b-card-body>
          <b-card-text>accordion 1 text</b-card-text>
        </b-card-body>
      </b-collapse>
    </b-card>

    <b-card no-body class="mb-1">
      <b-card-header header-tag="header" class="p-1" role="tab">
        <b-button block href="#" v-b-toggle.accordion-2 variant="info">accordion 2</b-button>
      </b-card-header>
      <b-collapse id="accordion-2" accordion="my-accordion" role="tabpanel">
        <b-card-body>
          <b-card-text>accordion 2 text</b-card-text>
        </b-card-body>
      </b-collapse>
    </b-card>

    <b-card no-body class="mb-1">
      <b-card-header header-tag="header" class="p-1" role="tab">
        <b-button block href="#" v-b-toggle.accordion-3 variant="info">accordion 3</b-button>
      </b-card-header>
      <b-collapse id="accordion-3" accordion="my-accordion" role="tabpanel">
        <b-card-body>
          <b-card-text>accordion 3 text</b-card-text>
        </b-card-body>
      </b-collapse>
    </b-card>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We have 3 cards which are combined to create our accordion.

We have the b-card components on the outside.

Then we have the b-collapse component on the inside to create the accordion.

This way, we have the headers always displayed.

But the body is only displayed when we clicked on the heading, which also hides the other cards.

Dropdowns

Dropdowns are toggleable and contextual overlays for displaying list of links and actions.

To create one, we create the b-dropdown component with some b-dropdown-item components inside.

For instance, we can create a simple dropdown by writing:

<template>
  <div id="app">
    <b-dropdown id="dropdown-1" text="menu" class="m-md-2">
      <b-dropdown-item>apple</b-dropdown-item>
      <b-dropdown-divider></b-dropdown-divider>
      <b-dropdown-item active>active</b-dropdown-item>
      <b-dropdown-item disabled>disabled</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We have the b-dropdown component with the text prop, which is set to the text of the drop-down button.

Therefore, we set our dropdown button text to 'menu' since that’s the value we have for text .

Inside it, we have the b-dropdown-item with various props.

The content is between the tags.

active makes a dropdown item active.

disabled makes a dropdown item disabled.

b-dropdown-divider is a divider on our dropdown menu.

Dropdown Button Content

We can customize our content of a drop with a template element to fill the button-content slot.

For instance, we can write:

<template>
  <div id="app">
    <b-dropdown>
      <template v-slot:button-content>
        <b>menu</b>
      </template>
      <b-dropdown-item>apple</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We have the template element which fills the button-content as indicated by v-slot:button-content .

Inside it, we have <b>menu</b> to show bold text.

Below it, we have the usual dropdown items.

Menu Alignment

The menu alignment can change according to our preference.

The default is to left-align the menu.

If we want to right-align the menu, we can add the right prop to the b-dropdown component.

For instance, we can write:

<template>
  <div id="app" style="padding: 100px">
    <b-dropdown right text="right">
      <b-dropdown-item>apple</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

to right-align the menu if there’s enough room on the left edge of the screen.

Otherwise, it’ll go back to left aligning the menu.

Dropup

We can also make the menu stay above the button instead of below it.

We just have to add the dropup prop.

For example, we write:

<template>
  <div id="app" style="padding-top: 100px">
    <b-dropdown dropup text="right">
      <b-dropdown-item>apple</b-dropdown-item>
    </b-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

to do that.

Drop Right or Left

We can add similar props to make the menu show on the right or left instead of above or below the button.

dropright makes it show to the right of the button.

dropleft makes it show on the left of the button.

Conclusion

We can create accordion with cards and the collapse components.

Also, BootstrapVue has menu components we can use.