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.

Looking for a TypeScript Developer on your next project?

I help agencies, startups, and individuals stand out by creating exceptional websites, mobile apps, and digital experiences. Based in Surrey, I work with clients in London and internationally, providing flexible, tailored solutions for every project.

Let's chat about your next project

Whether you need a single contractor or a full team, I collaborate with a trusted network of designers and developers. Having worked on large-scale projects, I'm equally committed to supporting businesses of all sizes, locally in Surrey, London, and internationally.

See also...

CnCNet - Website & App Development

Comprehensive design and development of responsive web and desktop apps for CnCNet, enhancing user experience and accessibility.

AR Configurator for E-Bikes

Bespoke augmented reality web configurator for E-Bikes, offering full customization options, including color choices and feature configurations.

Stock Investment App

Real-time stock investment app for web and mobile, built with TypeScript, React, and WebSockets, delivering fast, responsive, and data-driven experiences.

KickTown Football - Website

Custom-built website and API's for KickTown Football, integrating a merchandise store, booking system, and seamless user experience.

Tempest Rising - Official Website

Website development for Slipgate Ironworks' Tempest Rising, crafted to deliver a sleek, immersive experience for fans and players.

Cosmetic Visualiser - Web App

React and TypeScript-powered web app for visualizing facial cosmetic treatments, offering a cutting-edge, interactive user experience.

C&C Community Website

Command & Conquer Community platform with improved SEO, a sleek interface, and content integrations like Twitch and Steam, creating a hub for fans and creators.

React Native Health App

Custom Android and iOS health app developed with React Native and TypeScript, tailored for a health-tech startup, ensuring cross-platform compatibility.

Oriental Garden Restaurant - Website Redesign

Website redesign featuring an online ordering system that boosts sales and enhances customer engagement with a modern, intuitive interface.