Using PatternFly Elements (web components) in your Vue App

Kyle Buchanan
PatternFly Elements
5 min readJun 18, 2019

To get web components to work with Vue, it’s pretty easy and straightforward. If you’d like to follow along, go ahead and create a new Vue CodeSandbox on codesandbox.io. The Vue sandbox uses the vue-cli to scaffold an app and you can view your changes in real-time right in the web app. With CodeSandbox, you can also add any npm dependency with just a few button clicks. If you want to run this app locally, you can clone the repository on GitHub.

“Using PatternFly Elements in your Vue App” is broken down into four sections

  • Initial setup
  • Adding PatternFly Elements (web components)
  • Interacting with our web components API
  • Adding icing on the cake

Each section will show you exactly what you need to do with code snippets and an accompanying CodeSandbox that you can edit or fork.

Initial setup

First, we need to add the web component polyfills so we can support IE11 and Edge. If we were building this app locally, we’d want to install the web components polyfills from npm.

npm install --save @webcomponents/webcomponentsjs

But if you’re using CodeSandbox, you can just expand the dependencies menu in the Explorer region on the left side of the page, click “Add Dependency”, search for “@webcomponents/webcomponentsjs”, and click on the result of the search. After the dependency is installed, add these two lines at the top of the main.js file in the /src/ directory.

import Vue from "vue";
import App from "./App.vue";
import "@webcomponents/webcomponentsjs/custom-elements-es5-adapter";
import "@webcomponents/webcomponentsjs/webcomponents-loader";

Adding PatternFly Elements (web components)

With the setup complete, let’s add a couple of PatternFly Elements web components to our application to make sure everything is hooked up properly. We’re going to add a card (pfe-card) and a call-to-action button (pfe-cta). Later, we’ll add an accordion (pfe-accordion) and some CSS to help with our layout (pfe-layouts).

Once again, if we were building this app locally, we’d install our dependencies from npm.

npm install --save @patternfly/pfe-card @patternfly/pfe-cta

But if you’re using CodeSandbox, just search for “@patternfly/pfe-card” and “@patternfly/pfe-cta”.

In our HelloWorld.vue file in the /src/components/ directory, let’s add the import statements for our components to the top of the <script> tag in the file.

<script>
import "@patternfly/pfe-card";
import "@patternfly/pfe-cta";
export default {
name: "HelloWorld",
props: {
msg: String
}
};
</script>

Let’s add some simple markup in the template section of theHellowWorld.vue file to see that our pfe-card and pfe-cta are working.

Below is the accompanying CodeSandbox to see that our initial setup is correct and that we’ve successfully added our web components to our app.

Now that we have a card and a call-to-action, let’s add an accordion (pfe-accordion) to our app to spice things up a bit. If we were building this app locally, we’d install our dependencies from npm.

npm install --save @patternfly/pfe-accordion

If you’re using CodeSandbox, just search for “@patternfly/pfe-accordion”.

After installing pfe-accordion, add the markup to the template section in the HelloWorld.vue file.

And we have to import pfe-accordion in the <script> tag inHellowWorld.vue.

<script>
import "@patternfly/pfe-card";
import "@patternfly/pfe-cta";
import "@patternfly/pfe-accordion";
export default {
name: "HelloWorld",
props: {
msg: String
}
};
</script>

Below is the accompanying CodeSandbox for adding pfe-accordion to our app.

Interacting with our web components API

After adding our accordion, let’s say that we’d like to have the first panel of the accordion open up after the page loads. To work with the accordion, there’s just one thing we need to do.

In our HelloWorld component, all we need to do is add the mounted lifecycle callback, use this.$el.querySelector to locate the pfe-accordion, and then call the toggle method on the pfe-accordion DOM API.

<script>
import "@patternfly/pfe-card";
import "@patternfly/pfe-cta";
import "@patternfly/pfe-accordion";
export default {
name: "HelloWorld",
props: {
msg: String
},
mounted: function() {
this.$el.querySelector("pfe-accordion").toggle(0);
}

};
</script>

This should open the first panel of the accordion when the page loads. Below is the accompanying CodeSandbox.

Adding icing on the cake

Right now our app has a single card (pfe-card) with a call-to-action (pfe-cta) link inside it. Beneath that, we have an accordion (pfe-accordion) with the first panel opening after the page loads. Let’s make things look a bit nicer by adding in a few more cards and a grid for layout (pfe-layouts).

We’ll start by installing pfe-styles, which contains pfe-layouts, into our app. If we were building this app locally, we’d install our dependencies from npm.

npm install --save @patternfly/pfe-styles

If you’re using CodeSandbox, search for “@patternfly/pfe-styles”.

Next, in HelloWorld.vue, let’s import the pfe-layouts stylesheet.

<script>
import "@patternfly/pfe-card";
import "@patternfly/pfe-cta";
import "@patternfly/pfe-accordion";
import "@patternfly/pfe-styles/dist/pfe-layouts.min.css";
export default {
name: "HelloWorld",
props: {
msg: String
},
mounted: function() {
this.$el.querySelector("pfe-accordion").toggle(0);
}
};
</script>

Finally, we’ll add the classes we need in our section of cards in the HelloWorld.vue file so we have three cards across on screens wider than or equal to 992px, two cards across on screens wider than or equal to 576px, and one card across on smaller screens.

<div className="pfe-l-grid pfe-m-gutters pfe-m-all-12-col-on-xs pfe-m-all-6-col-on-sm pfe-m-all-4-col-on-lg">
... cards are in here
</div>

If all of the classes above look confusing and don’t make any sense, don’t worry about it. We’ll write a post that explains how to use pfe-layouts. If you’re still curious check out pfe-layouts to getter a better understanding of the classes above.

The end result of adding pfe-layouts and a grid is in the CodeSandbox below.

I realize that may have been a lot. So let’s recap what we did.

  1. Initial Setup: Added the web component polyfills
  2. Adding PatternFly Elements (web components): Added the following web components as dependencies in our app: pfe-card, pfe-cta, and pfe-accordion
  3. Adding PatternFly Elements (web components): Imported the web components into our HelloWorld.vue file
  4. Adding PatternFly Elements (web components): Added the markup for our components in HelloWorld.vue
  5. Interacting with our web components API: Created a reference to the accordion so we could open the first panel after the page loads
  6. Adding icing on the cake: Added pfe-layouts to create a grid for our cards

Wrap up

So there you have it. We’ve added web components to our Vue app and gained the benefits of using portable, pre-made components that can also be used in other frameworks like Angular and React. If your app is written in Angular or React, check out our other two posts: “Using PatternFly Elements in your Angular App” and “Using PatternFly Elements in your React App.”

--

--