Skip to content

Repository files navigation

React Timer Hook UseTimer Editable

An independent integration of @stephanluis/react-input-duration and react-timer-hook. It displays the remaining time in the duration input while useTimer counts down. Applications provide their own controls and styling.

Try the live demo

Editable timer demo

Installation

npm install @stephanluis/react-timer-hook-usetimer-editable

React 18.2 or 19 and React DOM are required.

Usage

import { useRef, useState } from 'react';
import EditableTimer from '@stephanluis/react-timer-hook-usetimer-editable';

function Example() {
    const timer = useRef(null);
    const [running, setRunning] = useState(false);
    const [status, setStatus] = useState('Ready.');

    function toggle() {
        try {
            if (running) timer.current.pause();
            else timer.current.start();
        } catch (error) {
            setStatus(error.message);
        }
    }

    return <>
        <EditableTimer ref={timer} initialValue="00:00:10.000"
            onStart={() => setRunning(true)}
            onPause={() => setRunning(false)}
            onExpire={() => { setRunning(false); setStatus('Complete.'); }} />
        <button type="button" onClick={toggle}>{running ? 'Pause' : 'Start'}</button>
        <p role="status">{status}</p>
    </>;
}

initialValue accepts HH:mm:ss.SSS or D:HH:mm:ss.SSS and is used only on mount. The ref exposes start(), pause(), resume(), and restart(expiryTimestamp, autoStart?). start() and resume() use the committed input value; they throw if the input is not ready, disabled, invalid, zero, or outside JavaScript's safe integer and date ranges. Both do nothing while already running. pause() preserves the remaining duration for editing, and does nothing when already paused. Because edits can change the duration, starting and resuming restart the underlying hook with the committed value, rather than calling its start or resume directly. restart() accepts a future Date and an optional boolean (default true); a false value sets the remaining duration without starting. Expiry or pausing after the deadline calls onExpire and re-enables the input at zero. onStart, onPause, and onResume notify the corresponding actions; a running restart() calls onStart, and restart(..., false) calls onPause if the timer was running. The update interval is in milliseconds and defaults to 10; it must be positive.

Use inputProps for React input-duration options and HTML attributes. value, defaultValue, and children are managed by the integration. Its onChange receives committed edits. The input is always disabled while running; inputProps.disabled can also disable it while idle. When noMicroseconds is enabled, pausing rounds the remaining time up to a whole second. Set showDays to display the days field; hidden fields retain their values.

To render additional content from the underlying hook, use renderTimerState:

<EditableTimer ref={timer}
    renderTimerState={({ isRunning, totalMilliseconds }) => (
        <p>{isRunning ? 'Running' : 'Paused'}: {totalMilliseconds} ms</p>
    )} />

The function receives isRunning, totalMilliseconds, totalSeconds, days, hours, minutes, seconds, and milliseconds directly from useTimer. These are hook state, not the editable input value: initially the hook is paused at zero, and edits while paused are reflected in the hook only after starting or resuming. Paused hook time may differ slightly from the input because the input captures the deadline at the instant of pause. The hook rounds totalSeconds up and derives its day, hour, minute, and second fields from that rounded value.

This project is not affiliated with or endorsed by react-timer-hook.

Local demo

Run npm install, then npm run demo, and open the URL printed by Vite. Run npm test for the integration and TypeScript checks. The demo reproduces the styled timer input with a demo-owned Start/Pause button. The GitHub Pages workflow builds and deploys the demo when changes are pushed to main.

Releases

Packages

Contributors

Languages