-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ResizablePanel): new drag look (#545)
- Loading branch information
Showing
4 changed files
with
96 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@cube-dev/ui-kit': patch | ||
--- | ||
|
||
New drag appearance for ResizablePanel to avoid confusion with a scrollbar. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useState, useEffect, useRef } from 'react'; | ||
|
||
export function useDebouncedValue<T>( | ||
value: T, | ||
delay: number = 300, | ||
maxWait?: number, | ||
): T { | ||
const [debouncedValue, setDebouncedValue] = useState(value); | ||
const lastValueRef = useRef(value); | ||
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
const maxWaitTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
||
useEffect(() => { | ||
// If value returns to the original while waiting, clear timeouts | ||
if (value === lastValueRef.current) { | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current); | ||
if (maxWaitTimeoutRef.current) clearTimeout(maxWaitTimeoutRef.current); | ||
return; | ||
} | ||
|
||
// Handle delay | ||
timeoutRef.current = setTimeout(() => { | ||
setDebouncedValue(value); | ||
lastValueRef.current = value; | ||
if (maxWaitTimeoutRef.current) { | ||
clearTimeout(maxWaitTimeoutRef.current); | ||
maxWaitTimeoutRef.current = null; | ||
} | ||
}, delay); | ||
|
||
// Handle maxWait if provided | ||
if (maxWait && !maxWaitTimeoutRef.current) { | ||
maxWaitTimeoutRef.current = setTimeout(() => { | ||
setDebouncedValue(value); | ||
lastValueRef.current = value; | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
timeoutRef.current = null; | ||
} | ||
}, maxWait); | ||
} | ||
|
||
return () => { | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current); | ||
if (maxWaitTimeoutRef.current) clearTimeout(maxWaitTimeoutRef.current); | ||
}; | ||
}, [value, delay, maxWait]); | ||
|
||
return debouncedValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters