Easily build unique, accessible, and lightweight Vue components
Tags: a11y, javascript, vue, performance
Published: 2019-09-14 15:37:32

---
title: Easily build unique, accessible, and lightweight Vue components
published: true
tags: Accessibility,JavaScript,Vue.js,Performance
canonical_url: https://stegosource.com/unique-accessible-lightweight-vue-components/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/6gshrdt5tir1iwc15alf.jpeg
---

 **tl;dr:** Checkout [Vuetensils](https://vuetensils.stegosource.com/) if you care about building semantic and accessible Vue projects, but prefer to use your own custom styles.

Over the last few years, I have built several web sites and apps with Vue.js. Some of them were built on top of 3rd party libraries such as [Vuetify](https://vuetifyjs.com/en/) or [Bootstrap-vue](https://bootstrap-vue.js.org/), and others were totally custom.

Both approaches offer great benefits, but I take pride in the work that I do, and many projects have that one thing that you walk away with thinking, “I could have done that better.”

Generally speaking, when I use a 3rd party library, I feel guilty for one or more of the following:

- It bloats my bundle size because I have to import more code than I actually use.
- The project ends up looking like any other project that uses the same library.
- I’ve had to customize the project by overwriting styles with some hacky CSS due to specificity issues, and therefore more CSS goes unused.
- The 3rd party library didn’t actually do a great job of writing semantic or accessible components.

On the other hand when I design/code everything myself, my gripes are:

- It would have taken half the time if I just used a library.
- Whenever a new feature requirement comes up that we haven’t already accounted for, I’m going to have to build that by hand as well.
- Getting semantics and accessibility right is hard, and doing it from scratch for each project is a lot of work.
- I don’t know how many times I’ve implemented the same code for a modal

Lately, I’ve been approaching more projects by building them from scratch. With each project, I have a bit more useful code that I can just copy over to the next and just replace the styles. Which lead me to the thought:

> What if there was a Vue library that handled all the semantics and accessibility, but let me use my own custom styles?

## Why accessibility matters

There are already [a plethora of blog posts](https://lmgtfy.com/?q=why+does+web+accessibility+matter) out there on why accessibility matters, so I won’t go into too much detail, but here are the main points I care about.

- **Avoid lawsuits**: For 2019, the average of ADA related lawsuit has reached 8 per day ([source](https://blog.usablenet.com/midyear-ada-web-accessibility-lawsuit-report-blog)).
- **Better business**: There’s actually a surprising number of users with some sort of impairment. If they can’t use your site, you lose clicks, emails, sales, conversions, etc.
- **Better code**: Since focusing on accessibility, I have seen a big improvement in my code. Semantics plays a large role for accessibility, and as a result, I end up with better SEO.
- **Feels great**: Not to mention, I just feel like a better person when I am writing code that is more inclusive. It feels like a disservice to forego accessibility.

All that is good, but there’s one catch: **accessibility is not easy**.

You have to educate yourself on the many [ARIA roles](https://www.w3.org/WAI/PF/aria/roles), account for keyboard navigation, make sure the focus states make sense, and so much more. In fact, it’s also very easy for very talented developers to try implementing accessibility, just to make it even worse than if they did nothing at all.

What’s the solution? Reach for a library that already has these things taken care of (see below).

## Introducing [Vuetensils](https://vuetensils.stegosource.com/)

<g id="3" data-gr-id="3">Vuetensils</g> is a collection of unstyled Vue components that focus on accessibility, semantics, and completely custom design.

It includes components for my UI needs such as alerts, modals, dropdowns, inputs, and more, as well as a few functional components like an image [lazy loader](https://en.wikipedia.org/wiki/Lazy_loading), and a component for awaiting promises, and a wrapper for [intersection observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).

The components follow best practices from the [WAI-ARIA](https://www.w3.org/WAI/standards-guidelines/aria/) and [WCAG](https://www.w3.org/TR/WCAG20/) standards as much as possible, and offer support for features like [keyboard navigation](https://webaim.org/techniques/keyboard/), [focus trapping](https://medium.com/@im_rahul/focus-trapping-looping-b3ee658e5177), and managing [aria roles and attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques).

#### What are “naked” components?

Vuetensils components are almost entirely unstyled. This is on purpose. They are designed to be starting points providing the functional requirements while expecting you to write the CSS (ie. dress them up).

As a result, the components are completely unique to your project, there are no messy CSS overrides, no unused styles that bloat the project, and they handle the accessibility part so you don’t have to.

#### Usage:

To get started, you can install Vuetensils into your project with:

```
npm install vuetensils
```

Import only the components you need for your app.

You can do so globally:

```js
// main.js
import Vue from "vue"
import { VAlert } from "vuetensils"

Vue.component("VAlert", VAlert)
```

Or registered locally:

```html
<script>
  // SomeComponent.vue
  import { VAlert } from "vuetensils"

  export default {
    components: {
      VAlert,
    },
    // ...
  }
</script>
```

Use the components in your template:

```html
<template>
  <div class="some-component">
    <VAlert>Hey, I'm an alert!</VAlert>
  </div>
</template>
```

Bring your own styles:

```css
/* Some CSS file */
.vts-alert {
  border: 1px solid currentColor;
  border-radius: 4px;
  padding: 0 10px;
  color: #900;
  background: #fdd;
}
```
The result is an alert element that has nice ARIA attributes and nice functionality with your own custom styles:

![Example Vuetensils alert with styles](https://thepracticaldev.s3.amazonaws.com/i/kwvesc373hu7m29ze5uo.png)

## Closing remarks

Starting with "naked" components actually opens this library up to a lot of interesting options.

- Build accessible websites or apps (basic use).
- Themes designs for use with Vuetensils.
- Extend components to create your own custom, accessible UI library.

Vuetensils came as the result of scratching my own itch. I want my projects to have totally custom styles, and also be accessible, but I don’t want to build everything from scratch, and I don’t want a bunch of unused code lying around.

**As a result, it’s probably not for everyone.**

You may not have the same needs, and that is totally ok. There is nothing wrong with using a 3rd party library like those I mentioned above. In fact, those are excellent projects. They just serve a different need.

I hope this sort of thing picks up, and people give more focus on making the web accessible for all. Ideally, this makes it easier to get started doing so, and maybe the concept of “bring your own styles” picks up.