🔥 Vue Tips #30: Reusability Fundamentals: The Configuration Pattern
Tags: vue, webdev, javascript, tutorial
Published: 2021-10-19 15:26:53

---
title: "🔥 Vue Tips #30: Reusability Fundamentals: The Configuration Pattern"
published: true
description: "Accessing parent components, easily creating component variations, structuring a Vue project, and automating chaos."
canonical_url: https://michaelnthiessen.com/weekly-030-october-13/
tags: vue, webdev, javascript, tutorial
series: Vue Tips
---

_This newsletter was sent out to my list on October 13, 2021. [Sign up here](https://michaelnthiessen.com/newsletter) to get emails like this each week!_

Hey there,

I'm still trying to adjust to the darkness this time of year in Canada.

It's dark when I start work at 8, and dark again when I finish around 5. I work in my basement, so I don't get a lot of light during the day either.

So I'm trying to take some time during the day to get outside and get some sunlight (and fresh air).

Do you change your working habits throughout the year? A different schedule during winter or summer?

— Michael

## 🔥 How I deal with dynamic classes

A pattern I use constantly is triggering classes with `boolean` flags:
```html
<template>
  <div :class="disabled && 'disabled-component'">
    Sometimes this component is disabled. Other times it isn't.
  </div>
</template>
```
```css
/* Some styles */
.disabled-component {
  background-color: gray;
  color: darkgray;
  cursor: not-allowed;
}
```

Either the trigger is a prop I use directly, or a computed prop that tests for a specific condition:
```js
disabled() {
  return this.isDisabled || this.isLoading;
}
```

If I just need one class on an element, I use the logical AND to trigger it:
```html
<div :class="disabled && 'disabled-component'"></div>
```

Sometimes it's a decision between two classes, so I'll use a ternary:
```html
<div
  :class="disabled ? 'disabled-component' : 'not-yet-disabled'"
/>
```

I don't often use more than two classes like this, but that's where an `Object` or `Array` comes in handy:
```html
<div
  :class="{
    primary: isPrimary,
    secondary: isSecondary,
    tertiary: isTertiary,
  }"
/>
```
```html
<div
  :class="[
    isPrimary && 'primary',
    isSecondary && 'secondary',
    isTertiary && 'tertiary',
  ]"
/>
```

Of course, when it gets complex enough it's better to just have a computed prop that returns a string of class names (or returns an `Object` or `Array`):
```html
<div :class="computedClasses" />
```


## 🔥 Reusability Fundamentals: The Configuration Pattern

So you've got an awesome `CodeBlock` component that does syntax highlighting, and even shows line numbers:

```html
<CodeBlock language="js">
  const myMessage = 'Highlighting code is supa ez';
</CodeBlock>
```

But now you need to support a second color theme.

Instead of copy and pasting (which is sometimes the right solution!), we can use props to help us create variations:
```html
<!-- Uhhh, maybe not the best solution -->
<DarkModeCodeBlock language="js">
  const myMessage = 'Highlighting code is supa ez';
</DarkModeCodeBlock>
```
```html
<!-- This is what props were meant for -->
<CodeBlock
  language="js"
  theme="darkMode"
>
  const myMessage = 'Highlighting code is supa ez';
</CodeBlock>
```

You already do this intuitively, so this may not be a huge revelation.

But **the Configuration pattern is a fundamental pattern** — you can't ignore it if you want to master reusability.

Dealing with prop explosions and understanding the Base Component Pattern are also part of mastering Configuration, the second level of reusability.

And the other, more interesting levels of reusability?

Well, mastering Configuration is vital to unlocking them. All the other levels build on top of this one.


## 📜 Using Vue for a VS Code extension?

This is a very interesting article on creating your own VS Code extension...

But you get to build the UI using Vue.

It goes into all the steps necessary to set up and get your extension running. From there, the sky is the limit (unless you're building rockets 🚀).

Read it now: [Building a VS Code Extension Using Vue.js](https://www.codemag.com/article/2107071)

## 🗞 Nuxt 3! Yeah, it's here!

It was officially released yesterday, and comes with some amazing stuff:
- Vue 3
- Vite
- Typescript
- and so many other features

[Check it out here](https://nuxtjs.org/v3).

Also, don't forget that [Vue.js London](https://vuejs.london/) is happening next week!

## 💬 Repeated failure

**_As a rule, software systems do not work well until they have been used, and have failed repeatedly, in real applications. — Dave Parnas_**

I think Dave is talking about edge cases.

You _think_ you've covered them all, but you never have. Not until real users start using it for real.

## 🧠 Spaced-repetition: Reducing Objects

__The best way to commit something to long-term memory is to periodically review it, gradually increasing the time between reviews 👨‍🔬__

__Actually remembering these tips is much more useful than just a quick distraction, so here's a tip from a couple weeks ago to jog your memory.__

The `reduce` function is really great for converting arrays into objects, but it can be intimidating.

If we have a bunch of items that all have an `id`:
```js
{
  id,
  //...
}
```

We can reorganize the array into an object, where the key for each item is the item's `id`:
```js
const obj = arr.reduce((prev, next) => {
  // Grab the id from the item
  const { id } = next;

  // Add the item to our object
  prev[id] = next;

  // Return the object so we can add more items
  return prev;
}, {});
```

You get an object that looks like this:
```js
{
  'id1': { id: 'id1', ... },
  'id2': { id: 'id2', ... },
  'id3': { id: 'id3', ... },
}
```

If you want to group all objects in an array by a specific property, you can do something very similar:
```js
const obj = arr.reduce((prev, next) => {
  // Grab the property from the item that we want to group by
  const { prop } = next;

  // Add a new array to the object if this is the first one
  // with this value
  if (prev[prop] === undefined) {
    prev[prop] = [];
  }

  // Add our item to this array
  prev[prop].push(next);

  // Return the object so we can add more items
  return prev;
}, {});
```

Our final object looks like this:
```js
{
  'prop1': [
    { prop: 'prop1', ... },
    { prop: 'prop1', ... },
    { prop: 'prop1', ... },
  ],
  'prop2': [
    { prop: 'prop2', ... },
    { prop: 'prop2', ... },
    { prop: 'prop2', ... },
  ],
}
```

## Exclusive tips and insights every week

Join 8335 other Vue devs and [get exclusive tips and insights](michaelnthiessen.com/newsletter) like these delivered straight to your inbox, every week.

> You have great content in your emails. I seriously learn something from every one of them. — Titus Decali

> Thanks for another beautiful tip 🙏 — Victor Onuoha

> Loving these, and the spaced repetition — Mark Goldstein

[Sign up here](michaelnthiessen.com/newsletter)