mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-05 06:50:28 +03:00
Add code block copy and collapse functionality (#2361)
* add buttons to codeblocks * add functionality * Document functions * Improve accessibility * Remove pointless DefaultReset * implement some requested changes * fix content shift when expanding or collapsing --------- Co-authored-by: Ajay Bura <32841439+ajbura@users.noreply.github.com>
This commit is contained in:
parent
acc7d4ff56
commit
3cdb5c2fe6
4 changed files with 155 additions and 15 deletions
37
src/app/hooks/useTimeoutToggle.ts
Normal file
37
src/app/hooks/useTimeoutToggle.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
/**
|
||||
* Temporarily sets a boolean state.
|
||||
*
|
||||
* @param duration - Duration in milliseconds before resetting (default: 1500)
|
||||
* @param initial - Initial value (default: false)
|
||||
*/
|
||||
export function useTimeoutToggle(duration = 1500, initial = false): [boolean, () => void] {
|
||||
const [active, setActive] = useState(initial);
|
||||
const timeoutRef = useRef<number | null>(null);
|
||||
|
||||
const clear = () => {
|
||||
if (timeoutRef.current !== null) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
timeoutRef.current = null;
|
||||
}
|
||||
};
|
||||
|
||||
const trigger = useCallback(() => {
|
||||
setActive(!initial);
|
||||
clear();
|
||||
timeoutRef.current = window.setTimeout(() => {
|
||||
setActive(initial);
|
||||
timeoutRef.current = null;
|
||||
}, duration);
|
||||
}, [duration, initial]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
clear();
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
return [active, trigger];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue