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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@timekeeper-countdown/react

v0.1.1

Published

React adapter for the Timekeeper Countdown engine

Readme

@timekeeper-countdown/react

React hook for the Timekeeper Countdown engine. This package provides an idiomatic way to manage countdown timers in React components while keeping the underlying engine framework-agnostic.

  • Single hook: useCountdown(initialSeconds, options?).
  • Snapshot-first API that keeps your UI in sync with timer state.
  • Zero runtime dependencies besides React.
  • Works in React 17+ (including React 18 concurrent mode).
  • Ships modern ESM output with type definitions.

Installation

npm install @timekeeper-countdown/react

Peer dependencies (must already exist in your application):

  • react ≥ 17
  • @timekeeper-countdown/core is installed automatically as a dependency.

Quick Start

import { useCountdown } from '@timekeeper-countdown/react';
import { formatTime } from '@timekeeper-countdown/core/format';

export function CountdownCard() {
  const countdown = useCountdown(5 * 60, {
    autoStart: true,
  });

  const clock = formatTime(countdown.snapshot);

  return (
    <section>
      <h2>
        {clock.minutes}:{clock.seconds}
      </h2>
      <p>Status: {countdown.state}</p>
      <button onClick={countdown.pause} disabled={!countdown.isRunning}>
        Pause
      </button>
      <button onClick={countdown.resume} disabled={countdown.isRunning}>
        Resume
      </button>
      <button onClick={() => countdown.reset(5 * 60)}>Restart</button>
    </section>
  );
}

Each hook instance owns a dedicated engine. When the component unmounts the engine is destroyed automatically.


Hook Signature

const result = useCountdown(initialSeconds: number, options?: UseCountdownOptions);

Options

interface UseCountdownOptions extends Omit<CountdownEngineOptions, 'onSnapshot' | 'onStateChange' | 'onError'> {
  autoStart?: boolean; // Start automatically on mount (default false)
  onSnapshot?: CountdownEngineOptions['onSnapshot'];
  onStateChange?: CountdownEngineOptions['onStateChange'];
  onError?: CountdownEngineOptions['onError'];
}
  • tickIntervalMs: Polling interval. Default 100.
  • timeProvider: Function or engine time provider (see core README) for deterministic timing or shared clocks.
  • autoStart: If true, the hook starts immediately after mounting.

Return Value

interface UseCountdownResult {
  snapshot: CountdownSnapshot;
  state: TimerState;
  totalSeconds: number;
  parts: CountdownSnapshot['parts'];
  isRunning: boolean;
  isCompleted: boolean;

  start(): boolean;
  pause(): boolean;
  resume(): boolean;
  reset(nextInitialSeconds?: number): boolean;
  stop(): boolean;
  setSeconds(value: number): void;
}
  • All control methods mirror the engine and return false for invalid transitions.
  • snapshot is stable per render; derive memoised values with useMemo if needed.
  • totalSeconds, parts, isRunning, and isCompleted are re-exposed for convenience.

Formatting Helpers

Import helpers directly from the core package to render zero-padded strings:

import { formatTime } from '@timekeeper-countdown/core/format';

const clock = formatTime(countdown.snapshot);

formatTime returns { minutes: string; seconds: string }. Additional helpers include formatMinutes, formatHours, formatDays, etc.


Integrating with Custom Time Providers

You can plug a fake or shared time provider using the utilities bundled with the core package:

import { createFakeTimeProvider, toTimeProvider } from '@timekeeper-countdown/core/testing-utils';

const fake = createFakeTimeProvider({ startMs: 0 });

function InspectableTimer() {
  const countdown = useCountdown(30, {
    autoStart: true,
    timeProvider: toTimeProvider(fake),
    tickIntervalMs: 10,
  });

  return (
    <div>
      <span>{countdown.totalSeconds}s</span>
      <button onClick={() => fake.advance(1000)}>Advance 1s</button>
    </div>
  );
}

This decouples the timer from the real clock and enables deterministic testing or synchronized timers.


Testing

@testing-library/react and Vitest/RTL work seamlessly with the hook. Inject the fake provider to avoid relying on real timers:

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { createFakeTimeProvider, toTimeProvider } from '@timekeeper-countdown/core/testing-utils';
import { useCountdown } from '@timekeeper-countdown/react';

function TestComponent() {
  const fake = useMemo(() => createFakeTimeProvider({ startMs: 0 }), []);
  const countdown = useCountdown(5, {
    autoStart: true,
    timeProvider: toTimeProvider(fake),
  });

  return (
    <div>
      <output>{countdown.totalSeconds}</output>
      <button onClick={() => fake.advance(1000)}>Advance</button>
    </div>
  );
}

it('counts down when the fake clock advances', async () => {
  render(<TestComponent />);
  await userEvent.click(screen.getByRole('button', { name: /advance/i }));
  expect(screen.getByText('4')).toBeInTheDocument();
});

The hook handles cleanup automatically, so tests do not need to destroy the engine manually.


Multiple Timers

Each hook call is independent. Compose them to build complex flows:

function MultiStageTimer() {
  const focus = useCountdown(25 * 60, { autoStart: true });
  const breakTimer = useCountdown(5 * 60);

  useEffect(() => {
    if (focus.isCompleted) {
      breakTimer.reset();
      breakTimer.start();
    }
  }, [focus.isCompleted, breakTimer]);

  return (
    <div>
      <TimerCard title="Focus" countdown={focus} />
      <TimerCard title="Break" countdown={breakTimer} />
    </div>
  );
}

Related Packages


Contributing

Bug reports and pull requests are welcome. Please read the repository guidelines and check your work with:

npm run lint --workspaces
npm run test --workspaces
npm run typecheck --workspaces

License

MIT © Eduardo Kohn