498

useTimeout

Hook that executes a callback function after a specified delay

timemediumtest coverage
Hold on
import { useTimeout } from '@siberiacancode/reactuse';

const DELAY = 5000;

const Demo = () => {
  const timeout = useTimeout(() => {}, DELAY);

  return (
    <section className='flex w-full max-w-md flex-col items-center gap-6 p-8'>
      <div
        key={String(timeout.ready)}
        className='animate-in fade-in slide-in-from-bottom-2 duration-500'
      >
        <span className='text-foreground text-5xl font-bold tracking-tight'>
          {timeout.ready ? "You're awesome" : 'Hold on'}
        </span>
      </div>

      <div className='bg-muted h-1 w-32 overflow-hidden rounded-full'>
        <div
          className='bg-foreground h-full origin-left'
          style={{ animation: `progress ${DELAY}ms linear forwards` }}
        />
      </div>

      <style>{`
        @keyframes progress {
          from { transform: scaleX(0); }
          to { transform: scaleX(1); }
        }
      `}</style>
    </section>
  );
};

export default Demo;

Installation

pnpm add @siberiacancode/reactuse

Usage

const { clear, ready } = useTimeout(() => {}, 5000);

Type Declarations

interface UseTimeoutReturn {
  /**  Timeout is ready state value */
  ready: boolean;
  /** Function to clear timeout */
  clear: () => void;
}

API

Parameters

NameTypeDefaultNote
callback() => void-The function to be executed after the timeout
delaynumber-The delay in milliseconds before the timeout executes the callback function

Returns

UseTimeoutReturn

Contributors

ddebabinhhywaxttriangle

Last updated on