Dev Helpers
← Back

Inactivity hint field

Import and render <InactivityHintField /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/InactivityHintField.tsx

import { useState, useEffect } from "react";

export default function InactivityHintField({
  seconds = 10,
}: { seconds?: number } = {}) {
  const [value, set] = useState("");
  const [idle, hint] = useState(false);
  useEffect(() => {
    hint(false);
    const t = setTimeout(() => hint(true), seconds * 1000);
    return () => clearTimeout(t);
  }, [value, seconds]);
  return (
    <label>
      Draft
      <input value={value} onChange={(e) => set(e.target.value)} />
      {idle && <small>Pause detected. Your draft is still here.</small>}
    </label>
  );
}