NPM Logo

Creating an NPM package with TypeScript

I took to publishing a simple scroll animation library with TypeScript, here were my steps.

Intersection Observer API
npm package
simple-scroll-animate
scroll animations

Published 3 years ago

2 minutes read

Having used countless NPM libraries in my day to day job, I wanted to take a bit of time to publish a simple NPM package myself.

The aim was to build something that could be useful, but wasn't just your typical "Hello World" package. The library had to be something manageable, small and quick to create in my spare time. It would also be great to use it in future projects if there was a need.

The aim

With all that in mind, I decided on creating a simple scroll animation library.

  • When a user scrolls past an element that comes into the users viewport, a configurable CSS classname would be added to the element.
  • The classname would have a CSS animation attached, triggering when the user scrolled to the element.

Setting up the NPM package structure

Before I could dive into the building the library, I needed a solid foundation for the NPM package. I wanted a quick and no fuss way to create a package that still allowed me to use TypeScript.

TSDX as quoted from their project repo, “is a zero-config CLI that helps you develop, test, and publish modern TypeScript packages”. TSDX gives you some fantastic options to boilerplate your project for an npm package all of which can be found in the TSDX Documentation

Running npx tsdx create simple-scroll-animate was my first step, and just like that I had a ready to go NPM package.

Next, was to create my library. To find out more about the Intersection Observer API, read more from the mozilla.org website, “The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.”

See how I used the API in my library in the GithHub src.

Publishing the library

Needing an NPMJS account, I simply signed up and within a few minutes I could run npm login so I could publish from the CLI.

Make sure your codebase is in a public repository, with a good README, and simply npm run build and then npm publish.

Introducing Simple Scroll Animate

I now have a simple library in which I can add css animations to elements as they come into view. View the project on GitHub or by running npm install simple-scroll-animate and following the README for usage.

A demo page of the library in action can be found here.

Let's chat about your next project

Whether you're looking for a single contractor or a full team, I regularly collaborate with a trusted collective of designers and developers to deliver large-scale projects.

See also...

KickTown Football

Website Development for KickTown Football: Custom Build, Merchandise Store, and Booking System

Stock Investment App

Mobile and web app for stock investment, built with TypeScript, React, and WebSockets, offering real-time data and responsive design.

CnCNet - Website & App Development

Comprehensive design and development of web and desktop apps for CnCNet

Bespoke AR Web Configurator for E-Bikes

A bespoke augmented reality web configurator developed to showcase the client's E-Bike, offering fully customizable options such as color selections and feature configurations.

Oriental Garden Restaurant

A complete website redesign featuring a new online ordering system for enhanced customer experience and increased revenue.

Web App Visualizer - Facial Cosmetic Treatment Simulation

A React and TypeScript-powered web app designed to visualize facial cosmetic treatment results for enhanced customer decision-making

React Native - Android & iOS App

Development of a bespoke Android & iOS app for a Health technology startup, built with React Native & TypeScript.

Website - C&C Community

Enhancing the platform with a sleek user interface, improved SEO coverage, and integrated content features like Twitch, and Steam. Offering a central resource for Command & Conquer fans and creators.