Check That Email Verification Again - or How I Published My First npm-Package
Tags: showdev, react, webdev, css
Published: 2021-02-06 09:33:19

I have a habit of getting into the small, irrelevant details when making a demo for a talk.  I had an event coming up where I live coded an app. It uses [NASA's Asteroids NeoWs -API][2], and I needed something to display when the data is loading. I ended up creating a loading spinner, which shows an asteroid moving: 

<img src="https://dev-to-uploads.s3.amazonaws.com/i/uou37s1keuww3udn81wb.gif" alt="A drawn asteroid, which moves slowly up and down. Animation of lines after it indicate that it is moving" style="height: 100px;" />

In this blog post, I'll share the process of how I created and published the npm-package of the loading indicator. I have only a little experience with SVGs, so this was quite a learning journey both because of the spinner itself and because I've never published an npm-package before.

## Initial Design with Figma

Initially, I thought about creating the loading spinner purely in CSS. I even started it with some divs and CSS-styling but soon realized that I don't have the time to learn all the cool tricks. 

The next idea was to use an SVG. I opened up Figma and started drawing. I had this idea about what I wanted: A reasonably simple asteroid with some lines indicating that it is moving. I spent some time finetuning the initial design, and when it was ready, I exported it as SVG. This is what it looked like:

![Simple vector asteroid](https://dev-to-uploads.s3.amazonaws.com/i/ggjh83du0smd5z3m5ui0.png)

After exporting the SVG, I imported it into the project. It was time to start coding (if you call CSS and some fiddling with SVGs coding). 

## Styling and Animating the Asteroid
 
When digging deeper into animating SVGs, I found this [amazing tutorial from Cassie Evans][3]. There are these two properties in SVGs; `stroke-dasharray` and `stroke-dashoffset.` When learning about those, I realized that I don't need to create dashed lines for the asteroid myself - four straight lines with the properties mentioned above will do the trick.

This led to some fiddling with the SVG's code. I am pleased to say that I managed to make the four straight lines instead of the dashed lines you can see from the picture above, and all that with just editing (and deleting) the `<path>`-elements.

With help from Cassie's tutorial, I also managed to create the animations for lines moving at different paces. It was a traditional trial and error case - I investigated different values until the timing and movement were right. 

I also decided that the whole asteroid should move slowly up and down to indicate the movement better. Another thing I'm proud of - I found the right element to use to wrap the contents (`g`) and gave it a class name to be used for the animations. This might seem small to someone who has more experience with SVGs. However, I am a beginner in the field of SVGs, so this feels like a victory. 🏆

I had a working loading spinner, and it was time to extract it to a separate npm package. 

## Publishing the Package

For publishing an npm package, I followed [this tutorial by Ovie Okeh][1]. It was pretty straightforward to follow, and had there not been this one (self-caused) problem, I would have accomplished my goal in under 30 minutes. 

I won't go through the steps for publishing the npm-package; you can check the tutorial for that, but I will talk a bit about the importance of thoroughly reading an email. I mean, everything was working up until the point where I was supposed to actually publish the package. I started getting this mysterious 403-error for no apparent reason.

I honestly spent something like two hours trying to debug this. Finally, I realized I hadn't verified my email, which was the reason for the error. I thought I had clicked the right link, but apparently, I had not. 

After the successful verification, everything worked smoothly. To be honest, I am surprised how easy it was in the end. I was waiting for something like slaying an actual dragon, a gatekeeper for the gates of the npm registry.

## Published!

So, I got the package published, and I was happy... Until I realized all the things I could have done better. In the first version, I had given class names to the component, and then they were passed to the correct SVG-elements. I realized that what I was trying to accomplish with that approach was to provide the user with the ability to change the loading spinner's colors. 

Colors could actually be given as inline properties to SVG-elements, and thus the spinner would need only the color. This way, the user doesn't have to remember to implement the CSS-class behind the class name. This led to some refactoring, and v2 to be out pretty fast after the initial version. 

Heh, I also am learning how to work with npm and keep forgetting things. Once I forgot to run the `build' script before publishing, I wondered why nothing changed. I had to make another patch release to actually update the spinner. 

If you want to see the code, here's the repository:

{% github eevajonnapanula/asteroid-loading-spinner no-readme %}

And here's a link to the [Asteroid Loading Spinner][4] npm-package.

## Wrapping Up

It has been interesting to learn more about npm and also SVGs. And animations. I'm also proud that I actually got something out - I have so many projects that just lie there, and I know I will never finish them.

Have you learned something new this year? Please, share if you do! 😊

## Resources
- [Cassie Evans - Creating My Logo Animation][3]
- [Ovie Okeh - The Complete Guide to Publishing a React Package to NPM][1]
- [Nasa's API's][2]
- [Asteroid Loading Spinner][4]

*Cover photo by [Chris Henry][5] on [Unsplash][6]*

[1]: https://blog.logrocket.com/the-complete-guide-to-publishing-a-react-package-to-npm/
[2]: https://api.nasa.gov/
[3]: https://www.cassie.codes/posts/creating-my-logo-animation/
[4]: https://www.npmjs.com/package/asteroid-loading-spinner
[5]: https://unsplash.com/@chrishenryphoto
[6]: https://unsplash.com/s/photos/asteroid