Rebuilding Bartik (Drupal's Default Theme) with Vue.js and Tailwind CSS

Earlier this week, I built a clone of Drupal's default theme, Bartik, with Vue.js and Tailwind CSS. You can view the code on GitHub and the site itself on Netlify.

I built a clone of Bartik, #Drupal's default theme, with @vuejs and @tailwindcss. See the result at https://t.co/nPsTt2cawL, and the code at https://t.co/Dn8eysV4gf.

Blog post coming soon... pic.twitter.com/7BgqjmkCX0

— Oliver Davies (@opdavies) November 20, 2018

Why build a Bartik clone?

I'm a big fan of utility based styling and Tailwind CSS in particular, I was and originally thinking of a way to more easily integrate Tailwind within Drupal - something like I've since done with the Tailwind CSS starter kit theme. Whilst thinking about that, I wondered about doing the opposite - rebuilding Drupal (or Bartik) with Tailwind.

Others including Adam Wathan (one of the creators of Tailwind CSS) have rebuilt existing UIs like Netlify, YouTube, Twitter, Coinbase and Transistor.fm with Tailwind as an opportunity for learning and also to demonstrate using Tailwind - this was my opportunity to do the same.

Whilst Drupal itself has adopted React, I've personally been looking into Vue.js and have used it for some small personal projects, including some elements of the site. So I decided to use Vue for the interactive parts of my Bartik clone to create a fully functional clone rather than focussing only on the CSS.

Building a static template with Tailwind

The first stage was to build the desktop version, which was done as a simple HTML file with Tailwind CSS pulled in from it's CDN. This stage took just over an hour to complete.

As Tailwind was added via a CDN, there was no opportunity to customise it's configuration, so I needed to use to Tailwind's default configuration for colours, padding, spacing, fonts, breakpoints etc. The page is built entirely with classes provided by Tailwind and uses no custom CSS, except for one inline style that is used to add the background colour for the Search block, as there wasn't a suitable Tailwind option.

When I decided that I was going to later add some interactivity onto the mobile navigation menu, the existing code was ported into a new Vue.js application generated by the Vue CLI, with the majority of the markup within a Welcome component. This meant that Tailwind was also added as a dependency with it's own configuration file, though although I had the opportunity to customise it I decided not to and made no changes to it and continued with the default values.

src/App.vue:



<Welcome title="Rebuilding Bartik"/>




import Welcome from './components/Welcome.vue'

export default {
name: 'app',
components: {
Welcome,
}
}



@tailwind preflight;
@tailwind components;
@tailwind utilities;


src/components/Welcome.vue:















Log in




Welcome to

No front page content has been created yet.

Follow the User Guide to start building your site.

Search

Contact

A clone of Drupal's default theme (Bartik). Built by Oliver Davies using Vue.js and Tailwind CSS.





import MainMenu from './MainMenu.vue';

export default {
components: {
MainMenu,
},

props: {
title: {
type: String,
required: true
}
}
}


````


## Making it responsive

The second stage began with making the existing desktop version responsive - particularly making the navigation menu behave and appear differently on mobile and tablet screens, and stacking the main content area and the sidebar on mobile screens. This was all achieved using Tailwind's responsive variants.

```html

...

````

In this example, the `pb-4` class adds 1rem of bottom padding to the element by
default, then increases it to 3rem at large screen sizes due to the `lg:pb-12`
class.

## Adding interactivity

This is how the main navigation menu works on mobile:

![The main navigation menu on mobile.](/images/blog/rebuilding-bartik-vue-tailwind/rebuilt-mobile.png)

The show and hide text appears next to a hamburger menu, and clicking it toggles
the visibility of the menu links which are stacked below, as well as the wording
of the text itself.

The code for this was moved into a separate `MainMenu` component, which means
that it was easier to have dedicated data properties for whether the menu was
open or not, as well as computed properties for building the show/hide text. The
`open` value can then be used to apply the appropriate classes to the main menu
to toggle it.

I also moved the links into `data` too - each link is it's own object with it's
`title` and `href` values. This means that I can use a `v-for` directive to loop
over the data items and inject dynamic values, removing the duplication of
markup which makes the component easier to read and maintain.

`src/components/MainMenu.vue`:



```vue-html


<button
type="button"
class="block p-3 w-full text-sm text-left text-gray-800 sm:hidden focus:outline-none bg-blue-light"
@click="open = !open"
>


- Main navigation










<div
class="inline-block px-1 mb-1 w-full sm:pl-0 sm:w-1/3 md:mb-0 md:w-auto"
:key="link.title"
v-for="(link, index) in links"
>
<a
class="block py-2 px-3 text-sm text-black no-underline rounded-lg sm:text-center md:rounded-none md:rounded-t-lg"
:class="[ index == ? activeTab'bg-white' : 'bg-blue-light hover:bg-white' ]"
:href="link.href"
>










export default {
data: function () {
return {
activeTab: 0,
open: false,
links: [
{
title: 'Home',
href: '#0',
},
{
title: 'Drupal',
href: 'https://www.drupal.org',
},
{
title: 'Vue.js',
href: 'https://vuejs.org',
},
{
title: 'Tailwind CSS',
href: 'https://tailwindcss.com',
},
{
title: 'View code on GitHub',
href: 'https://github.com/opdavies/rebuilding-bartik',
},
{
title: 'Read blog post',
href: 'https://www.oliverdavies.uk/blog/rebuilding-bartik-with-vuejs-tailwind-css',
},
]
}
},

computed: {
navText: function () {
return this.open ? 'Hide' : 'Show';
}
}
}


The result

The whole task only took around two hours to complete, and although some of the colours and spacings are slightly different due to the decision to stick with the default Tailwind configuration values, I'm happy with the result.

The original version

The Vue.js and Tailwind CSS version

I've also made some additional changes since this version, which are described in this follow-up post.