oliverdavies.uk/source/_posts/published-my-first-npm-package.md

120 lines
3.2 KiB
Markdown
Raw Normal View History

2018-12-16 17:01:59 +00:00
---
title: Published my first NPM package
2020-03-08 14:32:13 +00:00
date: 2018-12-16
excerpt:
Yesterday I published my first module onto NPM, and its a plugin for Tailwind
CSS to be used alongside Vue.js.
2018-12-16 17:01:59 +00:00
tags:
- npm
- tailwind-css
- vuejs
2018-12-16 17:01:59 +00:00
---
Yesterday I published my first module onto NPM, and its a plugin for [Tailwind
CSS][tailwind] to be used alongside [Vue.js](https://vuejs.org).
The plugin adds classes for showing and hiding elements in different display
variations in combination with Vue's
[v-cloak directive](https://vuejs.org/v2/api/#v-cloak), which I originally saw
in [the first 'Building Kitetail' video](https://youtu.be/XUXpcbYQ_iQ?t=2360).
These are useful for when you want an element to be visible whilst Vue is
compiling, and hidden afterwards.
2018-12-16 17:01:59 +00:00
Here is the compiled CSS that is added by the plugin:
```css
[v-cloak] .v-cloak-block {
display: block;
}
[v-cloak] .v-cloak-flex {
display: flex;
}
[v-cloak] .v-cloak-hidden {
display: none;
}
[v-cloak] .v-cloak-inline {
display: inline;
}
[v-cloak] .v-cloak-inline-block {
display: inline-block;
}
[v-cloak] .v-cloak-inline-flex {
display: inline-flex;
}
[v-cloak] .v-cloak-invisible {
visibility: hidden;
}
.v-cloak-block,
.v-cloak-flex,
.v-cloak-inline,
.v-cloak-inline-block,
.v-cloak-inline-flex {
display: none;
}
```
The `v-cloak` directive exists on an element until Vue finishes compiling, after
which it is removed. Therefore adding a `v-cloak-block` class to an element will
make it `display: block` whilst Vue is compiling and the element is cloaked, and
`display: none` afterwards when the Vue markup is compiled and rendered.
2018-12-16 17:01:59 +00:00
In my `base.html.twig` template, Ive added `v-cloak` to the wrapper div within
the `body`.
2018-12-16 17:01:59 +00:00
2019-04-10 16:59:53 +00:00
{% verbatim %}<div v-pre markdown="1">
2018-12-16 17:01:59 +00:00
```twig
<body class="font-sans leading-normal">
<div id="app" v-cloak>
{# ... #}
</div>
</body>
```
2019-04-10 16:59:53 +00:00
</div>{% endverbatim %}
2018-12-16 17:01:59 +00:00
Within my `navbar.html.twig` partial, I have a placeholder div that also
contains the site name, which is instantly visible but has the `v-cloak-block`
class so its hidden once Vue has compiled and the `Navbar` Vue component is
visible instead.
2018-12-16 17:01:59 +00:00
2019-04-10 16:59:53 +00:00
{% verbatim %}<div v-pre markdown="1">
2018-12-16 17:01:59 +00:00
```twig
2019-03-21 09:38:09 +00:00
<div class="border-bottom border-b border-gray-300 mb-6">
2018-12-16 17:01:59 +00:00
<div class="container mx-auto">
<div class="block py-5 v-cloak-block">
{{ site.title }}
</div>
<navbar
site-name="{{ site.title }}"
page-url="{{ page.url }}"
></navbar>
</div>
</div>
```
2019-04-10 16:59:53 +00:00
</div>{% endverbatim %}
2018-12-16 17:01:59 +00:00
I was originally surprised that these classes werent included as part of
Tailwind or as part of an existing plugin, but as Ive already used these styles
on several projects that include Vue.js with Symfony or Sculpin, it made sense
to extract it into a plugin and make it available as a npm package which I can
easily add to any project - as well as making it easier to maintain if I need to
add additional variations at a later point.
2018-12-16 17:01:59 +00:00
**You can view [the package on npmjs.com][npm], and [the code repository on
GitHub][github].**
2018-12-16 17:01:59 +00:00
[github]: https://github.com/opdavies/tailwindcss-vuejs
[npm]: https://www.npmjs.com/package/tailwindcss-vuejs
[tailwind]: https://tailwindcss.com