@mantine/hooks 9.0.2 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.cjs +8 -0
- package/cjs/use-click-outside/use-click-outside.cjs +11 -8
- package/cjs/use-click-outside/use-click-outside.cjs.map +1 -1
- package/cjs/use-clipboard/use-clipboard.cjs +13 -6
- package/cjs/use-clipboard/use-clipboard.cjs.map +1 -1
- package/cjs/use-collapse/use-collapse.cjs +1 -1
- package/cjs/use-collapse/use-collapse.cjs.map +1 -1
- package/cjs/use-collapse/use-horizontal-collapse.cjs +4 -3
- package/cjs/use-collapse/use-horizontal-collapse.cjs.map +1 -1
- package/cjs/use-counter/use-counter.cjs +12 -3
- package/cjs/use-counter/use-counter.cjs.map +1 -1
- package/cjs/use-debounced-callback/use-debounced-callback.cjs +28 -4
- package/cjs/use-debounced-callback/use-debounced-callback.cjs.map +1 -1
- package/cjs/use-debounced-state/use-debounced-state.cjs +1 -1
- package/cjs/use-debounced-state/use-debounced-state.cjs.map +1 -1
- package/cjs/use-debounced-value/use-debounced-value.cjs +22 -2
- package/cjs/use-debounced-value/use-debounced-value.cjs.map +1 -1
- package/cjs/use-event-listener/use-event-listener.cjs.map +1 -1
- package/cjs/use-favicon/use-favicon.cjs +3 -1
- package/cjs/use-favicon/use-favicon.cjs.map +1 -1
- package/cjs/use-fetch/use-fetch.cjs +7 -4
- package/cjs/use-fetch/use-fetch.cjs.map +1 -1
- package/cjs/use-file-dialog/use-file-dialog.cjs +1 -0
- package/cjs/use-file-dialog/use-file-dialog.cjs.map +1 -1
- package/cjs/use-floating-window/use-floating-window.cjs +33 -36
- package/cjs/use-floating-window/use-floating-window.cjs.map +1 -1
- package/cjs/use-focus-trap/use-focus-trap.cjs +12 -9
- package/cjs/use-focus-trap/use-focus-trap.cjs.map +1 -1
- package/cjs/use-fullscreen/use-fullscreen.cjs +3 -1
- package/cjs/use-fullscreen/use-fullscreen.cjs.map +1 -1
- package/cjs/use-headroom/use-headroom.cjs +4 -1
- package/cjs/use-headroom/use-headroom.cjs.map +1 -1
- package/cjs/use-hotkeys/parse-hotkey.cjs +1 -2
- package/cjs/use-hotkeys/parse-hotkey.cjs.map +1 -1
- package/cjs/use-hover/use-hover.cjs +1 -0
- package/cjs/use-hover/use-hover.cjs.map +1 -1
- package/cjs/use-in-viewport/use-in-viewport.cjs +11 -7
- package/cjs/use-in-viewport/use-in-viewport.cjs.map +1 -1
- package/cjs/use-interval/use-interval.cjs +15 -6
- package/cjs/use-interval/use-interval.cjs.map +1 -1
- package/cjs/use-local-storage/create-storage.cjs +1 -1
- package/cjs/use-local-storage/create-storage.cjs.map +1 -1
- package/cjs/use-long-press/use-long-press.cjs +6 -2
- package/cjs/use-long-press/use-long-press.cjs.map +1 -1
- package/cjs/use-mask/use-mask.cjs +457 -0
- package/cjs/use-mask/use-mask.cjs.map +1 -0
- package/cjs/use-mouse/use-mouse.cjs +2 -2
- package/cjs/use-mouse/use-mouse.cjs.map +1 -1
- package/cjs/use-move/use-move.cjs +8 -0
- package/cjs/use-move/use-move.cjs.map +1 -1
- package/cjs/use-orientation/use-orientation.cjs +8 -2
- package/cjs/use-orientation/use-orientation.cjs.map +1 -1
- package/cjs/use-os/use-os.cjs +1 -1
- package/cjs/use-os/use-os.cjs.map +1 -1
- package/cjs/use-radial-move/use-radial-move.cjs +4 -4
- package/cjs/use-radial-move/use-radial-move.cjs.map +1 -1
- package/cjs/use-roving-index/use-roving-index.cjs +200 -0
- package/cjs/use-roving-index/use-roving-index.cjs.map +1 -0
- package/cjs/use-scroll-direction/use-scroll-direction.cjs +7 -7
- package/cjs/use-scroll-direction/use-scroll-direction.cjs.map +1 -1
- package/cjs/use-scroll-into-view/use-scroll-into-view.cjs +14 -4
- package/cjs/use-scroll-into-view/use-scroll-into-view.cjs.map +1 -1
- package/cjs/use-scroll-spy/use-scroll-spy.cjs +7 -3
- package/cjs/use-scroll-spy/use-scroll-spy.cjs.map +1 -1
- package/cjs/use-scroller/use-scroller.cjs +3 -3
- package/cjs/use-scroller/use-scroller.cjs.map +1 -1
- package/cjs/use-set/use-set.cjs +6 -1
- package/cjs/use-set/use-set.cjs.map +1 -1
- package/cjs/use-throttled-callback/use-throttled-callback.cjs +3 -1
- package/cjs/use-throttled-callback/use-throttled-callback.cjs.map +1 -1
- package/cjs/use-timeout/use-timeout.cjs +3 -1
- package/cjs/use-timeout/use-timeout.cjs.map +1 -1
- package/cjs/use-viewport-size/use-viewport-size.cjs +3 -3
- package/cjs/use-viewport-size/use-viewport-size.cjs.map +1 -1
- package/cjs/use-window-scroll/use-window-scroll.cjs +2 -2
- package/cjs/use-window-scroll/use-window-scroll.cjs.map +1 -1
- package/esm/index.mjs +3 -1
- package/esm/use-click-outside/use-click-outside.mjs +11 -8
- package/esm/use-click-outside/use-click-outside.mjs.map +1 -1
- package/esm/use-clipboard/use-clipboard.mjs +14 -7
- package/esm/use-clipboard/use-clipboard.mjs.map +1 -1
- package/esm/use-collapse/use-collapse.mjs +1 -1
- package/esm/use-collapse/use-collapse.mjs.map +1 -1
- package/esm/use-collapse/use-horizontal-collapse.mjs +5 -4
- package/esm/use-collapse/use-horizontal-collapse.mjs.map +1 -1
- package/esm/use-counter/use-counter.mjs +12 -3
- package/esm/use-counter/use-counter.mjs.map +1 -1
- package/esm/use-debounced-callback/use-debounced-callback.mjs +28 -4
- package/esm/use-debounced-callback/use-debounced-callback.mjs.map +1 -1
- package/esm/use-debounced-state/use-debounced-state.mjs +1 -1
- package/esm/use-debounced-state/use-debounced-state.mjs.map +1 -1
- package/esm/use-debounced-value/use-debounced-value.mjs +22 -2
- package/esm/use-debounced-value/use-debounced-value.mjs.map +1 -1
- package/esm/use-event-listener/use-event-listener.mjs.map +1 -1
- package/esm/use-favicon/use-favicon.mjs +3 -1
- package/esm/use-favicon/use-favicon.mjs.map +1 -1
- package/esm/use-fetch/use-fetch.mjs +7 -4
- package/esm/use-fetch/use-fetch.mjs.map +1 -1
- package/esm/use-file-dialog/use-file-dialog.mjs +1 -0
- package/esm/use-file-dialog/use-file-dialog.mjs.map +1 -1
- package/esm/use-floating-window/use-floating-window.mjs +33 -36
- package/esm/use-floating-window/use-floating-window.mjs.map +1 -1
- package/esm/use-focus-trap/use-focus-trap.mjs +12 -9
- package/esm/use-focus-trap/use-focus-trap.mjs.map +1 -1
- package/esm/use-fullscreen/use-fullscreen.mjs +3 -1
- package/esm/use-fullscreen/use-fullscreen.mjs.map +1 -1
- package/esm/use-headroom/use-headroom.mjs +4 -1
- package/esm/use-headroom/use-headroom.mjs.map +1 -1
- package/esm/use-hotkeys/parse-hotkey.mjs +1 -2
- package/esm/use-hotkeys/parse-hotkey.mjs.map +1 -1
- package/esm/use-hover/use-hover.mjs +1 -0
- package/esm/use-hover/use-hover.mjs.map +1 -1
- package/esm/use-in-viewport/use-in-viewport.mjs +11 -7
- package/esm/use-in-viewport/use-in-viewport.mjs.map +1 -1
- package/esm/use-interval/use-interval.mjs +15 -6
- package/esm/use-interval/use-interval.mjs.map +1 -1
- package/esm/use-local-storage/create-storage.mjs +1 -1
- package/esm/use-local-storage/create-storage.mjs.map +1 -1
- package/esm/use-long-press/use-long-press.mjs +6 -2
- package/esm/use-long-press/use-long-press.mjs.map +1 -1
- package/esm/use-mask/use-mask.mjs +453 -0
- package/esm/use-mask/use-mask.mjs.map +1 -0
- package/esm/use-mouse/use-mouse.mjs +2 -2
- package/esm/use-mouse/use-mouse.mjs.map +1 -1
- package/esm/use-move/use-move.mjs +8 -0
- package/esm/use-move/use-move.mjs.map +1 -1
- package/esm/use-orientation/use-orientation.mjs +8 -2
- package/esm/use-orientation/use-orientation.mjs.map +1 -1
- package/esm/use-os/use-os.mjs +1 -1
- package/esm/use-os/use-os.mjs.map +1 -1
- package/esm/use-radial-move/use-radial-move.mjs +5 -5
- package/esm/use-radial-move/use-radial-move.mjs.map +1 -1
- package/esm/use-roving-index/use-roving-index.mjs +200 -0
- package/esm/use-roving-index/use-roving-index.mjs.map +1 -0
- package/esm/use-scroll-direction/use-scroll-direction.mjs +7 -7
- package/esm/use-scroll-direction/use-scroll-direction.mjs.map +1 -1
- package/esm/use-scroll-into-view/use-scroll-into-view.mjs +15 -5
- package/esm/use-scroll-into-view/use-scroll-into-view.mjs.map +1 -1
- package/esm/use-scroll-spy/use-scroll-spy.mjs +8 -4
- package/esm/use-scroll-spy/use-scroll-spy.mjs.map +1 -1
- package/esm/use-scroller/use-scroller.mjs +3 -3
- package/esm/use-scroller/use-scroller.mjs.map +1 -1
- package/esm/use-set/use-set.mjs +6 -1
- package/esm/use-set/use-set.mjs.map +1 -1
- package/esm/use-throttled-callback/use-throttled-callback.mjs +3 -1
- package/esm/use-throttled-callback/use-throttled-callback.mjs.map +1 -1
- package/esm/use-timeout/use-timeout.mjs +3 -1
- package/esm/use-timeout/use-timeout.mjs.map +1 -1
- package/esm/use-viewport-size/use-viewport-size.mjs +3 -3
- package/esm/use-viewport-size/use-viewport-size.mjs.map +1 -1
- package/esm/use-window-scroll/use-window-scroll.mjs +2 -2
- package/esm/use-window-scroll/use-window-scroll.mjs.map +1 -1
- package/lib/index.d.mts +5 -1
- package/lib/index.d.ts +5 -1
- package/lib/use-click-outside/use-click-outside.d.ts +1 -1
- package/lib/use-collapse/use-horizontal-collapse.d.ts +5 -0
- package/lib/use-counter/use-counter.d.ts +1 -0
- package/lib/use-debounced-callback/use-debounced-callback.d.ts +4 -0
- package/lib/use-debounced-value/use-debounced-value.d.ts +6 -1
- package/lib/use-event-listener/use-event-listener.d.ts +1 -1
- package/lib/use-hotkeys/parse-hotkey.d.ts +4 -5
- package/lib/use-long-press/use-long-press.d.ts +1 -0
- package/lib/use-mask/use-mask.d.ts +60 -0
- package/lib/use-roving-index/use-roving-index.d.ts +49 -0
- package/lib/use-scroll-into-view/use-scroll-into-view.d.ts +4 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-scroller.cjs","names":[],"sources":["../../src/use-scroller/use-scroller.ts"],"sourcesContent":["import { RefCallback, useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseScrollerOptions {\n /** Amount of pixels to scroll when calling scroll functions, `200` by default */\n scrollAmount?: number;\n\n /** Determines whether content can be scrolled by dragging with mouse, `true` by default */\n draggable?: boolean;\n\n /** Called when scroll state changes (canScrollStart or canScrollEnd) */\n onScrollStateChange?: (state: UseScrollerScrollState) => void;\n}\n\nexport interface UseScrollerScrollState {\n /** Whether content can be scrolled towards the start (left in LTR, right in RTL) */\n canScrollStart: boolean;\n\n /** Whether content can be scrolled towards the end (right in LTR, left in RTL) */\n canScrollEnd: boolean;\n}\n\nexport interface UseScrollerReturnValue<T extends HTMLElement = HTMLDivElement> {\n /** Ref callback to attach to the scrollable container element */\n ref: RefCallback<T | null>;\n\n /** Whether content can be scrolled towards the start */\n canScrollStart: boolean;\n\n /** Whether content can be scrolled towards the end */\n canScrollEnd: boolean;\n\n /** Scrolls towards the start direction */\n scrollStart: () => void;\n\n /** Scrolls towards the end direction */\n scrollEnd: () => void;\n\n /** `true` if the user is currently dragging the content */\n isDragging: boolean;\n\n /** Props to spread on the scrollable container for drag functionality */\n dragHandlers: {\n onMouseDown: (e: React.MouseEvent) => void;\n onMouseMove: (e: React.MouseEvent) => void;\n onMouseUp: () => void;\n onMouseLeave: () => void;\n };\n}\n\nexport function useScroller<T extends HTMLElement = HTMLDivElement>(\n options: UseScrollerOptions = {}\n): UseScrollerReturnValue<T> {\n const { scrollAmount = 200, draggable = true, onScrollStateChange } = options;\n\n const containerRef = useRef<T | null>(null);\n\n const [canScrollStart, setCanScrollStart] = useState(false);\n const [canScrollEnd, setCanScrollEnd] = useState(false);\n const [isDragging, setIsDragging] = useState(false);\n\n const isDraggingRef = useRef(false);\n const hasDraggedRef = useRef(false);\n const startX = useRef(0);\n const scrollLeftStart = useRef(0);\n\n const onScrollStateChangeRef = useRef(onScrollStateChange);\n onScrollStateChangeRef.current = onScrollStateChange;\n\n const updateScrollState = useCallback(() => {\n const container = containerRef.current;\n if (container) {\n const { scrollLeft, scrollWidth, clientWidth } = container;\n const isRtl = getComputedStyle(container).direction === 'rtl';\n\n let newCanScrollStart: boolean;\n let newCanScrollEnd: boolean;\n\n if (isRtl) {\n newCanScrollStart = scrollLeft <
|
|
1
|
+
{"version":3,"file":"use-scroller.cjs","names":[],"sources":["../../src/use-scroller/use-scroller.ts"],"sourcesContent":["import { RefCallback, useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseScrollerOptions {\n /** Amount of pixels to scroll when calling scroll functions, `200` by default */\n scrollAmount?: number;\n\n /** Determines whether content can be scrolled by dragging with mouse, `true` by default */\n draggable?: boolean;\n\n /** Called when scroll state changes (canScrollStart or canScrollEnd) */\n onScrollStateChange?: (state: UseScrollerScrollState) => void;\n}\n\nexport interface UseScrollerScrollState {\n /** Whether content can be scrolled towards the start (left in LTR, right in RTL) */\n canScrollStart: boolean;\n\n /** Whether content can be scrolled towards the end (right in LTR, left in RTL) */\n canScrollEnd: boolean;\n}\n\nexport interface UseScrollerReturnValue<T extends HTMLElement = HTMLDivElement> {\n /** Ref callback to attach to the scrollable container element */\n ref: RefCallback<T | null>;\n\n /** Whether content can be scrolled towards the start */\n canScrollStart: boolean;\n\n /** Whether content can be scrolled towards the end */\n canScrollEnd: boolean;\n\n /** Scrolls towards the start direction */\n scrollStart: () => void;\n\n /** Scrolls towards the end direction */\n scrollEnd: () => void;\n\n /** `true` if the user is currently dragging the content */\n isDragging: boolean;\n\n /** Props to spread on the scrollable container for drag functionality */\n dragHandlers: {\n onMouseDown: (e: React.MouseEvent) => void;\n onMouseMove: (e: React.MouseEvent) => void;\n onMouseUp: () => void;\n onMouseLeave: () => void;\n };\n}\n\nexport function useScroller<T extends HTMLElement = HTMLDivElement>(\n options: UseScrollerOptions = {}\n): UseScrollerReturnValue<T> {\n const { scrollAmount = 200, draggable = true, onScrollStateChange } = options;\n\n const containerRef = useRef<T | null>(null);\n\n const [canScrollStart, setCanScrollStart] = useState(false);\n const [canScrollEnd, setCanScrollEnd] = useState(false);\n const [isDragging, setIsDragging] = useState(false);\n\n const isDraggingRef = useRef(false);\n const hasDraggedRef = useRef(false);\n const startX = useRef(0);\n const scrollLeftStart = useRef(0);\n\n const onScrollStateChangeRef = useRef(onScrollStateChange);\n onScrollStateChangeRef.current = onScrollStateChange;\n\n const updateScrollState = useCallback(() => {\n const container = containerRef.current;\n if (container) {\n const { scrollLeft, scrollWidth, clientWidth } = container;\n const isRtl = getComputedStyle(container).direction === 'rtl';\n\n let newCanScrollStart: boolean;\n let newCanScrollEnd: boolean;\n\n if (isRtl) {\n newCanScrollStart = scrollLeft < -1;\n newCanScrollEnd = scrollLeft > -(scrollWidth - clientWidth) + 1;\n } else {\n newCanScrollStart = scrollLeft > 1;\n newCanScrollEnd = scrollLeft < scrollWidth - clientWidth - 1;\n }\n\n setCanScrollStart(newCanScrollStart);\n setCanScrollEnd(newCanScrollEnd);\n\n onScrollStateChangeRef.current?.({\n canScrollStart: newCanScrollStart,\n canScrollEnd: newCanScrollEnd,\n });\n }\n }, []);\n\n useEffect(() => {\n updateScrollState();\n const container = containerRef.current;\n if (container) {\n container.addEventListener('scroll', updateScrollState);\n const resizeObserver = new ResizeObserver(updateScrollState);\n resizeObserver.observe(container);\n return () => {\n container.removeEventListener('scroll', updateScrollState);\n resizeObserver.disconnect();\n };\n }\n return undefined;\n }, [updateScrollState]);\n\n const scroll = useCallback(\n (direction: 'start' | 'end') => {\n const container = containerRef.current;\n if (container) {\n const isRtl = getComputedStyle(container).direction === 'rtl';\n const amount = scrollAmount;\n const scrollBy = direction === 'end' ? amount : -amount;\n const adjustedScrollBy = isRtl ? -scrollBy : scrollBy;\n\n container.scrollBy({\n left: adjustedScrollBy,\n behavior: 'smooth',\n });\n }\n },\n [scrollAmount]\n );\n\n const scrollStart = useCallback(() => scroll('start'), [scroll]);\n const scrollEnd = useCallback(() => scroll('end'), [scroll]);\n\n const handleMouseDown = useCallback(\n (event: React.MouseEvent) => {\n if (!draggable) {\n return;\n }\n const container = containerRef.current;\n if (container) {\n isDraggingRef.current = true;\n hasDraggedRef.current = false;\n setIsDragging(true);\n startX.current = event.pageX - container.offsetLeft;\n scrollLeftStart.current = container.scrollLeft;\n container.style.cursor = 'grabbing';\n container.style.userSelect = 'none';\n }\n },\n [draggable]\n );\n\n const handleMouseMove = useCallback((event: React.MouseEvent) => {\n if (!isDraggingRef.current) {\n return;\n }\n event.preventDefault();\n const container = containerRef.current;\n if (container) {\n const x = event.pageX - container.offsetLeft;\n const walk = x - startX.current;\n if (Math.abs(walk) > 5) {\n hasDraggedRef.current = true;\n }\n container.scrollLeft = scrollLeftStart.current - walk;\n }\n }, []);\n\n const handleMouseUp = useCallback(() => {\n const wasDragged = hasDraggedRef.current;\n isDraggingRef.current = false;\n hasDraggedRef.current = false;\n setIsDragging(false);\n const container = containerRef.current;\n if (container) {\n container.style.cursor = '';\n container.style.userSelect = '';\n\n if (wasDragged) {\n const suppressClick = (event: MouseEvent) => {\n event.stopPropagation();\n event.preventDefault();\n container.removeEventListener('click', suppressClick, true);\n };\n container.addEventListener('click', suppressClick, true);\n }\n }\n }, []);\n\n const handleMouseLeave = useCallback(() => {\n if (isDraggingRef.current) {\n handleMouseUp();\n }\n }, [handleMouseUp]);\n\n const assignRef: RefCallback<T | null> = useCallback(\n (node) => {\n containerRef.current = node;\n if (node) {\n updateScrollState();\n }\n },\n [updateScrollState]\n );\n\n return {\n ref: assignRef,\n canScrollStart,\n canScrollEnd,\n scrollStart,\n scrollEnd,\n isDragging,\n dragHandlers: {\n onMouseDown: handleMouseDown,\n onMouseMove: handleMouseMove,\n onMouseUp: handleMouseUp,\n onMouseLeave: handleMouseLeave,\n },\n };\n}\n\nexport namespace useScroller {\n export type Options = UseScrollerOptions;\n export type ReturnValue<T extends HTMLElement = HTMLDivElement> = UseScrollerReturnValue<T>;\n export type ScrollState = UseScrollerScrollState;\n}\n"],"mappings":";;;AAiDA,SAAgB,YACd,UAA8B,EAAE,EACL;CAC3B,MAAM,EA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I,WAAW;AACb,aAAU,MAAM,SAAS;AACzB,aAAU,MAAM,aAAa;AAE7B,OAAI,YAAY;IACd,MAAM,iBAAiB,UAAsB;AAC3C,WAAM,iBAAiB;AACvB,WAAM,gBAAgB;AACtB,eAAU,oBAAoB,SAAS,eAAe,KAAK;;AAE7D,cAAU,iBAAiB,SAAS,eAAe,KAAK;;;IAG3D,EAAE,CAAC;CAEN,MAAM,oBAAA,GAAA,MAAA,mBAAqC;AACzC,MAAI,cAAc,QAChB,gBAAe;IAEhB,CAAC,cAAc,CAAC;AAYnB,QAAO;EACL,MAAA,GAAA,MAAA,cAVC,SAAS;AACR,gBAAa,UAAU;AACvB,OAAI,KACF,oBAAmB;KAGvB,CAAC,kBAAkB,CACpB;EAIC;EACA;EACA;EACA;EACA;EACA,cAAc;GACZ,aAAa;GACb,aAAa;GACb,WAAW;GACX,cAAc;GACf;EACF"}
|
package/cjs/use-set/use-set.cjs
CHANGED
|
@@ -5,7 +5,12 @@ let react = require("react");
|
|
|
5
5
|
function readonlySetLikeToSet(input) {
|
|
6
6
|
if (input instanceof Set) return input;
|
|
7
7
|
const result = /* @__PURE__ */ new Set();
|
|
8
|
-
|
|
8
|
+
const iterator = input.keys();
|
|
9
|
+
let next = iterator.next();
|
|
10
|
+
while (!next.done) {
|
|
11
|
+
result.add(next.value);
|
|
12
|
+
next = iterator.next();
|
|
13
|
+
}
|
|
9
14
|
return result;
|
|
10
15
|
}
|
|
11
16
|
function useSet(values) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-set.cjs","names":["useForceUpdate"],"sources":["../../src/use-set/use-set.ts"],"sourcesContent":["import { useRef } from 'react';\nimport { useForceUpdate } from '../use-force-update/use-force-update';\n\nexport function readonlySetLikeToSet<T>(input: ReadonlySetLike<T>): Set<T> {\n if (input instanceof Set) {\n return input;\n }\n const result = new Set<T>();\n
|
|
1
|
+
{"version":3,"file":"use-set.cjs","names":["useForceUpdate"],"sources":["../../src/use-set/use-set.ts"],"sourcesContent":["import { useRef } from 'react';\nimport { useForceUpdate } from '../use-force-update/use-force-update';\n\nexport function readonlySetLikeToSet<T>(input: ReadonlySetLike<T>): Set<T> {\n if (input instanceof Set) {\n return input;\n }\n const result = new Set<T>();\n const iterator = input.keys();\n let next = iterator.next();\n while (!next.done) {\n result.add(next.value);\n next = iterator.next();\n }\n return result;\n}\n\nexport function useSet<T>(values?: T[]): Set<T> {\n const setRef = useRef(new Set(values));\n const forceUpdate = useForceUpdate();\n\n setRef.current.add = (...args) => {\n const res = Set.prototype.add.apply(setRef.current, args);\n forceUpdate();\n return res;\n };\n\n setRef.current.clear = (...args) => {\n Set.prototype.clear.apply(setRef.current, args);\n forceUpdate();\n };\n\n setRef.current.delete = (...args) => {\n const res = Set.prototype.delete.apply(setRef.current, args);\n forceUpdate();\n return res;\n };\n\n setRef.current.union = <U>(other: ReadonlySetLike<U>): Set<T | U> => {\n const result = new Set<T | U>(setRef.current as Set<T>);\n readonlySetLikeToSet(other).forEach((item) => result.add(item));\n return result;\n };\n\n setRef.current.intersection = <U>(other: ReadonlySetLike<U>): Set<T & U> => {\n const result = new Set<T & U>();\n const otherSet = readonlySetLikeToSet(other);\n setRef.current.forEach((item) => {\n if (otherSet.has(item as any)) {\n result.add(item as T & U);\n }\n });\n return result;\n };\n\n setRef.current.difference = <U>(other: ReadonlySetLike<U>): Set<T> => {\n const result = new Set<T>();\n const otherSet = readonlySetLikeToSet(other);\n setRef.current.forEach((item) => {\n if (!otherSet.has(item as any)) {\n result.add(item);\n }\n });\n return result;\n };\n\n setRef.current.symmetricDifference = <U>(other: ReadonlySetLike<U>): Set<T | U> => {\n const result = new Set<T | U>();\n const otherSet = readonlySetLikeToSet(other);\n\n setRef.current.forEach((item) => {\n if (!otherSet.has(item as any)) {\n result.add(item);\n }\n });\n\n otherSet.forEach((item) => {\n if (!setRef.current.has(item as any)) {\n result.add(item);\n }\n });\n\n return result;\n };\n\n return setRef.current;\n}\n"],"mappings":";;;;AAGA,SAAg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S,IAAI,KAAY,CAC5B,QAAO,IAAI,KAAK;IAElB;AACF,SAAO;;AAGT,QAAO,QAAQ,uBAA0B,UAA0C;EACjF,MAAM,yBAAS,IAAI,KAAY;EAC/B,MAAM,WAAW,qBAAqB,MAAM;AAE5C,SAAO,QAAQ,SAAS,SAAS;AAC/B,OAAI,CAAC,SAAS,IAAI,KAAY,CAC5B,QAAO,IAAI,KAAK;IAElB;AAEF,WAAS,SAAS,SAAS;AACzB,OAAI,CAAC,OAAO,QAAQ,IAAI,KAAY,CAClC,QAAO,IAAI,KAAK;IAElB;AAEF,SAAO;;AAGT,QAAO,OAAO"}
|
|
@@ -34,7 +34,9 @@ function useThrottledCallbackWithClearTimeout(callback, wait) {
|
|
|
34
34
|
return [throttled, clearTimeout];
|
|
35
35
|
}
|
|
36
36
|
function useThrottledCallback(callback, wait) {
|
|
37
|
-
|
|
37
|
+
const [throttled, clearTimeout] = useThrottledCallbackWithClearTimeout(callback, wait);
|
|
38
|
+
(0, react.useEffect)(() => clearTimeout, []);
|
|
39
|
+
return throttled;
|
|
38
40
|
}
|
|
39
41
|
//#endregion
|
|
40
42
|
exports.useThrottledCallback = useThrottledCallback;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-throttled-callback.cjs","names":["useCallbackRef"],"sources":["../../src/use-throttled-callback/use-throttled-callback.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { useCallbackRef } from '../utils';\n\nexport function useThrottledCallbackWithClearTimeout<T extends (...args: any[]) => any>(\n callback: T,\n wait: number\n) {\n const handleCallback = useCallbackRef(callback);\n const latestInArgsRef = useRef<Parameters<T>>(null);\n const latestOutArgsRef = useRef<Parameters<T>>(null);\n const active = useRef(true);\n const waitRef = useRef(wait);\n const timeoutRef = useRef<number>(-1);\n\n const clearTimeout = () => window.clearTimeout(timeoutRef.current);\n\n const callThrottledCallback = useCallback(\n (...args: Parameters<T>) => {\n handleCallback(...args);\n latestInArgsRef.current = args;\n latestOutArgsRef.current = args;\n active.current = false;\n },\n [handleCallback]\n );\n\n const timerCallback = useCallback(() => {\n if (latestInArgsRef.current && latestInArgsRef.current !== latestOutArgsRef.current) {\n callThrottledCallback(...latestInArgsRef.current);\n\n timeoutRef.current = window.setTimeout(timerCallback, waitRef.current);\n } else {\n active.current = true;\n }\n }, [callThrottledCallback]);\n\n const throttled = useCallback(\n (...args: Parameters<T>) => {\n if (active.current) {\n callThrottledCallback(...args);\n timeoutRef.current = window.setTimeout(timerCallback, waitRef.current);\n } else {\n latestInArgsRef.current = args;\n }\n },\n [callThrottledCallback, timerCallback]\n );\n\n useEffect(() => {\n waitRef.current = wait;\n }, [wait]);\n\n return [throttled, clearTimeout] as const;\n}\n\nexport function useThrottledCallback<T extends (...args: any[]) => any>(callback: T, wait: number) {\n
|
|
1
|
+
{"version":3,"file":"use-throttled-callback.cjs","names":["useCallbackRef"],"sources":["../../src/use-throttled-callback/use-throttled-callback.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { useCallbackRef } from '../utils';\n\nexport function useThrottledCallbackWithClearTimeout<T extends (...args: any[]) => any>(\n callback: T,\n wait: number\n) {\n const handleCallback = useCallbackRef(callback);\n const latestInArgsRef = useRef<Parameters<T>>(null);\n const latestOutArgsRef = useRef<Parameters<T>>(null);\n const active = useRef(true);\n const waitRef = useRef(wait);\n const timeoutRef = useRef<number>(-1);\n\n const clearTimeout = () => window.clearTimeout(timeoutRef.current);\n\n const callThrottledCallback = useCallback(\n (...args: Parameters<T>) => {\n handleCallback(...args);\n latestInArgsRef.current = args;\n latestOutArgsRef.current = args;\n active.current = false;\n },\n [handleCallback]\n );\n\n const timerCallback = useCallback(() => {\n if (latestInArgsRef.current && latestInArgsRef.current !== latestOutArgsRef.current) {\n callThrottledCallback(...latestInArgsRef.current);\n\n timeoutRef.current = window.setTimeout(timerCallback, waitRef.current);\n } else {\n active.current = true;\n }\n }, [callThrottledCallback]);\n\n const throttled = useCallback(\n (...args: Parameters<T>) => {\n if (active.current) {\n callThrottledCallback(...args);\n timeoutRef.current = window.setTimeout(timerCallback, waitRef.current);\n } else {\n latestInArgsRef.current = args;\n }\n },\n [callThrottledCallback, timerCallback]\n );\n\n useEffect(() => {\n waitRef.current = wait;\n }, [wait]);\n\n return [throttled, clearTimeout] as const;\n}\n\nexport function useThrottledCallback<T extends (...args: any[]) => any>(callback: T, wait: number) {\n const [throttled, clearTimeout] = useThrottledCallbackWithClearTimeout(callback, wait);\n useEffect(() => clearTimeout, []);\n return throttled;\n}\n"],"mappings":";;;;AAGA,SAAgB,qCACd,UACA,MACA;CACA,MAAM,iBAAiBA,yBAAAA,eAAe,SAAS;CAC/C,MAAM,mBAAA,GAAA,MAAA,QAAwC,KAAK;CACnD,MAAM,oBAAA,GAAA,MAAA,QAAyC,KAAK;CACpD,MAAM,UAAA,GAAA,MAAA,QAAgB,KAAK;CAC3B,MAAM,WAAA,GAAA,MAAA,QAAiB,KAAK;CAC5B,MAAM,cAAA,GAAA,MAAA,QAA4B,GAAG;CAErC,MAAM,qBAAqB,OAAO,aAAa,WAAW,QAAQ;CAElE,MAAM,yBAAA,GAAA,MAAA,cACH,GAAG,SAAwB;AAC1B,iBAAe,GAAG,KAAK;AACvB,kBAAgB,UAAU;AAC1B,mBAAiB,UAAU;AAC3B,SAAO,UAAU;IAEnB,CAAC,eAAe,CACjB;CAED,MAAM,iBAAA,GAAA,MAAA,mBAAkC;AACtC,MAAI,gBAAgB,WAAW,gBAAgB,YAAY,iBAAiB,SAAS;AACnF,yBAAsB,GAAG,gBAAgB,QAAQ;AAEjD,cAAW,UAAU,OAAO,WAAW,eAAe,QAAQ,QAAQ;QAEtE,QAAO,UAAU;IAElB,CAAC,sBAAsB,CAAC;CAE3B,MAAM,aAAA,GAAA,MAAA,cACH,GAAG,SAAwB;AAC1B,MAAI,OAAO,SAAS;AAClB,yBAAsB,GAAG,KAAK;AAC9B,cAAW,UAAU,OAAO,WAAW,eAAe,QAAQ,QAAQ;QAEtE,iBAAgB,UAAU;IAG9B,CAAC,uBAAuB,cAAc,CACvC;AAED,EAAA,GAAA,MAAA,iBAAgB;AACd,UAAQ,UAAU;IACjB,CAAC,KAAK,CAAC;AAEV,QAAO,CAAC,WAAW,aAAa;;AAGlC,SAAgB,qBAAwD,UAAa,MAAc;CACjG,MAAM,CAAC,WAAW,gBAAgB,qCAAqC,UAAU,KAAK;AACtF,EAAA,GAAA,MAAA,iBAAgB,cAAc,EAAE,CAAC;AACjC,QAAO"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
const require_use_callback_ref = require("../utils/use-callback-ref/use-callback-ref.cjs");
|
|
2
3
|
let react = require("react");
|
|
3
4
|
//#region packages/@mantine/hooks/src/use-timeout/use-timeout.ts
|
|
4
5
|
function useTimeout(callback, delay, options = { autoInvoke: false }) {
|
|
5
6
|
const timeoutRef = (0, react.useRef)(null);
|
|
7
|
+
const handleCallback = require_use_callback_ref.useCallbackRef(callback);
|
|
6
8
|
const start = (0, react.useCallback)((...args) => {
|
|
7
9
|
if (!timeoutRef.current) timeoutRef.current = window.setTimeout(() => {
|
|
8
|
-
|
|
10
|
+
handleCallback(...args);
|
|
9
11
|
timeoutRef.current = null;
|
|
10
12
|
}, delay);
|
|
11
13
|
}, [delay]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-timeout.cjs","names":[],"sources":["../../src/use-timeout/use-timeout.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport interface UseTimeoutOptions {\n autoInvoke: boolean;\n}\n\nexport interface UseTimeoutReturnValue {\n start: (...args: any[]) => void;\n clear: () => void;\n}\n\nexport function useTimeout(\n callback: (...args: any[]) => void,\n delay: number,\n options: UseTimeoutOptions = { autoInvoke: false }\n): UseTimeoutReturnValue {\n const timeoutRef = useRef<number | null>(null);\n\n const start = useCallback(\n (...args: any[]) => {\n if (!timeoutRef.current) {\n timeoutRef.current = window.setTimeout(() => {\n
|
|
1
|
+
{"version":3,"file":"use-timeout.cjs","names":["useCallbackRef"],"sources":["../../src/use-timeout/use-timeout.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { useCallbackRef } from '../utils';\n\nexport interface UseTimeoutOptions {\n autoInvoke: boolean;\n}\n\nexport interface UseTimeoutReturnValue {\n start: (...args: any[]) => void;\n clear: () => void;\n}\n\nexport function useTimeout(\n callback: (...args: any[]) => void,\n delay: number,\n options: UseTimeoutOptions = { autoInvoke: false }\n): UseTimeoutReturnValue {\n const timeoutRef = useRef<number | null>(null);\n const handleCallback = useCallbackRef(callback);\n\n const start = useCallback(\n (...args: any[]) => {\n if (!timeoutRef.current) {\n timeoutRef.current = window.setTimeout(() => {\n handleCallback(...args);\n timeoutRef.current = null;\n }, delay);\n }\n },\n [delay]\n );\n\n const clear = useCallback(() => {\n if (timeoutRef.current) {\n window.clearTimeout(timeoutRef.current);\n timeoutRef.current = null;\n }\n }, []);\n\n useEffect(() => {\n if (options.autoInvoke) {\n start();\n }\n\n return clear;\n }, [clear, start]);\n\n return { start, clear };\n}\n\nexport namespace useTimeout {\n export type Options = UseTimeoutOptions;\n export type ReturnValue = UseTimeoutReturnValue;\n}\n"],"mappings":";;;;AAYA,SAAgB,WACd,UACA,OACA,UAA6B,EAAE,YAAY,OAAO,EAC3B;CACvB,MAAM,cAAA,GAAA,MAAA,QAAmC,KAAK;CAC9C,MAAM,iBAAiBA,yBAAAA,eAAe,SAAS;CAE/C,MAAM,SAAA,GAAA,MAAA,cACH,GAAG,SAAgB;AAClB,MAAI,CAAC,WAAW,QACd,YAAW,UAAU,OAAO,iBAAiB;AAC3C,kBAAe,GAAG,KAAK;AACvB,cAAW,UAAU;KACpB,MAAM;IAGb,CAAC,MAAM,CACR;CAED,MAAM,SAAA,GAAA,MAAA,mBAA0B;AAC9B,MAAI,WAAW,SAAS;AACtB,UAAO,aAAa,WAAW,QAAQ;AACvC,cAAW,UAAU;;IAEtB,EAAE,CAAC;AAEN,EAAA,GAAA,MAAA,iBAAgB;AACd,MAAI,QAAQ,WACV,QAAO;AAGT,SAAO;IACN,CAAC,OAAO,MAAM,CAAC;AAElB,QAAO;EAAE;EAAO;EAAO"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const require_use_window_event = require("../use-window-event/use-window-event.cjs");
|
|
3
3
|
let react = require("react");
|
|
4
4
|
//#region packages/@mantine/hooks/src/use-viewport-size/use-viewport-size.ts
|
|
5
|
-
const
|
|
5
|
+
const eventListenerOptions = { passive: true };
|
|
6
6
|
function useViewportSize() {
|
|
7
7
|
const [windowSize, setWindowSize] = (0, react.useState)({
|
|
8
8
|
width: 0,
|
|
@@ -14,8 +14,8 @@ function useViewportSize() {
|
|
|
14
14
|
height: window.innerHeight || 0
|
|
15
15
|
});
|
|
16
16
|
}, []);
|
|
17
|
-
require_use_window_event.useWindowEvent("resize", setSize,
|
|
18
|
-
require_use_window_event.useWindowEvent("orientationchange", setSize,
|
|
17
|
+
require_use_window_event.useWindowEvent("resize", setSize, eventListenerOptions);
|
|
18
|
+
require_use_window_event.useWindowEvent("orientationchange", setSize, eventListenerOptions);
|
|
19
19
|
(0, react.useEffect)(setSize, []);
|
|
20
20
|
return windowSize;
|
|
21
21
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-viewport-size.cjs","names":[],"sources":["../../src/use-viewport-size/use-viewport-size.ts"],"sourcesContent":["import { useCallback, useEffect, useState } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nconst
|
|
1
|
+
{"version":3,"file":"use-viewport-size.cjs","names":[],"sources":["../../src/use-viewport-size/use-viewport-size.ts"],"sourcesContent":["import { useCallback, useEffect, useState } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nconst eventListenerOptions = {\n passive: true,\n};\n\nexport function useViewportSize() {\n const [windowSize, setWindowSize] = useState({\n width: 0,\n height: 0,\n });\n\n const setSize = useCallback(() => {\n setWindowSize({ width: window.innerWidth || 0, height: window.innerHeight || 0 });\n }, []);\n\n useWindowEvent('resize', setSize, eventListenerOptions);\n useWindowEvent('orientationchange', setSize, eventListenerOptions);\n useEffect(setSize, []);\n\n return windowSize;\n}\n"],"mappings":";;;;AAGA,MAAM,uBAAuB,EAC3B,SAAS,MACV;AAED,SAAgB,kBAAkB;CAChC,MAAM,CAAC,YAAY,kBAAA,GAAA,MAAA,UAA0B;EAC3C,OAAO;EACP,QAAQ;EACT,CAAC;CAEF,MAAM,WAAA,GAAA,MAAA,mBAA4B;AAChC,gBAAc;GAAE,OAAO,OAAO,cAAc;GAAG,QAAQ,OAAO,eAAe;GAAG,CAAC;IAChF,EAAE,CAAC;AAEN,0BAAA,eAAe,UAAU,SAAS,qBAAqB;AACvD,0BAAA,eAAe,qBAAqB,SAAS,qBAAqB;AAClE,EAAA,GAAA,MAAA,WAAU,SAAS,EAAE,CAAC;AAEtB,QAAO"}
|
|
@@ -24,8 +24,8 @@ function useWindowScroll() {
|
|
|
24
24
|
x: 0,
|
|
25
25
|
y: 0
|
|
26
26
|
});
|
|
27
|
-
require_use_window_event.useWindowEvent("scroll", () => setPosition(getScrollPosition()));
|
|
28
|
-
require_use_window_event.useWindowEvent("resize", () => setPosition(getScrollPosition()));
|
|
27
|
+
require_use_window_event.useWindowEvent("scroll", () => setPosition(getScrollPosition()), { passive: true });
|
|
28
|
+
require_use_window_event.useWindowEvent("resize", () => setPosition(getScrollPosition()), { passive: true });
|
|
29
29
|
(0, react.useEffect)(() => {
|
|
30
30
|
setPosition(getScrollPosition());
|
|
31
31
|
}, []);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-window-scroll.cjs","names":[],"sources":["../../src/use-window-scroll/use-window-scroll.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport interface UseWindowScrollPosition {\n x: number;\n y: number;\n}\n\nexport type UseWindowScrollTo = (position: Partial<UseWindowScrollPosition>) => void;\nexport type UseWindowScrollReturnValue = [UseWindowScrollPosition, UseWindowScrollTo];\n\nfunction getScrollPosition(): UseWindowScrollPosition {\n return typeof window !== 'undefined' ? { x: window.scrollX, y: window.scrollY } : { x: 0, y: 0 };\n}\n\nfunction scrollTo({ x, y }: Partial<UseWindowScrollPosition>) {\n if (typeof window !== 'undefined') {\n const scrollOptions: ScrollToOptions = { behavior: 'smooth' };\n\n if (typeof x === 'number') {\n scrollOptions.left = x;\n }\n\n if (typeof y === 'number') {\n scrollOptions.top = y;\n }\n\n window.scrollTo(scrollOptions);\n }\n}\n\nexport function useWindowScroll(): UseWindowScrollReturnValue {\n const [position, setPosition] = useState<UseWindowScrollPosition>({ x: 0, y: 0 });\n\n useWindowEvent('scroll', () => setPosition(getScrollPosition()));\n useWindowEvent('resize', () => setPosition(getScrollPosition()));\n\n useEffect(() => {\n setPosition(getScrollPosition());\n }, []);\n\n return [position, scrollTo] as const;\n}\n\nexport namespace useWindowScroll {\n export type Position = UseWindowScrollPosition;\n export type ScrollTo = UseWindowScrollTo;\n export type ReturnValue = UseWindowScrollReturnValue;\n}\n"],"mappings":";;;;AAWA,SAAS,oBAA6C;AACpD,QAAO,OAAO,WAAW,cAAc;EAAE,GAAG,OAAO;EAAS,GAAG,OAAO;EAAS,GAAG;EAAE,GAAG;EAAG,GAAG;EAAG;;AAGlG,SAAS,SAAS,EAAE,GAAG,KAAuC;AAC5D,KAAI,OAAO,WAAW,aAAa;EACjC,MAAM,gBAAiC,EAAE,UAAU,UAAU;AAE7D,MAAI,OAAO,MAAM,SACf,eAAc,OAAO;AAGvB,MAAI,OAAO,MAAM,SACf,eAAc,MAAM;AAGtB,SAAO,SAAS,cAAc;;;AAIlC,SAAgB,kBAA8C;CAC5D,MAAM,CAAC,UAAU,gBAAA,GAAA,MAAA,UAAiD;EAAE,GAAG;EAAG,GAAG;EAAG,CAAC;AAEjF,0BAAA,eAAe,gBAAgB,YAAY,mBAAmB,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"use-window-scroll.cjs","names":[],"sources":["../../src/use-window-scroll/use-window-scroll.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport interface UseWindowScrollPosition {\n x: number;\n y: number;\n}\n\nexport type UseWindowScrollTo = (position: Partial<UseWindowScrollPosition>) => void;\nexport type UseWindowScrollReturnValue = [UseWindowScrollPosition, UseWindowScrollTo];\n\nfunction getScrollPosition(): UseWindowScrollPosition {\n return typeof window !== 'undefined' ? { x: window.scrollX, y: window.scrollY } : { x: 0, y: 0 };\n}\n\nfunction scrollTo({ x, y }: Partial<UseWindowScrollPosition>) {\n if (typeof window !== 'undefined') {\n const scrollOptions: ScrollToOptions = { behavior: 'smooth' };\n\n if (typeof x === 'number') {\n scrollOptions.left = x;\n }\n\n if (typeof y === 'number') {\n scrollOptions.top = y;\n }\n\n window.scrollTo(scrollOptions);\n }\n}\n\nexport function useWindowScroll(): UseWindowScrollReturnValue {\n const [position, setPosition] = useState<UseWindowScrollPosition>({ x: 0, y: 0 });\n\n useWindowEvent('scroll', () => setPosition(getScrollPosition()), { passive: true });\n useWindowEvent('resize', () => setPosition(getScrollPosition()), { passive: true });\n\n useEffect(() => {\n setPosition(getScrollPosition());\n }, []);\n\n return [position, scrollTo] as const;\n}\n\nexport namespace useWindowScroll {\n export type Position = UseWindowScrollPosition;\n export type ScrollTo = UseWindowScrollTo;\n export type ReturnValue = UseWindowScrollReturnValue;\n}\n"],"mappings":";;;;AAWA,SAAS,oBAA6C;AACpD,QAAO,OAAO,WAAW,cAAc;EAAE,GAAG,OAAO;EAAS,GAAG,OAAO;EAAS,GAAG;EAAE,GAAG;EAAG,GAAG;EAAG;;AAGlG,SAAS,SAAS,EAAE,GAAG,KAAuC;AAC5D,KAAI,OAAO,WAAW,aAAa;EACjC,MAAM,gBAAiC,EAAE,UAAU,UAAU;AAE7D,MAAI,OAAO,MAAM,SACf,eAAc,OAAO;AAGvB,MAAI,OAAO,MAAM,SACf,eAAc,MAAM;AAGtB,SAAO,SAAS,cAAc;;;AAIlC,SAAgB,kBAA8C;CAC5D,MAAM,CAAC,UAAU,gBAAA,GAAA,MAAA,UAAiD;EAAE,GAAG;EAAG,GAAG;EAAG,CAAC;AAEjF,0BAAA,eAAe,gBAAgB,YAAY,mBAAmB,CAAC,EAAE,EAAE,SAAS,MAAM,CAAC;AACnF,0BAAA,eAAe,gBAAgB,YAAY,mBAAmB,CAAC,EAAE,EAAE,SAAS,MAAM,CAAC;AAEnF,EAAA,GAAA,MAAA,iBAAgB;AACd,cAAY,mBAAmB,CAAC;IAC/B,EAAE,CAAC;AAEN,QAAO,CAAC,UAAU,SAAS"}
|
package/esm/index.mjs
CHANGED
|
@@ -84,4 +84,6 @@ import { useSelection } from "./use-selection/use-selection.mjs";
|
|
|
84
84
|
import { useFloatingWindow } from "./use-floating-window/use-floating-window.mjs";
|
|
85
85
|
import { useCollapse } from "./use-collapse/use-collapse.mjs";
|
|
86
86
|
import { useHorizontalCollapse } from "./use-collapse/use-horizontal-collapse.mjs";
|
|
87
|
-
|
|
87
|
+
import { formatMask, generatePattern, isMaskComplete, unformatMask, useMask } from "./use-mask/use-mask.mjs";
|
|
88
|
+
import { useRovingIndex } from "./use-roving-index/use-roving-index.mjs";
|
|
89
|
+
export { assignRef, clamp, clampUseMovePosition, formatMask, generatePattern, getHotkeyHandler, isMaskComplete, lowerFirst, mergeRefs, normalizeRadialValue, randomId, range, readLocalStorageValue, readSessionStorageValue, shallowEqual, unformatMask, upperFirst, useCallbackRef, useClickOutside, useClipboard, useCollapse, useColorScheme, useCounter, useDebouncedCallback, useDebouncedState, useDebouncedValue, useDidUpdate, useDisclosure, useDocumentTitle, useDocumentVisibility, useElementSize, useEventListener, useEyeDropper, useFavicon, useFetch, useFileDialog, useFloatingWindow, useFocusReturn, useFocusTrap, useFocusWithin, useForceUpdate, useFullscreenDocument, useFullscreenElement, useHash, useHeadroom, useHorizontalCollapse, useHotkeys, useHover, useId, useIdle, useInViewport, useInputState, useIntersection, useInterval, useIsFirstRender, useIsomorphicEffect, useListState, useLocalStorage, useLogger, useLongPress, useMap, useMask, useMediaQuery, useMergedRef, useMounted, useMouse, useMousePosition, useMove, useMutationObserver, useMutationObserverTarget, useNetwork, useOrientation, useOs, usePageLeave, usePagination, usePrevious, useQueue, useRadialMove, useReducedMotion, useResizeObserver, useRovingIndex, useScrollDirection, useScrollIntoView, useScrollSpy, useScroller, useSelection, useSessionStorage, useSet, useSetState, useShallowEffect, useStateHistory, useTextSelection, useThrottledCallback, useThrottledState, useThrottledValue, useTimeout, useToggle, useUncontrolled, useValidatedState, useViewportSize, useWindowEvent, useWindowScroll };
|
|
@@ -2,22 +2,25 @@
|
|
|
2
2
|
import { useEffect, useEffectEvent, useRef } from "react";
|
|
3
3
|
//#region packages/@mantine/hooks/src/use-click-outside/use-click-outside.ts
|
|
4
4
|
const DEFAULT_EVENTS = ["mousedown", "touchstart"];
|
|
5
|
-
function useClickOutside(callback, events, nodes) {
|
|
5
|
+
function useClickOutside(callback, events, nodes, enabled = true) {
|
|
6
6
|
const ref = useRef(null);
|
|
7
7
|
const eventsList = events || DEFAULT_EVENTS;
|
|
8
8
|
const listener = useEffectEvent((event) => {
|
|
9
9
|
const { target } = event ?? {};
|
|
10
|
-
if (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
if (!document.body.contains(target) && target?.tagName !== "HTML") return;
|
|
11
|
+
const path = event.composedPath();
|
|
12
|
+
if (Array.isArray(nodes)) nodes.every((node) => !!node && !path.includes(node)) && callback(event);
|
|
13
|
+
else if (ref.current && !path.includes(ref.current)) callback(event);
|
|
14
14
|
});
|
|
15
|
+
const eventsKey = eventsList.join(",");
|
|
15
16
|
useEffect(() => {
|
|
16
|
-
|
|
17
|
+
if (!enabled) return;
|
|
18
|
+
const events = eventsKey.split(",");
|
|
19
|
+
events.forEach((fn) => document.addEventListener(fn, listener));
|
|
17
20
|
return () => {
|
|
18
|
-
|
|
21
|
+
events.forEach((fn) => document.removeEventListener(fn, listener));
|
|
19
22
|
};
|
|
20
|
-
}, []);
|
|
23
|
+
}, [eventsKey, enabled]);
|
|
21
24
|
return ref;
|
|
22
25
|
}
|
|
23
26
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-click-outside.mjs","names":[],"sources":["../../src/use-click-outside/use-click-outside.ts"],"sourcesContent":["import { useEffect, useEffectEvent, useRef } from 'react';\n\ntype EventType = MouseEvent | TouchEvent;\n\nconst DEFAULT_EVENTS = ['mousedown', 'touchstart'];\n\nexport function useClickOutside<T extends HTMLElement = any>(\n callback: (event: EventType) => void,\n events?: string[] | null,\n nodes?: (HTMLElement | null)[]\n) {\n const ref = useRef<T>(null);\n const eventsList = events || DEFAULT_EVENTS;\n\n const listener = useEffectEvent((event: Event) => {\n const { target } = event ?? {};\n
|
|
1
|
+
{"version":3,"file":"use-click-outside.mjs","names":[],"sources":["../../src/use-click-outside/use-click-outside.ts"],"sourcesContent":["import { useEffect, useEffectEvent, useRef } from 'react';\n\ntype EventType = MouseEvent | TouchEvent;\n\nconst DEFAULT_EVENTS = ['mousedown', 'touchstart'];\n\nexport function useClickOutside<T extends HTMLElement = any>(\n callback: (event: EventType) => void,\n events?: string[] | null,\n nodes?: (HTMLElement | null)[],\n enabled: boolean = true\n) {\n const ref = useRef<T>(null);\n const eventsList = events || DEFAULT_EVENTS;\n\n const listener = useEffectEvent((event: Event) => {\n const { target } = event ?? {};\n const shouldIgnore =\n !document.body.contains(target as Node) && (target as Element)?.tagName !== 'HTML';\n\n if (shouldIgnore) {\n return;\n }\n\n const path = event.composedPath();\n\n if (Array.isArray(nodes)) {\n const shouldTrigger = nodes.every((node) => !!node && !path.includes(node));\n shouldTrigger && callback(event as EventType);\n } else if (ref.current && !path.includes(ref.current)) {\n callback(event as EventType);\n }\n });\n\n const eventsKey = eventsList.join(',');\n\n useEffect(() => {\n if (!enabled) {\n return undefined;\n }\n\n const events = eventsKey.split(',');\n events.forEach((fn) => document.addEventListener(fn, listener));\n\n return () => {\n events.forEach((fn) => document.removeEventListener(fn, listener));\n };\n }, [eventsKey, enabled]);\n\n return ref;\n}\n"],"mappings":";;;AAIA,MAAM,iBAAiB,CAAC,aAAa,aAAa;AAElD,SAAgB,gBACd,UACA,QACA,OACA,UAAmB,MACnB;CACA,MAAM,MAAM,OAAU,KAAK;CAC3B,MAAM,aAAa,UAAU;CAE7B,MAAM,WAAW,gBAAgB,UAAiB;EAChD,MAAM,EAAE,WAAW,SAAS,EAAE;AAI9B,MAFE,CAAC,SAAS,KAAK,SAAS,OAAe,IAAK,QAAoB,YAAY,OAG5E;EAGF,MAAM,OAAO,MAAM,cAAc;AAEjC,MAAI,MAAM,QAAQ,MAAM,CACA,OAAM,OAAO,SAAS,CAAC,CAAC,QAAQ,CAAC,KAAK,SAAS,KAAK,CAAC,IAC1D,SAAS,MAAmB;WACpC,IAAI,WAAW,CAAC,KAAK,SAAS,IAAI,QAAQ,CACnD,UAAS,MAAmB;GAE9B;CAEF,MAAM,YAAY,WAAW,KAAK,IAAI;AAEtC,iBAAgB;AACd,MAAI,CAAC,QACH;EAGF,MAAM,SAAS,UAAU,MAAM,IAAI;AACnC,SAAO,SAAS,OAAO,SAAS,iBAAiB,IAAI,SAAS,CAAC;AAE/D,eAAa;AACX,UAAO,SAAS,OAAO,SAAS,oBAAoB,IAAI,SAAS,CAAC;;IAEnE,CAAC,WAAW,QAAQ,CAAC;AAExB,QAAO"}
|
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useState } from "react";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
//#region packages/@mantine/hooks/src/use-clipboard/use-clipboard.ts
|
|
4
|
-
function useClipboard(options = {
|
|
4
|
+
function useClipboard(options = {}) {
|
|
5
|
+
const timeout = options.timeout ?? 2e3;
|
|
5
6
|
const [error, setError] = useState(null);
|
|
6
7
|
const [copied, setCopied] = useState(false);
|
|
7
|
-
const
|
|
8
|
+
const timeoutRef = useRef(null);
|
|
9
|
+
useEffect(() => () => {
|
|
10
|
+
window.clearTimeout(timeoutRef.current);
|
|
11
|
+
}, []);
|
|
8
12
|
const handleCopyResult = (value) => {
|
|
9
|
-
window.clearTimeout(
|
|
10
|
-
|
|
13
|
+
window.clearTimeout(timeoutRef.current);
|
|
14
|
+
timeoutRef.current = window.setTimeout(() => setCopied(false), timeout);
|
|
11
15
|
setCopied(value);
|
|
12
16
|
};
|
|
13
17
|
const copy = (value) => {
|
|
14
|
-
if ("clipboard" in navigator) navigator.clipboard.writeText(value).then(() =>
|
|
18
|
+
if ("clipboard" in navigator) navigator.clipboard.writeText(value).then(() => {
|
|
19
|
+
setError(null);
|
|
20
|
+
handleCopyResult(true);
|
|
21
|
+
}).catch((err) => setError(err));
|
|
15
22
|
else setError(/* @__PURE__ */ new Error("useClipboard: navigator.clipboard is not supported"));
|
|
16
23
|
};
|
|
17
24
|
const reset = () => {
|
|
18
25
|
setCopied(false);
|
|
19
26
|
setError(null);
|
|
20
|
-
window.clearTimeout(
|
|
27
|
+
window.clearTimeout(timeoutRef.current);
|
|
21
28
|
};
|
|
22
29
|
return {
|
|
23
30
|
copy,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-clipboard.mjs","names":[],"sources":["../../src/use-clipboard/use-clipboard.ts"],"sourcesContent":["import { useState } from 'react';\n\nexport interface UseClipboardInput {\n /** Time in ms after which the copied state will reset, `2000` by default */\n timeout?: number;\n}\n\nexport interface UseClipboardReturnValue {\n /** Function to copy value to clipboard */\n copy: (value: any) => void;\n\n /** Function to reset copied state and error */\n reset: () => void;\n\n /** Error if copying failed */\n error: Error | null;\n\n /** Boolean indicating if the value was copied successfully */\n copied: boolean;\n}\n\nexport function useClipboard(
|
|
1
|
+
{"version":3,"file":"use-clipboard.mjs","names":[],"sources":["../../src/use-clipboard/use-clipboard.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nexport interface UseClipboardInput {\n /** Time in ms after which the copied state will reset, `2000` by default */\n timeout?: number;\n}\n\nexport interface UseClipboardReturnValue {\n /** Function to copy value to clipboard */\n copy: (value: any) => void;\n\n /** Function to reset copied state and error */\n reset: () => void;\n\n /** Error if copying failed */\n error: Error | null;\n\n /** Boolean indicating if the value was copied successfully */\n copied: boolean;\n}\n\nexport function useClipboard(options: UseClipboardInput = {}): UseClipboardReturnValue {\n const timeout = options.timeout ?? 2000;\n const [error, setError] = useState<Error | null>(null);\n const [copied, setCopied] = useState(false);\n const timeoutRef = useRef<number | null>(null);\n\n useEffect(\n () => () => {\n window.clearTimeout(timeoutRef.current!);\n },\n []\n );\n\n const handleCopyResult = (value: boolean) => {\n window.clearTimeout(timeoutRef.current!);\n timeoutRef.current = window.setTimeout(() => setCopied(false), timeout);\n setCopied(value);\n };\n\n const copy = (value: any) => {\n if ('clipboard' in navigator) {\n navigator.clipboard\n .writeText(value)\n .then(() => {\n setError(null);\n handleCopyResult(true);\n })\n .catch((err) => setError(err));\n } else {\n setError(new Error('useClipboard: navigator.clipboard is not supported'));\n }\n };\n\n const reset = () => {\n setCopied(false);\n setError(null);\n window.clearTimeout(timeoutRef.current!);\n };\n\n return { copy, reset, error, copied };\n}\n\nexport namespace useClipboard {\n export type Input = UseClipboardInput;\n export type ReturnValue = UseClipboardReturnValue;\n}\n"],"mappings":";;;AAqBA,SAAgB,aAAa,UAA6B,EAAE,EAA2B;CACrF,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,CAAC,OAAO,YAAY,SAAuB,KAAK;CACtD,MAAM,CAAC,QAAQ,aAAa,SAAS,MAAM;CAC3C,MAAM,aAAa,OAAsB,KAAK;AAE9C,uBACc;AACV,SAAO,aAAa,WAAW,QAAS;IAE1C,EAAE,CACH;CAED,MAAM,oBAAoB,UAAmB;AAC3C,SAAO,aAAa,WAAW,QAAS;AACxC,aAAW,UAAU,OAAO,iBAAiB,UAAU,MAAM,EAAE,QAAQ;AACvE,YAAU,MAAM;;CAGlB,MAAM,QAAQ,UAAe;AAC3B,MAAI,eAAe,UACjB,WAAU,UACP,UAAU,MAAM,CAChB,WAAW;AACV,YAAS,KAAK;AACd,oBAAiB,KAAK;IACtB,CACD,OAAO,QAAQ,SAAS,IAAI,CAAC;MAEhC,0BAAS,IAAI,MAAM,qDAAqD,CAAC;;CAI7E,MAAM,cAAc;AAClB,YAAU,MAAM;AAChB,WAAS,KAAK;AACd,SAAO,aAAa,WAAW,QAAS;;AAG1C,QAAO;EAAE;EAAM;EAAO;EAAO;EAAQ"}
|
|
@@ -32,7 +32,7 @@ function useCollapse({ transitionDuration, transitionTimingFunction = "ease", on
|
|
|
32
32
|
}));
|
|
33
33
|
};
|
|
34
34
|
const getTransitionStyles = (height) => {
|
|
35
|
-
const duration = transitionDuration
|
|
35
|
+
const duration = transitionDuration ?? getAutoHeightDuration(height);
|
|
36
36
|
return { transition: `height ${duration}ms ${transitionTimingFunction}, opacity ${duration}ms ${transitionTimingFunction}` };
|
|
37
37
|
};
|
|
38
38
|
useDidUpdate(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-collapse.mjs","names":[],"sources":["../../src/use-collapse/use-collapse.ts"],"sourcesContent":["import React, { CSSProperties, useEffectEvent, useRef, useState } from 'react';\nimport { flushSync } from 'react-dom';\nimport { useDidUpdate } from '../use-did-update/use-did-update';\nimport { mergeRefs } from '../use-merged-ref/use-merged-ref';\n\nfunction getAutoHeightDuration(height: number | string) {\n if (!height || typeof height === 'string') {\n return 0;\n }\n const constant = height / 36;\n return Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10);\n}\n\nexport function getElementHeight(elementRef: React.RefObject<HTMLElement | null>) {\n return elementRef.current ? elementRef.current.scrollHeight : 'auto';\n}\n\nexport interface UseCollapseInput {\n /** Expanded state */\n expanded: boolean;\n\n /** Transition duration in milliseconds, by default calculated based on content height */\n transitionDuration?: number;\n\n /** Transition timing function, `ease` by default */\n transitionTimingFunction?: string;\n\n /** Called when transition ends */\n onTransitionEnd?: () => void;\n\n /** Called when transition starts */\n onTransitionStart?: () => void;\n\n /** If true, collapsed content is kept in the DOM and hidden with `display: none` styles */\n keepMounted?: boolean;\n}\n\ninterface GetCollapsePropsInput {\n style?: CSSProperties;\n ref?: React.Ref<HTMLDivElement>;\n}\n\ninterface GetCollapsePropsReturnValue {\n 'aria-hidden': boolean;\n inert: boolean;\n ref: React.RefCallback<HTMLDivElement>;\n onTransitionEnd: (event: React.TransitionEvent<Element>) => void;\n style: React.CSSProperties;\n}\n\nexport type UseCollapseState = 'entering' | 'entered' | 'exiting' | 'exited';\n\nexport interface UseCollapseReturnValue {\n /** Current transition state */\n state: UseCollapseState;\n\n /** Props to pass down to the collapsible element */\n getCollapseProps: (input?: GetCollapsePropsInput) => GetCollapsePropsReturnValue;\n}\n\nexport function useCollapse({\n transitionDuration,\n transitionTimingFunction = 'ease',\n onTransitionEnd,\n onTransitionStart,\n expanded,\n keepMounted,\n}: UseCollapseInput): UseCollapseReturnValue {\n const collapsedStyles = {\n height: 0,\n overflow: 'hidden',\n ...(keepMounted ? {} : { display: 'none' }),\n };\n\n const onTransitionStartEvent = useEffectEvent(() => onTransitionStart?.());\n\n const elementRef = useRef<HTMLElement>(null);\n const [styles, setStylesRaw] = useState<CSSProperties>(expanded ? {} : collapsedStyles);\n const [state, setState] = useState<UseCollapseState>(expanded ? 'entered' : 'exited');\n const setStyles = (newStyles: React.SetStateAction<CSSProperties>) => {\n flushSync(() => setStylesRaw(newStyles));\n };\n\n const mergeStyles = (newStyles: CSSProperties) => {\n setStyles((oldStyles) => ({ ...oldStyles, ...newStyles }));\n };\n\n const getTransitionStyles = (height: number | string) => {\n const duration = transitionDuration
|
|
1
|
+
{"version":3,"file":"use-collapse.mjs","names":[],"sources":["../../src/use-collapse/use-collapse.ts"],"sourcesContent":["import React, { CSSProperties, useEffectEvent, useRef, useState } from 'react';\nimport { flushSync } from 'react-dom';\nimport { useDidUpdate } from '../use-did-update/use-did-update';\nimport { mergeRefs } from '../use-merged-ref/use-merged-ref';\n\nfunction getAutoHeightDuration(height: number | string) {\n if (!height || typeof height === 'string') {\n return 0;\n }\n const constant = height / 36;\n return Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10);\n}\n\nexport function getElementHeight(elementRef: React.RefObject<HTMLElement | null>) {\n return elementRef.current ? elementRef.current.scrollHeight : 'auto';\n}\n\nexport interface UseCollapseInput {\n /** Expanded state */\n expanded: boolean;\n\n /** Transition duration in milliseconds, by default calculated based on content height */\n transitionDuration?: number;\n\n /** Transition timing function, `ease` by default */\n transitionTimingFunction?: string;\n\n /** Called when transition ends */\n onTransitionEnd?: () => void;\n\n /** Called when transition starts */\n onTransitionStart?: () => void;\n\n /** If true, collapsed content is kept in the DOM and hidden with `display: none` styles */\n keepMounted?: boolean;\n}\n\ninterface GetCollapsePropsInput {\n style?: CSSProperties;\n ref?: React.Ref<HTMLDivElement>;\n}\n\ninterface GetCollapsePropsReturnValue {\n 'aria-hidden': boolean;\n inert: boolean;\n ref: React.RefCallback<HTMLDivElement>;\n onTransitionEnd: (event: React.TransitionEvent<Element>) => void;\n style: React.CSSProperties;\n}\n\nexport type UseCollapseState = 'entering' | 'entered' | 'exiting' | 'exited';\n\nexport interface UseCollapseReturnValue {\n /** Current transition state */\n state: UseCollapseState;\n\n /** Props to pass down to the collapsible element */\n getCollapseProps: (input?: GetCollapsePropsInput) => GetCollapsePropsReturnValue;\n}\n\nexport function useCollapse({\n transitionDuration,\n transitionTimingFunction = 'ease',\n onTransitionEnd,\n onTransitionStart,\n expanded,\n keepMounted,\n}: UseCollapseInput): UseCollapseReturnValue {\n const collapsedStyles = {\n height: 0,\n overflow: 'hidden',\n ...(keepMounted ? {} : { display: 'none' }),\n };\n\n const onTransitionStartEvent = useEffectEvent(() => onTransitionStart?.());\n\n const elementRef = useRef<HTMLElement>(null);\n const [styles, setStylesRaw] = useState<CSSProperties>(expanded ? {} : collapsedStyles);\n const [state, setState] = useState<UseCollapseState>(expanded ? 'entered' : 'exited');\n const setStyles = (newStyles: React.SetStateAction<CSSProperties>) => {\n flushSync(() => setStylesRaw(newStyles));\n };\n\n const mergeStyles = (newStyles: CSSProperties) => {\n setStyles((oldStyles) => ({ ...oldStyles, ...newStyles }));\n };\n\n const getTransitionStyles = (height: number | string) => {\n const duration = transitionDuration ?? getAutoHeightDuration(height);\n return {\n transition: `height ${duration}ms ${transitionTimingFunction}, opacity ${duration}ms ${transitionTimingFunction}`,\n };\n };\n\n useDidUpdate(() => {\n const shouldTransition = transitionDuration !== 0;\n\n if (shouldTransition) {\n onTransitionStartEvent();\n }\n\n if (expanded) {\n window.requestAnimationFrame(() => {\n flushSync(() => setState('entering'));\n mergeStyles({ willChange: 'height', display: 'block', overflow: 'hidden' });\n window.requestAnimationFrame(() => {\n const height = getElementHeight(elementRef);\n mergeStyles({ ...getTransitionStyles(height), height });\n });\n });\n } else {\n window.requestAnimationFrame(() => {\n flushSync(() => setState('exiting'));\n const height = getElementHeight(elementRef);\n mergeStyles({ ...getTransitionStyles(height), willChange: 'height', height });\n window.requestAnimationFrame(() => mergeStyles({ height: 0, overflow: 'hidden' }));\n });\n }\n }, [expanded]);\n\n const handleTransitionEnd = (event: React.TransitionEvent): void => {\n if (event.target !== elementRef.current || event.propertyName !== 'height') {\n return;\n }\n\n if (expanded) {\n const height = getElementHeight(elementRef);\n\n if (height === styles.height) {\n setStyles({});\n } else {\n mergeStyles({ height });\n }\n\n setState('entered');\n onTransitionEnd?.();\n } else if (styles.height === 0) {\n setStyles(collapsedStyles);\n setState('exited');\n onTransitionEnd?.();\n }\n };\n\n return {\n state,\n getCollapseProps: (input) => ({\n 'aria-hidden': !expanded,\n inert: !expanded,\n ref: mergeRefs(elementRef, input?.ref),\n onTransitionEnd: handleTransitionEnd,\n style: { boxSizing: 'border-box', ...input?.style, ...styles },\n }),\n };\n}\n\nexport namespace useCollapse {\n export type Input = UseCollapseInput;\n export type ReturnValue = UseCollapseReturnValue;\n export type State = UseCollapseState;\n}\n"],"mappings":";;;;;;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,mBAAgB,SAAS,UAAU,CAAC;GACpC,MAAM,SAAS,iBAAiB,WAAW;AAC3C,eAAY;IAAE,GAAG,oBAAoB,OAAO;IAAE,YAAY;IAAU;IAAQ,CAAC;AAC7E,UAAO,4BAA4B,YAAY;IAAE,QAAQ;IAAG,UAAU;IAAU,CAAC,CAAC;IAClF;IAEH,CAAC,SAAS,CAAC;CAEd,MAAM,uBAAuB,UAAuC;AAClE,MAAI,MAAM,WAAW,WAAW,WAAW,MAAM,iBAAiB,SAChE;AAGF,MAAI,UAAU;GACZ,MAAM,SAAS,iBAAiB,WAAW;AAE3C,OAAI,WAAW,OAAO,OACpB,WAAU,EAAE,CAAC;OAEb,aAAY,EAAE,QAAQ,CAAC;AAGzB,YAAS,UAAU;AACnB,sBAAmB;aACV,OAAO,WAAW,GAAG;AAC9B,aAAU,gBAAgB;AAC1B,YAAS,SAAS;AAClB,sBAAmB;;;AAIvB,QAAO;EACL;EACA,mBAAmB,WAAW;GAC5B,eAAe,CAAC;GAChB,OAAO,CAAC;GACR,KAAK,UAAU,YAAY,OAAO,IAAI;GACtC,iBAAiB;GACjB,OAAO;IAAE,WAAW;IAAc,GAAG,OAAO;IAAO,GAAG;IAAQ;GAC/D;EACF"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useDidUpdate } from "../use-did-update/use-did-update.mjs";
|
|
3
3
|
import { mergeRefs } from "../use-merged-ref/use-merged-ref.mjs";
|
|
4
|
-
import { useRef, useState } from "react";
|
|
4
|
+
import { useEffectEvent, useRef, useState } from "react";
|
|
5
5
|
import { flushSync } from "react-dom";
|
|
6
6
|
//#region packages/@mantine/hooks/src/use-collapse/use-horizontal-collapse.ts
|
|
7
7
|
function getAutoWidthDuration(width) {
|
|
@@ -18,6 +18,7 @@ function useHorizontalCollapse({ transitionDuration, transitionTimingFunction =
|
|
|
18
18
|
overflow: "hidden",
|
|
19
19
|
...keepMounted ? {} : { display: "none" }
|
|
20
20
|
};
|
|
21
|
+
const onTransitionStartEvent = useEffectEvent(() => onTransitionStart?.());
|
|
21
22
|
const elementRef = useRef(null);
|
|
22
23
|
const [styles, setStylesRaw] = useState(expanded ? {} : collapsedStyles);
|
|
23
24
|
const [state, setState] = useState(expanded ? "entered" : "exited");
|
|
@@ -31,11 +32,11 @@ function useHorizontalCollapse({ transitionDuration, transitionTimingFunction =
|
|
|
31
32
|
}));
|
|
32
33
|
};
|
|
33
34
|
const getTransitionStyles = (width) => {
|
|
34
|
-
const duration = transitionDuration
|
|
35
|
+
const duration = transitionDuration ?? getAutoWidthDuration(width);
|
|
35
36
|
return { transition: `width ${duration}ms ${transitionTimingFunction}, opacity ${duration}ms ${transitionTimingFunction}` };
|
|
36
37
|
};
|
|
37
38
|
useDidUpdate(() => {
|
|
38
|
-
if (transitionDuration !== 0)
|
|
39
|
+
if (transitionDuration !== 0) onTransitionStartEvent();
|
|
39
40
|
if (expanded) window.requestAnimationFrame(() => {
|
|
40
41
|
flushSync(() => setState("entering"));
|
|
41
42
|
mergeStyles({
|
|
@@ -64,7 +65,7 @@ function useHorizontalCollapse({ transitionDuration, transitionTimingFunction =
|
|
|
64
65
|
overflow: "hidden"
|
|
65
66
|
}));
|
|
66
67
|
});
|
|
67
|
-
}, [expanded
|
|
68
|
+
}, [expanded]);
|
|
68
69
|
const handleTransitionEnd = (event) => {
|
|
69
70
|
if (event.target !== elementRef.current || event.propertyName !== "width") return;
|
|
70
71
|
if (expanded) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-horizontal-collapse.mjs","names":[],"sources":["../../src/use-collapse/use-horizontal-collapse.ts"],"sourcesContent":["import React, { CSSProperties, useRef, useState } from 'react';\nimport { flushSync } from 'react-dom';\nimport { useDidUpdate } from '../use-did-update/use-did-update';\nimport { mergeRefs } from '../use-merged-ref/use-merged-ref';\n\nfunction getAutoWidthDuration(width: number | string) {\n if (!width || typeof width === 'string') {\n return 0;\n }\n const constant = width / 36;\n return Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10);\n}\n\nexport function getElementWidth(elementRef: React.RefObject<HTMLElement | null>) {\n return elementRef.current ? elementRef.current.scrollWidth : 'auto';\n}\n\nexport interface UseHorizontalCollapseInput {\n /** Expanded state */\n expanded: boolean;\n\n /** Transition duration in milliseconds, by default calculated based on content width */\n transitionDuration?: number;\n\n /** Transition timing function, `ease` by default */\n transitionTimingFunction?: string;\n\n /** Called when transition ends */\n onTransitionEnd?: () => void;\n\n /** Called when transition starts */\n onTransitionStart?: () => void;\n\n /** If true, collapsed content is kept in the DOM and hidden with `display: none` styles */\n keepMounted?: boolean;\n}\n\ninterface GetHorizontalCollapsePropsInput {\n style?: CSSProperties;\n ref?: React.Ref<HTMLDivElement>;\n}\n\ninterface GetHorizontalCollapsePropsReturnValue {\n 'aria-hidden': boolean;\n inert: boolean;\n ref: React.RefCallback<HTMLDivElement>;\n onTransitionEnd: (event: React.TransitionEvent<Element>) => void;\n style: React.CSSProperties;\n}\n\nexport type UseHorizontalCollapseState = 'entering' | 'entered' | 'exiting' | 'exited';\n\nexport interface UseHorizontalCollapseReturnValue {\n /** Current transition state */\n state: UseHorizontalCollapseState;\n\n /** Props to pass down to the collapsible element */\n getCollapseProps: (\n input?: GetHorizontalCollapsePropsInput\n ) => GetHorizontalCollapsePropsReturnValue;\n}\n\nexport function useHorizontalCollapse({\n transitionDuration,\n transitionTimingFunction = 'ease',\n onTransitionEnd,\n onTransitionStart,\n expanded,\n keepMounted,\n}: UseHorizontalCollapseInput): UseHorizontalCollapseReturnValue {\n const collapsedStyles = {\n width: 0,\n overflow: 'hidden',\n ...(keepMounted ? {} : { display: 'none' }),\n };\n\n const elementRef = useRef<HTMLElement>(null);\n const [styles, setStylesRaw] = useState<CSSProperties>(expanded ? {} : collapsedStyles);\n const [state, setState] = useState<UseHorizontalCollapseState>(expanded ? 'entered' : 'exited');\n const setStyles = (newStyles: React.SetStateAction<CSSProperties>) => {\n flushSync(() => setStylesRaw(newStyles));\n };\n\n const mergeStyles = (newStyles: CSSProperties) => {\n setStyles((oldStyles) => ({ ...oldStyles, ...newStyles }));\n };\n\n const getTransitionStyles = (width: number | string) => {\n const duration = transitionDuration
|
|
1
|
+
{"version":3,"file":"use-horizontal-collapse.mjs","names":[],"sources":["../../src/use-collapse/use-horizontal-collapse.ts"],"sourcesContent":["import React, { CSSProperties, useEffectEvent, useRef, useState } from 'react';\nimport { flushSync } from 'react-dom';\nimport { useDidUpdate } from '../use-did-update/use-did-update';\nimport { mergeRefs } from '../use-merged-ref/use-merged-ref';\n\nfunction getAutoWidthDuration(width: number | string) {\n if (!width || typeof width === 'string') {\n return 0;\n }\n const constant = width / 36;\n return Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10);\n}\n\nexport function getElementWidth(elementRef: React.RefObject<HTMLElement | null>) {\n return elementRef.current ? elementRef.current.scrollWidth : 'auto';\n}\n\nexport interface UseHorizontalCollapseInput {\n /** Expanded state */\n expanded: boolean;\n\n /** Transition duration in milliseconds, by default calculated based on content width */\n transitionDuration?: number;\n\n /** Transition timing function, `ease` by default */\n transitionTimingFunction?: string;\n\n /** Called when transition ends */\n onTransitionEnd?: () => void;\n\n /** Called when transition starts */\n onTransitionStart?: () => void;\n\n /** If true, collapsed content is kept in the DOM and hidden with `display: none` styles */\n keepMounted?: boolean;\n}\n\ninterface GetHorizontalCollapsePropsInput {\n style?: CSSProperties;\n ref?: React.Ref<HTMLDivElement>;\n}\n\ninterface GetHorizontalCollapsePropsReturnValue {\n 'aria-hidden': boolean;\n inert: boolean;\n ref: React.RefCallback<HTMLDivElement>;\n onTransitionEnd: (event: React.TransitionEvent<Element>) => void;\n style: React.CSSProperties;\n}\n\nexport type UseHorizontalCollapseState = 'entering' | 'entered' | 'exiting' | 'exited';\n\nexport interface UseHorizontalCollapseReturnValue {\n /** Current transition state */\n state: UseHorizontalCollapseState;\n\n /** Props to pass down to the collapsible element */\n getCollapseProps: (\n input?: GetHorizontalCollapsePropsInput\n ) => GetHorizontalCollapsePropsReturnValue;\n}\n\nexport function useHorizontalCollapse({\n transitionDuration,\n transitionTimingFunction = 'ease',\n onTransitionEnd,\n onTransitionStart,\n expanded,\n keepMounted,\n}: UseHorizontalCollapseInput): UseHorizontalCollapseReturnValue {\n const collapsedStyles = {\n width: 0,\n overflow: 'hidden',\n ...(keepMounted ? {} : { display: 'none' }),\n };\n\n const onTransitionStartEvent = useEffectEvent(() => onTransitionStart?.());\n\n const elementRef = useRef<HTMLElement>(null);\n const [styles, setStylesRaw] = useState<CSSProperties>(expanded ? {} : collapsedStyles);\n const [state, setState] = useState<UseHorizontalCollapseState>(expanded ? 'entered' : 'exited');\n const setStyles = (newStyles: React.SetStateAction<CSSProperties>) => {\n flushSync(() => setStylesRaw(newStyles));\n };\n\n const mergeStyles = (newStyles: CSSProperties) => {\n setStyles((oldStyles) => ({ ...oldStyles, ...newStyles }));\n };\n\n const getTransitionStyles = (width: number | string) => {\n const duration = transitionDuration ?? getAutoWidthDuration(width);\n return {\n transition: `width ${duration}ms ${transitionTimingFunction}, opacity ${duration}ms ${transitionTimingFunction}`,\n };\n };\n\n useDidUpdate(() => {\n const shouldTransition = transitionDuration !== 0;\n\n if (shouldTransition) {\n onTransitionStartEvent();\n }\n\n if (expanded) {\n window.requestAnimationFrame(() => {\n flushSync(() => setState('entering'));\n mergeStyles({ willChange: 'width', display: 'block', overflow: 'hidden' });\n window.requestAnimationFrame(() => {\n const width = getElementWidth(elementRef);\n mergeStyles({ ...getTransitionStyles(width), width });\n });\n });\n } else {\n window.requestAnimationFrame(() => {\n flushSync(() => setState('exiting'));\n const width = getElementWidth(elementRef);\n mergeStyles({ ...getTransitionStyles(width), willChange: 'width', width });\n window.requestAnimationFrame(() => mergeStyles({ width: 0, overflow: 'hidden' }));\n });\n }\n }, [expanded]);\n\n const handleTransitionEnd = (event: React.TransitionEvent): void => {\n if (event.target !== elementRef.current || event.propertyName !== 'width') {\n return;\n }\n\n if (expanded) {\n const width = getElementWidth(elementRef);\n\n if (width === styles.width) {\n setStyles({});\n } else {\n mergeStyles({ width });\n }\n\n setState('entered');\n onTransitionEnd?.();\n } else if (styles.width === 0) {\n setStyles(collapsedStyles);\n setState('exited');\n onTransitionEnd?.();\n }\n };\n\n return {\n state,\n getCollapseProps: (input) => ({\n 'aria-hidden': !expanded,\n inert: !expanded,\n ref: mergeRefs(elementRef, input?.ref),\n onTransitionEnd: handleTransitionEnd,\n style: { boxSizing: 'border-box', ...input?.style, ...styles },\n }),\n };\n}\n\nexport namespace useHorizontalCollapse {\n export type Input = UseHorizontalCollapseInput;\n export type ReturnValue = UseHorizontalCollapseReturnValue;\n export type State = UseHorizontalCollapseState;\n}\n"],"mappings":";;;;;;AAKA,SAAS,qBAAqB,OAAwB;AACpD,KAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,QAAO;CAET,MAAM,WAAW,QAAQ;AACzB,QAAO,KAAK,OAAO,IAAI,KAAK,YAAY,MAAO,WAAW,KAAK,GAAG;;AAGpE,SAAgB,gBAAgB,YAAiD;AAC/E,QAAO,WAAW,UAAU,WAAW,QAAQ,cAAc;;AAgD/D,SAAgB,sBAAsB,EACpC,oBACA,2BAA2B,QAC3B,iBACA,mBACA,UACA,eAC+D;CAC/D,MAAM,kBAAkB;EACtB,OAAO;EACP,UAAU;EACV,GAAI,cAAc,EAAE,GAAG,EAAE,SAAS,QAAQ;EAC3C;CAED,MAAM,yBAAyB,qBAAqB,qBAAqB,CAAC;CAE1E,MAAM,aAAa,OAAoB,KAAK;CAC5C,MAAM,CAAC,QAAQ,gBAAgB,SAAwB,WAAW,EAAE,GAAG,gBAAgB;CACvF,MAAM,CAAC,OAAO,YAAY,SAAqC,WAAW,YAAY,SAAS;CAC/F,MAAM,aAAa,cAAmD;AACpE,kBAAgB,aAAa,UAAU,CAAC;;CAG1C,MAAM,eAAe,cAA6B;AAChD,aAAW,eAAe;GAAE,GAAG;GAAW,GAAG;GAAW,EAAE;;CAG5D,MAAM,uBAAuB,UAA2B;EACtD,MAAM,WAAW,s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
|
7
7
|
max: Infinity
|
|
8
8
|
};
|
|
9
9
|
function useCounter(initialValue = 0, options) {
|
|
10
|
-
const { min, max } = {
|
|
10
|
+
const { min, max, step: _step = 1 } = {
|
|
11
11
|
...DEFAULT_OPTIONS,
|
|
12
12
|
...options
|
|
13
13
|
};
|
|
14
|
+
const step = Math.abs(_step);
|
|
14
15
|
const [count, setCount] = useState(clamp(initialValue, min, max));
|
|
15
16
|
return [count, {
|
|
16
|
-
increment: useCallback(() => setCount((current) => clamp(current +
|
|
17
|
-
|
|
17
|
+
increment: useCallback(() => setCount((current) => clamp(current + step, min, max)), [
|
|
18
|
+
min,
|
|
19
|
+
max,
|
|
20
|
+
step
|
|
21
|
+
]),
|
|
22
|
+
decrement: useCallback(() => setCount((current) => clamp(current - step, min, max)), [
|
|
23
|
+
min,
|
|
24
|
+
max,
|
|
25
|
+
step
|
|
26
|
+
]),
|
|
18
27
|
set: useCallback((value) => setCount(clamp(value, min, max)), [min, max]),
|
|
19
28
|
reset: useCallback(() => setCount(clamp(initialValue, min, max)), [
|
|
20
29
|
initialValue,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-counter.mjs","names":[],"sources":["../../src/use-counter/use-counter.ts"],"sourcesContent":["import { useCallback, useState } from 'react';\nimport { clamp } from '../utils';\n\nconst DEFAULT_OPTIONS = {\n min: -Infinity,\n max: Infinity,\n};\n\nexport interface UseCounterOptions {\n min?: number;\n max?: number;\n}\n\nexport interface UseCounterHandlers {\n increment: () => void;\n decrement: () => void;\n set: (value: number) => void;\n reset: () => void;\n}\n\nexport type UseCounterReturnValue = [number, UseCounterHandlers];\n\nexport function useCounter(initialValue = 0, options?: UseCounterOptions): UseCounterReturnValue {\n const { min, max } = { ...DEFAULT_OPTIONS, ...options };\n const [count, setCount] = useState<number>(clamp(initialValue, min, max));\n\n const increment = useCallback(\n () => setCount((current) => clamp(current +
|
|
1
|
+
{"version":3,"file":"use-counter.mjs","names":[],"sources":["../../src/use-counter/use-counter.ts"],"sourcesContent":["import { useCallback, useState } from 'react';\nimport { clamp } from '../utils';\n\nconst DEFAULT_OPTIONS = {\n min: -Infinity,\n max: Infinity,\n};\n\nexport interface UseCounterOptions {\n min?: number;\n max?: number;\n step?: number;\n}\n\nexport interface UseCounterHandlers {\n increment: () => void;\n decrement: () => void;\n set: (value: number) => void;\n reset: () => void;\n}\n\nexport type UseCounterReturnValue = [number, UseCounterHandlers];\n\nexport function useCounter(initialValue = 0, options?: UseCounterOptions): UseCounterReturnValue {\n const { min, max, step: _step = 1 } = { ...DEFAULT_OPTIONS, ...options };\n const step = Math.abs(_step);\n const [count, setCount] = useState<number>(clamp(initialValue, min, max));\n\n const increment = useCallback(\n () => setCount((current) => clamp(current + step, min, max)),\n [min, max, step]\n );\n\n const decrement = useCallback(\n () => setCount((current) => clamp(current - step, min, max)),\n [min, max, step]\n );\n\n const set = useCallback((value: number) => setCount(clamp(value, min, max)), [min, max]);\n\n const reset = useCallback(\n () => setCount(clamp(initialValue, min, max)),\n [initialValue, min, max]\n );\n\n return [count, { increment, decrement, set, reset }];\n}\n\nexport namespace useCounter {\n export type Options = UseCounterOptions;\n export type Handlers = UseCounterHandlers;\n export type ReturnValue = UseCounterReturnValue;\n}\n"],"mappings":";;;;AAGA,MAAM,kBAAkB;CACtB,KAAK;CACL,KAAK;CACN;AAiBD,SAAgB,WAAW,eAAe,GAAG,SAAoD;CAC/F,MAAM,EAAE,KAAK,KAAK,MAAM,QAAQ,MAAM;EAAE,GAAG;EAAiB,GAAG;EAAS;CACxE,MAAM,OAAO,KAAK,IAAI,MAAM;CAC5B,MAAM,CAAC,OAAO,YAAY,SAAiB,MAAM,cAAc,KAAK,IAAI,CAAC;AAmBzE,QAAO,CAAC,OAAO;EAAE,WAjBC,kBACV,UAAU,YAAY,MAAM,UAAU,MAAM,KAAK,IAAI,CAAC,EAC5D;GAAC;GAAK;GAAK;GAAK,CACjB;EAc2B,WAZV,kBACV,UAAU,YAAY,MAAM,UAAU,MAAM,KAAK,IAAI,CAAC,EAC5D;GAAC;GAAK;GAAK;GAAK,CACjB;EASsC,KAP3B,aAAa,UAAkB,SAAS,MAAM,OAAO,KAAK,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;EAO5C,OAL9B,kBACN,SAAS,MAAM,cAAc,KAAK,IAAI,CAAC,EAC7C;GAAC;GAAc;GAAK;GAAI,CACzB;EAEkD,CAAC"}
|
|
@@ -3,22 +3,38 @@ import { useCallbackRef } from "../utils/use-callback-ref/use-callback-ref.mjs";
|
|
|
3
3
|
import { useEffect, useMemo, useRef } from "react";
|
|
4
4
|
//#region packages/@mantine/hooks/src/use-debounced-callback/use-debounced-callback.ts
|
|
5
5
|
function useDebouncedCallback(callback, options) {
|
|
6
|
-
const { delay, flushOnUnmount, leading } = typeof options === "number" ? {
|
|
6
|
+
const { delay, flushOnUnmount, leading, maxWait } = typeof options === "number" ? {
|
|
7
7
|
delay: options,
|
|
8
8
|
flushOnUnmount: false,
|
|
9
|
-
leading: false
|
|
9
|
+
leading: false,
|
|
10
|
+
maxWait: void 0
|
|
10
11
|
} : options;
|
|
11
12
|
const handleCallback = useCallbackRef(callback);
|
|
12
13
|
const debounceTimerRef = useRef(0);
|
|
14
|
+
const maxWaitTimerRef = useRef(0);
|
|
15
|
+
const latestArgsRef = useRef(null);
|
|
13
16
|
const lastCallback = useMemo(() => {
|
|
14
17
|
const currentCallback = Object.assign((...args) => {
|
|
15
18
|
window.clearTimeout(debounceTimerRef.current);
|
|
19
|
+
latestArgsRef.current = args;
|
|
16
20
|
const isFirstCall = currentCallback._isFirstCall;
|
|
17
21
|
currentCallback._isFirstCall = false;
|
|
18
22
|
function clearTimeoutAndLeadingRef() {
|
|
19
23
|
window.clearTimeout(debounceTimerRef.current);
|
|
24
|
+
window.clearTimeout(maxWaitTimerRef.current);
|
|
20
25
|
debounceTimerRef.current = 0;
|
|
26
|
+
maxWaitTimerRef.current = 0;
|
|
21
27
|
currentCallback._isFirstCall = true;
|
|
28
|
+
currentCallback._hasPendingCallback = false;
|
|
29
|
+
}
|
|
30
|
+
function startMaxWaitTimer() {
|
|
31
|
+
if (maxWait !== void 0 && maxWaitTimerRef.current === 0) maxWaitTimerRef.current = window.setTimeout(() => {
|
|
32
|
+
if (debounceTimerRef.current !== 0) {
|
|
33
|
+
const latestArgs = latestArgsRef.current;
|
|
34
|
+
clearTimeoutAndLeadingRef();
|
|
35
|
+
handleCallback(...latestArgs);
|
|
36
|
+
}
|
|
37
|
+
}, maxWait);
|
|
22
38
|
}
|
|
23
39
|
if (leading && isFirstCall) {
|
|
24
40
|
handleCallback(...args);
|
|
@@ -37,9 +53,11 @@ function useDebouncedCallback(callback, options) {
|
|
|
37
53
|
currentCallback.flush = flush;
|
|
38
54
|
currentCallback.cancel = cancel;
|
|
39
55
|
debounceTimerRef.current = window.setTimeout(resetLeadingState, delay);
|
|
56
|
+
startMaxWaitTimer();
|
|
40
57
|
return;
|
|
41
58
|
}
|
|
42
59
|
if (leading && !isFirstCall) {
|
|
60
|
+
currentCallback._hasPendingCallback = true;
|
|
43
61
|
const flush = () => {
|
|
44
62
|
if (debounceTimerRef.current !== 0) {
|
|
45
63
|
clearTimeoutAndLeadingRef();
|
|
@@ -55,8 +73,10 @@ function useDebouncedCallback(callback, options) {
|
|
|
55
73
|
clearTimeoutAndLeadingRef();
|
|
56
74
|
};
|
|
57
75
|
debounceTimerRef.current = window.setTimeout(resetLeadingState, delay);
|
|
76
|
+
startMaxWaitTimer();
|
|
58
77
|
return;
|
|
59
78
|
}
|
|
79
|
+
currentCallback._hasPendingCallback = true;
|
|
60
80
|
const flush = () => {
|
|
61
81
|
if (debounceTimerRef.current !== 0) {
|
|
62
82
|
clearTimeoutAndLeadingRef();
|
|
@@ -69,16 +89,20 @@ function useDebouncedCallback(callback, options) {
|
|
|
69
89
|
currentCallback.flush = flush;
|
|
70
90
|
currentCallback.cancel = cancel;
|
|
71
91
|
debounceTimerRef.current = window.setTimeout(flush, delay);
|
|
92
|
+
startMaxWaitTimer();
|
|
72
93
|
}, {
|
|
73
94
|
flush: () => {},
|
|
74
95
|
cancel: () => {},
|
|
75
|
-
|
|
96
|
+
isPending: () => currentCallback._hasPendingCallback,
|
|
97
|
+
_isFirstCall: true,
|
|
98
|
+
_hasPendingCallback: false
|
|
76
99
|
});
|
|
77
100
|
return currentCallback;
|
|
78
101
|
}, [
|
|
79
102
|
handleCallback,
|
|
80
103
|
delay,
|
|
81
|
-
leading
|
|
104
|
+
leading,
|
|
105
|
+
maxWait
|
|
82
106
|
]);
|
|
83
107
|
useEffect(() => () => {
|
|
84
108
|
if (flushOnUnmount) lastCallback.flush();
|