Update post

This commit is contained in:
Oliver Davies 2018-12-28 01:32:09 +00:00
parent 20e33a2f45
commit 5649751944
3 changed files with 107 additions and 50 deletions

View file

@ -6,16 +6,21 @@ tags:
- tweet
- vuejs
has_tweets: true
draft: true
---
{% block excerpt %}
In the [original post](/blog/rebuilding-bartik-with-vuejs-tailwind-css) I detailed how I built a clone of Drupals Bartik theme with [Vue.js][1] and [Tailwind CSS][2]. This post details some updates that Ive made to it since then.
In [the original post](/blog/rebuilding-bartik-with-vuejs-tailwind-css) I detailed how I built a clone of Drupals Bartik theme with [Vue.js][vuejs] and [Tailwind CSS][tailwind]. This follow-up post details some updates that Ive made to it since then.
{% endblock %}
{% block content %}
## Customising Tailwind
## Customising Tailwinds colours
### Colours
During the first version of the page, my thoughts were to not edit the Tailwind configuration, however I changed my mind on this whilst working on the subsequent updates and did make some changes and customisations to the `tailwind.js` file.
By default, Tailwind includes a full colour palette including colours such as yellows, oranges, reds that werent being used in this page so they were removed. This makes the file more readable as well as reduces the number of classes that Tailwind generates.
Whist I was changing the colours, I also took the opportunity to tweak the values of the remaining colours to more closely match Bartiks original colours.
I also added a `black-60` class which uses [RGBA](https://css-tricks.com/the-power-of-rgba) to provide a semi-transparent background. I used this when adding the [skip to main content link](#adding-the-skip-to-main-content-link).
```js
let colors = {
@ -35,24 +40,19 @@ let colors = {
'blue-dark': '#2779bd',
'blue': '#3490dc',
'blue-light': '#bcdefa',
'green-dark': '#325E1C',
'green': '#77B159',
'green-light': '#CDE2C2',
'green-lighter': '#F3FAEE',
}
```
### Plugins
## Adding default styling for links
```js
plugins: [
require('tailwindcss/plugins/container')({
// center: true,
// padding: '1rem',
}),
require('tailwindcss-skip-link')(),
],
```
In the first version, every link was individually styled which resulted in a lot of duplicate classes and a potential maintenance issue.
## Extracting Tailwind components for link styling
`src/components/Welcome.vue`:
I added a `style` section within `Welcome.vue`, and added some default styling for links based on their location on the page - [extracting some Tailwind components](https://tailwindcss.com/docs/extracting-components).
<div v-pre markdown="1">{% raw %}
```vuejs
@ -80,6 +80,8 @@ plugins: [
</template>
```
Within the `style` section, Im able to use Tailwinds custom `@apply` directive to inject its rules into more traditional CSS, rather than needing to add them onto every link.
```vuejs
<style lang="sass">
#header a
@ -108,48 +110,53 @@ plugins: [
## Extracting a Vue component for Drupal blocks
`src/components/DrupalBlock.vue`:
As well as being able to extract re-usable components within Tailwind, the same can be done within Vue. As the page could potentially have multiple sidebar blocks, I extracted a `SidebarBlock` component which would act as a wrapper around the blocks contents.
```vuejs
// src/components/Sidebar.vue
<template>
<div class="drupal-block">
<slot></slot>
</div>
<div class="bg-grey-lighter p-4 mb-4">
<slot></slot>
</div>
</template>
<style lang="sass" scoped>
.drupal-block
@apply bg-grey-lightest p-4
&:not(:last-child)
@apply mb-4
</style>
```
`src/components/Welcome.vue`:
The component provides the wrapping div and the appropriate classes in a single easy-to-maintain location, and [uses a slot](https://vuejs.org/v2/guide/components-slots.html) as a placeholder for the main content.
```vuejs
<drupal-block>
<h2 class="font-serif font-normal text-base text-grey-darkest border-b border-solid border-grey-light mb-3">Search</h2>
That means that within `Welcome.vue`, the markup within the `sidebar-block` tags will be used as the block contents.
<div>
<form action="#" class="flex">
<input type="text" class="border border-solid border-grey p-2 w-full xl:w-auto">
<button type="submit" class="bg-grey-lighter px-3 rounded-full border-b border-solid border-grey-dark ml-2 flex-none">
<img src="img/loupe.svg" class="block">
</button>
</form>
</div>
</drupal-block>
```html
<sidebar-block>
<p>My block contents.</p>
</sidebar-block>
```
## Adding the Skip to Main Content Link
One thing [that was missing](https://github.com/opdavies/rebuilding-bartik/issues/1) was the 'Skip to main content link'. This an accessibility feature that allows for users who are navigating the page using only a keyboard to bypass the navigation links and skip straight to the main content if they wish by clicking a link that is hidden and only visible whilst its focussed on.
Here is the markup that I used, which is placed directly after the opening `<body>` tag.
```html
<a href="#0" class="skip-link text-white bg-black-60 py-1 px-2 rounded-b-lg focus:no-underline focus:outline-none">Skip to main content</a>
<a href="#0" class="skip-link text-white bg-black-60 py-1 px-2 rounded-b-lg focus:no-underline focus:outline-none">
Skip to main content
</a>
```
I initially tried to implement the same feature on this website using [Tailwinds visually hidden plugin](https://www.npmjs.com/package/tailwindcss-visuallyhidden) which also contains a `focussable` class, though I wasnt able to style it the way that I needed. I created my own [Tailwind skip link plugin](https://www.npmjs.com/package/tailwindcss-skip-link) and moved the re-usable styling there.
To enable the plugin, I needed to add it within the `plugins` section of my `tailwind.js` file:
```js
plugins: [
require('tailwindcss/plugins/container')(),
require('tailwindcss-skip-link')(),
],
```
I added only the page-specific styling classes to the link (as well as the `skip-link` class that the plugin requires) as well as my own focus state to the skip link that I did within the `style` section of `App.vue`.
```vuejs
<style lang="sass">
@tailwind preflight
@ -162,11 +169,61 @@ plugins: [
@tailwind utilities
</style>
```
![The Bartik clone with the skip to main content link visible](/images/blog/rebuilding-bartik-vue-tailwind-part-2/skip-link.png){.border}
## Adding the DrupalMessage component
I also added a version of Drupals status message. For readability, this is contained within its own component, `DrupalMessage.vue`, though it contains only a static template and has no `script` or any of its own data.
```html
<template>
<div class="bg-green pl-2 rounded-sm">
<div class="py-4 pl-3 pr-4 mb-4 border flex items-center border-green-light text-green-dark bg-green-lighter rounded-sm">
<svg class="fill-current w-4 h-4 text-green mr-2" xmlns="http://www.w3.org/2000/svg"><path d="M6.464 13.676a.502.502 0 0 1-.707 0L.797 8.721a.502.502 0 0 1 0-.707l1.405-1.407a.5.5 0 0 1 .707 0l2.849 2.848a.504.504 0 0 0 .707 0l6.629-6.626a.502.502 0 0 1 .707 0l1.404 1.404a.504.504 0 0 1 0 .707l-8.741 8.736z"/></svg>
<p>
A Bartik clone, built with
<a href="https://vuejs.org" class="text-blue-dark hover:text-blue no-underline border-b border-dotted hover:border-solid border-blue-dark">Vue.js</a>
and <a href="https://tailwindcss.com" class="text-blue-dark hover:text-blue no-underline border-b border-dotted hover:border-solid border-blue-dark">Tailwind CSS</a>.
</p>
</div>
</div>
</template>
```
I did need to make one change to the `tailwind.js` file - within `modules` I needed to enable the `borderStyle` module for hover and focus states in order for Tailwind to generate the additional classes.
```js
modules: {
// ...
borderStyle: ['responsive', 'hover', 'focus'],
// ...
}
```
The message is included within the Welcome component by including the `<drupal-message />` element, though rather than importing it there, its registed as a global component so it would be available to any other components that could be added in the future.
This is done within `main.js`:
```js
// ...
import DrupalMessage from '@/components/DrupalMessage.vue'
Vue.component('DrupalMessage', DrupalMessage)
new Vue({
render: h => h(App),
}).$mount('#app')
```
![The Bartik clone with the Drupal Message component visible](/images/blog/rebuilding-bartik-vue-tailwind-part-2/drupal-message.png){.border}
__The updated version is [live on Netlify][netlify], and the [latest source code is available on GitHub][github].__
{% endblock %}
[0]: https://www.drupal.org
[1]: https://vuejs.org
[2]: https://tailwindcss.com
[3]: https://github.com/opdavies/rebuilding-bartik
[4]: https://rebuilding-bartik.netlify.com
[5]: https://www.drupal.org/project/tailwindcss
[github]: https://github.com/opdavies/rebuilding-bartik
[netlify]: https://rebuilding-bartik.netlify.com
[tailwind]: https://tailwindcss.com
[vuejs]: https://vuejs.org

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB