This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
oliverdavies.uk-old-sculpin/source/_posts/2018-12-27-rebuilding-bartik-with-vuejs-tailwind-css-part-2.md

251 lines
8.9 KiB
Markdown
Raw Normal View History

2018-12-11 00:10:17 +00:00
---
title: Rebuilding Bartik (Drupals Default Theme) with Vue.js and Tailwind CSS - part 2
2018-12-29 02:13:05 +00:00
meta:
description: How I rebuilt Drupals Bartik theme using Vue.js and Tailwind CSS.
image: ~
2018-12-11 00:10:17 +00:00
tags:
- drupal
- tailwind-css
- tweet
- vuejs
has_tweets: true
---
{% block excerpt %}
2018-12-28 11:46:37 +00:00
In [the original post](/blog/rebuilding-bartik-with-vuejs-tailwind-css) I detailed how I built [a clone of Drupals Bartik theme][netlify] with [Vue.js][vuejs] and [Tailwind CSS][tailwind]. This follow-up post details some updates that Ive made to it since then.
2018-12-11 00:10:17 +00:00
{% endblock %}
{% block content %}
2018-12-28 01:32:09 +00:00
## Customising Tailwinds colours
2018-12-11 00:10:17 +00:00
2018-12-28 01:32:09 +00:00
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).
2018-12-26 15:12:09 +00:00
2018-12-11 00:10:17 +00:00
```js
let colors = {
'transparent': 'transparent',
'black': '#22292f',
'grey-darkest': '#3d4852',
'grey-dark': '#8795a1',
'grey': '#b8c2cc',
'grey-light': '#dae1e7',
'grey-lighter': '#f0f0f0',
'grey-lightest': '#F6F6F2',
'white': '#ffffff',
2018-12-26 15:12:09 +00:00
'black-60': 'rgba(0, 0, 0, .6)',
2018-12-11 00:10:17 +00:00
'blue-dark': '#2779bd',
'blue': '#3490dc',
'blue-light': '#bcdefa',
2018-12-28 01:32:09 +00:00
'green-dark': '#325E1C',
'green': '#77B159',
'green-light': '#CDE2C2',
'green-lighter': '#F3FAEE',
2018-12-11 00:10:17 +00:00
}
```
2018-12-28 01:32:09 +00:00
## Adding default styling for links
2018-12-26 15:12:09 +00:00
2018-12-28 01:32:09 +00:00
In the first version, every link was individually styled which resulted in a lot of duplicate classes and a potential maintenance issue.
2018-12-11 00:10:17 +00:00
2018-12-28 01:32:09 +00:00
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).
2018-12-11 00:10:17 +00:00
<div v-pre markdown="1">{% raw %}
```vuejs
<template>
...
<div id="footer" class="text-xs text-white">
<div class="container mx-auto px-4 pt-16 pb-4">
<div class="border-t border-solid border-grey-darkest pt-6 -mb-6">
<div class="mb-6">
<p><a href="#0">Contact</a></p>
</div>
<div class="mb-6">
<p>
A clone of <a href="https://www.drupal.org">Drupal</a>s default theme (Bartik).
Built by <a href="https://www.oliverdavies.uk">Oliver Davies</a>
using <a href="https://vuejs.org">Vue.js</a>
and <a href="https://tailwindcss.com">Tailwind CSS</a>.
</p>
</div>
</div>
</div>
</div>
</template>
```
2018-12-28 01:32:09 +00:00
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.
2018-12-11 00:10:17 +00:00
```vuejs
<style lang="sass">
#header a
@apply text-white no-underline
&:hover,
&:focus
@apply underline
#main a
@apply text-blue-dark no-underline border-b border-blue border-dotted
&:hover,
&:focus
@apply text-blue border-solid
#footer a
@apply text-white no-underline border-b border-dotted border-white
&:hover,
&:focus
@apply border-none
</style>
```
{% endraw %}</div>
## Extracting a Vue component for Drupal blocks
2018-12-28 01:32:09 +00:00
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.
2018-12-11 00:10:17 +00:00
```vuejs
2018-12-28 01:32:09 +00:00
// src/components/Sidebar.vue
2018-12-11 00:10:17 +00:00
<template>
2018-12-28 01:32:09 +00:00
<div class="bg-grey-lighter p-4 mb-4">
<slot></slot>
</div>
2018-12-11 00:10:17 +00:00
</template>
2018-12-28 01:32:09 +00:00
```
2018-12-11 00:10:17 +00:00
2018-12-28 01:32:09 +00:00
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.
2018-12-11 00:10:17 +00:00
2018-12-28 01:32:09 +00:00
That means that within `Welcome.vue`, the markup within the `sidebar-block` tags will be used as the block contents.
```html
<sidebar-block>
<p>My block contents.</p>
</sidebar-block>
2018-12-11 00:10:17 +00:00
```
2018-12-28 01:32:09 +00:00
## Adding the Skip to Main Content Link
2018-12-11 00:10:17 +00:00
2018-12-28 01:32:09 +00:00
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.
2018-12-11 00:10:17 +00:00
2018-12-28 01:32:09 +00:00
Here is the markup that I used, which is placed directly after the opening `<body>` tag.
2018-12-11 00:10:17 +00:00
2018-12-28 01:32:09 +00:00
```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>
2018-12-11 00:10:17 +00:00
```
2018-12-26 15:12:09 +00:00
2018-12-28 01:32:09 +00:00
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.
2018-12-26 15:12:09 +00:00
2018-12-28 01:32:09 +00:00
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')(),
],
2018-12-26 15:12:09 +00:00
```
2018-12-28 01:32:09 +00:00
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`.
2018-12-26 15:12:09 +00:00
```vuejs
<style lang="sass">
@tailwind preflight
@tailwind components
.skip-link:focus
left: 50%
transform: translateX(-50%)
@tailwind utilities
</style>
```
2018-12-28 01:32:09 +00:00
![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
2018-12-28 19:06:34 +00:00
I also added a version of Drupals status message as another Vue component. This also uses a slot to include the message contents and accepts a [prop](https://vuejs.org/v2/guide/components-props.html) for the message type.
2018-12-28 01:32:09 +00:00
```html
<template>
2018-12-28 19:06:34 +00:00
<div :class="[ wrapperClasses, wrapperClasses ? 'pl-2 rounded-sm' : '' ]">
<div class="py-4 pl-3 pr-4 mb-4 border flex items-center rounded-sm" :class="classes">
<svg v-if="type == 'status'" class="fill-current w-4 h-4 text-green mr-3" 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>
<slot></slot>
2018-12-28 01:32:09 +00:00
</div>
</div>
</template>
```
2018-12-28 19:06:34 +00:00
The value of the `type` prop is then used within some computed properties to determine the type specific classes to add (e.g. green for success, and red for warning), as well as whether or not to include the checkmark SVG image.
```js
<script>
export default {
props: {
type: String,
},
computed: {
classes: function () {
return {
status: 'border-green-light text-green-dark bg-green-lighter',
}[this.type]
},
wrapperClasses: function () {
return {
status: 'bg-green',
}[this.type]
},
}
}
</script>
```
I did need to make one change to the `tailwind.js` file in order to change the border on links when they are hovered over - within `modules` I needed to enable the `borderStyle` module for hover and focus states in order for Tailwind to generate the additional classes.
2018-12-28 01:32:09 +00:00
```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
// ...
2018-12-28 11:33:04 +00:00
Vue.component('drupal-message', require('@/components/DrupalMessage').default)
2018-12-28 01:32:09 +00:00
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].__
2018-12-11 00:10:17 +00:00
{% endblock %}
2018-12-28 01:32:09 +00:00
[github]: https://github.com/opdavies/rebuilding-bartik
[netlify]: https://rebuilding-bartik.netlify.com
[tailwind]: https://tailwindcss.com
[vuejs]: https://vuejs.org