Categories
BootstrapVue

BootstrapVue —Customizing Overlays and Pagination

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

We also look at how to add pagination buttons to our page.

Width

We can change the b-overlay to an inline-block component, then we can change the width.

For example, we can write:

<b-overlay class="d-inline-block">
  ...
</b-overlay>

Now we can change the width.

Non-Wrapping Mode

We can add the no-wrap prop to disable rendering of the wrapping and ignore the default slot.

For example, we can write:

<template>
  <div id="app">
    <div>baz</div>
    <b-overlay no-wrap show>
      <b-card title="Card">
        <b-card-text>foo</b-card-text>
      </b-card>
    </b-overlay>
    <div>qux</div>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

This way, everything is behind the overlay instead of only what’s inside the b-overlay component.

Pagination

We can add pagination buttons into our app with the b-pagination component.

For example, we can write:

<template>
  <div id="app">
    <b-pagination v-model="page" :total-rows="rows" :per-page="perPage"></b-pagination>

    <p>Current Page: {{ page }}</p>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      rows: 100,
      perPage: 10,
      page: 1
    };
  }
};
</script>

We have the b-pagination component that takes a few props.

v-model binds the current page number to the page state.

total-rows has the total number of rows of our data.

per-page is how many rows we want to display per page.

Now we get a series of pagination links that we can click.

The page value in the p element will also be updated.

Button Content

The button content can be customized.

For example, we can write:

<template>
  <div id="app">
    <b-pagination
      v-model="page"
      :total-rows="rows"
      :per-page="perPage"
      first-text="First"
      prev-text="Prev"
      next-text="Next"
      last-text="Last"
    ></b-pagination>

    <p>Current Page: {{ page }}</p>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      rows: 100,
      perPage: 10,
      page: 1
    };
  }
};
</script>

We have the first-text prop to set the content of the button to go to the first page.

The prev-text prop is used to set the content of the button to go to the previous page.

The next-text prop is used to set the content of the button to fo to the next page.

And the last-text prop is used to set the content of the button to go to the last page.

If we want to set HTML content for the buttons, we can populate the slots for them.

For example, we can write:

<template>
  <div id="app">
    <b-pagination v-model="page" :total-rows="rows" :per-page="perPage">
      <template v-slot:first-text>
        <span class="text-success">First</span>
      </template>
      <template v-slot:prev-text>
        <span class="text-success">Prev</span>
      </template>
      <template v-slot:next-text>
        <span class="text-success">Next</span>
      </template>
      <template v-slot:last-text>
        <span class="text-success">Last</span>
      </template>
      <template v-slot:ellipsis-text>
        <b-spinner small type="grow"></b-spinner>
        <b-spinner small type="grow"></b-spinner>
      </template>
      <template v-slot:page="{ page, active }">
        <b v-if="active">{{ page }}</b>
        <span v-else>{{ page }}</span>
      </template>
    </b-pagination>

    <p>Current Page: {{ page }}</p>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      rows: 100,
      perPage: 10,
      page: 1
    };
  }
};
</script>

We populated the first-text slot to set the content for the button to go to the first page.

We populated the next-text slot to set the content for the button to go to the next page.

Also, we populated the prev-text slot to set the content for the button to go to the previous page.

We populated the last-text slot to set the content for the button to go to the last page.

The ellipsist-text is populated with the content for the ellipsis.

We replaced the default text with flashing dots.

The page slot is used for populating content for the page number.

We can get the active and page states from it.

active indicates if the page is active.

page is the page number.

Conclusion

We can change the width of the overlay as long we make it an inline-block component.

The pagination lets us create a pagination bar to navigate through pages.

We can populate the pagination items with our own content.

Categories
BootstrapVue

BootstrapVue — Overlays

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

Overlay

We can use the b-overlay component to visually obscure a particular element or component and its content.

It’s available since BootstrapVue 2.7.0.

For example, we can create one by writing:

<template>  
  <div id="app">  
    <b-overlay :show="show">  
      <b-card title="Card">  
        <b-card-text>foo</b-card-text>  
      </b-card>  
    </b-overlay>  
    <b-button @click="show = !show">Toggle overlay</b-button>  
  </div>  
</template>  
<script>  
export default {  
  name: "App",  
  data() {  
    return {  
      show: false  
    };  
  }  
};  
</script>

We have a b-card inside the b-overlay component.

b-overlay takes a show prop to let us set when to show the overlay.

When we click Toggle overlay, then we’ll toggle the overlay on and off as we toggle the show state between true and false .

When the overlay is shown, then we’ll see the card content covered in gray and a spinner in the middle.

Overlay Backdrop Color

We can change the backdrop color of an overlay.

For example, we change the opacity with the opacity prop:

<template>  
  <div id="app">  
    <b-overlay :show="show" opacity="0.5">  
      <b-card title="Card">  
        <b-card-text>foo</b-card-text>  
      </b-card>  
    </b-overlay>  
    <b-button @click="show = !show">Toggle overlay</b-button>  
  </div>  
</template>  
<script>  
export default {  
  name: "App",  
  data() {  
    return {  
      show: false  
    };  
  }  
};  
</script>

We set opacity to 0.5, so we’ll see a translucent overlay.

Also, we can change how the background is blurred with the blur prop:

<template>  
  <div id="app">  
    <b-overlay :show="show" blur="2px">  
      <b-card title="Card">  
        <b-card-text>foo</b-card-text>  
      </b-card>  
    </b-overlay>  
    <b-button @click="show = !show">Toggle overlay</b-button>  
  </div>  
</template>  
<script>  
export default {  
  name: "App",  
  data() {  
    return {  
      show: false  
    };  
  }  
};  
</script>

The value is specified in pixels.

Spinner Styling

We can change the spinner styling with various props.

The spinner-type prop lets us change the spinner type.

spinner-variant lets us change the color for the spinner.

spinner-small makes the spinner smaller if it’s set to true .

We can write:

<template>  
  <div id="app">  
    <b-overlay show spinner-variant="primary" spinner-type="grow" spinner-small>  
      <b-card title="Card">  
        <b-card-text>foo</b-card-text>  
      </b-card>  
    </b-overlay>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Now get a different spinner than the default.

We have a flashing dot.

primary makes the spinner blue.

spinner-small made it small.

Corner Rounding

We can round the corners of the overlay.

To do that, we can set the rounded prop.

The possible values are:

  • true — default styling
  • false — no rounding
  • 'sm' — small rounded corners
  • 'lg' — large rounded corners
  • 'pill' — pill style rounded corners
  • 'circle' — circular or oval rounding
  • 'top' — rounding only the top 2 corners
  • 'bottom' — touring only the bottom 2 corners
  • 'left' — rounding only the 2 left corners
  • 'right' — rounding only the 2 right corners

For example, we can write:

<template>  
  <div id="app">  
    <b-overlay rounded="circle" show>  
      <b-card title="Card">  
        <b-card-text>foo</b-card-text>  
      </b-card>  
    </b-overlay>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Since we set rounded to 'circle' , we’ll see an oval overlay.

Custom Overlay Content

We can customize the overlay’s content.

To do that, we populate the overlay slot.

For example, we write:

<template>  
  <div id="app">  
    <b-overlay show>  
      <b-card title="Card">  
        <b-card-text>foo</b-card-text>  
      </b-card>  
      <template v-slot:overlay>  
        <div>loading</div>  
      </template>  
    </b-overlay>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Then we see ‘loading’ on our overlay instead of a spinner.

Centering Overlay Content

The content of an overlay is centered by default.

To disable that, we can set the no-center prop to true :

<template>  
  <div id="app">  
    <b-overlay no-center show>  
      <b-card title="Card">  
        <b-card-text>foo</b-card-text>  
      </b-card>  
    </b-overlay>  
  </div>  
</template>  
<script>  
export default {  
  name: "App"  
};  
</script>

Now the spinner will be shown on the top left corner.

Conclusion

We can add overlays to obscure the content behind it.

The overlay can be customized to do what we want.

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.