npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@danielsledz/react-countdown-hook

v1.1.3

Published

A simple React hook for creating countdown timers to any target date or time

Readme

🕒 @danielsledz/react-countdown-hook

A simple React hook for creating countdown timers to any target date or time.

npm version License: MIT


🚀 Installation

npm install @danielsledz/react-countdown-hook
# or
yarn add @danielsledz/react-countdown-hook
# or
pnpm add @danielsledz/react-countdown-hook

🧠 Usage

Basic Example

import { useCountdown } from "@danielsledz/react-countdown-hook";

const MyComponent = () => {
  const { timeLeft, isPaused, togglePause, error } = useCountdown(
    "2025-12-31T23:59:59"
  );

  if (error) return <p>Error: {error}</p>;

  return (
    <div>
      <p>
        {timeLeft.direction} {timeLeft.days}d {timeLeft.hours}h{" "}
        {timeLeft.minutes}m {timeLeft.seconds}s
      </p>
      <button onClick={togglePause}>{isPaused ? "Resume" : "Pause"}</button>
    </div>
  );
};

Advanced Example with Date Object

import { useCountdown } from "@danielsledz/react-countdown-hook";

const MyComponent = () => {
  const targetDate = new Date("2025-12-31T23:59:59");
  const { timeLeft, isPaused, togglePause, error } = useCountdown(targetDate);

  if (error) return <p>Error: {error}</p>;

  const formatTime = (time: number) => time.toString().padStart(2, "0");

  return (
    <div>
      <h2>Countdown to New Year 2026</h2>
      <div style={{ fontSize: "2rem", fontFamily: "monospace" }}>
        {timeLeft.direction === "-" ? "Time remaining:" : "Time since:"}
        <br />
        {formatTime(timeLeft.days)}:{formatTime(timeLeft.hours)}:
        {formatTime(timeLeft.minutes)}:{formatTime(timeLeft.seconds)}
      </div>
      <button onClick={togglePause}>
        {isPaused ? "▶️ Resume" : "⏸️ Pause"}
      </button>
    </div>
  );
};

Event Countdown Example

import { useCountdown } from "@danielsledz/react-countdown-hook";

const EventCountdown = () => {
  const { timeLeft, isPaused, togglePause, error } = useCountdown(
    "2025-06-15T18:00:00"
  );

  if (error) return <p>Error: {error}</p>;

  const isEventPassed = timeLeft.direction === "+";

  return (
    <div>
      <h3>Summer Conference 2025</h3>
      {isEventPassed ? (
        <p>Event has passed! It's been {timeLeft.days} days since the event.</p>
      ) : (
        <div>
          <p>Event starts in:</p>
          <div>
            {timeLeft.days} days, {timeLeft.hours} hours, {timeLeft.minutes}{" "}
            minutes, {timeLeft.seconds} seconds
          </div>
          <button onClick={togglePause}>
            {isPaused ? "Resume Countdown" : "Pause Countdown"}
          </button>
        </div>
      )}
    </div>
  );
};

📦 API

useCountdown(targetDate: string | Date): CountdownHook

Parameters:

  • targetDate: The date and time to count down to (string or Date object).

Returns:

| Name | Type | Description | | ------------- | ---------------------------------------------- | ----------------------------------------------------------------- | | timeLeft | { days, hours, minutes, seconds, direction } | Remaining time and countdown direction (- = future, + = past) | | isPaused | boolean | Whether the countdown is paused | | togglePause | () => void | Function to toggle pause/resume | | error | string \| null | Error message if invalid input |

Types

interface TimeLeft {
  days: number;
  hours: number;
  minutes: number;
  seconds: number;
  direction: string; // "-" for future, "+" for past
}

interface CountdownHook {
  timeLeft: TimeLeft;
  isPaused: boolean;
  togglePause: () => void;
  error: string | null;
}

⚠️ Error Handling

If the target date is invalid (e.g., useCountdown("invalid-date")), the error field will contain an appropriate message.

const { error } = useCountdown("invalid-date");
if (error) {
  console.log(error); // "Invalid target date."
}

🧪 Testing

The package includes comprehensive unit and integration tests using Vitest and React Testing Library.

# Run tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:coverage

Test Coverage

The test suite covers:

  • ✅ Basic countdown functionality
  • ✅ Pause/resume functionality
  • ✅ Error handling for invalid dates
  • ✅ Time calculations accuracy
  • ✅ Edge cases and boundary conditions
  • ✅ Integration scenarios
  • ✅ Memory leak prevention

🔧 Development

# Install dependencies
npm install

# Build the package (CommonJS + ESM)
npm run build

# Build TypeScript declarations
npm run build:types

# Watch for changes during development
npm run dev

# Clean build directory
npm run clean

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

Build Output

The build process generates:

  • dist/index.js - CommonJS bundle
  • dist/index.esm.js - ES Module bundle
  • dist/index.d.ts - TypeScript declarations

📄 License

MIT © 2025 Daniel Śledź


🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Fork the repository
  2. Clone your fork
  3. Install dependencies: npm install
  4. Make your changes
  5. Add tests for new functionality
  6. Run tests: npm test
  7. Build the package: npm run build
  8. Submit a pull request