@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 3.07 kB
Source Map (JSON)
{"version":3,"file":"use-debounced-value.cjs","names":[],"sources":["../../src/use-debounced-value/use-debounced-value.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseDebouncedValueOptions {\n leading?: boolean;\n}\n\nexport interface UseDebouncedValueHandlers {\n cancel: () => void;\n flush: () => void;\n}\n\nexport type UseDebouncedValueReturnValue<T> = [T, () => void, UseDebouncedValueHandlers];\n\nexport function useDebouncedValue<T = any>(\n value: T,\n wait: number,\n options: UseDebouncedValueOptions = { leading: false }\n): UseDebouncedValueReturnValue<T> {\n const [_value, setValue] = useState(value);\n const mountedRef = useRef(false);\n const timeoutRef = useRef<number | null>(null);\n const cooldownRef = useRef(false);\n\n const latestValueRef = useRef(value);\n latestValueRef.current = value;\n\n const cancel = useCallback(() => {\n window.clearTimeout(timeoutRef.current!);\n timeoutRef.current = null;\n cooldownRef.current = false;\n }, []);\n\n const flush = useCallback(() => {\n if (timeoutRef.current) {\n cancel();\n cooldownRef.current = false;\n setValue(latestValueRef.current);\n }\n }, []);\n\n useEffect(() => {\n if (mountedRef.current) {\n if (!cooldownRef.current && options.leading) {\n cooldownRef.current = true;\n setValue(value);\n timeoutRef.current = window.setTimeout(() => {\n cooldownRef.current = false;\n }, wait);\n } else {\n cancel();\n timeoutRef.current = window.setTimeout(() => {\n cooldownRef.current = false;\n setValue(value);\n }, wait);\n }\n }\n }, [value, options.leading, wait]);\n\n useEffect(() => {\n mountedRef.current = true;\n return cancel;\n }, []);\n\n return [_value, cancel, { cancel, flush }];\n}\n\nexport namespace useDebouncedValue {\n export type Handlers = UseDebouncedValueHandlers;\n export type Options = UseDebouncedValueOptions;\n export type ReturnValue<T> = UseDebouncedValueReturnValue<T>;\n}\n"],"mappings":";;;AAaA,SAAgB,kBACd,OACA,MACA,UAAoC,EAAE,SAAS,MAAM,GACpB;CACjC,MAAM,CAAC,QAAQ,aAAA,GAAA,MAAA,UAAqB,KAAK;CACzC,MAAM,cAAA,GAAA,MAAA,QAAoB,KAAK;CAC/B,MAAM,cAAA,GAAA,MAAA,QAAmC,IAAI;CAC7C,MAAM,eAAA,GAAA,MAAA,QAAqB,KAAK;CAEhC,MAAM,kBAAA,GAAA,MAAA,QAAwB,KAAK;CACnC,eAAe,UAAU;CAEzB,MAAM,UAAA,GAAA,MAAA,mBAA2B;EAC/B,OAAO,aAAa,WAAW,OAAQ;EACvC,WAAW,UAAU;EACrB,YAAY,UAAU;CACxB,GAAG,CAAC,CAAC;CAEL,MAAM,SAAA,GAAA,MAAA,mBAA0B;EAC9B,IAAI,WAAW,SAAS;GACtB,OAAO;GACP,YAAY,UAAU;GACtB,SAAS,eAAe,OAAO;EACjC;CACF,GAAG,CAAC,CAAC;CAEL,CAAA,GAAA,MAAA,iBAAgB;EACd,IAAI,WAAW,SACb,IAAI,CAAC,YAAY,WAAW,QAAQ,SAAS;GAC3C,YAAY,UAAU;GACtB,SAAS,KAAK;GACd,WAAW,UAAU,OAAO,iBAAiB;IAC3C,YAAY,UAAU;GACxB,GAAG,IAAI;EACT,OAAO;GACL,OAAO;GACP,WAAW,UAAU,OAAO,iBAAiB;IAC3C,YAAY,UAAU;IACtB,SAAS,KAAK;GAChB,GAAG,IAAI;EACT;CAEJ,GAAG;EAAC;EAAO,QAAQ;EAAS;CAAI,CAAC;CAEjC,CAAA,GAAA,MAAA,iBAAgB;EACd,WAAW,UAAU;EACrB,OAAO;CACT,GAAG,CAAC,CAAC;CAEL,OAAO;EAAC;EAAQ;EAAQ;GAAE;GAAQ;EAAM;CAAC;AAC3C"}