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.

Categories
BootstrapVue

BootstrapVue — Carousel and Collapse

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 a carousel and collapse components into a Vue app.

Carousel

A carousel is a slide show for cycling through a series of content.

It can include image, text, or custom markup.

BootstrapVue’s carousel has support for previous/next controls and indicators.

We can add one by using the b-carousel component.

To use it, we can write:

<template>
  <div id="app">
    <b-carousel
      v-model="slide"
      :interval="5000"
      controls
      indicators
      background="#ababab"
      img-width="1024"
      img-height="480"
      @sliding-start="onSlideStart"
      @sliding-end="onSlideEnd"
    >
      <b-carousel-slide caption="cat" text="cat" img-src="https://placekitten.com/g/600/200"></b-carousel-slide>

<b-carousel-slide img-src="https://picsum.photos/1024/480/?image=54">
        <h1>hello</h1>
      </b-carousel-slide>
    </b-carousel>

<p class="mt-4">
      Slide #: {{ slide }}
      <br>
      Sliding: {{ sliding }}
    </p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      slide: 0,
      sliding: null
    };
  },
  methods: {
    onSlideStart(slide) {
      this.sliding = true;
    },
    onSlideEnd(slide) {
      this.sliding = false;
    }
  }
};
</script>

We have a carousel in the component above.

interval indicates that we cycle through the code every 5 seconds.

slide is the index of the slide we’re on.

controls indicates that we show the controls

indicators indicates that we show the dashes to show which slide we’re on.

background is the background color if it’s shown.

img-width is the slide image width in pixels.

img-height is the slide image height in pixels.

sliding-start is the handler that runs when the slide starts sliding.

sliding-end is the handler that runs when the slide finishes sliding.

The handlers have the slide parameter to get the slide index.

v-model is set to slide to set the slide state with the value being the slide index.

Crossfade Animation

We can add crossfade animation with the fade prop on the b-carousel component.

Disabling Animation

To disable animation, we can use the no-animation prop on the b-carousel component.

Slide Wrapping

The no-wrap prop added to b-carousel will disable the carousel wrapping back to the first slide.

Collapse

The collapse component is a box with content that can be toggled on and off.

To use it we use the v-b-toggle directive with the b-collapse component.

For instance, we can write:

<template>
  <div id="app">
    <b-button v-b-toggle.collapse-1 variant="primary">toggle</b-button>
    <b-collapse id="collapse-1" class="mt-2">
      <b-card>
        <p class="card-text">collapse content</p>
      </b-card>
    </b-collapse>
  </div>
</template>

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

We have a b-button that has the v-b-toggle directive and with the collapse-1 modifier to indicate that it’s toggling the b-collapse component with the ID collapse-1 on and off.

Then we can see the b-collapse component being toggled.

We can nest collapse component inside another component by adding b-collapse and a component with the v-b-toggle .

For instance, we can write:

<template>
  <div id="app">
    <b-button v-b-toggle.collapse-1 variant="primary">toggle</b-button>
    <b-collapse id="collapse-1" class="mt-2">
      <b-card>
        <p class="card-text">collapse content</p>
        <b-button v-b-toggle.collapse-1-inner size="sm">toggle inner</b-button>
        <b-collapse id="collapse-1-inner" class="mt-2">
          <b-card>inner content</b-card>
        </b-collapse>
      </b-card>
    </b-collapse>
  </div>
</template>

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

We have:

<b-button v-b-toggle.collapse-1-inner size="sm">toggle inner</b-button>
<b-collapse id="collapse-1-inner" class="mt-2">
  <b-card>inner content</b-card>
</b-collapse>

which is the inner collapse component.

We can toggle it on and off the same way.

We just have to make sure that we don’t use the same IDs as the outer one.

v-model

We can get the collapsed state of the collapse component by binding a state with v-model .

For instance, we can write:

<template>
  <div id="app">
    <b-button v-b-toggle.collapse-1 variant="primary">toggle</b-button>
    <b-collapse id="collapse-1" class="mt-2" v-model="visible">
      <b-card>
        <p class="card-text">collapse content</p>
      </b-card>
    </b-collapse>
    <p>{{visible}}</p>
  </div>
</template>

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

We added v-model to bind to the visible state so we can set the visibility state as the value of that property.

Then visible will show true if the collapse is expanded and false otherwise.

Conclusion

We can create a carousel with BoostrapVue. The slide options and controls can be changed.

Collapse is a component that we can toggle on and off.

Categories
BootstrapVue

BootstrapVue — Cards

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 cards to our Vue app.

Cards

In Bootstrap, cards are a flexible and extensible content container.

It can gold header, footer, and content.

The background colors can also be changed.

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

For instance, we can write:

<template>
  <div id="app">
    <b-card
      title="Card Title"
      img-src="https://placekitten.com/g/600/200"
      img-alt="cat"
      img-top
      tag="article"
      style="max-width: 20rem;"
      class="mb-2"
    >
      <b-card-text>a cat</b-card-text>

<b-button href="#" variant="primary">Go</b-button>
    </b-card>
  </div>
</template>

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

We have the b-card component with a few props.

The img-src is the URL for an image.

img-alt is the alt attribute for the image.

style has the styles for the card.

class has the class for the card.

b-card-text has the content for the card.

title has the title for the card.

img-top indicates that the card image is at the top of the card.

We also have a button with the link.

We can also place thre image at the bottom with image-bottom .

To add a reader, we can use the header prop.

Likewise, we can add the footer prop for the footer.

We can add a header and footer by writing the following:

<template>
  <div id="app">
    <b-card
      header="header"
      header-tag="header"
      footer="footer"
      footer-tag="footer"
      title="title"
    >
      <b-card-text>Header and footers</b-card-text>
      <b-button href="#" variant="primary">Go</b-button>
    </b-card>
  </div>
</template>

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

We just add the props the place we expect them to be added in the b-card component to add headers and footers.

Horizontal Layout

We don’t have to stick with a vertical layout.

We can also create a card with a horizontal layout with b-row and b-col for layout.

For instance, we can write:

<template>
  <div id="app">
    <b-card no-body class="overflow-hidden">
      <b-row no-gutters>
        <b-col md="6">
          <b-card-img src="https://placekitten.com/200/50" alt="cat"></b-card-img>
        </b-col>
        <b-col md="6">
          <b-card-body title="Horizontal Card">
            <b-card-text>card</b-card-text>
          </b-card-body>
        </b-col>
      </b-row>
    </b-card>
  </div>
</template>

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

We have b-col to divide up the card into 2 halves.

md with value 6 means that we have a left column and a right column.

no-body means that we don’t let BootstrapVue add the card body automatically.

Instead, we used b-card-body to place the body of the card.

b-row makes sure that the b-col components stay in the row.

Text Variants

We can change the text string with the bg-variant prop.

For instance, we can write:

<template>
  <div id="app">
    <b-card bg-variant="dark" text-variant="white" title="title">
      <b-card-text>more content</b-card-text>
      <b-button href="#" variant="primary">Go</b-button>
    </b-card>
  </div>
</template>

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

We set bg-variant to 'dark' to make the background dark gray.

title is still the card title.

text-variant is the styling for the card text, which we change to white.

Other variants include primary, secondary, success, info, warning, danger, light, and dark.

Bordered

We can add borders to our cards.

To do that, we use the border-variant prop.

For instance, we can write:

<template>
  <div id="app">
    <b-card border-variant="warning" header="header" header-border-variant="warning" align="center">
      <b-card-text>lorem ipsum.</b-card-text>
    </b-card>
  </div>
</template>

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

We have the header prop to set the header text.

header-border-variant to set the border style of the header.

border-variant is used to set the border style of the body.

Header and Footer variants

Headers and footers have their own variants.

We can set them with the header-bg-variant and footer-bg-variant respectively.

Those will set the background color.

There’s also the header-border-variant and footer-border-variant props to set the border styles of the header and footer.

Nav Integration

We can add navbar integration into a card by using the b-nav component.

To add a navbar to a card, we can write:

<template>
  <div id="app">
  <b-card title="Ctitle" body-class="text-center" header-tag="nav">
    <template v-slot:header>
      <b-nav card-header tabs>
        <b-nav-item active>active</b-nav-item>
        <b-nav-item>inactive</b-nav-item>
        <b-nav-item disabled>disabled</b-nav-item>
      </b-nav>
    </template>

    <b-card-text>
      lorem ipsum.
    </b-card-text>

<b-button variant="primary">go</b-button>
  </b-card>
  </div>
</template>

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

We just put the b-nav component in the template tag to add the navbar.

It’s put into the header slot as indicated by the v-slot:header directive.

We have the active prop to set a link to be active.

If there’s no active prop set then it’s inactive.

If there’s a disabled prop, then it’s disabled.

Conclusion

There are many things that we can do with cards.

We can style it, change the layout, and add a navbar to it.